使用二项树模型计算VBA中的路径概率

时间:2018-03-11 23:00:01

标签: excel vba excel-vba probability

对于我的硕士论文,我想用不同的模型为CVR(一种外来衍生物)定价。 我被我的二项式树模型困住了,因为我想计算导致"绿色区域的所有概率的总和" (查找带有模型屏幕截图的链接)例如,如果我以概率pu上升8个节点,以概率下降4次(1-pu),我将到达"绿色区域&#34的上限;。该路径的概率为0.06%。现在我想计算最终进入绿区的所有概率的总和。

我知道excel中有一个binom.dist和combin函数但是我不知道如何将这个问题用于我的问题,因为概率是随时间变化的(上升的第一个概率可以在单元格C12中找到)。 / p>

二叉树的设置如下:

Cell B31是起点,C32是一步,C30是一步。总的来说,二叉树有12个时间步长,导致90个节点。

Binomial Model

1 个答案:

答案 0 :(得分:1)

这是一个VBA函数,它计算二叉树中的所有路径概率。您需要将其与电子表格关联并提取所需的值。代码基于定义Pascal三角形的基本递归关系:

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%。