簡體   English   中英

R中的二叉樹圖

[英]Binomial Tree Plot in R

我對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開始的問題?

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM