Expand description
Pure-Rust port of PyPortfolioOpt.
The crate mirrors PyPortfolioOpt’s module structure:
expected_returns— historical mean / EMA / CAPM return estimators.risk_models— sample / EWMA / semi-covariance + Ledoit-Wolf and oracle-approximating shrinkage; cov ↔ corr helpers.efficient_frontier— mean-variance optimisation: minimum variance, tangency (max Sharpe), efficient risk / return, with weight bounds.black_litterman— equilibrium prior + view-blended posterior.hrp— hierarchical risk parity (correlation distance, single linkage, recursive bisection).cla— Markowitz’s Critical Line Algorithm.discrete_allocation— convert continuous weights to integer share counts under a budget.
All matrix / vector inputs and outputs use nalgebra’s DMatrix /
DVector so they slot into broader nalgebra-based pipelines.
Each estimator and optimiser also has a _labeled companion that
accepts ticker names alongside the prices and returns
BTreeMap<String, f64> (ordered by ticker), mirroring how
PyPortfolioOpt accepts a pandas.DataFrame and returns an
OrderedDict. See LabeledVector / LabeledMatrix for the
intermediate types.
Every estimator and optimiser keeps the same default annualisation convention as PyPortfolioOpt: 252 trading days unless explicitly overridden by the caller.
Modules§
- black_
litterman - Black-Litterman: blend an equilibrium prior with investor views.
- cla
- Critical Line Algorithm (CLA) for exact mean-variance efficient frontier tracing.
- discrete_
allocation - Discrete allocation: convert continuous portfolio weights into integer share counts given a total budget.
- efficient_
frontier - Mean-variance optimisation à la
pypfopt.efficient_frontier. - expected_
returns - Expected-return estimators.
- hrp
- Hierarchical Risk Parity (López de Prado, 2016).
- risk_
models - Covariance / risk-matrix estimators.
Structs§
- Labeled
Matrix - A square N×N matrix paired with N ticker labels (used for both row
and column indexing — the same labelling pandas applies to a
covariance/correlation
DataFrame). - Labeled
Vector - A length-N vector of floats paired with N ticker labels. Mirrors a
pandas
Seriesindexed by ticker name. Returned by every estimator that has a_labeledcompanion (e.g.crate::expected_returns::mean_historical_return_labeled) so callers can keep ticker order alongside the numerical values.
Enums§
- Portfolio
Error - Crate-wide error type. Most fallible operations return
PortfolioErrorrather than panicking so callers can decide how to recover from misconfigured inputs (mismatched shapes, infeasible optimisation, etc.).
Constants§
- TRADING_
DAYS_ PER_ YEAR - Default annualisation factor — 252 trading days per year, matching PyPortfolioOpt’s defaults.
Functions§
- clean_
weights - Round entries of
weightswhose absolute value is belowcutoffto zero, renormalise the remainder so they sum to the original total, and optionally round toroundingdecimal places. Mirrors PyPortfolioOpt’sbase_optimizer.BaseOptimizer.clean_weights. - column_
means - Column-wise mean of an
T x Nmatrix → length-N vector. - log_
returns_ from_ prices - Log returns:
ln(p_t / p_{t-1}). - returns_
from_ prices - Compute simple period-over-period returns from a
T x Nprice matrix. - sample_
covariance - Sample covariance with Bessel’s correction (divisor
T-1). - symmetrise
- Symmetrise a matrix in-place (
(A + A^T) / 2). Useful after numerical operations that produce minute asymmetry. - to_
weight_ map - Convert a
(values, tickers)pair into a ticker-keyedBTreeMap, matching PyPortfolioOpt’s habit of returning weights as anOrderedDict[str, float]. Errors if the lengths disagree.
Type Aliases§
- Result
- Convenience alias used by every module in the crate.