openquant.hrp
Compiled from Rust (crates/pyopenquant). Signatures and types are from the shipped .pyi stubs; descriptions are the functions’ own docstrings, as help() shows them. All Python modules.
Functions
allocate_hrp
allocate_hrp(
asset_names: Sequence[str],
asset_prices: Sequence[Sequence[float]] | None = None,
asset_returns: Sequence[Sequence[float]] | None = None,
covariance_matrix: Sequence[Sequence[float]] | None = None,
resample_by: str | None = None,
use_shrinkage: bool = False,
distance: str | None = None,
) -> tuple[list[float], list[int]]Hierarchical Risk Parity weights (AFML chapter 16, Snippets 16.1-16.4).
Allocates without inverting the covariance matrix: (1) tree clustering on correlation distances d = sqrt((1 - rho) / 2) by single linkage (section 16.4.1); (2) quasi-diagonalisation so similar assets are adjacent (16.4.2); (3) recursive bisection, splitting weight between halves in inverse proportion to their inverse-variance cluster variances (16.4.3). The result is long-only and fully invested. The covariance is taken from covariance_matrix if given, else estimated from asset_returns, else from simple returns of asset_prices; HRP weights do not depend on its scale.
Parameters
asset_names: list[str]One name per asset; its length fixes the number of assets
N.asset_prices: list[list[float]] | None, default NonePrices, one inner list per observation (oldest first) and
Ncolumns. Used only when neitherasset_returnsnorcovariance_matrixis given.asset_returns: list[list[float]] | None, default NonePer-period returns, one inner list per observation and
Ncolumns. Used when nocovariance_matrixis given.covariance_matrix: list[list[float]] | None, default NoneN x Ncovariance, used as given when present.resample_by: str | None, default NoneFor prices only:
"W"/"week"/"weekly"keeps every 5th row,"M"/"month"/"monthly"every 21st (case-insensitive); anything else, or None, keeps every row.use_shrinkage: bool, default FalseMultiply the off-diagonal terms of an estimated covariance by 0.9 (a fixed shrink, not Ledoit-Wolf); no effect on a supplied
covariance_matrix.distance: str | None, default NoneDistance the tree is built on (case-insensitive):
"distance_of_distances"(the default; the Euclidean distance between columns ofd, as AFML Snippet 16.4 computes) or"correlation"(cluster onditself, as mlfinlab does).
Returns
- tuple[list[float], list[int]]
(weights, ordered_indices): non-negative weights summing to 1, one per asset inasset_namesorder, and the asset indices in quasi-diagonal (leaf) order.
Raises
- ValueError
If
distanceis unknown, a matrix is empty or ragged, or the core rejects the input (e.g. no prices, returns or covariance given, emptyasset_names, fewer than two price or return rows, a zero price, a non-positive variance, or a matrix whose shape disagrees with the number of asset names).