Professional Documents
Culture Documents
Efficient Portfolios in Excel Using The Solver and Matrix Algebra
Efficient Portfolios in Excel Using The Solver and Matrix Algebra
SolverandMatrixAlgebra
ThisnoteoutlineshowtousethesolverandmatrixalgebrainExceltocomputeefficientportfolios.The
exampleusedinthisnoteisinthespreadsheet3firmExample.xlsx,andisthesameexampleusedinthe
lecturenotestitledPortfolioTheorywithMatrixAlgebra.
Lastupdated:November24,2009
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
Description
Computeinverseofmatrix
Matrixmultiplication
Computetransposeofmatrix
ToevaluateanarrayfunctioninExcel,youmustusethemagickeystokecombination:<CTRL><SHIFT>
<ENTER>(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 p2 ,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.
ThefieldnamedSetTargetCellmustcontaineitherthenameorthereferencetothecellcontainingthe
formulatooptimize.Youhavethreechoicesforthetypeofoptimization:Max,MinandValueof.Here,
wewanttominimizetheportfoliovariancesoMinshouldbeselected.Next,wemustspecifythecells
containingthevariableswhicharebeingoptimized.ThesearespecifiedintheByChangingCellsfield.
Here,wecantypeinthenamemvecorspecifytherangeofcellsD10:D12.Finally,wemustAddthe
constraintthattheweightssumtoone.WedothisbyclickingtheAddbutton,whichopenstheAdd
Constraintdialogueboxshowbelow.
TheCellReferencecontainsthecell(E10)thathastheformulafortheconstraint
Torunthesolver,clicktheSolvebutton.Thecomputationisgenerallyveryfast.Ifsuccessful,youshould
seethefollowingdialoguebox
ThemessageSolverfoundasolution.Allconstraintsandoptimalityconditionsaresatisfiedmeansthat
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
Thisoptimizationproblemcanalsobeeasilysolvedusingthesolverwithmatrixalgebrafunctions.The
screenshotbelowshowshowtosetupthisoptimizationprobleminExcelwherethetargetexpected
returnistheexpectedreturnonMicrosoft(4.27%).
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.
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 p2 ,m + (1 ) 2 p2 , y + 2 (1 ) my
wherethecovariancebetweenthereturnsonportfoliosmandyiscomputedusing my = my .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)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
thearrayformula{=MMULT(TRANSPOSE(tvec),MMULT(sigma,tvec))}.Finally,Sharpesslopeis
evaluatedincellH33usingtheformula=F33/SQRT(G33).Thisisthecellthatispassedtothesolver.
Torunthesolver,clickcellH33andthenclickonthesolverbutton.Makesurethesolverdialogueboxis
filledouttolookliketheonebelow.
MakesurethattheMaxbuttonisselectedbecausewewanttomaximizetheSharpesslope.Torunthe
solver,clicktheSolvebutton.Youshouldseeadialogueboxthatsaysthatthesolverfoundasolution
andthatalloptimalityconditionsaresatisfied.KeepthesolutionandclickOK.Yourspreadsheetshould
lookliketheonebelow.
ComputingEfficientPortfoliosofTBillsandRiskyAssets
Fromthemutualfundseparationtheorem,theefficientportfoliosofTBillsandriskyassetsare
combinationsofTBillsandthetangencyportfolio.Theexpectedreturnandstandarddeviationvalues
oftheseportfoliosarecomputedusing
ep = rf + xtan ( tan rf )
ep = xtan tan
Ascreenshotofthespreadsheetwheretheseportfoliosarecomputedisgivenbelow.
EfficientPortfolioswithNoShortSalesConstraints
Inmanysituationsshortsalesofassetsarenotallowed.Recall,ashortsaleofanassetoccurswhenyou
borrowtheassetandthensellit.Theproceedsoftheshortsaleareusuallyusedtofinancethepurchase
ofotherassets.Becausetheassetwasborrowediteventuallyhastobereturned.Youdothisby
repurchasingtheassetatsometimeinthefutureandthenreturningtheassettowhomeveryou
borroweditfrom.Youmakeaprofitonashortsaleifthepriceoftheassetdropsduringtheperiodof
timeyouhaveborrowedtheassetbecauseyourepurchasetheassetforapricelessthanforwhatyou
originallysoldit.Inthecontextofportfoliotheory,whenyoushortsellanassetthecorresponding
portfolioweightisnegative.Hence,whenshortsalesareprohibitedalloftheportfolioweightsmustbe
constrainedtobepositive.Thistypeofnonnegativityconstraintiseasytoimposeinthesolver.
MinimumVariancePortfoliosubjecttoTargetExpectedReturnwithNoShort
Sales
Aminimumvarianceportfoliowithtargetexpectedreturnequalto 0 andnoshortsalessolvesthe
optimizationproblem
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
{=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.