Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions pypfopt/black_litterman.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ def _set_risk_aversion(self, risk_aversion):
self.risk_aversion = risk_aversion

def _set_omega(self, omega, view_confidences):
self._active_views = None
if isinstance(omega, pd.DataFrame):
self.omega = omega.values
elif isinstance(omega, np.ndarray):
Expand Down Expand Up @@ -336,6 +337,13 @@ def _set_omega(self, omega, view_confidences):
self.tau,
self.risk_aversion,
)
# Keep the public omega matrix and all view labels intact. A
# zero-confidence Idzorek view conveys no information; a fixed,
# finite uncertainty is only a placeholder, not its likelihood.
active = np.asarray(view_confidences[: len(self.Q)]).reshape(-1) != 0
if not active.all():
self._active_views = active
self._idzorek_omega = self.omega
elif omega is None or omega == "default":
self.omega = BlackLittermanModel.default_omega(
self.cov_matrix, self.P, self.tau
Expand Down Expand Up @@ -414,6 +422,31 @@ def idzorek_method(view_confidences, cov_matrix, pi, Q, P, tau, risk_aversion=1)

return np.diag(view_omegas)

def _posterior_view_system(self):
# Before the first solve, respect an explicit replacement of omega
# or a modification to a placeholder's row or column.
if self._A is None and self._active_views is not None:
inactive = ~self._active_views
if self.omega is not self._idzorek_omega or (
np.count_nonzero(self.omega[inactive, :]) != inactive.sum()
or np.count_nonzero(self.omega[:, inactive]) != inactive.sum()
or not np.all(self.omega.diagonal()[inactive] == 1e6)
):
self._active_views = None

# Keep the legacy full-view caches. Only posterior solves see the
# reduced system; other consumers must not see a changed shape.
if self._tau_sigma_P is None:
self._tau_sigma_P = self.tau * self.cov_matrix @ self.P.T
if self._A is None:
self._A = self.P @ self._tau_sigma_P + self.omega

if self._active_views is None:
return self.P, self.Q, self._tau_sigma_P, self._A
active = self._active_views
return (self.P[active], self.Q[active], self._tau_sigma_P[:, active],
self._A[np.ix_(active, active)])

def bl_returns(self):
"""
Calculate the posterior estimate of the returns vector,
Expand All @@ -425,21 +458,20 @@ def bl_returns(self):
posterior returns vector
"""

if self._tau_sigma_P is None:
self._tau_sigma_P = self.tau * self.cov_matrix @ self.P.T
picking, views, cross, system = self._posterior_view_system()
if not len(views):
return pd.Series(self.pi.flatten(), index=self.tickers)

# Solve the linear system Ax = b to avoid inversion
if self._A is None:
self._A = (self.P @ self._tau_sigma_P) + self.omega
b = self.Q - self.P @ self.pi
# Solve the linear system Ax = b to avoid inversion.
b = views - picking @ self.pi
try:
solution = np.linalg.solve(self._A, b)
solution = np.linalg.solve(system, b)
except np.linalg.LinAlgError as e:
if "Singular matrix" in str(e):
solution = np.linalg.lstsq(self._A, b, rcond=None)[0]
solution = np.linalg.lstsq(system, b, rcond=None)[0]
else:
raise e
post_rets = self.pi + self._tau_sigma_P @ solution
post_rets = self.pi + cross @ solution
return pd.Series(post_rets.flatten(), index=self.tickers)

def bl_cov(self):
Expand All @@ -454,20 +486,20 @@ def bl_cov(self):
pd.DataFrame
posterior covariance matrix
"""
if self._tau_sigma_P is None:
self._tau_sigma_P = self.tau * self.cov_matrix @ self.P.T
if self._A is None:
self._A = (self.P @ self._tau_sigma_P) + self.omega
_, views, cross, system = self._posterior_view_system()
if not len(views):
posterior_cov = self.cov_matrix + self.tau * self.cov_matrix
return pd.DataFrame(posterior_cov, index=self.tickers, columns=self.tickers)

b = self._tau_sigma_P.T
b = cross.T
try:
M_solution = np.linalg.solve(self._A, b)
M_solution = np.linalg.solve(system, b)
except np.linalg.LinAlgError as e:
if "Singular matrix" in str(e):
M_solution = np.linalg.lstsq(self._A, b, rcond=None)[0]
M_solution = np.linalg.lstsq(system, b, rcond=None)[0]
else:
raise e
M = self.tau * self.cov_matrix - self._tau_sigma_P @ M_solution
M = self.tau * self.cov_matrix - cross @ M_solution
posterior_cov = self.cov_matrix + M
return pd.DataFrame(posterior_cov, index=self.tickers, columns=self.tickers)

Expand Down
106 changes: 106 additions & 0 deletions tests/test_black_litterman_zero_confidence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import numpy as np
import pandas as pd
import pytest

from pypfopt.black_litterman import BlackLittermanModel


@pytest.mark.parametrize("cov_first", [False, True])
@pytest.mark.parametrize("scale", [1.0, 1e9])
def test_idzorek_zero_confidence_is_no_information(cov_first, scale):
sigma = scale * np.array([[2.0, 0.7], [0.7, 1.0]])
labels = ["synthetic_b", "synthetic_a"]
prior = np.array([0.01, -0.02])
model = BlackLittermanModel(
pd.DataFrame(sigma, index=labels, columns=labels), pi=prior,
absolute_views={"synthetic_a": -1.0, "synthetic_b": 1.0},
omega="idzorek", view_confidences=np.array([[0.0], [0.0]]), tau=0.1,
)
if cov_first:
model.bl_cov()
mean = model.bl_returns()
covariance = model.bl_cov()
np.testing.assert_array_equal(mean, prior)
np.testing.assert_allclose(covariance, 1.1 * sigma, rtol=1e-13, atol=0)
assert list(mean.index) == list(covariance.columns) == list(covariance.index) == labels
assert model.Q.shape == (2, 1) and model.P.shape == model.omega.shape == (2, 2)
np.testing.assert_array_equal(model.omega, np.eye(2) * 1e6)


@pytest.mark.parametrize("confidence", [0.25, 1.0])
@pytest.mark.parametrize("cov_first", [False, True])
def test_idzorek_mixed_matches_scalar_gaussian_conditioning(confidence, cov_first):
sigma = 1e7 * np.array([[3.0, 0.5], [0.5, 1.0]])
prior = np.array([0.2, -0.4])
active_view = np.array([1.0, 1.0])
ignored_view = np.array([1.0, -1.0])
correct = np.array([0.7, 25.0])
tau = 0.1
model = BlackLittermanModel(
sigma, pi=prior, Q=correct, P=np.array([active_view, ignored_view]),
omega="idzorek", view_confidences=[confidence, 0.0], tau=tau,
)
if cov_first:
model.bl_cov()
p = sigma @ active_view
v = active_view @ p
gain = confidence / v
expected = prior + gain * p * (correct[0] - active_view @ prior)
predictive = sigma + tau * (sigma - gain * np.outer(p, p))
np.testing.assert_allclose(model.bl_returns(), expected, atol=1e-14, rtol=0)
np.testing.assert_allclose(model.bl_cov(), predictive, atol=1e-8, rtol=1e-14)
system = model._A
np.testing.assert_allclose(model.bl_returns(), expected, atol=1e-14, rtol=0)
assert model._A is system


def test_custom_finite_noise_value_is_not_an_idzorek_marker():
sigma = np.array([[1e7]])
noise = np.array([[1e6]])
model = BlackLittermanModel(
sigma, pi=np.array([0.0]), Q=np.array([1.0]), P=np.array([[1.0]]),
omega=noise, tau=0.1,
)
np.testing.assert_allclose(model.bl_returns(), [0.5], atol=0, rtol=0)
np.testing.assert_allclose(model.bl_cov(), [[10.5e6]], atol=0, rtol=0)
np.testing.assert_array_equal(model.omega, noise)


@pytest.mark.parametrize("replacement", [True, False])
def test_omega_explicitly_modified_before_solve_still_applies(replacement):
model = BlackLittermanModel(
np.array([[1e7]]), pi=np.array([0.0]), Q=np.array([1.0]),
P=np.array([[1.0]]), omega="idzorek", view_confidences=[0], tau=0.1,
)
if replacement:
model.omega = np.array([[2e6]])
else:
model.omega[0, 0] = 2e6
np.testing.assert_allclose(model.bl_returns(), [1 / 3])
np.testing.assert_allclose(model.bl_cov(), [[1e7 + 1e6 * 2 / 3]])


def test_original_confidence_input_not_retained():
confidence = np.array([0.])
model = BlackLittermanModel(
np.array([[1e7]]), pi=np.array([0.0]), Q=np.array([1.0]),
P=np.array([[1.0]]), omega="idzorek", view_confidences=confidence, tau=0.1,
)
confidence[0] = 0.5
np.testing.assert_allclose(model.bl_returns(), [0.0])


def test_idzorek_zero_confidence_preserves_legacy_singular_weight_path():
direction = np.array([2.0, 1.0])
sigma = np.outer(direction, direction)
prior = 0.2 * direction
model = BlackLittermanModel(
sigma, pi=prior, Q=prior.copy(), P=np.eye(2),
omega="idzorek", view_confidences=[0.0, 0.0], tau=0.1, risk_aversion=2,
)
weights = model.bl_weights()
# In the consistent rank-one system, the minimum-norm solution is
# parallel to the range direction and its normalized sum is 1.
np.testing.assert_allclose(list(weights.values()), [2 / 3, 1 / 3])
np.testing.assert_allclose(model.posterior_rets, prior)
assert model._A.shape == (2, 2)