You are on page 1of 8

TheJournalofFinancialModelingandEducationalTechnology Volume1,Number1

GeneratingCorrelatedNormallyDistributedRandomNumbersinExcel

TimothyR.Mayes,MetropolitanStateCollegeofDenver

ABSTRACT
Simulatingassetpricesand/orreturnsisoftenusefulfordemonstrating
importantconceptsinclass.Becausethereturnsonfinancialassetsare
typicallycorrelated,itisusefultobeabletogeneratenewreturnsthat
follow a particular covariance or correlation matrix. However, Excel
has no builtin tools to generate correlated returns. In this paper we
discuss several methods of generating random numbers in Excel, and
showtwomethodsofgeneratingcorrelatedrandomnumbers.

Introduction
Theabilitytogeneraterandomnumbersisveryusefulinfinancialeducation.Forexample,aninstructor
maywishtodemonstratethemeaningofcorrelationcoefficients,howtheefficientfrontierchangesin
response to various correlations, or a variety of other concepts. These illustrations often require the
creationofasetofcorrelatedrandomnumbersthatfollowanormalprobabilitydistribution.Whilethe
normal distribution is not a perfect description of the realworld distributions of asset returns, it is
nonethelessusefulinteachingmanyconcepts.
ThispaperdiscussesseveralmethodsofgeneratingrandomnumbersinExcel,andshowstwomethods
of generating correlated random number series. Producing uncorrelated random numbers in Excel is
easy using builtin tools. However, because various asset classes tend to be correlated, these
uncorrelated random numbers are not of much direct use. Thus techniques for transforming
uncorrelatedrandomnumberseriesintocorrelatedrandomnumberseriesarepresented.

UniformlyDistributedRandomNumbers
Mostpseudorandomnumbergenerationalgorithmsstartbygeneratingaseriesofuniformlydistributed
randomnumbers.Thosenumbersarethentransformedbyanalgorithmintotherequireddistribution
with appropriate parameters. There are at least three builtin methods in Excel for creating uniform
randomnumbers:
1. TheRandandRandBetweenworksheetfunctions.
2. TheRndVBAfunction
3. TheRandomNumberGenerationtoolintheAnalysisToolPakaddin
BoththeRandworksheetfunctionandVBAsRndfunctiongeneraterandomnumbersbetween0and1
from a uniform distribution. The RandBetween worksheet function creates uniformly distributed
random integers that fall into a usersupplied range.1 The worksheet functions are the easiest to use,
but are only useful if the transformation algorithm can be easily implemented within a spreadsheet
(e.g.,theBoxMullertransform).

www.afmetonline.org 59
GeneratingCorrelatedNormallyDistributedRandomNumbersinExcel

For more complicated algorithms (we will use Cholesky Decomposition) some VBA programming is
necessary.Fortunately,itisverylikelythattheVBAcodecanbefoundontheInternetsooneprobably
willnotneedtowritemuchcode.

UsingtheRandomNumberGenerationTool

The Random Number Generation tool is similar to using the worksheet functions, but it can generate
anynumberofobservationsforanynumberofvariablesallatonce.Thistoolhasanadvantageoverthe
builtinworksheetfunctionsinthatonecanspecifyarandomseed.2Furthermore,thistoolcangenerate
randomnumbersfromanyofsevendistributions(e.g.uniform,normal,binomial,etc),includingauser
defineddiscreteprobabilitydistribution.
SincetheExcel2007onlinehelpprovidesonlyaveryshortdescriptionofthistool,wewillprovidean
example of its usage. To launch the tool click the Data tab on the Ribbon and then the Data Analysis
button.AlistoftheavailabletoolsappearsfromwhichoneshouldselectRandomNumberGeneration.
ThiswillleadtothedialogboxshowninFigure1.
Figure1
TheRandomNumberGenerationTool

Thedialogboxissetuptogenerate20observationsof5randomvariablesfromanormaldistribution
with a mean of 0 and a standard deviation of 1. The upper left corner of the output is cell A2 of the
active worksheet. A random seed of 10 was used so that the reader can reproduce the exact results
shownbelow.
It must be stressed that the results shown in Figure 2 are uncorrelated. Additionally, the mean and
standard deviation for each variable cannot be specified independently, so all of the variables are
generatedfromidenticaldistributions.Evenifcorrelationsareunimportantforaparticularproject,this
limitstheusefulnessofthetool.
Becausefinancialassetreturnstendtobecorrelatedwithoneanother,thesetechniquesareusefulonly
as a starting point. They are not a complete solution to the problem at hand; namely, generating
correlatednormallydistributedrandomvariablesthatareusefulfordemonstratingfinancialconceptsin
theclassroomorforresearchpurposes.

60 Winter2010
TheJournalofFinancialModelingandEducationalTechnology Volume1,Number1

Figure2
OutputfromtheRandomNumberGenerationTool

TheBoxMullerTransform
Aspreviouslynoted,manypseudorandomnumbergeneratorstakesetsofuniformlydistributedrandom
numbersasastartingpoint.Thesenumbersarethentransformedtohavetheappropriatedistribution
and parameters. One such transformation is the BoxMuller transform (Box & Muller, 1958), which
producesuncorrelatedbivariatenormallydistributedrandomnumbers(Weisstein).
The algorithm used is simple to implement using only worksheet functions and requires no VBA
programming. First generate two uniformly distributed random variables (U1 and U2) using the Rand
function, and create as many observations as desired.3 Next, create two normally distributed random
numbers (N1 and N2) by applying the BoxMuller transform to the numbers generated in the previous
step.Thetransformationequationsare:

2 ln cos 2 (1)

2 ln sin 2 (2)

N1andN2areuncorrelated,sowewillgenerateathirdvariable(N3)thathasthespecifiedcorrelation
withN1(vandenBerg):

1 (3)

whereisthedesiredcorrelationcoefficient.OncetheN3serieshasbeencreated,wenolongerneed
theN2series.
Atthispointwehavetwonormallydistributedrandomvariables(N1andN3)withaspecifiedcorrelation
coefficient. The next step is to transform each of these number series so that they have the desired
meanandstandarddeviation.BecausethenumbersarecurrentlydistributedasapproximatelyN(0,1),
we can scale them by reversing the standardization formula. Recall that to standardize a number we
use:

www.afmetonline.org 61
GeneratingCorrelatedNormallyDistributedRandomNumbersinExcel

(4)

Solvingforx:
(5)
Using the desired mean and standard deviation for each series, apply that transformation to each
observationoftheseriesN1,andthentoN3.Thisproducestwonormallydistributedrandomserieswith
thespecifiedmeans,standarddeviations,andcorrelationcoefficient.

TransformationUsingtheNormSInvorNormInvWorksheetFunctions
WhiletheBoxMullertransformationiscommonlymentionedinliterature,itismorecomplicatedthan
necessarywhenusingExcel.Therearetwobuiltinworksheetfunctionsthatconvertaprobabilityintoa
drawfromanormaldistribution:
1. NormSInvReturnstheinverseofthestandardnormalcumulativedistributionfunction.
2. NormInv Returns the inverse of the normal cumulative distribution function with a
specifiedmeanandstandarddeviation.
Because the probability is between 0 and 1, we can generate it randomly using the Rand function.
Therefore,togenerateapseudorandomstandardnormalvariable,wecanuse:
=NORMSINV(RAND())
or,tospecifyameanandstandarddeviationof,say,10%and15%wecanuse:
=NORMINV(RAND(),0.10,0.15)
This method is much easier to remember than the formulas for the BoxMuller transformation. Using
theNormSInvfunctionresultsinazscore,whichmustthenbeconvertedtohavetheappropriatemean
andstandarddeviationjustasabove.TheresultoftheNormInvfunctiondoesnotneedtobeconverted,
soitisamoredirectmethod.
Usingeitherfunction,thealgorithmisessentiallythesameaswiththeBoxMullertransform.Wefirst
generatetwouncorrelatednormallydistributedvariables(N1andN2)andthencreateathird(N3)that
hasthespecifiedcorrelationwithN1.

GeneratingReturnsUsingaSpecifiedCovarianceMatrix
Intheprevioussectiontogenerateapairofcorrelatedrandomnumberseriesonlyasinglecorrelation
coefficientisneeded.However,togeneratemultiplerandomnumberseriesthatconformtoaspecified
covariance(orcorrelation)matrix,acovariancematrixmustbecreated.Ifitiscreatedmanually,great
caremustbetakentoensurethatitisconsistent.Soitisofteneasiertocalculateacovariancematrix
fromhistoricaldata.
Onceacovariancematrixisavailable,thefollowingalgorithmcanbeusedtogenerateasetofnormally
distributedrandomnumberswiththecorrect(onaverage)meansandstandarddeviations:

62 Winter2010
TheJournalofFinancialModelingandEducationalTechnology Volume1,Number1

1. CalculatetheCholeskydecomposition(C)ofthecovariancematrix.Thiswillbediscussedin
thenextsection.
2. Generate a set of random numbers from a standard normal distribution (i.e., N(0,1)). This
canbedonewiththeRandomNumberGenerationtoolasdescribedabove,orbyusingthe
worksheetfunction:=NORMSINV(RAND()).CallthismatrixX.
3. Multiply the matrix of standard normal variables by the transpose of the Cholesky matrix:
N=XCT.
Matrix N is the simulated data with appropriate (on average) means, standard deviations, and
covariancematrix.

CholeskyDecompositionoftheCovarianceMatrix

The Cholesky decomposition of a symmetric positive definite matrix is simply the square root of the
matrix.Thus,theCholeskydecompositionofthecovariancematrix()isthematrix(L)thatmeetsthe
followingcriteria:
(6)
The algorithm to find L is wellknown, and source code is freely available on the Internet in many
computer languages. In the sample spreadsheet, we have slightly modified VBA code from
VBNumericalMethods.com(Jacquier,2004),whichisanexcellentresourceforopensourceVBAcode.

CholeskyDecompositionoftheCorrelationMatrix

If one prefers to specify the correlation matrix, instead of the covariance matrix, the process is very
similartothatshownabove,butitrequiresanadjustment.Withoutanadjustmenttheaboveprocedure
leadstoacorrectcorrelationmatrix,butthemeansandstandarddeviationswillbeincorrect.
Step2ofthealgorithmcreatedseveralseriesofstandardnormalrandomnumbersusingtheformula:
=NORMSINV(RAND()).WhenusingtheCholeskydecompositionofthecorrelationmatrix,thenumbers
must be scaled to have the proper means and standard deviations. This can be done using the de
standardizationformulagiveninequation5,andtheworksheetformula:
=J$8+NORMSINV(RAND())*J$9
whereJ$8containsthedesiredmeanandJ$9containsthestandarddeviation.Alternatively,wecanuse
theNormInvfunctionasfollows:
=NORMINV(RAND(),J$8,J$9)
Asidefromthatminorchange,thealgorithmisimplementedinexactlythesamemanner.

SimulatingaPortfolioUsingtheCovarianceMatrix
Imagine that an instructor wishes to simulate the performance of a portfolio comprised of five
exchangetraded funds (ETFs) over time. Assuming that past returns, standard deviations, and
covariancesarerepresentativeofthefuture,historicaldatacanbeusedtodeterminetheseparameters.

www.afmetonline.org 63
GeneratingCorrelatedNormallyDistributedRandomNumbersinExcel

TheCholeskyUsingCovarMatrixworksheetinthesampleworkbookusesmonthlyreturndatafrom
January2004toDecember2008forthefollowingETFs:

AGGiSharesBarclaysAggregateBond(U.S.Bonds)
SPYS&PDepositaryReceipts(U.S.LargecapStocks)
EWJiSharesMSCIJapanIndex
EWGiSharesMSCIGermanyIndex
EWUiSharesMCSIUKIndex
Incalculatingtheportfoliostatistics,itisassumedthattheportfolioisrebalancedeachperiodsoasto
maintainconstantweightings.
Figure3
TheOriginalVariance/CovarianceMatrix

Beginbycreatingavariance/covariancematrixasshowninFigure3usingthehistoricalmonthlyreturns
for the five exchangetraded funds. This matrix could be created from scratch with the desired
characteristics, or, as in the sample workbook, using an addin for Excel 2007 to create a live
variance/covariance matrix that changes if the underlying data is changed (Mayes, 2009). The
variance/covariancematrixcanalsobecreatedusingtheCovariancetoolfromtheAnalysisToolPakadd
in,thoughthismethoddoesnotlinktounderlyingdatathroughformulas.
The sample worksheet makes use of the algorithm above and VBA code to calculate the Cholesky
decomposition of the covariance matrix. Because the result of the Cholesky function is a matrix, the
entireoutputregion(a5x5range)J13:N17mustbeselectedpriortoenteringtheformula.Next,clickin
the formula bar and type: =Cholesky(J3:N7). Instead of pressing the Enter key, the formula must be
arrayentered using Shift+Ctrl+Enter. This fills the selected range with the output of the Cholesky
function,whichisthesquarerootofthecovariancematrixandshouldmatchthatshowninFigure4.
Figure4
CholeskyDecompositionofVariance/CovarianceMatrix

64 Winter2010
TheJournalofFinancialModelingandEducationalTechnology Volume1,Number1

The next step is to create many observations (say, 100) of five standard normal random variables. As
shown above, we can enter the formula: =NORMSINV(RAND()) in a cell and then copy this acrossand
down.
Finally,thesampleworksheetcreates100observationsofthefivecorrelatednormalrandomnumbers
withtheappropriatemeansandstandarddeviationsbyusingtheformula:
=MMULT(O2:S101,TRANSPOSE(I13:M17))
whereO2:S101containsthestandardnormalvariables,andI13:M17istheCholeskydecompositionof
thecovariancematrix.Notethattheaboveformulamustbearrayentered(Shift+Ctrl+Enter)afterfirst
selectingtheentireoutputrange(U2:Y101inthesampleworkbook).
A set of portfolio returns can now be calculated by a weighted average of the randomly generated
returnsover100periods.Fromthisdata,wecancalculatetheportfolioexpectedreturnandstandard
deviation.TheresultsofasingleiterationofthesimulationareshowninFigure5.
Figure5
PortfolioStatisticsfromanIterationoftheSimulation

Inthiscaseequalweightswerechosenforeachsecurity,buttheweightscanbechangedasdesired.A
linechartoftheresultcanbecreatedasshowninFigure6.
In Figure 6 the darkest line is the portfolio, and the lighter lines are the returns of the individual
securities.Thelighterlinesarecreatedbysettingthetransparencyto80%,sothattheycanbeseenbut
dont interfere with the portfolio line. It is obvious that combining the securities into a portfolio has
resultedinsomereductioninvolatility.
Figure6
AChartoftheSimulatedPortfolioOver100Periods

www.afmetonline.org 65
GeneratingCorrelatedNormallyDistributedRandomNumbersinExcel

Summary
This paper demonstrates simple techniques for generating correlated normally distributed random
numbersinExcel.Formanyinclasssimulationsitisusefultocreatetwocorrelatedvariables.Thesecan
be handled by either a BoxMuller transformation or, more easily, by using the NormInv function to
generate two uncorrelated normally distributed random series and then a third via a simple
transformation.Thefirstandthirdnumberswillhavethespecifiedcorrelationonaverage.
When it is necessary to generate several random variables that conform to a specified covariance or
correlationmatrix,wecanusetheCholeskydecomposition.TheVBAcodetoprovidethisdecomposition
is provided in Module1 of the VBA project in the sample workbook. It can be copied into other open
workbooksbydraggingittotheVBAprojectoftheotherworkbookwithintheVBAEditor.

References
Box,G.E.,&Muller,M.E.(1958).Anoteonthegenerationofrandomnormaldeviates.TheAnnalsof
MathematicalStatistics,29,610611.
Haugh,M.(2004).TheMonteCarloFramework,ExamplesfromFinanceandGeneratingCorrelated
RandomVariables.RetrievedAugust31,2009,fromColumbiaUniversity:
http://www.columbia.edu/~mh2078/MCS04.html
Jacquier,A.(2004,November10).Math.RetrievedSeptember11,2009,fromVBNumericalMethods:
http://www.vbnumericalmethods.com/math/
Mayes,T.R.(2009,January7).Variance/CovarianceMatrixAddinforExcel2007.RetrievedSeptember
21,2009,fromTVMCalcs.com:
http://www.tvmcalcs.com/blog/comments/VarianceCovariance_Matrix_Addin_for_Excel_2007/
vandenBerg,M.(n.d.).GeneratingCorrelatedRandomNumbers.RetrievedSeptember9,2009,from
Sitmo.com:http://www.sitmo.com/doc/Generating_Correlated_Random_Numbers
Weisstein,E.W.(n.d.).BoxMullerTransformation.RetrievedSeptember9,2009,fromMathWorldA
WolframWebResource:http://mathworld.wolfram.com/BoxMullerTransformation.html

Endnotes
1
The RandBetween function was formerly a part of the Analysis ToolPak addin that ships with Excel, but isnt
enabled by default. In Excel 2007 this function was promoted from the addin to being built directly into Excel.
UsersofolderversionswillneedtoenabletheAnalysisToolPakaddintousethisfunction.
2
Specifyingarandomseedisusefulforgeneratingidenticalsetsofrandomnumbers.Thisisparticularlyusefulina
classroomsettingaseachstudentcangetpreciselythesameresultsastheinstructor.Notusingthisfeatureofthe
RandomNumberGenerationtoolmeansthatstudentswillallgetuniqueresults,whichcancauseconfusionifit
isntexplainedclearly.
3
TheRandfunctionisvolatile,meaningthatitproducesadifferentrandomnumbereachtimetheworksheetis
recalculated. To eliminate this problem, select the cells containing the Rand function and copy them. Now, use
PasteSpecialValuestolockintheoriginalnumbers.Analternativemethod,whichwillpreservethefunction,is
tosetthecalculationmodetomanual.ThiswillpreventrecalculationuntiltheuserpressestheF9functionkey.

66 Winter2010

You might also like