Example #1
0
 def test_maximizeAlpha_3(self):
     """限定单个权重值"""
     obj = MaximizeAlpha(self.alphas)
     cvx_objective = obj.to_cvxpy(None)
     w = obj.new_weights
     constraints = [
         cvx.abs(w) <= 0.2,
     ]
     prob = cvx.Problem(cvx_objective, constraints)
     desired_value = 0.18
     desired_weights = pd.Series(
         [0.2, 0.2, 0.2, -0.2, -0.2], index=self.stocks)
     self.check(prob, obj, desired_value, desired_weights)
Example #2
0
 def test_maximizeAlpha_2(self):
     """限定空头权重和为1,不允许多头"""
     obj = MaximizeAlpha(self.alphas)
     cvx_objective = obj.to_cvxpy(None)
     w = obj.new_weights
     constraints = [
         cvx.sum(cvx.abs(w)) <= 1.0,
         w <= 0.0,
     ]
     prob = cvx.Problem(cvx_objective, constraints)
     desired_value = 0.3
     desired_weights = pd.Series([0, 0., 0., 0, -1.0], index=self.stocks)
     self.check(prob, obj, desired_value, desired_weights)
Example #3
0
 def test_maximizeAlpha_4(self):
     """限定总权重值,且单个空头权重不得小于-0.2,即最大空头绝对值不得超过0.2"""
     obj = MaximizeAlpha(self.alphas)
     cvx_objective = obj.to_cvxpy(None)
     w = obj.new_weights
     constraints = [
         cvx.sum(cvx.abs(w)) <= 1.0,
         w >= -0.2,
     ]
     prob = cvx.Problem(cvx_objective, constraints)
     desired_value = 0.22
     desired_weights = pd.Series(
         [0., 0.4, 0.4, -0., -0.2], index=self.stocks)
     self.check(prob, obj, desired_value, desired_weights)
Example #4
0
 def test_maximizeAlpha_5(self):
     """
     单纯限定总权重,而不限定多头或空头值
     """
     obj = MaximizeAlpha(self.alphas)
     cvx_objective = obj.to_cvxpy(None)
     w = obj.new_weights
     constraints = [
         cvx.sum(cvx.abs(w)) <= 1.0,
         # w >= -0.2,
     ]
     prob = cvx.Problem(cvx_objective, constraints)
     desired_value = 0.30
     desired_weights = pd.Series([0., 0., 0., -0., -1.], index=self.stocks)
     self.check(prob, obj, desired_value, desired_weights)
Example #5
0
    def test_maximizeAlpha_6(self):
        """
        如果限定总权重,且限定多头及多头权重值

        最会依次取正收益,直至权重和为限定值
        """
        obj = MaximizeAlpha(self.alphas)
        cvx_objective = obj.to_cvxpy(None)
        w = obj.new_weights
        constraints = [
            cvx.sum(cvx.abs(w)) <= 1.0,
            w >= 0,
        ]
        prob = cvx.Problem(cvx_objective, constraints)
        desired_value = 0.20
        desired_weights = pd.Series(
            [0., 0.5, 0.5, -0., -0.], index=self.stocks)
        self.check(prob, obj, desired_value, desired_weights)
Example #6
0
 def check(self, cons_obj, desired_value, desired_weights, init_w_s=None):
     # 基础限制:单个权重不超过0.5,空头不超过-1.0,总体不超过1.5
     obj = MaximizeAlpha(self.alphas)
     cvx_objective = obj.to_cvxpy(None)
     w = obj.new_weights
     w_s = obj.new_weights_series
     constraints = [con for con in cons_obj.to_cvxpy(w, w_s, init_w_s)]
     constraints += [cvx.norm(w, 1) <= 1.5, w <= 0.5, w >= -1.0]
     prob = cvx.Problem(cvx_objective, constraints)
     prob.solve()
     # print('求解状态', prob.status)
     # print('最优解', prob.value)
     # print('权重序列\n', obj.new_weights_value)
     # print('初始权重序列\n', init_w_s)
     self.assertAlmostEqual(desired_value, prob.value, 2)
     assert_series_equal(obj.new_weights_value,
                         desired_weights,
                         check_exact=False,
                         check_less_precise=3)  # 精度:小数点后3位