Skip to content
Merged
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
All notable changes to RiskLabAI.py are documented here.
Format: [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versioning: [SemVer](https://semver.org/).

## [2.0.1]

### Fixed
- `optimization.hrp.hrp` no longer raises `ValueError: Distance matrix must be
symmetric` when the correlation matrix is only symmetric to floating-point
tolerance (as produced by `cov_to_corr` or by denoising). The correlation
distance is now symmetrised (`(d + dᵀ)/2`, zero diagonal) before
`squareform`. Regression test added (`test_hrp_asymmetric_correlation`).
Mirrors the same fix in RiskLabAI.jl v0.5.1.

## [2.0.0]

A **breaking** release that standardises the public API on PEP 8 names and makes
Expand Down
7 changes: 7 additions & 0 deletions RiskLabAI/optimization/hrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ def hrp(cov: pd.DataFrame, corr: pd.DataFrame) -> pd.Series:
# 1. Calculate distance
distance = distance_corr(corr_df.values)

# Enforce exact symmetry: a correlation matrix produced by ``cov_to_corr`` or
# by denoising can be asymmetric at the floating-point level, which makes
# ``squareform`` reject it ("Distance matrix must be symmetric"). Averaging
# with the transpose removes that asymmetry without changing the clustering.
distance = (distance + distance.T) / 2.0
np.fill_diagonal(distance, 0.0)

dist_condensed = scd.squareform(distance, force="tovector")

# 2. Cluster
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "RiskLabAI"
version = "2.0.0"
version = "2.0.1"
authors = [
{ name = "RiskLab", email = "arian@risklab.ai" },
]
Expand Down
24 changes: 24 additions & 0 deletions test/optimization/test_hrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,27 @@ def test_hrp(mock_cov_matrix):
pd.testing.assert_series_equal(
weights, expected_weights, atol=1e-5, check_names=False
)


def test_hrp_asymmetric_correlation():
"""A correlation matrix that is only symmetric to floating-point tolerance
(as produced by ``cov_to_corr`` or denoising) must not break ``hrp``.

Regression test: ``squareform`` previously rejected the tiny asymmetry with
"Distance matrix must be symmetric"."""
rng = np.random.default_rng(0)
n = 8
a = rng.normal(size=(n, n))
corr = np.corrcoef(a @ a.T)
# Inject a sub-epsilon asymmetry, the kind cov_to_corr leaves behind.
corr[0, 1] += 1e-15
names = [f"A{i}" for i in range(n)]
corr_df = pd.DataFrame(corr, index=names, columns=names)
cov_df = corr_df # unit variances → cov == corr

assert (corr != corr.T).any() # genuinely asymmetric (not bit-identical)
weights = hrp(cov_df, corr_df)
assert isinstance(weights, pd.Series)
assert weights.shape == (n,)
assert np.isclose(weights.sum(), 1.0)
assert (weights >= 0).all()
Loading