本文整理匯總了Python中scipy.optimize.fmin_slsqp方法的典型用法代碼示例。如果您正苦於以下問題:Python optimize.fmin_slsqp方法的具體用法?Python optimize.fmin_slsqp怎麽用?Python optimize.fmin_slsqp使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy.optimize
的用法示例。
在下文中一共展示了optimize.fmin_slsqp方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: portfolio_optimization
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def portfolio_optimization(weights, nav_table, opt_type, multiplier, rebalance=False, lb=0., ub=1.):
if not rebalance and opt_type == OptTarget.SORTINO:
raise ValueError("Portfolio optimization target can't be set as "
"maximize sortino ratio")
if opt_type == OptTarget.SORTINO or \
opt_type == OptTarget.RETURN or \
opt_type == OptTarget.VOL or \
opt_type == OptTarget.SHARP or \
(rebalance and opt_type == OptTarget.SORTINO):
x0 = weights
bounds = [(lb, ub) for _ in weights]
def eq_cond(x, *args):
return sum(x) - 1.0
def func(weights):
returns = portfolio_returns(weights, nav_table, rebalance)
return -utility_calculator(returns, opt_type, multiplier)
out, fx, its, imode, smode = opt.fmin_slsqp(func=func,
x0=x0,
bounds=bounds,
eqcons=[eq_cond],
full_output=True,
iprint=-1,
acc=1e-12,
iter=300000)
out = {col: weight for col, weight in zip(nav_table.columns, out)}
return pd.DataFrame(out, index=['weight']), fx, its, imode, smode
示例2: test_unbounded_given
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def test_unbounded_given(self):
""" SLSQP: unbounded, given jacobian. """
res = fmin_slsqp(self.fun, [-1.0, 1.0], args=(-1.0, ),
fprime = self.jac, iprint = 0,
full_output = 1)
x, fx, its, imode, smode = res
assert_(imode == 0, imode)
assert_array_almost_equal(x, [2, 1])
示例3: test_unbounded_approximated
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def test_unbounded_approximated(self):
""" SLSQP: unbounded, approximated jacobian. """
res = fmin_slsqp(self.fun, [-1.0, 1.0], args=(-1.0, ),
iprint = 0, full_output = 1)
x, fx, its, imode, smode = res
assert_(imode == 0, imode)
assert_array_almost_equal(x, [2, 1])
示例4: test_equality_approximated
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def test_equality_approximated(self):
""" SLSQP: equality constraint, approximated jacobian. """
res = fmin_slsqp(self.fun,[-1.0,1.0], args=(-1.0,),
eqcons = [self.f_eqcon],
iprint = 0, full_output = 1)
x, fx, its, imode, smode = res
assert_(imode == 0, imode)
assert_array_almost_equal(x, [1, 1])
示例5: test_equality_given
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def test_equality_given(self):
""" SLSQP: equality constraint, given jacobian. """
res = fmin_slsqp(self.fun, [-1.0, 1.0],
fprime=self.jac, args=(-1.0,),
eqcons = [self.f_eqcon], iprint = 0,
full_output = 1)
x, fx, its, imode, smode = res
assert_(imode == 0, imode)
assert_array_almost_equal(x, [1, 1])
示例6: test_equality_given2
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def test_equality_given2(self):
""" SLSQP: equality constraint, given jacobian for fun and const. """
res = fmin_slsqp(self.fun, [-1.0, 1.0],
fprime=self.jac, args=(-1.0,),
f_eqcons = self.f_eqcon,
fprime_eqcons = self.fprime_eqcon,
iprint = 0,
full_output = 1)
x, fx, its, imode, smode = res
assert_(imode == 0, imode)
assert_array_almost_equal(x, [1, 1])
示例7: test_bound_equality_given2
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def test_bound_equality_given2(self):
""" SLSQP: bounds, eq. const., given jac. for fun. and const. """
res = fmin_slsqp(self.fun, [-1.0, 1.0],
fprime=self.jac, args=(-1.0, ),
bounds = [(-0.8, 1.), (-1, 0.8)],
f_eqcons = self.f_eqcon,
fprime_eqcons = self.fprime_eqcon,
iprint = 0, full_output = 1)
x, fx, its, imode, smode = res
assert_(imode == 0, imode)
assert_array_almost_equal(x, [0.8, 0.8], decimal=3)
示例8: test_scalar_constraints
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def test_scalar_constraints(self):
""" Ticket #1657 """
x = fmin_slsqp(lambda z: z**2, [3.],
ieqcons=[lambda z: z[0] - 1],
iprint=0)
assert_array_almost_equal(x, [1.])
x = fmin_slsqp(lambda z: z**2, [3.],
f_ieqcons=lambda z: [z[0] - 1],
iprint=0)
assert_array_almost_equal(x, [1.])
示例9: test_integer_bounds
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def test_integer_bounds(self):
# this should not raise an exception
fmin_slsqp(lambda z: z**2 - 1, [0], bounds=[[0, 1]], iprint=0)
示例10: get_policies_time1
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def get_policies_time1(self,x,s,Vf):
'''
Finds the optimal policies
'''
Para,beta,Theta,G,S,Pi = self.Para,self.beta,self.Theta,self.G,self.S,self.Pi
U,Uc,Un = Para.U,Para.Uc,Para.Un
def objf(z):
c,n,xprime = z[0],z[1],z[2:]
Vprime = np.empty(S)
for sprime in range(S):
Vprime[sprime] = Vf[sprime](xprime[sprime])
return -(U(c,n)+beta*Pi[s].dot(Vprime))
def cons(z):
c,n,xprime = z[0],z[1],z[2:]
return np.hstack([
x - Uc(c,n)*c-Un(c,n)*n - beta*Pi[s].dot(xprime),
(Theta*n - c - G)[s]
])
out,fx,_,imode,smode = fmin_slsqp(objf,self.z0[x,s],f_eqcons=cons,
bounds=[(0.,100),(0.,100)]+[self.xbar]*S,full_output=True,iprint=0)
if imode >0:
raise Exception(smode)
self.z0[x,s] = out
return np.hstack([-fx,out])
示例11: get_policies_time0
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def get_policies_time0(self,B_,s0,Vf):
'''
Finds the optimal policies
'''
Para,beta,Theta,G,S,Pi = self.Para,self.beta,self.Theta,self.G,self.S,self.Pi
U,Uc,Un = Para.U,Para.Uc,Para.Un
def objf(z):
c,n,xprime = z[0],z[1],z[2:]
Vprime = np.empty(S)
for sprime in range(S):
Vprime[sprime] = Vf[sprime](xprime[sprime])
return -(U(c,n)+beta*Pi[s0].dot(Vprime))
def cons(z):
c,n,xprime = z[0],z[1],z[2:]
return np.hstack([
-Uc(c,n)*(c-B_)-Un(c,n)*n - beta*Pi[s0].dot(xprime),
(Theta*n - c - G)[s0]
])
out,fx,_,imode,smode = fmin_slsqp(objf,self.zFB[s0],f_eqcons=cons,
bounds=[(0.,100),(0.,100)]+[self.xbar]*S,full_output=True,iprint=0)
if imode >0:
raise Exception(smode)
return np.hstack([-fx,out])
示例12: get_policies_time1
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def get_policies_time1(self, x, s, Vf):
'''
Finds the optimal policies
'''
model, β, Θ, = self.model, self.β, self.Θ,
G, S, π = self.G, self.S, self.π
U, Uc, Un = model.U, model.Uc, model.Un
def objf(z):
c, n, xprime = z[0], z[1], z[2:]
Vprime = np.empty(S)
for sprime in range(S):
Vprime[sprime] = Vf[sprime](xprime[sprime])
return -(U(c, n) + β * π[s] @ Vprime)
def cons(z):
c, n, xprime = z[0], z[1], z[2:]
return np.hstack([x - Uc(c, n) * c - Un(c, n) * n - β * π[s] @ xprime,
(Θ * n - c - G)[s]])
out, fx, _, imode, smode = fmin_slsqp(objf,
self.z0[x, s],
f_eqcons=cons,
bounds=[(0, 100), (0, 100)] +
[self.xbar] * S,
full_output=True,
iprint=0,
acc=1e-10)
if imode > 0:
raise Exception(smode)
self.z0[x, s] = out
return np.hstack([-fx, out])
示例13: get_policies_time0
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def get_policies_time0(self, B_, s0, Vf):
'''
Finds the optimal policies
'''
model, β, Θ, = self.model, self.β, self.Θ,
G, S, π = self.G, self.S, self.π
U, Uc, Un = model.U, model.Uc, model.Un
def objf(z):
c, n, xprime = z[0], z[1], z[2:]
Vprime = np.empty(S)
for sprime in range(S):
Vprime[sprime] = Vf[sprime](xprime[sprime])
return -(U(c, n) + β * π[s0] @ Vprime)
def cons(z):
c, n, xprime = z[0], z[1], z[2:]
return np.hstack([-Uc(c, n) * (c - B_) - Un(c, n) * n - β * π[s0] @ xprime,
(Θ * n - c - G)[s0]])
out, fx, _, imode, smode = fmin_slsqp(objf, self.zFB[s0], f_eqcons=cons,
bounds=[(0, 100), (0, 100)] +
[self.xbar] * S,
full_output=True, iprint=0, acc=1e-10)
if imode > 0:
raise Exception(smode)
return np.hstack([-fx, out])
示例14: get_policies_time1
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def get_policies_time1(self,x,s,Vf):
'''
Finds the optimal policies
'''
Para,beta,Theta,G,S,Pi = self.Para,self.beta,self.Theta,self.G,self.S,self.Pi
U,Uc,Un = Para.U,Para.Uc,Para.Un
def objf(z):
c,n,xprime = z[0],z[1],z[2:]
Vprime = np.empty(S)
for sprime in range(S):
Vprime[sprime] = Vf[sprime](xprime[sprime])
return -(U(c,n)+beta*Pi[s].dot(Vprime))
def cons(z):
c,n,xprime = z[0],z[1],z[2:]
return np.hstack([
x - Uc(c,n)*c-Un(c,n)*n - beta*Pi[s].dot(xprime),
(Theta*n - c - G)[s]
])
out,fx,_,imode,smode = fmin_slsqp(objf,self.z0[x,s],f_eqcons=cons,
bounds=[(0.,100),(0.,100)]+[self.xbar]*S,full_output=True,iprint=0)
if imode >0:
raise Exception(smode)
self.z0[x,s] = out
return np.hstack([-fx,out])
示例15: get_policies_time1
# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import fmin_slsqp [as 別名]
def get_policies_time1(self, x, s_, Vf):
'''
Finds the optimal policies
'''
model, β, Θ, G, S, π = self.model, self.β, self.Θ, self.G, self.S, self.π
U, Uc, Un = model.U, model.Uc, model.Un
def objf(z):
c, n, xprime = z[:S], z[S:2 * S], z[2 * S:3 * S]
Vprime = np.empty(S)
for s in range(S):
Vprime[s] = Vf[s](xprime[s])
return -π[s_] @ (U(c, n) + β * Vprime)
def cons(z):
c, n, xprime, T = z[:S], z[S:2 * S], z[2 * S:3 * S], z[3 * S:]
u_c = Uc(c, n)
Eu_c = π[s_] @ u_c
return np.hstack([
x * u_c / Eu_c - u_c * (c - T) - Un(c, n) * n - β * xprime,
Θ * n - c - G])
if model.transfers:
bounds = [(0., 100)] * S + [(0., 100)] * S + \
[self.xbar] * S + [(0., 100.)] * S
else:
bounds = [(0., 100)] * S + [(0., 100)] * S + \
[self.xbar] * S + [(0., 0.)] * S
out, fx, _, imode, smode = fmin_slsqp(objf, self.z0[x, s_],
f_eqcons=cons, bounds=bounds,
full_output=True, iprint=0,
acc=self.tol, iter=self.maxiter)
if imode > 0:
raise Exception(smode)
self.z0[x, s_] = out
return np.hstack([-fx, out])