Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

matplotlib - how to plot binomial tree with arrays in python

I have a two tridiagonal matrix that (with equal dimensions) and want to plot it like the binomial tree in the picture below in python.

Binomial tree enter image description here

here is my code :

stockPrice = 100; u = 1.2; d = 0.8; n = 5; k=80; r = 0.1; prob = 0.5;

for i in range(n):
    for j in range(n):
        if i==j:
            if i==0:
                s[i,j] = stockprice
            else:
                s[i,j] = s[i-1,j-1]*d
        if j>i:
            s[i,j] = s[i,j-1]*u

for i in range(n):
    for j in range(n):
        if putcall =='c':
            payoff[i,j] = max(s[i,j]-k,0)
        elif putcall=='p':
            payoff[i,j] = max(k-s[i,j],0)

for i in range(n-1):
    for j in range(n-1):
        if putcall=='c':
            f[i,j] = (prob*payoff[i,j+1] + (1-prob)*payoff[i+1,j+1])*np.exp(-r*t)
        elif putcall=='p':
            f[i,j] = (prob*payoff[i,j+1] + (1-prob)*payoff[i+1,j+1])*np.exp(-r*t)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...