Python scipy.optimize.fmin_slsqp() Examples

The following are 30 code examples of scipy.optimize.fmin_slsqp(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.optimize , or try the search function .
Example #1
Source File: test_slsqp.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_integer_bounds(self):
        # This should not raise an exception
        fmin_slsqp(lambda z: z**2 - 1, [0], bounds=[[0, 1]], iprint=0) 
Example #2
Source File: code_2.py    From QuantEcon.lectures.code with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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_].dot(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_].dot(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=1e-12, iter=1000)

        if imode > 0:
            raise Exception(smode)

        self.z0[x, s_] = out
        return np.hstack([-fx, out]) 
Example #3
Source File: test_slsqp.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
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]) 
Example #4
Source File: test_slsqp.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
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]) 
Example #5
Source File: test_slsqp.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
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]) 
Example #6
Source File: test_slsqp.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
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]) 
Example #7
Source File: test_slsqp.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
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]) 
Example #8
Source File: test_slsqp.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
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)
        assert_(-0.8 <= x[0] <= 1)
        assert_(-1 <= x[1] <= 0.8) 
Example #9
Source File: test_slsqp.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_scalar_constraints(self):
        # Regression test for gh-2182
        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.]) 
Example #10
Source File: test_slsqp.py    From Computable with MIT License 5 votes vote down vote up
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]) 
Example #11
Source File: test_slsqp.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_obj_must_return_scalar(self):
        # Regression test for Github Issue #5433
        # If objective function does not return a scalar, raises ValueError
        with assert_raises(ValueError):
            fmin_slsqp(lambda x: [0, 1], [1, 2, 3]) 
Example #12
Source File: test_slsqp.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_obj_returns_scalar_in_list(self):
        # Test for Github Issue #5433 and PR #6691
        # Objective function should be able to return length-1 Python list
        #  containing the scalar
        fmin_slsqp(lambda x: [0], [1, 2, 3], iprint=0) 
Example #13
Source File: c15_16_GJR_GARCH_function.py    From Python-for-Finance-Second-Edition with MIT License 5 votes vote down vote up
def GJR_GARCH(ret): 
    import numpy as np
    import scipy.optimize as op 
    startV=np.array([ret.mean(),ret.var()*0.01,0.03,0.09,0.90])
    finfo=np.finfo(np.float64)
    t=(0.0,1.0)
    bounds=[(-10*ret.mean(),10*ret.mean()),(finfo.eps,2*ret.var()),t,t,t] 
    T=np.size(ret,0)
    sigma2=np.repeat(ret.var(),T) 
    inV=(ret,sigma2)
    return op.fmin_slsqp(gjr_garch_likelihood,startV,f_ieqcons=gjr_constraint,bounds=bounds,args=inV)
# 
Example #14
Source File: c15_17_GIR_GARCH_result.py    From Python-for-Finance-Second-Edition with MIT License 5 votes vote down vote up
def GJR_GARCH(ret): 
    import numpy as np
    import scipy.optimize as op 
    startV=np.array([ret.mean(),ret.var()*0.01,0.03,0.09,0.90])
    finfo=np.finfo(np.float64)
    t=(0.0,1.0)
    bounds=[(-10*ret.mean(),10*ret.mean()),(finfo.eps,2*ret.var()),t,t,t] 
    T=np.size(ret,0)
    sigma2=np.repeat(ret.var(),T) 
    inV=(ret,sigma2)
    return op.fmin_slsqp(gjr_garch_likelihood,startV,f_ieqcons=gjr_constraint,bounds=bounds,args=inV)
# 
Example #15
Source File: synth.py    From synth with MIT License 5 votes vote down vote up
def get_w(w, v, x0, x1):
    result = fmin_slsqp(w_rss, w, f_eqcons=w_constraint, bounds=[(0.0, 1.0)]*len(w),
             args=(v, x0, x1), disp=False, full_output=True)
    weights = result[0]
    return weights 
Example #16
Source File: synth.py    From synth with MIT License 5 votes vote down vote up
def get_v_0(v, w, x0, x1, z0, z1):
    weights = fmin_slsqp(w_rss, w, f_eqcons=w_constraint, bounds=[(0.0, 1.0)]*len(w),
             args=(v, x0, x1), disp=False, full_output=True)[0]
    rss = v_rss(weights, z0, z1)
    return rss 
Example #17
Source File: deepSuperLearnerLib.py    From DeepSuperLearner with MIT License 5 votes vote down vote up
def _get_weights(self, y, m_set_predictions_fold):
        """
        Find weights that minimize the estimated logloss.

        Parameters
        ----------
        y: numpy.array of shape [n,j]

        m_set_predictions_fold: numpy.array of shape [n, m, j] of fold-k

        Returns
        _______
        weights: numpy.array of normalized non-negative weights to combine
              base learners
              
        
        """
        def objective_f(w):  # Logloss(y,w*y_pred)
            return self._get_logloss(y, self._get_weighted_prediction(m_set_predictions_fold, w))
        def normalized_constraint(w):  # Sum(w)-1 == 0
            return np.array([ np.sum(w) - 1 ])
        w0 = np.array([1. / self.n_baselearners] * self.n_baselearners)
        wbounds = [(0, 1)] * self.n_baselearners
        out, _, _, imode, _ = fmin_slsqp(objective_f, \
            w0, f_eqcons=normalized_constraint, bounds=wbounds, \
             disp=0, full_output=1)
        if imode is not 0:
            raise Exception("Optimization failed to find weights")

        out = np.array(out)
        out[out < np.sqrt(np.finfo(np.double).eps)] = 0
        weights = out / np.sum(out)
        return weights 
Example #18
Source File: optimizers.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self):
        from scipy.optimize import fmin_slsqp
        super().__init__(fmin_slsqp)
        self.fit_info = {
            'final_func_val': None,
            'numiter': None,
            'exit_mode': None,
            'message': None
        } 
Example #19
Source File: amss.py    From QuantEcon.lectures.code with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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[: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 -Pi[s_].dot(U(c,n)+beta*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 = Pi[s_].dot(u_c)
            return np.hstack([
            x*u_c/Eu_c - u_c*(c-T)-Un(c,n)*n - beta*xprime,
            Theta*n - c - G          
            ])
            
        if Para.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)
                            
        if imode >0:
            raise Exception(smode)
           
        self.z0[x,s_] = out
        return np.hstack([-fx,out]) 
Example #20
Source File: Optimizer.py    From Finance-Python with MIT License 5 votes vote down vote up
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 
Example #21
Source File: test_slsqp.py    From Computable with MIT License 5 votes vote down vote up
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]) 
Example #22
Source File: test_slsqp.py    From Computable with MIT License 5 votes vote down vote up
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]) 
Example #23
Source File: test_slsqp.py    From Computable with MIT License 5 votes vote down vote up
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]) 
Example #24
Source File: test_slsqp.py    From Computable with MIT License 5 votes vote down vote up
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]) 
Example #25
Source File: test_slsqp.py    From Computable with MIT License 5 votes vote down vote up
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) 
Example #26
Source File: test_slsqp.py    From Computable with MIT License 5 votes vote down vote up
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.]) 
Example #27
Source File: test_slsqp.py    From Computable with MIT License 5 votes vote down vote up
def test_integer_bounds(self):
        # this should not raise an exception
        fmin_slsqp(lambda z: z**2 - 1, [0], bounds=[[0, 1]], iprint=0) 
Example #28
Source File: lucas_stokey.py    From QuantEcon.lectures.code with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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]) 
Example #29
Source File: lucas_stokey.py    From QuantEcon.lectures.code with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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]) 
Example #30
Source File: recursive_allocation.py    From QuantEcon.lectures.code with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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])