【问题标题】:Monte Carlo Simulation of Probability of Shortfall - VBA短缺概率的蒙特卡罗模拟 - VBA
【发布时间】: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 个输出得到。

有人对可能存在编码问题或输入错误的地方有任何建议吗?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

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

    选择 B1:D1 并输入您的

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

    然后 CSE 它和你有你的号码。

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

    编辑
    我忘了我换行了

    Prshortfall = V
    

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

    因此,此解决方案仅在使用 2 行范围(如 A1:B3)时才有效。对不起:P

    【讨论】:

    • 谢谢!虽然它仍然输出 3 个零,但我不确定这是为什么。
    • @Trevor 我完全错过了这个......我弄乱了你的代码并用Prshortfall = Array(probability, averagevalue, Sqr(variance / n))替换了Prshortfall = V......对不起:P
    【解决方案2】:

    在模块顶部添加Option Base 1

    或者把v线改成

    Dim v(1 to 1,1 to 3)

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

    【讨论】:

      【解决方案3】:

      可能是单元格的格式?

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

      a 包含以下内容:

      您必须掌握 a(1,1)a(1,2)a(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
      

      结果

      【讨论】:

      • 嗯,所以看起来代码正在做我想做的事。这可能是我如何在 Excel 中输入公式的问题吗?现在我只是输入 = Prshortfall(100, 0.02, 0.04, 1, 100),我读到你可能需要按 ctrl + shift + enter 键?
      • 结果是一个数组,其中包含的不仅仅是 一个 数字。试试这个:Prshortfall = V(1, 1) & " | " & V(1, 2) & " | " & V(1, 3) 在你的代码中。现在您将在一个单元格中获得 3 个结果……您必须将每个数字与其他数字分开,并可能通过 VBA 进行设置。另一种方法是将计算拆分为 3 个不同的函数。我已经用这个例子编辑了我的答案。
      • 啊,有趣。这是一种独特的方法。我会做我能做的。
      • Dirk Reichel 的回答是我不知道的。这更容易,你的想法是正确的。 :)
      猜你喜欢
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 2015-02-10
      • 1970-01-01
      相关资源
      最近更新 更多