重组二项式价格格

python - 在二项式图中显示绝对值-LMLPHP

嗨,我想在此图的每个节点中显示绝对值(数字)。我尝试绘制每个节点的值,但是发生了错误。
谁能帮助我解决这个问题。感谢您的帮助。期待您的帮助。

from math import *
import pylab as z;
import numpy as np;

def PriceMovements(S0, down, up, totalsteps, upsteps):
    S = S0*(pow(up, upsteps))*(pow(down, totalsteps-upsteps))
    return S

def binomial(d, u, p):
    g = np.random.binomial(1,p)

    if g == 1:
        return u
    else:
        return d

#Use the following numbers to console the setting for binomial graph

nodes = 8 #Nodes
S = 1080.0 #Initial spot price
u = 1.1346 #Up factor
d = 0.8814 #Down factor
p = 0.7844 # Probability
r = 1.08 #1+Interest rate
n = 3 #Steps

numberofpaths = 2**nodes
valuelist = [] #Emptylist
z.figure(0) #Generating the figure
temp = S #Temporary variable

for i in range(0, numberofpaths, 1):
    valuelist =[]
    S = temp;
    for c in range(0, nodes + 1, 1):
        valuelist.append(S)
        S = S*binomial(d, u, p)
    z.plot(range(0, nodes + 1, 1), valuelist)

z.title("Price movements")
z.xlabel("Nodes")
z.ylabel("S(n)")
z.show()

最佳答案

使用text

# after z.plot(range(0, nodes + 1, 1), valuelist)
for i in range(nodes):
    z.text(i+.2,valuelist[i]-25,'{:4.1f}'.format(valuelist[i]))

对于
python - 在二项式图中显示绝对值-LMLPHP
只需调整偏移量即可获得最佳外观。

关于python - 在二项式图中显示绝对值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35276284/

10-10 07:52