Skip to main content

Crate rust_portfolio_opt

Crate rust_portfolio_opt 

Source
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§

LabeledMatrix
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).
LabeledVector
A length-N vector of floats paired with N ticker labels. Mirrors a pandas Series indexed by ticker name. Returned by every estimator that has a _labeled companion (e.g. crate::expected_returns::mean_historical_return_labeled) so callers can keep ticker order alongside the numerical values.

Enums§

PortfolioError
Crate-wide error type. Most fallible operations return PortfolioError rather 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 weights whose absolute value is below cutoff to zero, renormalise the remainder so they sum to the original total, and optionally round to rounding decimal places. Mirrors PyPortfolioOpt’s base_optimizer.BaseOptimizer.clean_weights.
column_means
Column-wise mean of an T x N matrix → 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 N price 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-keyed BTreeMap, matching PyPortfolioOpt’s habit of returning weights as an OrderedDict[str, float]. Errors if the lengths disagree.

Type Aliases§

Result
Convenience alias used by every module in the crate.