fmincon 以及 其他优化
不同软件的优化器有所区别,MATLAB中的fmincon 是较为简单好用的, Python中的Scipy Minimize,以及Ox中可以选择 BFGS, 以及BFGS-BOUND。
MATLAB 输入 和 输出:
一个优化器cvxpy, 如何安装 Python:安装cvxpy,进行凸优化; 解凸优化的工具箱或者包
scipy.optimize.minimize; Matlab fmincon algorithm
返回message信息:
ineq_constraints = ({'type': 'ineq', 'fun': lambda x: 1-x[0]-x[1]})
unrest = Bounds([1e-5, 0.01], [1, 1], keep_feasible=True) # create bounds objects
options = {'gtol': 1e-6, 'disp': True}
tol = 1e-6
SLSQP
或者 fmin_slsqp
opt_ = minimize(
lik_fn,
sv,
args=(stdData, unconcorr, backcast),
jac='2-point',
# hess=SR1(),
method="SLSQP",
bounds=bounds,
constraints=ineq_constraints,
tol=tol,
options=options,
# callback=llf._callback
)
fun: 9571.083803555019
jac: array([0.00292969, 0.00012207])
message: 'Optimization terminated successfully'
nfev: 36
nit: 10
njev: 10
status: 0
success: True
x: array([0.02523875, 0.94423303])
BFGS
res_ = minimize(lik_fn, sv, method='BFGS',
jac='2-point',
args=(stdData, unconcorr, backcast),
options={'gtol': 1e-6, 'disp': True})
fun: 9571.083803555195
hess_inv: array([[ 0.0001261 , -0.00031329],
[-0.00031329, 0.00078031]])
jac: array([0.00146484, 0.00012207])
message: 'Desired error not necessarily achieved due to precision loss.'
nfev: 281
nit: 10
njev: 87
status: 2
success: False
x: array([0.02523853, 0.94423353])
Nelder-Mead
res_NM = minimize(lik_fn, sv, method='Nelder-Mead',
# jac='2-point',
bounds=bounds,
args=(stdData, unconcorr, backcast),
options={'gtol': 1e-6, 'disp': True})
final_simplex: (array([[0.0252493 , 0.94420818],
[0.02522809, 0.94425249],
[0.02522306, 0.9442968 ]]), array([9571.08380417, 9571.08380451, 9571.08380711]))
fun: 9571.083804171478
message: 'Optimization terminated successfully.'
nfev: 72
nit: 38
status: 0
success: True
x: array([0.0252493 , 0.94420818])
L-BFGS-B
res_BFGS_B = minimize(lik_fn, sv, method='L-BFGS-B',
# jac='2-point',
bounds=bounds,
args=(stdData, unconcorr, backcast),
options={'gtol': 1e-6, 'disp': True})
res_BFGS_B.hess_inv.todense() # 返回矩阵
res_BFGS_B
fun: 9571.083803563144
hess_inv: <2x2 LbfgsInvHessProduct with dtype=float64>
jac: array([0.00400178, 0.00800355])
message: 'CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR*EPSMCH'
nfev: 78
nit: 10
njev: 26
status: 0
success: True
x: array([0.02523756, 0.94423662])
COBYLA
opt_C = minimize(
lik_fn,
sv,
args=(stdData, unconcorr, backcast),
# jac='2-point',
# hess=SR1(),
method="COBYLA",
# bounds=bounds,
constraints=ineq_constraints,
tol=tol,
options=options,
# callback=llf._callback
)
opt_C
fun: 9571.083804272734
maxcv: 0.0
message: 'Optimization terminated successfully.'
nfev: 376
status: 1
success: True
x: array([0.02524922, 0.94419908])
Basinhopping
basinhopping, 默认方法是 L-BFGS-B
;
niter = 80
step_r = bs.StepGM2013(rest=True, max=2.5)
step_u = bs.StepGM2013(rest=False, max=2.5)
rest_reg = basinhopping(gm_llf.simpleGM, x0, T=0,
minimizer_kwargs=bs.min_arg((args,), rest),
niter=niter,
take_step=step_r,
# callback=bs.print_fun
)