Skip to content

Commit

Permalink
test volatility factor
Browse files Browse the repository at this point in the history
  • Loading branch information
tschm committed Jun 20, 2023
1 parent 3e97683 commit 11f445a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cvx/risk/factor/factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def estimate_risk(self, weights, **kwargs):
"""
Compute the total variance
"""
var_residual = cvx.sum_squares(cvx.multiply(self.idiosyncratic_risk, weights))
var_residual = cvx.norm2(cvx.multiply(self.idiosyncratic_risk, weights))

y = kwargs.get("y", self.exposure @ weights)

return cvx.sum_squares(self.chol @ y) + var_residual
return cvx.norm2(cvx.vstack([cvx.norm2(self.chol @ y), var_residual]))

def update_data(self, **kwargs):
exposure = kwargs["exposure"]
Expand Down
8 changes: 4 additions & 4 deletions tests/test_risk/test_factor/test_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def test_timeseries_model(returns):
w = np.zeros(25)
w[:20] = 0.05

var = model.estimate_risk(w).value
np.testing.assert_almost_equal(var, 8.527444810470023e-05)
vola = model.estimate_risk(w).value
np.testing.assert_almost_equal(vola, 0.00923407730537884)


def test_minvar(returns):
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_estimate_risk():
upper=np.ones(20),
)
prob.solve()
assert prob.value == pytest.approx(0.01856273748192123)
assert prob.value == pytest.approx(0.13625197847921858)
assert np.array(weights.value[20:]) == pytest.approx(np.zeros(5), abs=1e-6)

model.update_data(
Expand All @@ -97,5 +97,5 @@ def test_estimate_risk():
upper=np.ones(20),
)
prob.solve()
assert prob.value == pytest.approx(0.16675109055174622)
assert prob.value == pytest.approx(0.40835167515605786)
assert np.array(weights.value[20:]) == pytest.approx(np.zeros(5), abs=1e-6)

0 comments on commit 11f445a

Please sign in to comment.