我想实施scipy的优化最小二乘法而不是下面代码中的fmin(几乎在代码的底部)。但是,我在将fmin部分更改为最小二乘法时遇到了以下错误,不知道如何解决这个问题。enter image description here
以下错误消息是什么意思: 值错误:太多的值无法解压缩
Parameters
==========
sigma: float
volatility factor in diffusion term
lamb: float
jump intensity
mu: float
expected jump size
delta: float
standard deviation of jump
Returns
=======
RMSE: float
root mean squared error
'''
global i, min_RMSE
sigma, lamb, mu, delta = p0
if sigma < 0.0 or delta < 0.0 or lamb < 0.0:
return 500.0
se = []
for row, option in options.iterrows():
T = (option['Maturity'] - option['Date']).days / 365.
r = option['Interest']
#absolut_error = abs(option['Call'] - option['Model'])
model_value = M76_value_call_FFT(S0, option['Strike'], T,
r, sigma, lamb, mu, delta)
se.append((model_value - option['Call']) ** 2)
RMSE = math.sqrt(sum(se) / len(se))
min_RMSE = min(min_RMSE, RMSE)
if i % 50 == 0:
print '%4d |' % i, np.array(p0), '| %7.3f | %7.3f' % (RMSE, min_RMSE)
print
i += 1
return RMSE
def generate_plot(opt, options):
#
# Calculating Model Prices
#
sigma, lamb, mu, delta = opt
options['Model'] = 0.0
for row, option in options.iterrows():
T = (option['Maturity'] - option['Date']).days / 365.
options.loc[row, 'Model'] = M76_value_call_FFT(S0, option['Strike'],
T, r, sigma, lamb, mu, delta)
#
# Plotting
#
mats = sorted(set(options['Maturity']))
options = options.set_index('Strike')
for i, mat in enumerate(mats):
options[options['Maturity'] == mat][['Call', 'Model']].\
plot(style=['bs', 'r--'], title='%s' % str(mat)[:10],
grid=True)
plt.ylabel('Call Value')
plt.savefig('img/M76_calibration_3_%s.pdf' % i)
if __name__ == '__main__':
#
# Calibration
#
i = 0 # counter initialization
min_RMSE = 100 # minimal RMSE initialization
print('BRUTE')
p0 = sop.brute(M76_error_function_FFT, ((0, 0.01, 1),
(0, 0.3, 5), (-10, 0.01, 10),
(0, 0.01, 40)), finish=None)
# p0 = [0.15, 0.2, -0.3, 0.2]
print('FMIN')
opt = sop.fmin(M76_error_function_FFT, p0,
maxiter=500, maxfun=750,
xtol=0.000001, ftol=0.000001)
generate_plot(opt, options)
print options
你有什么问题吗?fmin语法是:
对于最小二乘法,最后一行替换为:
^{pr2}$注意ftest返回残差,ftest2返回要最小化的标量。在
小心fmin。因为这是单纯形算法,你永远不能确定它在一个合理的时间内收敛。例如,如果将“model”行替换为:
你和fmin不一致。您可以尝试比较以下输出:
相关问题 更多 >
编程相关推荐