【问题标题】:Binomial Tree Plot in RR中的二叉树图
【发布时间】:2017-09-09 11:22:16
【问题描述】:

我对 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)

对应的情节是

我有两个问题。

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

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

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

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

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

【问题讨论】:

    标签: r plot data-visualization


    【解决方案1】:

    您必须修改 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))
    

    【讨论】:

    • 这很好用,但对我来说有点复杂。我以非常简单的方式解决了第二个问题,您对解决问题 n 有什么建议吗? 1 不使用长代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2012-01-09
    相关资源
    最近更新 更多