Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update black_litterman.py #541

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 24 additions & 3 deletions pypfopt/black_litterman.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,14 @@ def bl_returns(self):
if self._A is None:
self._A = (self.P @ self._tau_sigma_P) + self.omega
b = self.Q - self.P @ self.pi
post_rets = self.pi + self._tau_sigma_P @ np.linalg.solve(self._A, b)
try:
solution = np.linalg.solve(self._A, b)
except np.linalg.LinAlgError as e:
if 'Singular matrix' in str(e):
solution = np.linalg.lstsq(self._A, b, rcond=None)[0]
else:
raise e
post_rets = self.pi + self._tau_sigma_P @ solution
return pd.Series(post_rets.flatten(), index=self.tickers)

def bl_cov(self):
Expand All @@ -423,7 +430,14 @@ def bl_cov(self):
self._A = (self.P @ self._tau_sigma_P) + self.omega

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

Expand All @@ -449,7 +463,14 @@ def bl_weights(self, risk_aversion=None):
self.posterior_rets = self.bl_returns()
A = risk_aversion * self.cov_matrix
b = self.posterior_rets
raw_weights = np.linalg.solve(A, b)
try:
weight_solution = np.linalg.solve(A, b)
except np.linalg.LinAlgError as e:
if 'Singular matrix' in str(e):
weight_solution = np.linalg.lstsq(self._A, b, rcond=None)[0]
else:
raise e
raw_weights = weight_solution
self.weights = raw_weights / raw_weights.sum()
return self._make_output_weights()

Expand Down