繁体   English   中英

python scipy-fsolve 不起作用。 为什么 fsolve 返回“无”?

[英]python scipy-fsolve doesn`t work. why fsolve return 'None'?

首先,对不起我糟糕的英语水平。

我制作了 BDT 模型,但这里有问题。

此 function 是第 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

请帮助

以下是阅读错误的方法:关注not iterable 该行中的某些内容正在尝试迭代。 唯一有意义的部分是sum(map(float, final)) 所以我们知道finalNone

finalcalculate的返回值。 查看代码, calculate确实没有返回任何内容,而不是预期的可迭代对象。

在某一时刻, calculate状态

if set_row == 3:
    return cal_temp

但是,对应的 else 递归调用calculate(loop_temp, set_row)丢弃返回值。 您可能希望保留递归返回值并返回它:

return calculate(loop_temp, set_row)

与这些类型的问题一样,错误完全在您的代码中,而不是在您使用的经过彻底测试且通常经过深思熟虑的库中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM