sung-kwang
sung-kwang

Reputation: 23

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

first, sorry about my poor English skill.

I make BDT-model, but something problem here.

this function is change code for column 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

table before

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

table after

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

I try transform to loop, but not working.

this is my code

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

loop n=2,3 is fine. but loop n=4 something wrong

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

plz help

Upvotes: 2

Views: 224

Answers (1)

Mad Physicist
Mad Physicist

Reputation: 114578

Here's how to read the error: focus on not iterable. Something in the line is attempting to iterate. The only part that makes sense for is sum(map(float, final)). So we know that final is None.

final is the return value of calculate. Looking at the code, calculate does indeed return nothing, rather than the expected iterable.

At one point, calculate states

if set_row == 3:
    return cal_temp

However, the corresponding else recursively calls calculate(loop_temp, set_row) and discards the return value. You probably want to keep the recursive return value and return it:

return calculate(loop_temp, set_row)

As usual with these types of problems, the error is entirely in your code, and not in the thoroughly tested and generally well thought-out libraries you are using.

Upvotes: 1

Related Questions