Professional Documents
Culture Documents
TheSolverAddIn
ThesolverisanExcelAddIncreatedbyFrontlineSystems(www.solver.com)thatcanbeusedtosolve generaloptimizationproblemsthatmaybesubjecttocertainkindsofconstraints.Inthisnoteweshow howitcanbeusedtofindportfoliosthatminimizerisksubjecttocertainconstraints. ThesolveraddinmustbeactivatedbeforeitcanbeusedwithinExcel.InExcel2007,youactivateadd insbyclickingontheofficebuttonandthenclickingontheExcelOptionsboxatthebottomofthe menu.
ThisopenstheExceloptionsdialoguebox.ClickAddIns,whichdisplaystheavailableAddInsforExcel. MakesuretheSolverAddInisanActiveApplicationAddIn.
MatrixAlgebrainExcel
Excelhasseveralbuiltinarrayformulasthatcanperformbasicmatrixalgebraoperations.Themain functionsarelistedintablebelow ArrayFunction MINVERSE MMULT TRANSPOSE ToevaluateanarrayfunctioninExcel,youmustusethemagickeystokecombination:<CTRL><SHIFT> <ENTER>(holddownallthreekeysatoncethenrelease). Description Computeinverseofmatrix Matrixmultiplication Computetransposeofmatrix
ExampleData
IntheDatatabofthespreadsheet3firmExample.xlsistheexamplemonthlyreturndataonthreeassets: Microsoft,NordstromandStarbucks.Themonthlymeansandcovariancematrixofthereturnsare computedandthesearereferencedastheinputdataontheportfoliotabasillustratedinthescreen shotbelow.
TheGlobalMinimumVariancePortfolio
Theglobalminimumvarianceportfoliosolvestheoptimizationproblem
2 min p ,m = mm s.t. m1 = 1 m
Thisoptimizationproblemcanbesolvedeasilyusingthesolverwithmatrixalgebrafunctions.The screenshotoftheportfoliotabbelowshowshowtosetupthisoptimizationprobleminExcel.
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 = mm .Noticethat
theformulaissurroundedbycurlybraces{}.Thisindicatesthat<CTRL><Shift><Enter>wasusedto evaluatetheformulasothatitistobeinterpretedasanarrayformula.Ifyoudontseethecurlybraces thentheformulawillnotbeevaluatecorrectly.Wealsoneedacelltocontainaformulathatwillbe usedtoimposetheconstraintthattheportfolioweightssumtoone: m1 = mmsft + mnord + msbux = 1. ThisformulaisspecifiedincellE10as =SUM(mvec) Thesolveraddinislocatedonthedatatabofthetopmenuribbonintherighthandcorner.Torunthe solver,clickthecellcontainingtheformulayouwanttooptimize(cellF10,andnamedsig2px)andthen clickonthesolverbutton.Thiswillopenupthesolverdialogueboxasshownbelow.
TheCellReferencecontainsthecell(E10)thathastheformulafortheconstraint
Torunthesolver,clicktheSolvebutton.Thecomputationisgenerallyveryfast.Ifsuccessful,youshould seethefollowingdialoguebox
MinimumVariancePortfoliosubjecttoTargetExpectedReturn
Aminimumvarianceportfoliowithtargetexpectedreturnequalto 0 solvestheoptimizationproblem
2 min p , y = y y s.t. y = 0 and y1 = 1 y
TherangeofcellsK10:K12iscalledyvecandwillcontaintheweightsintheefficientportfoliooncethe solverisrunandthesolutiontotheoptimizationproblemisfound.Beforethesolveristoberun,these cellsshouldcontainaninitialguessoftheminimumvarianceportfolio.Asimpleguessforthisvector whoseweightssumtooneis ymsft = 0.3, ynord = 0.3, ysbux = 0.4. Thecellcontainingtheformulafor
2 portfoliovariance, p , y = yy ,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 + ysbux = 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
2 2 2 p , z = 2 p ,m + (1 ) 2 p , y + 2 (1 ) my
Theefficientfrontiercanbeplottedbymakingascatterplotwiththeexpectedreturnvalues(cells P20:P50)ontheyaxisandthestandarddeviationvalues(cellsR20:R50)onthehorizontalaxis.
ComputingtheTangencyPortfolio
ThetangencyportfolioistheportfolioofriskyassetsthathasthehighestSharpesslope.Thisportfolio canbefoundbysolvingtheoptimizationproblem
max
t
t rf
( tt )
1/2
s.t. t1 = 1
Thisoptimizationproblemcanalsobeeasilysolvedusingthesolverwithmatrixalgebrafunctions.The screenshotbelowshowshowtosetupthisoptimizationprobleminExcel.
TherangeofcellsD33:D35iscalledtvecandwillcontaintheweightsinthetangencyportfoliooncethe solverisrunandthesolutiontotheoptimizationproblemisfound.Beforethesolveristoberun,these cellsshouldcontainaninitialguessoftheminimumvarianceportfolio.Asimpleguessforthisvector whoseweightssumtooneis tmsft = 0.3, tnord = 0.3, t sbux = 0.4. ThecomputationofSharpesslopeis brokendownintotwopieces.ThefirstpieceisthenumeratorofSharpesslope, p ,t rf = t rf , andiscomputedincellF33usingthearrayformula{=MMULT(TRANSPOSE(tvec),muvec)rf}.Thesecond
2 pieceisthesquareofthedenominatorofSharpesslope, p ,t = tt ,andiscomputedincellG33using
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,theefficientportfoliosofTBillsandriskyassetsare combinationsofTBillsandthetangencyportfolio.Theexpectedreturnandstandarddeviationvalues oftheseportfoliosarecomputedusing
Ascreenshotofthespreadsheetwheretheseportfoliosarecomputedisgivenbelow.
EfficientPortfolioswithNoShortSalesConstraints
Inmanysituationsshortsalesofassetsarenotallowed.Recall,ashortsaleofanassetoccurswhenyou borrowtheassetandthensellit.Theproceedsoftheshortsaleareusuallyusedtofinancethepurchase ofotherassets.Becausetheassetwasborrowediteventuallyhastobereturned.Youdothisby repurchasingtheassetatsometimeinthefutureandthenreturningtheassettowhomeveryou borroweditfrom.Youmakeaprofitonashortsaleifthepriceoftheassetdropsduringtheperiodof timeyouhaveborrowedtheassetbecauseyourepurchasetheassetforapricelessthanforwhatyou originallysoldit.Inthecontextofportfoliotheory,whenyoushortsellanassetthecorresponding portfolioweightisnegative.Hence,whenshortsalesareprohibitedalloftheportfolioweightsmustbe constrainedtobepositive.Thistypeofnonnegativityconstraintiseasytoimposeinthesolver.
Thisoptimizationproblemcanalsobeeasilysolvedusingthesolverwithmatrixalgebrafunctions.The screenshotbelowshowshowtosetupthisoptimizationprobleminExcelwherethetargetexpected returnistheexpectedreturnonMicrosoft(4.27%).Previously,wesolvedthisproblemwherewe allowedforshortsales.Inthatcase,theefficientportfoliowas ymsft = 0.83, ynord = 0.09, ysbux = 0.26. NoticethatNordstromwassoldshortinthatportfolio.Nowwewanttoimposethenoshortsales restrictions.WesetuptheExcelspreadsheetexactlyhowwedidbefore.Theonlydifferenceoccursin howweusethesolver.Weaddanadditionalconstraintthatforcesalloftheportfolioweightstobe positive.Thescreenshotbelowshowstheinitialsetup.
TherangeofcellsAC5:AC7iscalledwvecandwillcontaintheweightsintheefficientportfoliooncethe solverisrunandthesolutiontotheoptimizationproblemisfound.Beforethesolveristoberun,these
cellsshouldcontainaninitialguessoftheminimumvarianceportfolio.Thecellcontainingtheformula
2 forportfoliovariance, p , w = wy ,isincellAG5whichcontainsthearrayformula
Toaddthenoshortsalesconstraints,clicktheAddbuttontoopentheAddConstraintdialogue.Theno shortsalesconstraintsinequalityconstraintsontheelementsofwvec:
ClickOK.Thefinalsolverdialogueshouldlookliketheonebelow.
Theefficientportfoliohas100%inMicrosoftand0%intheotherassets.