我对功能的最小化有疑问。我将发布所有代码。我知道代码太多,但是我不知道错误在哪里: 首先是数据:
Data:
date = dt.datetime(2018, 6, 26)
maturity_dtime = DatetimeIndex(['2020-04-07', '2020-08-07', '2020-12-07', '2023-12-07',
'2027-12-07', '2032-12-07', '2040-02-07'],
dtype='datetime64[ns]', freq=None)
data['Price']
Price
0 108.360589
1 102.579383
2 104.241200
3 106.449767
4 124.687315
5 133.424299
6 107.836756
Name: Dirty Price, dtype: float64
YF_tenors = [0.0027397260273972603, 0.0821917808219178, 0.25205479452054796, 0.5013698630136987, 0.7479452054794521, 1.0, 2.0027397260273974]
tenors_dates = [datetime.datetime(2018, 6, 27, 0, 0), datetime.datetime(2018, 7, 26, 0, 0), datetime.datetime(2018, 9, 26, 0, 0), datetime.datetime(2018, 12, 26, 0, 0), datetime.datetime(2019, 3, 26, 0, 0), datetime.datetime(2019, 6, 26, 0, 0), datetime.datetime(2020, 6, 26, 0, 0)]
然后,我开始计算费率。我使用6个参数,这些参数是我必须最小化的参数,以使平方误差之和最小:
''' Calculation the rates '''
parameters = [0.03,-0.03,0.0,0.0,1.0,1.0]
# Here is the function that use the parameters
def rates(params, t):
beta0, beta1, beta2, beta3, tau1, tau2 = params
rate = np.exp(beta0 + (beta1 + beta2) * (1 - np.exp(-t/tau1)) * tau1/t - beta2 * np.exp(-t/tau1) + beta3 * (1 - np.exp(-t/tau2)) * tau2 /t - beta3 * np.exp(-t/ tau2)) -1
return rate
def tenors_rates():
rates_tenors=[]
for aux in range(len(YF_tenors)):
rate_aux=rates(parameters,YF_tenors[aux])
rates_tenors.append(rate_aux)
return rates_tenors
curve=['act/365','Lineal','Anual']
def curve_data(curve):
for aux in range(len(tenors_rates())):
curve_aux=[tenors_dates[aux],tenors_rates()[aux]]
curve.append(curve_aux)
'''This is the interpolation function for the curve'''
def interpol_curva(value_date,maturity_date,curve):
base=curve[0]
interpol=curve[1]
#compo_fg=curve[2]
nrows=int(len(curve))
if (maturity_date > curve[nrows-1][0]).any():
maturity_date=curve[nrows-1][0]
if maturity_date<curve[3][0]: #
maturity_date=curve[3][0]
r1=3
while maturity_date>curve[r1][0] and r1<nrows-1:
r1=r1+1
r1=r1-1
if r1==2:
r1=3
if r1>=nrows-1:
r1=nrows-2
r2=r1+1
if base=='act/360' or base=='act/365':
yf1=(maturity_date-curve[r1][0]).days
yf2=(curve[r2][0]-maturity_date).days
yftt=(curve[r2][0]-curve[r1][0]).days
else:
print("fail")
if interpol=='Lineal':
return (curve[r1][1]*yf2+curve[r2][1]*yf1)/yftt
''' Day Count '''
def day_count(start_date, end_date, basis):
if basis == 'act/365':
days = (end_date - start_date).days
return days
else:
print('Basis not defined')
''' Year Fraction '''
def year_fraction(start_date, end_date, basis):
if basis == 'act/365':
yf = day_count(start_date ,end_date,basis) / 365.0
return yf
def Discount_Factor_2(value_date,maturity_date,curve):
basis=curve[0]
Composition=curve[2]
yf = year_fraction(value_date, maturity_date, basis)
r=interpol_curva(value_date,maturity_date,curve) valor?
if Composition == "Anual":
df = 1 / (1 + r) ** yf
else:
print("fail")
return df
所有这些代码之后,我尝试最小化下一个目标函数,即平方误差之和:
'''Minimization'''
def objective(params):
beta0, beta1, beta2, beta3, tau1, tau2 = parameters
return (((100 * Discount_Factor_2(value_date, maturity_dtime, curve)) - data['Price'])**2).sum()
x0 = [0.1,0.1,0.1,0.1,1,1] #Initial values of beta0, beta1...
我执行以下操作:
res = minimize(objective, x0,method="SLSQP", tol=0.00000000001)
,它不会失败,但不会改变任何内容。 我认为问题在于目标函数不使用参数(x0),而是向后移动,在某些情况下,它应该到达参数所在的rates函数。 抱歉,冗长的代码,但我坚持这样做。任何帮助都是完美的,非常感谢您抽出宝贵的时间并尝试提供帮助。