def test_cvoptimizer_with_cons_and_ieq(self): objective = np.array([0.1, 0.2, 0.3]) cov = np.array([[0.05, 0.01, 0.02], [0.01, 0.06, 0.03], [0.02, 0.03, 0.07]]) lbound = np.array([-0.3, -0.3, -0.3]) ubound = np.array([0.5, 0.5, 0.5]) cons = np.array([[1., 1., 1.]]) clbound = np.array([0.]) cubound = np.array([0.]) target_vol = 0.1 optimizer = CVOptimizer(objective, cov, lbound, ubound, cons, clbound, cubound, target_vol) # check against known good result np.testing.assert_array_almost_equal(optimizer.x_value(), [-0.3, -0.10919033, 0.40919033], 4)
def test_cvoptimizer_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.]) target_vol = 0.5 optimizer = CVOptimizer(objective, None, lbound, ubound, cons, clbound, cubound, target_vol, factor_var, factor_load, idsync) # check against cvxpy result np.testing.assert_array_almost_equal(optimizer.x_value(), [0.26595552, 0.21675092, 0.51729356], 4)
def test_cvoptimizer_without_cons(self): objective = np.array([0.1, 0.2, 0.3]) cov = np.array([[0.05, 0.01, 0.02], [0.01, 0.06, 0.03], [0.02, 0.03, 0.07]]) lbound = np.array([-0.3, -0.3, -0.3]) ubound = np.array([0.5, 0.5, 0.5]) target_vol = 0.1 optimizer = CVOptimizer(objective, cov, lbound, ubound, None, None, None, target_vol, target_vol) # check against known good result np.testing.assert_array_almost_equal(optimizer.x_value(), [.0231776, 0.1274768, 0.30130881], 4)
def target_vol_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]], vol_target: 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) optimizer = CVOptimizer(er, risk_model['cov'], lbound, ubound, cons_mat, clbound, cubound, vol_target, risk_model['factor_cov'], risk_model['factor_loading'], risk_model['idsync'], linear_solver=linear_solver) return _create_result(optimizer, bm)
def target_vol_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]], vol_low: float = 0., vol_high: float = 1.)-> Tuple[str, float, np.ndarray]: lbound, ubound, cons_mat, clbound, cubound = _create_bounds(lbound, ubound, bm, risk_exposure, risk_target) optimizer = CVOptimizer(er, cov, lbound, ubound, cons_mat, clbound, cubound, vol_low, vol_high) return _create_result(optimizer, bm)