R中的二项式树图

时间:2017-09-09 11:22:16

标签: r plot data-visualization

我对R中的二叉树图有一点问题;我正在使用包fOptions。鉴于St = 39,K = 40,T1 = 0.5,r = 0.02,sigma = 0.2,n = 2,我使用以下代码:

CRRTree<- BinomialTreeOption(TypeFlag='ce',39,40,0.5,0.02,0.02,0.2,2)
BinomialTreePlot(CRRTree)

并且相应的图是

Binomial Tree Plot

我有两个问题。

首先:我希望x轴从零开始并转到2

第二:我不明白为什么图中没有显示树的上限值;我该怎么解决? 非常感谢你。

编辑:我认为,我以最简单的方式解决了第二个问题。用这种方式对图进行编码就足够了:

BinomialTreePlot(CRRTree,ylim=c(-2,2.5))

还有一种简单的方法可以解决树从0开始的问题吗?

1 个答案:

答案 0 :(得分:2)

您必须修改BinomialTreePlot函数的代码。例如,您可以尝试这样的事情:

my_BinomialTreePlot<-function (BinomialTreeValues, dx = -0.025, dy = 0.4, cex = 1, 
    digits = 2, ...) 
{
    Tree = round(BinomialTreeValues, digits = digits)
    depth = ncol(Tree)
    plot(x = c(0, depth-1), y = c(-depth + 1, depth - 1), type = "n", 
        col = 0, ...)
    points(x = 0, y = 0)
    text(0 + dx, 0 + dy, deparse(Tree[1, 1]), cex = cex)
    for (i in 1:(depth - 1)) {
        y = seq(from = -i, by = 2, length = i + 1)
        x = rep(i, times = length(y)) + 0
        points(x, y, col = 1)
        for (j in 1:length(x)) text(x[j] + dx, y[j] + dy, deparse(Tree[length(x) + 
            1 - j, i + 1]), cex = cex)
        y = (-i):i
        x = rep(c(i, i-1), times = 2 * i)[1:length(y)]
        lines(x, y, col = 2)
    }
    invisible()
}

然后像这样使用它:

CRRTree<- BinomialTreeOption(TypeFlag='ce',39,40,0.5,0.02,0.02,0.2,2)
my_BinomialTreePlot(CRRTree,xlim=c(-0.1,2), ylim=c(-2.5,2.5))

enter image description here