MMult not Working
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: MMult not Working

  1. #1
    Join Date
    Jun 2005
    Posts
    181

    MMult not Working

    Hi there,

    In the following when I try to use the following

    CorRands = Fn.MMult(cholesky, rands)

    I get the error run-time error 1004 unable to get the MMult property of the WorksheetFunction class.

    I assume its because cholesky was read in as a Range from the spredsheet and rands is a VBA array. Can anyone tell me how to fix?

    Code:
    Sub SABRLMM()
    
    Dim fwds, correls, Bm, Cm, hParams, gParams, ffCorrels, vvCorrels, fvCorrels, exps, k, nTsteps, Beta, cholesky As Variant
    Dim Fn As Object
    Dim nfwds As Integer, numeraire As Integer, i As Integer, j As Integer, alpha As Integer, nfactors As Integer, Nf As Integer, Nv As Integer
    Dim dt As Double, T As Double, temp As Double, tau As Double, difFwd As Double, difVvol As Double, rand1 As Double, rand2 As Double, Nsims As Double, X 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)
    
    Dim square() As Double
    ReDim square(nfwds)
    
    Dim SD() As Double
    ReDim SD(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
    Nf = Range("Bm").Columns.Count
    
    Cm = Range("Cm").Value
    Nv = Range("Cm").Columns.Count
    
    nfactors = Nf + Nv
    
    
    Dim rands() As Double
    ReDim rands(nfactors - 1)
    
    Dim CorRands() As Double
    ReDim CorRands(nfactors)
    
    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
    
    cholesky = Range("cholesky").Value
    
    Dim strike As Double
    strike = 0.05601844
    
    For i = 1 To nfwds
    mu(i) = 0
    eta(i) = 0
    s(i) = 0
    totFwds(i) = 0
    Sumer(i) = 0
    square(i) = 0
    SD(i) = 0
    Next i
    
    
    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 = 0 To nfactors - 1
                                
                            rand1 = Rnd()
                            'rand2 = Rnd()
                            
                            Do While rand1 = 0
                            rand1 = Rnd()
                            Loop
                            
                            
                            'Do While rand2 = 0
                            'rand2 = Rnd()
                            'Loop
                            rands(j) = Fn.NormInv(rand1, 0, 1)
                        
                        Next j
        
                    CorRands = Fn.MMult(cholesky, rands)
                    'ffCorrels = Fn.MMult(Bm, Fn.Transpose(Bm))
                           For j = 1 To Nf
                                                                          
                            difFwd = Sqr(dt) * correls(i, j) * CorRands(j) 'BoxMuller(Halton(i, 3), Halton(i, 5))
                            
                        Next j
                    
                    
                        
                           For j = 1 To Nv
                              
                                                                          
                            difVvol = Sqr(dt) * correls(nfwds + i, j) * CorRands(Nf + j) 'BoxMuller(Halton(i, 3), Halton(i, 5)) '
                
                        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) + totFwds(i) - strike
            square(i) = square(i) + Sumer(i) ^ 2
        Next i
        
    Next X
    
        
        
        For i = 1 To nfwds
        SD(i) = (Sqr(square(i) - (1 / Nsims) * Sumer(i) ^ 2)) / (Nsims - 1)
        Next i
        
        For i = 1 To nfwds
               Sumer(i) = Sumer(i) / Nsims
        Next i
        
    End Sub

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: MMult not Working

    Where is this Fn class?
    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!

  3. #3
    Join Date
    Jun 2005
    Posts
    181

    Re: MMult not Working

    Set Fn = Application.WorksheetFunction

    Its no a new class just a market for the exisiting one

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: MMult not Working

    I still do not understand the error message.
    error 1004 unable to get the MMult property of the WorksheetFunction class...
    Does it say there is no MMult property, or does the call of this property (or method) produce the error?
    What type of parameters does the MMult expect?

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: MMult not Working

    Well, the first problem is the declaration:

    You have this:
    Code:
    Dim fwds, correls, Bm, Cm, hParams, gParams, ffCorrels, vvCorrels 
    
    Dim Fn As Object
    Dim dt As Double, T As Double, temp As Double
    fwds, correls, and the rest are VARIANTS. If you want to give them a specific type, you need to do so like you did in the other two statements.

    fwds as Double, correls as Long, etc...
    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!

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 is a CodeGuru survey question.


Featured


HTML5 Development Center