2

im new into Python and i try to figure out how everythings work. I have a little problem with the minimize function of the scipy.optimize package. I try to minimize a given function with some start values but python gives me very high parameter values. This ist my simple code:

import numpy as np
from scipy.optimize import minimize
global array
y_wert = np.array([1,2,3,4,5,6,7,8])
global x_wert
x_wert = np.array([1,2,3,4,5,6,7,8])
def Test(x):
    Summe = 0
    for i in range(0,len(y_wert)):
        Summe = Summe + (y_wert[i] - (x[0]*x_wert[i]+x[1]))
    return(Summe)
x_0 = [1,0]
xopt = minimize(Test,x_0, method='nelder-mead',options={'xatol': 1e-8, 'disp': True})
print(xopt)

If i run this script the best given parameters are:

[1.02325529e+44, 9.52347084e+40]

which really doesnt solve this problem. Ive also try some slightly different startvalues but that doesnt solve my problems. Can anyone give me a clue as to where my mistake lies? Thanks a lot for your help!

1
  • 1
    You want to minimize the absolute value of the sum. Your code minimizes a negative sum to -∞. In Test do: return(abs(Summe)). Also have a look at the output message, it is usually very valuable information. The success field needs checking before interpreting the results. Commented Mar 11, 2020 at 0:41

2 Answers 2

1

Your test function is effectively a straight line with negative gradient so there is no minimum, it's an infinitely decreasing function, that explains your large results, try something like x squared instead

Sign up to request clarification or add additional context in comments.

Comments

0

im new into Python and i try to figure out how everythings work. I have a little problem with the minimize function of the scipy.optimize package. I try to minimize a given function with some start values

you can minimize SSE loss-function as for Ordinary_Least_Squares fitting any model curve:

# # This does not give you the parameter errors though ... you'd have
# to estimate the HESSE matrix separately ...
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import minimize

#######################################
## Nelson_Siegel_Svensson_model
β0 = 0.01
β1 = 0.01
β2 = 0.01
β3 = 0.01
λ0 = 1.00
λ1 = 1.00

TimeVec = np.array([1,2,5,10,25])
YieldVec = np.array([0.0039, 0.0061, 0.0166, 0.0258, 0.0332])

def model(T, c):
    n= (c[0])+(c[1]*((1-np.exp(-T/c[4]))/(T/c[4])))+(c[2]*((((1-np.exp(-T/c[4]))/(T/c[4])))-(np.exp(-T/c[4]))))+(c[3]*((((1-np.exp(-T/c[5]))/(T/c[5])))-(np.exp(-T/c[5]))))

    return n

def loss(params,  TimeVec, YieldVec):
    sse= np.sum((model(TimeVec, params)-YieldVec)**2)
    return sse

c = minimize(loss, np.array([β0, β1, β2, β3, λ0, λ1]),  args = (TimeVec, YieldVec), method="BFGS" , tol= 1.4e-20).x

plt.scatter(TimeVec , YieldVec)
plt.plot(TimeVec , model(TimeVec, c) )
plt.xlabel('Period')
plt.ylabel('Interest (YTD)')
plt.title("Nelson-Siegel-Svensson Model - Fitted Yield Curve", fontsize= 10)
import matplotlib.ticker as mtick
plt.gca().yaxis.set_major_formatter(mtick.PercentFormatter(1))
##ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.tight_layout()
plt.show()

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.