Consulting

Results 1 to 2 of 2

Thread: Simulating returns

  1. #1

    Simulating returns

    Hi Guys,

    For an assignment i'm doing, I have to simulate returns which are normally distributed mu, sigma for t periods and then calculating the simulated annualised returns i.e ((1+r1)(1+r2)...(1+rt))^(1/t) -1.

    I have attempting to set up a loop to aggregate the returns but am struggling to get the loop working. Any help would be much appreciated.

    Function RandReturn(mu, sigma)


    RandReturn = WorksheetFunction.Norm_Inv(Rnd, mu, sigma)
    End Function


    Function SimAnnualReturn(mu, sigma, t)


    For i = 1 To t

    SimReturn = SimReturn * (1 + RandReturn(mu, sigma))

    Next

    SimAnnualReturn = (SimReturn ^ (1 / t)) - 1

    End Function

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Not really allowed to do homework assignents here (otherwise how would people learn), but since you did have a specific question about VBA .....


    1. I rearranged the code a little and I like to define everything

    2. Sometimes variables need to be initialized

    3. Hint -- 0.0 x N = 0.0 no matter how many times you do it


    'I like this since it makes me careful with typing and choosing / using variables
    Option Explicit
    
    'I like to define everything as specifically as possible
    Function RandReturn(mu As Double, sigma As Double) As Double
        RandReturn = Application.WorksheetFunction.Norm_Inv(Rnd, mu, sigma)
    End Function
    
    Function SimAnnualReturn(mu As Double, sigma As Double, t As Long) As Double
        Dim i As Long
        Dim SimReturn As Double
        
        SimReturn = 1#    ' 1.0
        
        For i = 1 To t
            SimReturn = SimReturn * (1 + RandReturn(mu, sigma))
        Next i
        SimAnnualReturn = (SimReturn ^ (1 / t)) - 1
    End Function
    
    
    'just a way to test
    Sub Driver()
        MsgBox SimAnnualReturn(0.5, 0.1, 10)
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •