Advertisement

04.16.2007 at 08:02AM PDT, ID: 22513751
[x]
Attachment Details

VB or VBA code to generate random distribution that takes into account the correlation ratio.

Asked by grav0005 in Investment Software, Visual Basic Programming, Math & Science Questions

Tags: vba, random, code, generate

Hi everybody,

My question is part math, part stats, using VBA.  Although I only have an average grasp of mathematical concepts, I'm writing a spreadsheet based on Michael Edleson's Value Averaging book to run investment portfolio simulations.  What I would like some help with is how to calculate the effects of correlation between asset classes in generating my random distributions.

Using one fund is fairly easy to do following Mr. Edleson's instructions.  Here is some VB code that creates the normal random distributions:

    curPrice = Me.txtStartValue
    sglStandardDeviation = Val(Me.txtStandardDeviation) / 100
    sglReturnPerPeriod = Val(Me.txtReturnPerPeriod) / 100
    intNumberOfPeriods = Me.txtPeriods
   
    ReDim aRandomNumbers(1 To intNumberOfPeriods)
    Randomize
    For i = LBound(aRandomNumbers) To UBound(aRandomNumbers)
        'STEP 1: Generate normally distributed random numbers
        aRandomNumbers(i) = NormRand()
       
        'STEP 2: Convert numbers into monthly stock returns
        aRandomNumbers(i) = aRandomNumbers(i) * sglStandardDeviation + sglReturnPerPeriod
       
        'STEP 3: genrerate stock prices
        curPrice = curPrice * (1 + aRandomNumbers(i))
    Next

Here is the code for the NormRand function that I downloaded from a website:

Function NormRand() As Double
    ' NormRand returns a randomly distributed drawing from a
    ' standard normal distribution i.e. one with:
    ' Average = 0 and Standard Deviation = 1.0
    Dim fac As Double, rsq As Double, v1 As Double, v2 As Double
    Static flag As Boolean, gset As Double
   
    ' Each pass through the calculation of the routine produces
    '  two normally-distributed deviates, so we only need to do
    '  the calculations every other call. So we set the flag
    '  variable (to true) if gset contains a spare NormRand value.
    If flag Then
        NormRand = gset
        ' Force calculation next time.
        flag = False
    Else
        ' Don't have anything saved so need to find a pair of values
        ' First generate a co-ordinate pair within the unit circle:
        Do
            v1 = 2 * Rnd - 1#
            v2 = 2 * Rnd - 1#
            rsq = v1 * v1 + v2 * v2
        Loop Until rsq <= 1#
       
        ' Do the Math:
        fac = Sqr(-2# * Log(rsq) / rsq)
       
        ' Return one of the values and save the other (gset) for next time:
        NormRand = v2 * fac
        gset = v1 * fac
        flag = True
    End If    
End Function

Now I want to add more funds to the mix.  Presumably, these are not completely unrelated, as they all have some correlation to each other (IE:  their correlation coefficient is from -1 to + 1).  My question is:  

How would I calculate the random distributions for additional funds while taking into account the correlation coefficient between the funds?

For example, lets say that I add a second fund has a correlation ratio of +0.75.  I would now like to generate a second set of random distributions that take into account the correlation ratio so that it's movements follow the first fund to some extent.

Thanks a lot,

RobStart Free Trial
[+][-]04.16.2007 at 08:58AM PDT, ID: 18918609

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.16.2007 at 09:41AM PDT, ID: 18918940

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.16.2007 at 09:42AM PDT, ID: 18918945

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.16.2007 at 11:16AM PDT, ID: 18919649

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.16.2007 at 11:26AM PDT, ID: 18919716

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Investment Software, Visual Basic Programming, Math & Science Questions
Tags: vba, random, code, generate
Sign Up Now!
Solution Provided By: NovaDenizen
Participating Experts: 1
Solution Grade: A
 
 
[+][-]04.16.2007 at 11:49AM PDT, ID: 18919859

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32