@@ -28,11 +28,11 @@ def test_random_covmat():
28
28
Sigma = U @ U .T + np .eye (N )
29
29
#pdb.set_trace()
30
30
my_portfolio = rpp .RiskParityPortfolio (Sigma , budget = b )
31
- my_portfolio .design (verbose = False )
31
+ my_portfolio .design ()
32
32
w = my_portfolio .weights
33
33
34
34
# assert that the portfolio respect the budget constraint
35
- np .testing .assert_almost_equal (np .sum (w ), 1 )
35
+ np .testing .assert_almost_equal (np .sum (w ), 1.0 )
36
36
# assert that the portfolio respect the no-shortselling constraint
37
37
np .testing .assert_equal (all (w >= 0 ), True )
38
38
# assert that the desired risk contributions are attained
@@ -49,23 +49,23 @@ def test_singularity_issues_with_G_matrix():
49
49
Sigma = U @ U .T # singular covariance matrix
50
50
51
51
my_portfolio = rpp .RiskParityPortfolio (Sigma , budget = b )
52
- my_portfolio .design (verbose = False , tau = 1e-10 , control_numerical_ill_conditioning = True )
53
- w1 = my_portfolio .weights
54
-
55
- my_portfolio = rpp .RiskParityPortfolio (Sigma , budget = b )
56
- my_portfolio .design (tau = 1e-4 )
57
- w2 = my_portfolio .weights
58
- np .testing .assert_allclose (w1 , w2 , rtol = 1e-3 )
52
+ my_portfolio .design (verbose = False , tau = 1e-10 ) # <-- force ill-conditioned matrix
53
+ w_ref = my_portfolio .weights
59
54
60
55
my_portfolio = rpp .RiskParityPortfolio (Sigma , budget = b )
61
- my_portfolio .design (tau = 0.05 * np . sum ( np . diag ( Sigma )) / ( 2 * N ))
62
- w3 = my_portfolio .weights
63
- np .testing .assert_allclose (w1 , w3 , rtol = 1e-3 )
56
+ my_portfolio .design (tau = 1e-4 ) # <-- default tau, should be fine
57
+ w = my_portfolio .weights
58
+ np .testing .assert_allclose (w , w_ref , rtol = 1e-3 )
64
59
65
- my_portfolio = rpp .RiskParityPortfolio (Sigma , budget = b )
66
- my_portfolio .design (tau = 2 * 0.1 ** 2 / (2 * N ))
67
- w4 = my_portfolio .weights
68
- np .testing .assert_allclose (w1 , w4 , rtol = 1e-3 )
60
+ # my_portfolio = rpp.RiskParityPortfolio(Sigma, budget=b)
61
+ # my_portfolio.design(verbose=True, tau=0.05 * np.sum(np.diag(Sigma)) / (2 * N))
62
+ # w = my_portfolio.weights
63
+ # np.testing.assert_allclose(w, w_ref, rtol=1e-3)
64
+ #
65
+ # my_portfolio = rpp.RiskParityPortfolio(Sigma, budget=b)
66
+ # my_portfolio.design(verbose=True, tau=2 * 0.1 ** 2 / (2 * N))
67
+ # w = my_portfolio.weights
68
+ # np.testing.assert_allclose(w, w_ref, rtol=1e-3)
69
69
70
70
71
71
def test_constraints ():
@@ -95,7 +95,7 @@ def test_constraints():
95
95
np .testing .assert_allclose (w1 , w3 , rtol = 1e-5 )
96
96
97
97
98
- # Upper bound w <= 0.03
98
+ # Additional upper bound: w <= 0.03
99
99
my_portfolio = rpp .RiskParityPortfolio (Sigma )
100
100
my_portfolio .design (Cmat = np .ones ((1 , N )), cvec = np .array ([1.0 ]),
101
101
Dmat = np .vstack ([np .eye (N ), - np .eye (N )]), dvec = np .concatenate ([0.03 * np .ones (N ), np .zeros (N )]))
@@ -105,16 +105,24 @@ def test_constraints():
105
105
print (max (w ))
106
106
np .testing .assert_array_less (w , (0.03 + 1e-3 )* np .ones (N ))
107
107
108
- # Bounds for sum: 0.5 <= sum(w) <= 1 (
108
+
109
+ # Bounds for sum(w): 0.5 <= sum(w) <= 1 (tending to upper bound)
109
110
my_portfolio = rpp .RiskParityPortfolio (Sigma )
111
+ my_portfolio .add_mean_return (alpha = 1e-6 , mean = np .ones (N )) # this adds sum(w) to also maximize sum(w)
110
112
my_portfolio .design (Cmat = np .empty ((0 , N )), cvec = [],
111
113
Dmat = np .vstack ([- np .ones ((1 ,N )), np .ones ((1 ,N ))]), dvec = np .array ([- 0.5 , 1 ]))
112
114
w = my_portfolio .weights
113
- np .testing .assert_equal (sum (w ) <= 1.0 + 1e-5 , True )
114
- np .testing .assert_equal (sum (w ) >= 0.5 - 1e-5 , True )
115
+ np .testing .assert_almost_equal (np .sum (w ), 1.0 )
116
+
117
+
118
+ # Bounds for sum(w): 0.5 <= sum(w) <= 1 (tending to lower bound)
119
+ my_portfolio = rpp .RiskParityPortfolio (Sigma )
120
+ my_portfolio .add_mean_return (alpha = 1e-6 , mean = - np .ones (N )) # this adds -sum(w) to also minimize sum(w)
121
+ my_portfolio .design (Cmat = np .empty ((0 , N )), cvec = [],
122
+ Dmat = np .vstack ([- np .ones ((1 ,N )), np .ones ((1 ,N ))]), dvec = np .array ([- 0.5 , 1 ]))
123
+ w = my_portfolio .weights
124
+ np .testing .assert_almost_equal (np .sum (w ), 0.5 , decimal = 3 )
115
125
116
- #np.testing.assert_array_less(sum(w), 1.0)
117
- #np.testing.assert_array_less(-sum(w), -0.5)
118
126
119
127
120
128
def test_dummy_variables ():
@@ -127,7 +135,7 @@ def test_dummy_variables():
127
135
my_portfolio = rpp .RiskParityPortfolio (Sigma )
128
136
my_portfolio .design (Cmat = np .ones ((1 , N )), cvec = np .array ([1.0 ]),
129
137
Dmat = np .vstack ([np .eye (N ), - np .eye (N )]), dvec = np .concatenate ([0.03 * np .ones (N ), np .zeros (N )]))
130
- w1 = my_portfolio .weights
138
+ w_ref = my_portfolio .weights
131
139
132
140
# Equivalently: sum(w) = 1, 0 <= w <= u, and u <= 0.03 (new dummy variable u, with w_tilde = [w; u])
133
141
my_portfolio = rpp .RiskParityPortfolio (Sigma )
@@ -136,6 +144,6 @@ def test_dummy_variables():
136
144
np .hstack ([np .eye (N ), - np .eye (N )]),
137
145
np .hstack ([np .zeros ((N , N )), np .eye (N )])]),
138
146
dvec = np .concatenate ([np .zeros (N ), np .zeros (N ), 0.03 * np .ones (N )]))
139
- w2 = my_portfolio .weights
147
+ w = my_portfolio .weights
140
148
141
- np .testing .assert_allclose (w1 , w2 , rtol = 1e-5 , atol = 1e-5 )
149
+ np .testing .assert_allclose (w , w_ref , rtol = 1e-5 , atol = 1e-5 )
0 commit comments