ValueError:解析scipy的fmin vs least_squares

时间:2017-12-19 09:23:57

标签: python scipy least-squares

我想在下面的代码中实现scipy的optimize.least_squares而不是fmin(几乎在代码的底部)。但是,在将fmin部分更改为least_squares并且不知道如何解决此问题时,我遇到以下错误。enter image description here

以下错误消息的含义是什么: ValueError:要解压缩的值太多

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 

1 个答案:

答案 0 :(得分:0)

你有什么问题吗? fmin语法是:

import scipy.optimize as sop
import numpy as np
def ftest(x):
    x,y,z = x
    data = np.array([3.65,2.41,1.59])# arbitrary & just for testing
    model = np.array([x**2,y**2,z**2])# arbitrary & just for testing
    residuals = data - model 
    return residuals
def ftest2(x):
    return sum(ftest(x)**2)# to ask fmin to minimize the chi2 but it can be anything
p0 = np.array([1,2,3])# arbitrary & just for testing
opt = sop.fmin(ftest2, p0)

而对于least_squares,您将最后一行替换为:

opt = sop.least_squares(ftest, p0)

注意ftest返回残差,而ftest2返回你想要最小化的标量。

小心fmin。由于这是单纯形算法,因此您无法确定它是否在合理的时间内收敛。例如,如果您更换&#39;模型&#39; line by:

model = np.array([x+y**2+z**3 , x+y+z , x-y**2-z])

你不与fmin会合。您可以尝试比较输出:

opt = sop.least_squares(ftest, p0)
opt2 = sop.fmin(ftest2, p0,xtol=1e-20,ftol=1e-20,maxfun=1e20,maxiter=5e4)