Black-Scholes:VBA代码和R代码之间的区别......哪里出错了?

时间:2012-08-17 08:02:44

标签: r vba

以下VBA代码应该是正确的:

Function BlackScholes(CallPutFlag As String, S As Double, X _
As Double, T As Double, r As Double, v As Double) As Double

Dim d1 As Double, d2 As Double

d1 = (Log(S / X) + (r - v ^ 2 / 2) * T) / (v * Sqr(T))
d2 = d1 - v * Sqr(T)
    If CallPutFlag = "c" Then
     BlackScholes = S * CND(d1) - X * Exp(-r * T) * CND(d2)
    ElseIf CallPutFlag = "p" Then
     BlackScholes = X * Exp(-r * T) * CND(-d2) - S * CND(-d1)
    End If

End Function

'// The cumulative normal distribution function
Public Function CND(X As Double) As Double

Dim L As Double, K As Double
Const a1 = 0.31938153: Const a2 = -0.356563782: Const a3 = 1.781477937:
Const a4 = -1.821255978: Const a5 = 1.330274429

L = Abs(X)
K = 1 / (1 + 0.2316419 * L)
CND = 1 - 1 / Sqr(2 * Application.Pi()) * Exp(-L ^ 2 / 2) * (a1 * K + a2 * K ^ 2 + a3 * K ^ 3 + a4 * K ^ 4 + a5 * K ^ 5)

If X < 0 Then
CND = 1 - CND
End If
End Function

现在我试着在R:

中做到这一点
blackscholes <- function(S, X, rf, T, sigma) {
values <- c(2)

d1 <- (log(S/X)+(rf-sigma^2/2)*T)/sigma*sqrt(T)
d2 <- d1 - sigma * sqrt(T)

values[1] <- S*pnorm(d1) - X*exp(-rf*T)*pnorm(d2)
values[2] <- X*exp(-rf*T) * pnorm(-d2) - S*pnorm(-d1)

values
}

通过以下输入,我得到的结果略有不同:

Spot    2,8
Strike  2,9
Risk free   0,01
T   0,083333333
Sigma   0,2

使用'fOptions'R包结果等于VBA,然后我不明白R代码错误在哪里。

有什么想法吗?

0 个答案:

没有答案