【发布时间】:2021-01-07 01:10:48
【问题描述】:
首先,对不起我的英语水平不好。
我制作了 BDT 模型,但这里出了点问题。
此函数是第 4 列的更改代码
def f(x):
a=1 / (1 + f_tree.loc[1, 4] + x)
b=1 / (1 + f_tree.loc[2, 4] + x)
c=1 / (1 + f_tree.loc[3, 4] + x)
d=1 / (1 + f_tree.loc[4, 4] + x)
e=0.5*(a+b)/ (1 + f_tree.loc[1, 3])
f=0.5*(b+c)/ (1 + f_tree.loc[2, 3])
g=0.5*(c+d)/ (1 + f_tree.loc[3, 3])
h=0.5*(e+f)/ (1 + f_tree.loc[1, 2])
i=0.5*(f+g)/ (1 + f_tree.loc[2, 2])
return -Market_data['PV'][3] + (0.5 * (h+i) / (1+f_tree.loc[1,1]))
x=fsolve(f,0)
x
f_tree.loc[1,4]+=x
f_tree.loc[2,4]+=x
f_tree.loc[3,4]+=x
f_tree.loc[4,4]+=x
f_tree
之前的表
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | 0.05 | 0.065334 | 0.081673 | 0.067493 | 0.074591 |
| 2 | 0.00 | 0.055317 | 0.070603 | 0.055259 | 0.061070 |
| 3 | 0.00 | 0.000000 | 0.061539 | 0.045242 | 0.050000 |
| 4 | 0.00 | 0.000000 | 0.000000 | 0.037041 | 0.040937 |
| 5 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.033516 |
表后
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | 0.05 | 0.065334 | 0.081673 | 0.099235 | 0.074591 |
| 2 | 0.00 | 0.055317 | 0.070603 | 0.087001 | 0.061070 |
| 3 | 0.00 | 0.000000 | 0.061539 | 0.076984 | 0.050000 |
| 4 | 0.00 | 0.000000 | 0.000000 | 0.068783 | 0.040937 |
| 5 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.033516 |
我尝试转换为循环,但不起作用。
这是我的代码
import numpy as np
import pandas as pd
import math as m
from scipy.optimize import fsolve
global cal_temp, loop_temp
cal_temp =[]
loop_temp = []
list_temp =[]
final_list = []
# find x
def f2(x):
temp_val = 0
temp_val = (1 / (1 + f_tree.loc[1, 2] + x)) + (1 / (1 + f_tree.loc[2, 2] + x))
return -pv[1] + (0.5 * (temp_val / (1+f_tree.loc[1,1])))
def f(x):
for j in range(1, n+1):
i = 1
while i <= j:
f_tree_x.loc[i,j] = 1 / (1 + f_tree.loc[i, j] + x)
i += 1
list_temp = [value for value in list(np.array(f_tree_x[set_row].tolist())) if value != 0]
final = calculate(list_temp, set_row)
return -pv[(set_row-1)] + (0.5 * sum(map(float, final)) / (1+f_tree.loc[1,1]))
def calculate(take_list, set_row):
reset = []
cal_temp = reset
for row_no in range(set_row-1):
cal_temp.append(0.5 * (take_list[row_no] + take_list[row_no+1]) / (1 + f_tree.loc[row_no+1, set_row-1]))
if set_row == 3:
return cal_temp
else:
set_row -= 1
loop_temp = cal_temp
calculate(loop_temp, set_row)
# main
n = int(input('typing year(2~10)'))
sr = [0.050, 0.055, 0.060, 0.070, 0.080, 0.090, 0.100, 0.110, 0.120, 0.130, 0.140]
s_fir = [0.050000, 0.060024, 0.070071, 0.142293, 0.130935, 0.151393, 0.171938, 0.192568, 0.213282, 0.234078]
pv= [0.952381, 0.898452, 0.839619, 0.735030, 0.649931, 0.564474, 0.481658, 0.403883, 0.332885, 0.269744]
V = 0.1
u = m.exp(V)
d = 1/u
f_tree = pd.DataFrame(np.zeros((n,n)), index=range(1,n+1), columns=range(1,n+1), dtype=float)
f_tree_x = pd.DataFrame(np.zeros((n,n)), index=range(1,n+1), columns=range(1,n+1), dtype=float)
for j in range(1, n+1):
i = 1
while i <= j:
f_tree.loc[i,j] = s_fir[0] * (u**(j-i)) * (d**(i-1))
i += 1
for set_row in range(1, n+1):
if set_row == 1:
print(f_tree)
continue
if set_row == 2:
x = fsolve(f2, 0)
print(x)
w = 1
while w <= set_row:
f_tree.loc[w, set_row] += x
w += 1
if set_row >= 3:
x = fsolve(f, 0)
print('x')
print(x)
# fsolve(f, 0)
w = 1
while w <= set_row:
f_tree.loc[w, set_row] += x
w += 1
循环 n=2,3 很好。但是循环 n=4 出了点问题
Traceback (most recent call last):
File "d:\BDT_model\BDT_3.py", line 102, in <module>
x = fsolve(f, 0)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\scipy\optimize\minpack.py", line 160, in fsolve
res = _root_hybr(func, x0, args, jac=fprime, **options)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\scipy\optimize\minpack.py", line 226, in _root_hybr
shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\scipy\optimize\minpack.py", line 24, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "d:\BDT_model\BDT_3.py", line 33, in f
return -pv[(set_row-1)] + (0.5 * sum(map(float, final)) / (1+f_tree.loc[1,1]))
TypeError: 'NoneType' object is not iterable
请帮忙
【问题讨论】:
-
基于错误,
calculate正在返回None而不是您尝试求和的迭代。这就是你告诉它要做的事情。不确定您还期望发生什么。 -
我投票结束是一个错字,但也赞成,因为你毕竟发布了足够的信息来回答这个问题。
-
我还是不明白。如果错误完全在我的代码中,为什么 'set_row=3' 循环返回正确的值?
-
顺便问一下,你为什么把同一个引用折腾这么多次?
cal_temp = reset和loop_temp = cal_temp使三个名称指向同一个对象。如果你去掉其中的两个,你将更容易遵循代码。 -
我已经更新了答案。我真的不知道是什么绊倒你,所以我把它拼出来