Ejemplo n.º 1
0
    def test_qpoptimizer_with_factor_model(self):
        objective = np.array([0.1, 0.2, 0.3])
        lbound = np.array([0.0, 0.0, 0.0])
        ubound = np.array([1.0, 1.0, 1.0])

        factor_var = np.array([[0.5, -0.3], [-0.3, 0.7]])
        factor_load = np.array([[0.8, 0.2], [0.5, 0.5], [0.2, 0.8]])
        idsync = np.array([0.1, 0.3, 0.2])

        cons = np.array([[1., 1., 1.]])
        clbound = np.array([1.])
        cubound = np.array([1.])

        optimizer = QPOptimizer(objective,
                                None,
                                lbound,
                                ubound,
                                cons,
                                clbound,
                                cubound,
                                1.,
                                factor_var,
                                factor_load,
                                idsync)

        # check against cvxpy result
        np.testing.assert_array_almost_equal(optimizer.x_value(),
                                             [0.2866857, 0.21416417, 0.49915014],
                                             4)
Ejemplo n.º 2
0
    def test_qpoptimizer(self):
        objective = np.array([0.01, 0.02, 0.03])
        cov = np.array([[0.02, 0.01, 0.02],
                        [0.01, 0.02, 0.03],
                        [0.02, 0.03, 0.02]])
        ids_var = np.diag([0.01, 0.02, 0.03])
        cov += ids_var
        lbound = np.array([0., 0., 0.])
        ubound = np.array([0.4, 0.4, 0.5])

        cons = np.array([[1., 1., 1.],
                         [1., 0., 1.]])
        clbound = np.array([1., 0.3])
        cubound = np.array([1., 0.7])

        optimizer = QPOptimizer(objective,
                                cov,
                                lbound,
                                ubound,
                                cons,
                                clbound,
                                cubound)

        # check against matlab result
        np.testing.assert_array_almost_equal(optimizer.x_value(),
                                             [0.1996, 0.3004, 0.5000],
                                             4)
Ejemplo n.º 3
0
    def test_qpoptimizer_with_identity_matrix(self):
        objective = np.array([-0.02, 0.01, 0.03])
        cov = np.diag([1., 1., 1.])
        lbound = np.array([-np.inf, -np.inf, -np.inf])
        ubound = np.array([np.inf, np.inf, np.inf])

        optimizer = QPOptimizer(objective,
                                cov,
                                lbound,
                                ubound,
                                risk_aversion=1.)

        np.testing.assert_array_almost_equal(optimizer.x_value(),
                                             [-0.02, 0.01, 0.03], 8)
Ejemplo n.º 4
0
def mean_variance_builder(er: np.ndarray,
                          cov: np.ndarray,
                          bm: np.ndarray,
                          lbound: Union[np.ndarray, float],
                          ubound: Union[np.ndarray, float],
                          risk_exposure: np.ndarray,
                          risk_target: Tuple[np.ndarray, np.ndarray],
                          lam: float = 1.) -> Tuple[str, float, np.ndarray]:

    lbound = lbound - bm
    ubound = ubound - bm

    bm_risk = risk_exposure.T @ bm

    clbound = risk_target[0] - bm_risk
    cubound = risk_target[1] - bm_risk

    optimizer = QPOptimizer(er, cov, lbound, ubound, risk_exposure.T, clbound,
                            cubound, lam)

    if optimizer.status() == 0 or optimizer.status() == 1:
        status = 'optimal'
    else:
        status = optimizer.status()

    return status, optimizer.feval(), optimizer.x_value() + bm
Ejemplo n.º 5
0
def mean_variance_builder(
        er: np.ndarray,
        risk_model: Dict[str, Union[None, np.ndarray]],
        bm: np.ndarray,
        lbound: Union[np.ndarray, float],
        ubound: Union[np.ndarray, float],
        risk_exposure: Optional[np.ndarray],
        risk_target: Optional[Tuple[np.ndarray, np.ndarray]],
        lam: float = 1.,
        linear_solver: str = 'ma27') -> Tuple[str, float, np.ndarray]:
    lbound, ubound, cons_mat, clbound, cubound = _create_bounds(
        lbound, ubound, bm, risk_exposure, risk_target)

    if np.all(lbound == -np.inf) and np.all(
            ubound == np.inf) and cons_mat is None:
        # using fast path cvxpy
        n = len(er)
        w = cvxpy.Variable(n)
        cov = risk_model['cov']
        special_risk = risk_model['idsync']
        risk_cov = risk_model['factor_cov']
        risk_exposure = risk_model['factor_loading']
        if cov is None:
            risk = cvxpy.sum_squares(cvxpy.multiply(cvxpy.sqrt(special_risk), w)) \
                   + cvxpy.quad_form((w.T * risk_exposure).T, risk_cov)
        else:
            risk = cvxpy.quad_form(w, cov)
        objective = cvxpy.Minimize(-w.T * er + 0.5 * lam * risk)
        prob = cvxpy.Problem(objective)
        prob.solve(solver='ECOS', feastol=1e-9, abstol=1e-9, reltol=1e-9)

        if prob.status == 'optimal' or prob.status == 'optimal_inaccurate':
            return 'optimal', prob.value, np.array(w.value).flatten() + bm
        else:
            raise PortfolioBuilderException(prob.status)
    else:
        optimizer = QPOptimizer(er,
                                risk_model['cov'],
                                lbound,
                                ubound,
                                cons_mat,
                                clbound,
                                cubound,
                                lam,
                                risk_model['factor_cov'],
                                risk_model['factor_loading'],
                                risk_model['idsync'],
                                linear_solver=linear_solver)

        return _create_result(optimizer, bm)
Ejemplo n.º 6
0
def mean_variance_builder(er: np.ndarray,
                          cov: np.ndarray,
                          bm: np.ndarray,
                          lbound: Union[np.ndarray, float],
                          ubound: Union[np.ndarray, float],
                          risk_exposure: Optional[np.ndarray],
                          risk_target: Optional[Tuple[np.ndarray, np.ndarray]],
                          lam: float=1.) -> Tuple[str, float, np.ndarray]:
    lbound, ubound, cons_mat, clbound, cubound = _create_bounds(lbound, ubound, bm, risk_exposure, risk_target)

    optimizer = QPOptimizer(er,
                            cov,
                            lbound,
                            ubound,
                            cons_mat,
                            clbound,
                            cubound,
                            lam)

    return _create_result(optimizer, bm)