使用二叉树模型计算 VBA 中的路径概率
Calculating Path Probabilities in VBA, using Binomial Tree Model
对于我的硕士论文,我想使用不同的模型为 CVR(一种奇异的衍生品)定价。
我坚持使用二叉树模型,因为我想计算导致 "green zone" 的所有概率的总和(找到带有模型屏幕截图的 link)例如,如果我上升 8概率为 pu 的节点和概率为 (1-pu) 的 4 次下降节点我将到达 "green zone" 的上限。此路径的概率为 0.06%。现在我想计算最终出现在绿色区域的所有概率的总和。
我知道 excel 中有一个 binom.dist 和组合函数,但我不知道如何将其应用于我的问题,因为概率是随时间变化的(上升的第一个概率可以在单元格 C12 中找到)。
二叉树的设置如下:
单元格B31为起点,C32为上一级,C30为下一级。二叉树总共有 12 个时间步,导致 90 个节点。
这是一个 VBA 函数,它计算二叉树中所有路径的概率。您需要 link 它与您的电子表格并提取您需要的值。该代码基于定义帕斯卡三角形的基本递归关系:
Function BTree(probs As Variant) As Variant
'Given a vector of probabilities of successes
'One for each level of the tree,
'returns a 0-based vector consisting of
'path probabilities
'the ith element is the probability
'corresponding to i successes in the path
Dim i As Long, j As Long, n As Long
Dim Level As Long
Dim cLevel As Variant, nLevel As Variant 'current and next level
Dim s As Double, f As Double 'success/failure probs
n = UBound(probs) - LBound(probs) + 1
Level = 0
ReDim nLevel(0 To 0) As Double
nLevel(0) = 1 'root prob at level 0
For i = LBound(probs) To UBound(probs)
Level = Level + 1
cLevel = nLevel
ReDim nLevel(0 To Level)
s = probs(i)
f = 1 - s
nLevel(0) = f * cLevel(0)
nLevel(Level) = s * cLevel(Level - 1)
For j = 1 To Level - 1
nLevel(j) = s * cLevel(j - 1) + f * cLevel(j)
Next j
Next i
BTree = nLevel
End Function
测试如下:
Sub test()
Dim probs As Variant, result As Variant
Dim i As Long
probs = Array(0.7058, 0.7162, 0.7162, 0.7162, 0.7201, 0.7201, 0.7201, 0.7201, 0.7201, 0.7229, 0.7229, 0.7229)
result = BTree(probs)
For i = LBound(result) To UBound(result)
Debug.Print result(i)
Next i
End Sub
输出:
2.45812470386415E-07
7.53656411298726E-06
1.05902304943039E-04
9.01850252454559E-04
5.18379755707418E-03
2.11875025735083E-02
0.063142073263539
0.138243496651607
0.220687443705805
0.250511742599513
0.191938571395668
8.91235060000786E-02
1.89663313192269E-02
您希望索引在 4 到 8 范围内的元素。请注意,您的 6% 概率是不正确的。您将得到 12 个数字(每个级别一个)的乘积,而不是 4 个数字 (8*pu*4*pd)。该特定路径的概率远小于 6%。
对于我的硕士论文,我想使用不同的模型为 CVR(一种奇异的衍生品)定价。 我坚持使用二叉树模型,因为我想计算导致 "green zone" 的所有概率的总和(找到带有模型屏幕截图的 link)例如,如果我上升 8概率为 pu 的节点和概率为 (1-pu) 的 4 次下降节点我将到达 "green zone" 的上限。此路径的概率为 0.06%。现在我想计算最终出现在绿色区域的所有概率的总和。
我知道 excel 中有一个 binom.dist 和组合函数,但我不知道如何将其应用于我的问题,因为概率是随时间变化的(上升的第一个概率可以在单元格 C12 中找到)。
二叉树的设置如下:
单元格B31为起点,C32为上一级,C30为下一级。二叉树总共有 12 个时间步,导致 90 个节点。
这是一个 VBA 函数,它计算二叉树中所有路径的概率。您需要 link 它与您的电子表格并提取您需要的值。该代码基于定义帕斯卡三角形的基本递归关系:
Function BTree(probs As Variant) As Variant
'Given a vector of probabilities of successes
'One for each level of the tree,
'returns a 0-based vector consisting of
'path probabilities
'the ith element is the probability
'corresponding to i successes in the path
Dim i As Long, j As Long, n As Long
Dim Level As Long
Dim cLevel As Variant, nLevel As Variant 'current and next level
Dim s As Double, f As Double 'success/failure probs
n = UBound(probs) - LBound(probs) + 1
Level = 0
ReDim nLevel(0 To 0) As Double
nLevel(0) = 1 'root prob at level 0
For i = LBound(probs) To UBound(probs)
Level = Level + 1
cLevel = nLevel
ReDim nLevel(0 To Level)
s = probs(i)
f = 1 - s
nLevel(0) = f * cLevel(0)
nLevel(Level) = s * cLevel(Level - 1)
For j = 1 To Level - 1
nLevel(j) = s * cLevel(j - 1) + f * cLevel(j)
Next j
Next i
BTree = nLevel
End Function
测试如下:
Sub test()
Dim probs As Variant, result As Variant
Dim i As Long
probs = Array(0.7058, 0.7162, 0.7162, 0.7162, 0.7201, 0.7201, 0.7201, 0.7201, 0.7201, 0.7229, 0.7229, 0.7229)
result = BTree(probs)
For i = LBound(result) To UBound(result)
Debug.Print result(i)
Next i
End Sub
输出:
2.45812470386415E-07
7.53656411298726E-06
1.05902304943039E-04
9.01850252454559E-04
5.18379755707418E-03
2.11875025735083E-02
0.063142073263539
0.138243496651607
0.220687443705805
0.250511742599513
0.191938571395668
8.91235060000786E-02
1.89663313192269E-02
您希望索引在 4 到 8 范围内的元素。请注意,您的 6% 概率是不正确的。您将得到 12 个数字(每个级别一个)的乘积,而不是 4 个数字 (8*pu*4*pd)。该特定路径的概率远小于 6%。