Compile Error Variable not Defined
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Compile Error Variable not Defined

  1. #1
    Join Date
    Jun 2005
    Posts
    181

    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??


    Code:
    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
    Last edited by Bazman; March 8th, 2010 at 12:14 PM.

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,249

    Re: Compile Error Variable not Defined

    nTsteps is not defined [declared] in the code posted.
    Last edited by DataMiser; March 8th, 2010 at 02:52 PM.

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Compile Error Variable not Defined

    Also, when you define your DIM like this:

    Code:
    Dim dt, t, temp, tau As Double
    The only variable that IS a valid type is tau. the rest are VARIANT

    Code:
    Dim dt As Double, t As Double, temp As Double, tau As Double
    This makes then all Double vs Variant
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jun 2005
    Posts
    181

    Re: Compile Error Variable not Defined

    that's it thank you!!

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)