openquant.cla
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_cla
allocate_cla(
asset_prices: Sequence[Sequence[float]] | None = None,
expected_returns: Sequence[float] | None = None,
covariance_matrix: Sequence[Sequence[float]] | None = None,
weight_bounds_lower: float | None = None,
weight_bounds_upper: float | None = None,
resample_by: str | None = None,
solution: str | None = None,
calculate_expected_returns: str = 'mean',
) -> AnyCritical Line Algorithm portfolios (Markowitz; Bailey and López de Prado, 2013; AFML chapter 16).
Computes every turning point of the efficient frontier exactly under one box bound for all assets and the budget constraint (weights sum to 1), walking from the maximum-return portfolio down to the minimum-variance portfolio, then returns the requested solution. Supply either asset_prices (expected returns and covariance are estimated from their simple returns, annualised with 252 / step periods per year) or both expected_returns and covariance_matrix (used as given, not annualised). The 252 assumes daily rows; for intraday rows the figures are per 252 rows, but the weights do not depend on that constant. If every expected return is identical, 1e-5 is added to the last one so the walk has a starting asset.
Parameters
asset_prices: list[list[float]] | None, default NonePrices, one inner list per observation (oldest first) and one column per asset. Required unless both
expected_returnsandcovariance_matrixare given.expected_returns: list[float] | None, default NoneExpected return per asset. When given it is used instead of an estimate from prices.
covariance_matrix: list[list[float]] | None, default NoneN x Ncovariance. When given it is used instead of an estimate from prices.weight_bounds_lower: float | None, default NoneLower bound on every weight; None means 0.
weight_bounds_upper: float | None, default NoneUpper bound on every weight; None means 1.
resample_by: str | None, default NoneFor prices only, positional resampling (case-insensitive):
"W"/"week"/"weekly"keeps every 5th row (step = 5),"M"/"month"/"monthly"every 21st (step = 21); anything else, or None, keeps every row (step = 1).solution: str | None, default None"cla_turning_points"(the default when None),"min_volatility","max_sharpe"(maximisesmu'w / sigma, i.e. a zero risk-free rate) or"efficient_frontier"(about 100 points along the frontier).calculate_expected_returns: str, default "mean"How expected returns are estimated from prices:
"mean"(mean periodic return) or"exponential"(exponentially weighted mean, span 500, seeded at the first return). Ignored whenexpected_returnsis given.
Returns
- dict[str, list]
weights: the requested portfolios, one list ofNweights each, in input column order (every turning point, maximum return first, for"cla_turning_points"; one portfolio for"min_volatility"and"max_sharpe"; the frontier points for"efficient_frontier").lambdas: the risk-aversion parameter at each turning point, infinite first and falling to 0.efficient_frontier_meansandefficient_frontier_sigma: the expected return and volatility of each frontier point (empty unlesssolution="efficient_frontier").
Raises
- ValueError
If a matrix is empty or ragged; no prices are given and either expected returns or covariance is missing; prices have fewer than two (resampled) rows or a zero price;
calculate_expected_returnsorsolutionis unknown; the inputs disagree on the number of assets; the bounds are infeasible (not finite, lower above upper, or unable to sum to 1); or the covariance of the free assets is singular at some step of the walk (e.g. two perfectly correlated assets, or more assets than observations).