最小化带有 scipy 参数的函数
Minimize a function with parameters with scipy
我想问一下如何用scipyminimize中的优化方法来最小化一个函数(误差平方和)。我试过了,但似乎我做得不好,因为错误和参数不会从初始值改变。
这是我的代码:
def objective(p, y):
y = np.array([98.494500, 97.828500, 97.610000, 97.314000, 97.014500, 92.959000, 96.696222])
p = beta0, beta1, beta2, beta3, tau1, tau2
return (((100 * DiscountFactor('rate',np.exp(p[0] + (p[1]+ p[2]) * (1 - np.exp(-yearfractionTenors()/p[4])) * p[4]/yearfractionTenors() - p[2] * np.exp(-yearfractionTenors()/p[4]) + p[3] * (1 - np.exp(-yearfractionTenors()/p[5])) * p[5] / yearfractionTenors() - p[3] * np.exp(-yearfractionTenors() / p[5])) -1, fecha_valoracion, maturity, composition= 'linear', basis= 'act/360').result) - y) ** 2).sum()
x0 = np.array([0.03, -0.03, 0, 0, 1, 1]) #Initial values of beta0, beta1, beta2, beta3, tau1 and tau2
res = optimize.minimize(objective, x0, args = y)
print(res)
output: fun: 64.30571361326217
hess_inv: array([[1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1]])
jac: array([0., 0., 0., 0., 0., 0.])
message: 'Optimization terminated successfully.'
nfev: 8
nit: 0
njev: 1
status: 0
success: True
x: array([ 0.03, -0.03, 0. , 0. , 1. , 1. ])
看来我的错误是我没有正确使用输入值(初始值)。我想知道是否有人可以帮助我解决这个问题。
基本上,我想知道如何通过更改初始数组中的参数来最小化函数。也许错误出在 objective 函数中。
问题的一个小例子:
def objective(p, y):
y = np.array([98.494500, 97.828500, 97.610000, 97.314000, 97.014500, 92.959000, 96.696222])
p = beta0, beta1, beta2, beta3, tau1, tau2
return (((100 * DiscountFactor('rate',np.exp(p[0] + (p[1]+ p[2]) * (1 - np.exp(-yearfractionTenors()/p[4])) * p[4]/yearfractionTenors() - p[2] * np.exp(-yearfractionTenors()/p[4]) + p[3] * (1 - np.exp(-yearfractionTenors()/p[5])) * p[5] / yearfractionTenors() - p[3] * np.exp(-yearfractionTenors() / p[5])) -1, fecha_valoracion, maturity, composition= 'linear', basis= 'act/360').result) - y) ** 2).sum()
x0 = np.array([0.03, -0.03, 0, 0, 1, 1]) #Initial values of beta0, beta1, beta2, beta3, tau1 and tau2
res = optimize.minimize(objective, x0, args = y)
print(res)
折扣因子函数无关紧要,但这里是 class 您需要 运行 它:
class DiscountFactor:
def __init__(self, val_given, value, start_date, end_date, composition, basis):
self.start_date = start_date
self.end_date = end_date
self.composition = composition
self.basis = basis
self.yf = year_fraction(start_date, end_date, basis)
if val_given == 'rate':
self.rate_to_df(value)
else:
raise ValueError('val_given must be: rate or df' )
def rate_to_df(self, rate):
if self.composition == 'linear':
df = 1/( 1 + rate*self.yf)
else:
raise ValueError('composition must be one of the following: linear, yearly, biannual, continuous')
self.result = df
return self.result
作为 Askold Ilvento,您正在函数范围内重新定义参数。这将使优化失败,因为该函数将始终产生相同的结果;这也是一种不好的做法,也是潜在的错误来源。尝试:
def objective(p, y):
return (((100 * DiscountFactor('rate',np.exp(p[0] + (p[1]+ p[2]) * (1 - np.exp(-yearfractionTenors()/p[4])) * p[4]/yearfractionTenors() - p[2] * np.exp(-yearfractionTenors()/p[4]) + p[3] * (1 - np.exp(-yearfractionTenors()/p[5])) * p[5] / yearfractionTenors() - p[3] * np.exp(-yearfractionTenors() / p[5])) -1, fecha_valoracion, maturity, composition= 'linear', basis= 'act/360').result) - y) ** 2).sum()
x0 = np.array([0.03, -0.03, 0, 0, 1, 1]) #Initial values of beta0, beta1, beta2, beta3, tau1 and tau2
y = np.array([98.494500, 97.828500, 97.610000, 97.314000, 97.014500, 92.959000, 96.696222])
beta0, beta1, beta2, beta3, tau1, tau2 = optimize.minimize(objective, x0, args = y)
我想问一下如何用scipyminimize中的优化方法来最小化一个函数(误差平方和)。我试过了,但似乎我做得不好,因为错误和参数不会从初始值改变。 这是我的代码:
def objective(p, y):
y = np.array([98.494500, 97.828500, 97.610000, 97.314000, 97.014500, 92.959000, 96.696222])
p = beta0, beta1, beta2, beta3, tau1, tau2
return (((100 * DiscountFactor('rate',np.exp(p[0] + (p[1]+ p[2]) * (1 - np.exp(-yearfractionTenors()/p[4])) * p[4]/yearfractionTenors() - p[2] * np.exp(-yearfractionTenors()/p[4]) + p[3] * (1 - np.exp(-yearfractionTenors()/p[5])) * p[5] / yearfractionTenors() - p[3] * np.exp(-yearfractionTenors() / p[5])) -1, fecha_valoracion, maturity, composition= 'linear', basis= 'act/360').result) - y) ** 2).sum()
x0 = np.array([0.03, -0.03, 0, 0, 1, 1]) #Initial values of beta0, beta1, beta2, beta3, tau1 and tau2
res = optimize.minimize(objective, x0, args = y)
print(res)
output: fun: 64.30571361326217
hess_inv: array([[1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1]])
jac: array([0., 0., 0., 0., 0., 0.])
message: 'Optimization terminated successfully.'
nfev: 8
nit: 0
njev: 1
status: 0
success: True
x: array([ 0.03, -0.03, 0. , 0. , 1. , 1. ])
看来我的错误是我没有正确使用输入值(初始值)。我想知道是否有人可以帮助我解决这个问题。 基本上,我想知道如何通过更改初始数组中的参数来最小化函数。也许错误出在 objective 函数中。 问题的一个小例子:
def objective(p, y):
y = np.array([98.494500, 97.828500, 97.610000, 97.314000, 97.014500, 92.959000, 96.696222])
p = beta0, beta1, beta2, beta3, tau1, tau2
return (((100 * DiscountFactor('rate',np.exp(p[0] + (p[1]+ p[2]) * (1 - np.exp(-yearfractionTenors()/p[4])) * p[4]/yearfractionTenors() - p[2] * np.exp(-yearfractionTenors()/p[4]) + p[3] * (1 - np.exp(-yearfractionTenors()/p[5])) * p[5] / yearfractionTenors() - p[3] * np.exp(-yearfractionTenors() / p[5])) -1, fecha_valoracion, maturity, composition= 'linear', basis= 'act/360').result) - y) ** 2).sum()
x0 = np.array([0.03, -0.03, 0, 0, 1, 1]) #Initial values of beta0, beta1, beta2, beta3, tau1 and tau2
res = optimize.minimize(objective, x0, args = y)
print(res)
折扣因子函数无关紧要,但这里是 class 您需要 运行 它:
class DiscountFactor:
def __init__(self, val_given, value, start_date, end_date, composition, basis):
self.start_date = start_date
self.end_date = end_date
self.composition = composition
self.basis = basis
self.yf = year_fraction(start_date, end_date, basis)
if val_given == 'rate':
self.rate_to_df(value)
else:
raise ValueError('val_given must be: rate or df' )
def rate_to_df(self, rate):
if self.composition == 'linear':
df = 1/( 1 + rate*self.yf)
else:
raise ValueError('composition must be one of the following: linear, yearly, biannual, continuous')
self.result = df
return self.result
作为 Askold Ilvento,您正在函数范围内重新定义参数。这将使优化失败,因为该函数将始终产生相同的结果;这也是一种不好的做法,也是潜在的错误来源。尝试:
def objective(p, y):
return (((100 * DiscountFactor('rate',np.exp(p[0] + (p[1]+ p[2]) * (1 - np.exp(-yearfractionTenors()/p[4])) * p[4]/yearfractionTenors() - p[2] * np.exp(-yearfractionTenors()/p[4]) + p[3] * (1 - np.exp(-yearfractionTenors()/p[5])) * p[5] / yearfractionTenors() - p[3] * np.exp(-yearfractionTenors() / p[5])) -1, fecha_valoracion, maturity, composition= 'linear', basis= 'act/360').result) - y) ** 2).sum()
x0 = np.array([0.03, -0.03, 0, 0, 1, 1]) #Initial values of beta0, beta1, beta2, beta3, tau1 and tau2
y = np.array([98.494500, 97.828500, 97.610000, 97.314000, 97.014500, 92.959000, 96.696222])
beta0, beta1, beta2, beta3, tau1, tau2 = optimize.minimize(objective, x0, args = y)