簡體   English   中英

ValueError:太多值無法解開scipy的fmin與Minimum_squares

[英]ValueError: too many values to unpack scipy's fmin vs least_squares

我想在下面的代碼中(幾乎在代碼底部)實現scipy的optimize.least_squares而不是fmin。 但是,將fmin部分更改為Minimum_squares時遇到以下錯誤,並且不知道如何解決。 在此處輸入圖片說明

以下錯誤消息是什么意思: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 

你有什么問題 ? 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)

而對於minimum_squares,您將最后一行替換為:

opt = sop.least_squares(ftest, p0)

注意ftest返回殘差,而ftest2返回要最小化的標量。

小心fmin。 由於這是單純形算法,因此您永遠無法確保它在合理的時間內收斂。 例如,如果您將“ model”行替換為:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM