短缺概率的蒙特卡罗模拟 – VBA

我正在做一个VBA项目,根据下面的公式计算一系列terminal股价的模拟值,以此来确定亏空的概率

St = S0e(μ-σ^ 2/2)t +σBt其中Bt是标准布朗运动,为sqrt(t)。

以下是我迄今为止所做的代码和评论

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)testing了代码,但是在一个单元格中只输出了0,而不是我期望得到的3个输出的概率,平均价格和价格标准差。

有没有人有任何build议可能会出现编码问题或input错误?

在模块顶部添加Option Base 1

或将v行更改为

Dim v(1 to 1,1 to 3)

对不起,我删除了第一个Dim v()As Variant,在我的版本中,我没有使用Redimming。

简单的答案是你需要一个数组来获得输出。

selectB1:D1并input你的

 =prshortfall(100,0.02,0.04,1,100) 

然后CSE,你有你的号码。

数组公式需要通过ctrl + shift + enter确认

在这里输入图像说明

编辑
我忘记了我改变了线路

 Prshortfall = V 

 Prshortfall = Array(probability, averagevalue, Sqr(variance / n)) 

所以这个解决scheme只适用于2行的范围(如A1:B3)。 对不起:P

也许这是单元格的格式?

 Sub Test() Dim a As Variant a = Prshortfall(100, 0.02, 0.04, 1, 100) End Sub 

一个包含以下内容:

一个包含

你必须赶上(1,1)(1,2)(1,3)的值 ,并将它们设置在单元格中。 可能需要将这些单元格的格式更改为带小数点的数字

编辑

 '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) '3 results are in one variable and is the return value Prshortfall = V(1, 1) & " " & V(1, 2) & " " & V(1, 3) End Function 

结果

结果在一个单元格中