Title | Excel Solver and Matrix Algebra |
---|---|
Course | Financial Economics |
Institution | Indian Institute of Technology Kanpur |
Pages | 16 |
File Size | 1.6 MB |
File Type | |
Total Downloads | 64 |
Total Views | 154 |
EXCEL HELPER...
EfficientPortfoliosinExcelUsingthe SolverandMatrixAlgebra ThisnoteoutlineshowtousethesolverandmatrixalgebrainExceltocomputeefficientportfolios.The exampleusedinthisnoteisinthespreadsheet3firmExample.xlsx,andisthesameexampleusedinthe lecturenotestitled“PortfolioTheorywithMatrixAlgebra”. Lastupdated:November24,2009
TheSolverAddIn ThesolverisanExcelAdd‐IncreatedbyFrontlineSystems(www.solver.com)thatcanbeusedtosolve generaloptimizationproblemsthat maybesubjecttocertainkindsofconstraints.Inthisnoteweshow howitcanbeusedtofindportfoliosthatminimizerisksubjecttocertainconstraints. Thesolveradd‐inmustbeactivatedbeforeitcanbeusedwithinExcel.InExcel2007,youactivateadd‐ insbyclickingontheofficebuttonandthenclickingontheExcelOptionsboxatthebottomofthe menu.
ThisopenstheExceloptionsdialoguebox.ClickAdd‐Ins,whichdisplaystheavailableAdd‐InsforExcel. MakesuretheSolverAdd‐InisanActiveApplicationAdd‐In.
MatrixAlgebrainExcel Excelhasseveralbuilt‐inarrayformulasthatcanperformbasicmatrixalgebraoperations.Themain functionsarelistedintablebelow ArrayFunction MINVERSE MMULT TRANSPOSE
Description Computeinverseofmatrix Matrixmultiplication Computetransposeofmatrix
ToevaluateanarrayfunctioninExcel,youmustusethemagickeystokecombination:‐‐ (holddownallthreekeysatoncethenrelease).
ExampleData IntheDatatabofthespreadsheet3firmExample.xlsistheexamplemonthlyreturndataonthreeassets: Microsoft,NordstromandStarbucks.Themonthlymeansandcovariancematrixofthereturnsare computedandthesearereferencedastheinputdataontheportfoliotabasillustratedinthescreen shotbelow.
Inthespreadsheet,cellscoloredlightbluecontaininputdata(fixeddatanotcreatedbysomeformula) andcellscoloredtancontainoutputdata(datacreatedbyapplyingsomeformula).Also,somecellsare explicitlynamed.ForexampletherangeofcellsB3:B5isnamedmuvec.Ifthesecellsarehighlighted thenmuvecwillappearintheNameBoxintheupperlefthandcornerofthespreadsheet.Similarly,the rangeofcellsE3:G5isnamedsigma.Formatrixalgebracalculations,itisconvenienttousenamed rangesinarrayformulas.
TheGlobalMinimumVariancePortfolio Theglobalminimumvarianceportfoliosolvestheoptimizationproblem
min σ 2p , m = m′Σm s.t. m′1 = 1 m
Thisoptimizationproblemcanbesolvedeasilyusingthesolverwithmatrixalgebrafunctions.The screenshotoftheportfoliotabbelowshowshowtoset‐upthisoptimizationprobleminExcel.
TherangeofcellsD10:D12iscalledmvecandwillcontaintheweightsintheminimumvarianceportfolio oncethesolverisrunandthesolutiontotheoptimizationproblemisfound.Beforethesolveristobe run,thesecellsshouldcontainaninitialguessoftheminimumvarianceportfolio.Asimpleguessfor thisvectorwhoseweightssumtooneis mmsft = 0.3, mnord = 0.3, msbux = 0.4. Tousethesolver,acellcontainingthefunctiontobemaximizedorminimizedmustbespecified.Here, thiscellisF10whichcontainsthearrayformula {=MMULT(TRANSPOSE(mvec),MMULT(sigma,mvec))} 2
whichevaluatesthematrixalgebraformulaforthevarianceofaportfolio: σ p ,m = m′Σm .Noticethat theformulaissurroundedbycurlybraces{}.Thisindicatesthat‐‐wasusedto evaluatetheformulasothatitistobeinterpretedasanarrayformula.Ifyoudon’tseethecurlybraces thentheformulawillnotbeevaluatecorrectly.Wealsoneedacelltocontainaformulathatwillbe usedtoimposetheconstraintthattheportfolioweightssumtoone: m ′1 = m msft + m nord + m sbux = 1. ThisformulaisspecifiedincellE10as =SUM(mvec) Thesolveradd‐inislocatedonthedatatabofthetopmenuribbonintherighthandcorner.Torunthe solver,clickthecellcontainingtheformulayouwanttooptimize(cellF10,andnamedsig2px)andthen clickonthesolverbutton.Thiswillopenupthesolverdialogueboxasshownbelow.
ThefieldnamedSetTargetCellmustcontaineitherthenameorthereferencetothecellcontainingthe formulatooptimize.Youhavethreechoicesforthetypeofoptimization:Max,MinandValueof.Here, wewanttominimizetheportfoliovariancesoMinshouldbeselected.Next,wemustspecifythecells containingthevariableswhicharebeingoptimized.ThesearespecifiedintheByChangingCellsfield. Here,wecantypeinthenamemvecorspecifytherangeofcellsD10:D12.Finally,wemustAddthe constraintthattheweightssumtoone.WedothisbyclickingtheAddbutton,whichopenstheAdd Constraintdialogueboxshowbelow.
TheCellReferencecontainsthecell(E10)thathastheformulafortheconstraint
m′1 = mmsft + mnord + msbux = 1. Wespecifythevalueoftheconstraint,1,intheConstraintfield.Once everythingisfilledin,clickOKtogobacktothesolverdialogue.Thecompletedialogueshouldlooklike oneshownbelow.
Torunthesolver,clicktheSolvebutton.Thecomputationisgenerallyveryfast.Ifsuccessful,youshould seethefollowingdialoguebox
Themessage“Solverfoundasolution.Allconstraintsandoptimalityconditionsaresatisfied”meansthat thefirstandsecondorderconditionsforaminimumaresatisfied.ClicktheKeepSolverSolutionoption buttonandthenclickOK.Yourspreadsheetshouldlookliketheonebelow.
Theglobalminimumvarianceportfoliohas44%inMicrosoft,36%inNordstromand19%inStarbucks. TheexpectedreturnonthisportfolioisgivenincellC13(calledmupx)andiscomputedusingthe formula μ p , m = m′μ .TheExcelarrayformulais {=MMULT(TRANSPOSE(mvec),muvec)} TheportfoliostandarddeviationincellC14isthesquarerootoftheportfoliovariance,sig2px,incell F10.
MinimumVariancePortfoliosubjecttoTargetExpectedReturn Aminimumvarianceportfoliowithtargetexpectedreturnequalto μ0 solvestheoptimizationproblem
min σ p2 ,y = y′Σ y s.t. y′μ = μ0 and y′1 = 1 y
Thisoptimizationproblemcanalsobeeasilysolvedusingthesolverwithmatrixalgebrafunctions.The screenshotbelowshowshowtoset‐upthisoptimizationprobleminExcelwherethetargetexpected returnistheexpectedreturnonMicrosoft(4.27%).
TherangeofcellsK10:K12iscalledyvecandwillcontaintheweightsintheefficientportfoliooncethe solverisrunandthesolutiontotheoptimizationproblemisfound.Beforethesolveristoberun,these cellsshouldcontainaninitialguessoftheminimumvarianceportfolio.Asimpleguessforthisvector whoseweightssumtooneis y msft = 0.3, y nord = 0.3, y sbux = 0.4. Thecellcontainingtheformulafor 2
portfoliovariance, σ p , y = y ′Σy ,isincellO10whichcontainsthearrayformula {=MMULT(TRANSPOSE(yvec),MMULT(sigma,yvec))} Wealsoneedtwoadditionalcellstocontainformulasthatwillbeusedtoimposetheconstraintsthat theportfolioexpectedreturnisequaltothetargetreturn, μ p , y = y ′μ = μ 0 ,andthattheportfolio weightssumtoone, y ′1 = ymsft + ynord + ysbux = 1. TheseformulasarespecifiedincellsL10andN10, whichcontaintheExcelformulas=SUM(yvec)and{=MMULT(TRANSPOSE(yvec),muvec)},respectively. Torunthesolver,clickcellO10(calledsig2py)andthenclickonthesolverbutton.Makesurethesolver dialogueboxisfilledouttolookliketheonebelow.
Noticethattherearenowtwoconstraintsspecified.Thefirstoneimposes y′1 = ymsft + ynord + y sbux = 1 , andthesecondoneimposes μp , y = y′μ = μ 0 = μ msft = 0.0475 .Torunthesolver,clicktheSolve button.Youshouldseeadialogueboxthatsaysthatthesolverfoundasolutionandthatalloptimality conditionsaresatisfied.KeepthesolutionandclickOK.Yourspreadsheetshouldlookliketheone below.
Theefficientportfoliohasweights ymsft = 0.83, ynord = − 0.09, ysbux = 0.26.NoticethatNordstromis soldshortinthisportfoliobecauseithasanegativeweight.Theexpectedreturnonthisportfoliois equaltothetargetexpectedreturn(seecellN10namedmupy)andtheweightssumtoone.Noticethat thestandarddeviationofthisportfolio(seecellP10)issmallerthanthestandarddeviationofMicrosoft (seecellC3).
ComputingtheEfficientFrontierofRiskyAssets Theefficientfrontierofriskyassetscanbeconstructedfromanytwoefficientportfolios.Anatural questiontoaskiswhichtwoefficientportfoliosshouldbeused?Ifindthatthefollowingtwoefficient portfoliosleadstotheeasycreationoftheefficientfrontier: 1. Efficientportfolio1:globalminimumvarianceportfolio 2. Efficientportfolio2:efficientportfoliowithtargetexpectedreturnequaltothehighestaverage returnamongtheassetsunderconsideration. Forthecurrentexample,theassetwiththehighestaveragereturnisMicrosoft(averagereturnis4.27%) andwealreadycomputedtheefficientportfoliowithtargetexpectedreturnequaltotheaveragereturn onMicrosoft. Givenanytwoefficientportfolioswithweightvectorsmandytheconvexcombination
z = α ⋅ m + (1 − α ) ⋅ y
foranyconstant α isalsoanefficientportfolio.Theexpectedreturnandvarianceofthisportfolioare
μ p, z = α ⋅ μ p, m + (1 − α ) ⋅ μ p, y σ p2, z = α 2σ 2p, m + (1 − α ) 2σ 2p, y + 2α (1 − α )σ my
,
wherethecovariancebetweenthereturnsonportfoliosmandyiscomputedusing σ my = m′Σy .To createtheefficientfrontier,createagridof α valuesstartingat1anddecreaseinincrementsof0.1. Useasmanyvaluesinthegridasnecessarytomakeaniceplot. Ascreenshotofthepartofthespreadsheettocreatetheseportfoliosisshownbelow.
Considerthefirstconvexcombinationwith α = 1 .Thisportfolioistheglobalminimumvariance portfolio.ThecellP20containstheformula=N20*mupx+O20*mupyfortheexpectedportfolioreturn, andthecellQ20containstheformula=N20^2*sig2px+O20^2*sig2py+2*N20*O20*sigmaxyforthe portfoliovariance.ThecovariancetermsigmaxyiscomputedinthecellR9(notshown)whichcontains thearrayformula{=MMULT(MMULT(TRANSPOSE(mvec),sigma),yvec)}.ThecellsS20:U20givethe weightsintheconvexcombinationcomputedusingthearrayformula {=TRANSPOSE(N20*D10:D12+O20*K10:K12)}.
Theefficientfrontiercanbeplottedbymakingascatterplotwiththeexpectedreturnvalues(cells P20:P50)onthey‐axisandthestandarddeviationvalues(cellsR20:R50)onthehorizontalaxis.
ComputingtheTangencyPortfolio ThetangencyportfolioistheportfolioofriskyassetsthathasthehighestSharpe’sslope.Thisportfolio canbefoundbysolvingtheoptimizationproblem
max t
t′μ − rf 1/ 2
( t′Σ t )
s.t. t ′1 = 1
Thisoptimizationproblemcanalsobeeasilysolvedusingthesolverwithmatrixalgebrafunctions.The screenshotbelowshowshowtoset‐upthisoptimizationprobleminExcel.
TherangeofcellsD33:D35iscalledtvecandwillcontaintheweightsinthetangencyportfoliooncethe solverisrunandthesolutiontotheoptimizationproblemisfound.Beforethesolveristoberun,these cellsshouldcontainaninitialguessoftheminimumvarianceportfolio.Asimpleguessforthisvector whoseweightssumtooneis t msft = 0.3, t nord = 0.3, t sbux = 0.4. ThecomputationofSharpe’sslopeis brokendownintotwopieces.ThefirstpieceisthenumeratorofSharpe’sslope, μ p ,t − rf = t ′μ − rf , andiscomputedincellF33usingthearrayformula{=MMULT(TRANSPOSE(tvec),muvec)‐rf}.Thesecond 2
pieceisthesquareofthedenominatorofSharpe’sslope, σ p ,t = t′Σt ,andiscomputedincellG33using thearrayformula{=MMULT(TRANSPOSE(tvec),MMULT(sigma,tvec))}.Finally,Sharpe’sslopeis evaluatedincellH33usingtheformula=F33/SQRT(G33).Thisisthecellthatispassedtothesolver. Torunthesolver,clickcellH33andthenclickonthesolverbutton.Makesurethesolverdialogueboxis filledouttolookliketheonebelow.
MakesurethattheMaxbuttonisselectedbecausewewanttomaximizetheSharpe’sslope.Torunthe solver,clicktheSolvebutton.Youshouldseeadialogueboxthatsaysthatthesolverfoundasolution andthatalloptimalityconditionsaresatisfied.KeepthesolutionandclickOK.Yourspreadsheetshould lookliketheonebelow.
Thetangencyportfoliohasweights tmsft = 1.03, tnord = −0.32, t sbux = 0.30. NoticethatNordstromissold shortinthisportfoliobecauseithasanegativeweight.Theexpectedreturnonthisportfolio, μ p ,t = t′μ , isgivenincellC36(calledmut)andiscomputedusingthearrayformula {=MMULT(TRANSPOSE(tvec),muvec)}.
ComputingEfficientPortfoliosofTBillsandRiskyAssets Fromthemutualfundseparationtheorem,theefficientportfoliosofT‐Billsandriskyassetsare combinationsofT‐Billsandthetangencyportfolio.Theexpectedreturnandstandarddeviationvalues oftheseportfoliosarecomputedusing
μep = rf + xtan ( μtan − r f ) σ ep = x tanσ tan
Ascreenshotofthespreadsheetwheretheseportfoliosarecomputedisgivenbelow.
Theportfoliowith xtan = 0 isshowninthecellsJ34:L34.TheexpectedreturniscomputedincellK34and isgivenbytheformula=rf+J34*(mut‐rf).ThestandarddeviationiscomputedincellL34andisgivenby theformula=J34*sigt.Thenamedrangesigtisthestandarddeviationofthetangencyportfolioandis givenincellC37.
EfficientPortfolioswithNoShortSalesConstraints Inmanysituationsshortsalesofassetsarenotallowed.Recall,ashortsaleofanassetoccurswhenyou borrowtheassetandthensellit.Theproceedsoftheshortsaleareusuallyusedtofinancethepurchase ofotherassets.Becausetheassetwasborrowediteventuallyhastobereturned.Youdothisby repurchasingtheassetatsometimeinthefutureandthenreturningtheassettowhomeveryou borroweditfrom.Youmakeaprofitonashortsaleifthepriceoftheassetdropsduringtheperiodof timeyouhaveborrowedtheassetbecauseyourepurchasetheassetforapricelessthanforwhatyou originallysoldit.Inthecontextofportfoliotheory,whenyoushortsellanassetthecorresponding portfolioweightisnegative.Hence,whenshortsalesareprohibitedalloftheportfolioweightsmustbe constrainedtobepositive.Thistypeofnon‐negativityconstraintiseasytoimposeinthesolver.
MinimumVariancePortfoliosubjecttoTargetExpectedReturnwithNoShort Sales Aminimumvarianceportfoliowithtargetexpectedreturnequalto μ0 andnoshortsalessolvesthe optimizationproblem
min σ 2p , y = y′Σy s.t. y′μ = μ0 , y′1 = 1 and yi ≥ 0 y
Thisoptimizationproblemcanalsobeeasilysolvedusingthesolverwithmatrixalgebrafunctions.The screenshotbelowshowshowtoset‐upthisoptimizationprobleminExcelwherethetargetexpected returnistheexpectedreturnonMicrosoft(4.27%).Previously,wesolvedthisproblemwherewe allowedforshortsales.Inthatcase,theefficientportfoliowas y msft = 0.83, y nord = −0.09, y sbux = 0.26. NoticethatNordstromwassoldshortinthatportfolio.Nowwewanttoimposethenoshortsales restrictions.WesetuptheExcelspreadsheetexactlyhowwedidbefore.Theonlydifferenceoccursin howweusethesolver.Weaddanadditionalconstraintthatforcesalloftheportfolioweightstobe positive.Thescreenshotbelowshowstheinitialset‐up.
TherangeofcellsAC5:AC7iscalledwvecandwillcontaintheweightsintheefficientportfoliooncethe solverisrunandthesolutiontotheoptimizationproblemisfound.Beforethesolveristoberun,these
cellsshouldcontainaninitialguessoftheminimumvarianceportfolio.Thecellcontaining theformula 2 forportfoliovariance, σ p ,w = w′ Σy ,isincellAG5whichcontainsthearrayformula
{=MMULT(TRANSPOSE(wvec),MMULT(sigma,wvec))} ThecellsAD5andAF5containtheExcelformulas=SUM(wvec)and{=MMULT(TRANSPOSE(wvec), muvec)}thatwillbeusedtoimposetherestrictionsthattheportfolioweightssumtooneandthatthe portfolioexpectedreturnisequaltothetargetreturn.Torunthesolver,clickcellAG5andthenclickon thesolverbutton.Makesurethesolverdialogueboxisfilledouttolookliketheonebelow.
Toaddthenoshortsalesconstraints,clicktheAddbuttontoopentheAddConstraintdialogue.Theno shortsalesconstraintsinequalityconstraintsontheelementsofwvec:
ClickOK.Thefinalsolverdialogueshouldlookliketheonebelow.
Youshouldseeadialogueboxthatsaysthatthesolverfoundasolutionandthatalloptimality conditionsaresatisfied.KeepthesolutionandclickOK.Yourspreadsheetshouldlookliketheone below.
Theefficientportfoliohas100%inMicrosoftand0%intheotherassets....