【发布时间】:2016-05-29 17:52:31
【问题描述】:
我正在 VBA 中进行一个项目,以确定在给定一系列模拟终端股票价格值的情况下遵循以下公式的短缺概率
St = S0e(µ− σ^2/2)t+σBt 其中 Bt 是标准布朗运动,表示为 sqrt(t)。
下面是我目前做的代码和cmets
Option Explicit
'The following is a subroutine that generates the random terminal stock prices
'An array is passed into it by reference so that the populated values are available to the caller
Private Sub GenerateRandomTerminalStockPrices(ByVal sNaught As Double, _
ByVal r As Double, _
ByVal sigma As Double, _
ByVal t As Double, _
ByVal nSize As Long, _
ByRef STerminal() As Double)
Dim i As Long
Dim Drift As Double
Dim SigmaSqrtT As Double
'This creates the drift term of the stock price
Drift = (r - 0.5 * sigma * sigma) * t
'This creates the Standard Brownian Motion parameter
SigmaSqrtT = sigma * (t ^ 0.5)
'The following creates simulated terminal price values for purposes of Monte Carlo Simulation
For i = 1 To nSize
STerminal(i) = sNaught * Exp(Drift + SigmaSqrtT * Excel.WorksheetFunction.NormSInv(Rnd()))
Next i
End Sub
'Creates the probability of shortfall equation
Function Prshortfall(sNaught As Double, r As Double, sigma As Double, t As Double, n As Double)
'Creating variables of use in the equation
Dim i As Long
Dim V() As Variant
Dim terminalstockprices() As Double
Dim probability As Double
Dim variance As Double
Dim sum As Double
Dim squaredvalue As Double
Dim totalvalue As Double
Dim riskfree As Double
Dim ret As Double
Dim averagevalue As Double
'Specifying parameters of arrays
ReDim V(1, 1 To 3) As Variant
ReDim terminalstockprices(n) As Double
'Setting initial values to 0 for certain variables
riskfree = 0.02
sum = 0#
squaredvalue = 0#
totalvalue = 0#
'Generating the terminal values. Notice that the array is passed by reference
Call GenerateRandomTerminalStockPrices(sNaught, r, sigma, t, n, terminalstockprices)
'Tests each terminal stock price in relation to the risk free rate and keeps count if less than risk free rate
For i = 1 To n
totalvalue = totalvalue + terminalstockprices(i)
squaredvalue = squaredvalue + terminalstockprices(i) * terminalstockprices(i)
ret = (terminalstockprices(i) / sNaught) - 1
If ret < riskfree Then
sum = sum + 1
End If
Next i
'Solves for probability, average price, and price variance based on the outcome of simulation
probability = sum / n
averagevalue = totalvalue / n
variance = (squaredvalue - averagevalue * sum) / (n - 1)
'Outputs the probability, average price, and price standard error
V(1, 1) = probability
V(1, 2) = averagevalue
V(1, 3) = Sqr(variance / n)
Prshortfall = V
End Function
我已经使用 =prshortfall(100,0.02,0.04,1,100) 测试了代码,但在一个单元格中只得到 0 的输出,而不是我期望的概率、平均价格和价格标准误差的 3 个输出得到。
有人对可能存在编码问题或输入错误的地方有任何建议吗?
【问题讨论】: