Bazman
03-08-2010, 05:46 PM
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
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