Consulting

Results 1 to 6 of 6

Thread: error 1004

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

    error 1004

    Hi there,

    Same code as earlier different problem:

    This code runs fine for the most part but every now and again is stop with error 1004.

    Unable to get the NomInv property of the worksheet function

    At either of the lines:

    difFwd = Sqr(dt) * correls(i, j) * Fn.NormInv(Rnd(), 0, 1)
    difVvol = Sqr(dt) * correls(nfwds + i, j) * Fn.NormInv(Rnd(), 0, 1)
    It seems that some values that rnd() is producing is upsetting NormInv?

    Can someone explain?


    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
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    Perhaps dimming your variables would help? Maybe XL is unsure if the range is a value or string on some occasions? Dave

  3. #3
    VBAX Regular
    Joined
    Apr 2009
    Posts
    16
    Location
    You are right about the dimming but the problem here seems to be that if Rnd=0 then NormInv can't handle it

  4. #4
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Sample workbook?
    Peace of mind is found in some of the strangest places.

  5. #5
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Try dimming Fn as type Variant.
    Peace of mind is found in some of the strangest places.

  6. #6
    VBAX Regular
    Joined
    Apr 2009
    Posts
    16
    Location
    hi there,

    happy to go back and test these thing and share the results, but right now I got a deadline looming.

Posting Permissions

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