Skip to content

updated HRPOpt to prefer the supplied covariance matrix #638

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

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
11 changes: 7 additions & 4 deletions pypfopt/hierarchical_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def optimize(self, linkage_method="single"):
if linkage_method not in sch._LINKAGE_METHODS:
raise ValueError("linkage_method must be one recognised by scipy")

if self.returns is None:
if self.cov_matrix is not None:
cov = self.cov_matrix
corr = risk_models.cov_to_corr(self.cov_matrix).round(6)
else:
Expand All @@ -176,7 +176,7 @@ def optimize(self, linkage_method="single"):
def portfolio_performance(self, verbose=False, risk_free_rate=0.0, frequency=252):
"""
After optimising, calculate (and optionally print) the performance of the optimal
portfolio. Currently calculates expected return, volatility, and the Sharpe ratio
portfolio. Currently, calculates expected return, volatility, and the Sharpe ratio
assuming returns are daily

:param verbose: whether performance should be printed, defaults to False
Expand All @@ -192,9 +192,12 @@ def portfolio_performance(self, verbose=False, risk_free_rate=0.0, frequency=252
:return: expected return, volatility, Sharpe ratio.
:rtype: (float, float, float)
"""
if self.returns is None:
if self.cov_matrix is not None:
cov = self.cov_matrix
mu = None
if self.returns is not None:
mu = self.returns.mean() * frequency
else:
mu = None
else:
cov = self.returns.cov() * frequency
mu = self.returns.mean() * frequency
Expand Down