Skip to content
Open
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
3 changes: 0 additions & 3 deletions pypfopt/hierarchical_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,6 @@ def optimize(self, linkage_method="single"):
OrderedDict
weights for the HRP portfolio
"""
if linkage_method not in sch._LINKAGE_METHODS:
raise ValueError("linkage_method must be one recognised by scipy")

if self.returns is None:
cov = self.cov_matrix
corr = cov_to_corr(self.cov_matrix).round(6)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_hrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,26 @@ def test_quasi_dag():
hrp.optimize(linkage_method="single")
clusters = hrp.clusters
assert HRPOpt._get_quasi_diag(clusters)[:5] == [12, 6, 15, 14, 2]


def test_hrp_linkage_methods():
# optimize() used to validate linkage_method against the private attribute
# sch._LINKAGE_METHODS, which scipy 1.18 removed, so every call raised
# AttributeError. scipy exposes no public list of linkage methods, so the
# seven documented by scipy.cluster.hierarchy.linkage are pinned here.
df = get_data()
returns = df.pct_change().dropna(how="all")
methods = [
"single",
"complete",
"average",
"weighted",
"centroid",
"median",
"ward",
]
for method in methods:
hrp = HRPOpt(returns)
w = hrp.optimize(linkage_method=method)
assert set(w.keys()) == set(returns.columns)
np.testing.assert_almost_equal(sum(w.values()), 1)