Consulting

Results 1 to 3 of 3

Thread: Compile Error Variable not Defined

  1. #1
    VBAX Regular
    Joined
    Apr 2009
    Posts
    16
    Location

    Compile Error Variable not Defined

    Hi there,

    I get a complie error, Variable not Defined.

    Ususallty this means thet one of my variables has not been declared.

    It seems to highlight nTsteps at the very point in the code where it is defined?

    very cofused??


    Sub SABRLMM()
    
    Dim fwds, correls, Bm, Cm, hParams, gParams, ffCorrels, vvCorrels, fvCorrels, exps, K, Beta As Variant
    Dim Fn As Object
    Dim nfwds, Nsims, numeraire, i, alpha, x As Integer
    Dim dt, t, temp, tau As Double
    
    Set Fn = Application.WorksheetFunction
    
    fwds = Range("fwds").Value
    nfwds = UBound(fwds)
    
    Dim mu() As Double
    ReDim mu(nfwds)
    
    Dim eta() As Double
    ReDim eta(nfwds)
    
    Dim s() As Double
    ReDim s(nfwds)
    
    Dim totFwds() As Double
    ReDim totFwds(nfwds)
    
    Dim Sumer() As Double
    ReDim Sumer(nfwds)
    
    Nsims = Range("Nsims").Value
    nTsteps = Range("nTsteps").Value
    Beta = Range("Beta").Value
    
    correls = Range("correls").Value
    
    nFactors = Range("correls").Columns.Count
    
    Bm = Range("Bm").Value
    
    Cm = Range("Cm").Value
    
    hParams = Range("hParams").Value
    gParams = Range("gParams").Value
    
    
    ffCorrels = Fn.MMult(Bm, Fn.Transpose(Bm))
    vvCorrels = Fn.MMult(Cm, Fn.Transpose(Cm))
    fvCorrels = Fn.MMult(Bm, Fn.Transpose(Cm))
    
    exps = Range("exps").Value
    
    dt = 1 / (nTsteps \ exps(nfwds, 1))
    
    correls = Range("correls").Value
    
    numeraire = Range("numeraire").Value
    
    K = Range("k").Value
    
    
    
    Dim strike As Double
    strike = 0.05601844
    
    
    
    For x = 1 To Nsims
    
        For i = 1 To nfwds
            totFwds(i) = fwds(i, 1)
        Next i
    
        For t = 0 To exps(nfwds, 1) Step dt
    
            For i = 1 To nfwds
    
                If i > numeraire Then
    
                    temp = 0
    
                        For alpha = numeraire + 1 To i
                
                            tau = exps(alpha, 1) - exps(alpha - 1, 1)
                
                            temp = temp + ffCorrels(i, alpha) * (fwds(alpha, 1) ^ Beta * g(exps(alpha, 1), t, gParams) * K(alpha, 1) * tau) / (1 + tau * fwds(alpha, 1) ^ Beta)
                        
                     Next alpha
            
                    mu(i) = temp * (fwds(i, 1) ^ Beta * g(exps(i, 1), t, gParams) * K(i, 1))
    
                ElseIf i = numeraire Then
            
                        mu(numeraire) = 0
        
        
                ElseIf i < numeraire Then
        
                     temp = 0
    
                        For alpha = i + 1 To numeraire
                
                            tau = exps(alpha, 1) - exps(alpha - 1, 1)
                
                            temp = temp + ffCorrels(i, alpha) * (fwds(alpha, 1) ^ Beta * g(exps(alpha, 1), t, gParams) * K(alpha, 1) * tau) / (1 + tau * fwds(alpha, 1) ^ Beta)
                        
                        Next alpha
            
                    mu(i) = temp * -(fwds(i, 1) ^ Beta * g(exps(i, 1), t, gParams) * K(i, 1))
        
                End If
        
            Next i
        
        
            For i = 1 To nfwds
    
                If i > numeraire Then
    
                    temp = 0
    
                        For alpha = numeraire + 1 To i
                
                            tau = exps(alpha, 1) - exps(alpha - 1, 1)
                
                            temp = temp + fvCorrels(i, alpha) * (fwds(alpha, 1) ^ Beta * g(exps(alpha, 1), t, gParams) * K(alpha, 1) * tau) / (1 + tau * fwds(alpha, 1) ^ Beta)
                        
                        Next alpha
            
                    eta(i) = temp * h(exps(i, 1), t, hParams)
    
                ElseIf i = numeraire Then
            
                        eta(numeraire) = 0
        
        
                ElseIf i < numeraire Then
        
                temp = 0
    
                        For alpha = i + 1 To numeraire
                
                            tau = exps(alpha, 1) - exps(alpha - 1, 1)
                
                            temp = temp + fvCorrels(i, alpha) * (fwds(alpha, 1) ^ Beta * g(exps(alpha, 1), t, gParams) * K(alpha, 1) * tau) / (1 + tau * fwds(alpha, 1) ^ Beta)
                        
                        Next alpha
            
                  eta(i) = -temp * h(exps(i, 1), t, hParams)
        
                End If
        
            Next i
        
      
    
            For i = 1 To nfwds
    
            If totFwds(i) <> 0 Then
            
            
                   s(i) = g(exps(i, 1), t, gParams) * K(i, 1)
    
                        For j = 1 To nFactors
            
                            difFwd = Sqr(dt) * correls(i, j) * Fn.NormInv(Rnd(), 0, 1)
                            difVvol = Sqr(dt) * correls(nfwds + i, j) * Fn.NormInv(Rnd(), 0, 1)
                
                        Next j
        
                    totFwds(i) = totFwds(i) + mu(i) * dt + totFwds(i) ^ Beta * s(i) * difFwd
    
                       If totFwds(i) <= 0 Then
                
                            totFwds(i) = 0
                
                        End If
    
                    K(i, 1) = K(i, 1) + eta(i) * dt + h(exps(i, 1), t, hParams) * difVvol
          
                End If
    
            
            Next i
    
            
    'mu = h(t, , t, a, b, c, d) *k(1,1)
            Next t
     
        For i = 1 To nfwds
            Sumer(i) = Sumer(i) + Fn.Max(totFwds(i) - strike, 0)
        Next i
        
    Next x
    
        For i = 1 To nfwds
            Sumer(i) = Sumer(i) / Nsims
        Next i
        
        
        
    End Sub

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I can't see it dim'ed anywhere in the code.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Apr 2009
    Posts
    16
    Location
    ah Ok found it!

Posting Permissions

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