portfolio_optimization
Subject
Section titled “Subject”Portfolio Construction and Risk
Why This Module Exists
Section titled “Why This Module Exists”Provides production-ready portfolio construction primitives with explicit options and constraints.
Mathematical Foundations
Section titled “Mathematical Foundations”Constrained Mean-Variance Program
Section titled “Constrained Mean-Variance Program”
Minimum Variance / Maximum Sharpe / Efficient Return
Section titled “Minimum Variance / Maximum Sharpe / Efficient Return”
Exponential Mean Estimator
Section titled “Exponential Mean Estimator”
Usage Examples
Section titled “Usage Examples”End-to-end: Compute and Compare Core Allocators
Section titled “End-to-end: Compute and Compare Core Allocators”use nalgebra::DMatrix;use openquant::portfolio_optimization::{ allocate_inverse_variance, allocate_min_vol, allocate_max_sharpe, allocate_efficient_risk,};
// rows=time, cols=assetslet prices: DMatrix<f64> = /* load matrix */ DMatrix::zeros(252, 6);
let ivp = allocate_inverse_variance(&prices)?;let mv = allocate_min_vol(&prices, None, None)?;let msr = allocate_max_sharpe(&prices, 0.01, None, None)?;let er = allocate_efficient_risk(&prices, 0.12, None, None)?;
assert_eq!(ivp.weights.len(), prices.ncols());assert!((mv.weights.iter().sum::<f64>() - 1.0).abs() < 1e-6);assert!((msr.weights.iter().sum::<f64>() - 1.0).abs() < 1e-6);assert!((er.weights.iter().sum::<f64>() - 1.0).abs() < 1e-6);End-to-end: Constrained Allocation with Exponential Returns and Resampling
Section titled “End-to-end: Constrained Allocation with Exponential Returns and Resampling”use std::collections::HashMap;use openquant::portfolio_optimization::{ allocate_max_sharpe_with, AllocationOptions, ReturnsMethod,};
let mut bounds = HashMap::new();// Cap concentration in first asset; enforce long-only defaults elsewherebounds.insert(0usize, (0.0, 0.20));
let opts = AllocationOptions { risk_free_rate: 0.02, returns_method: ReturnsMethod::Exponential { span: 60 }, resample_by: Some("W"), bounds: Some(bounds), tuple_bounds: Some((0.0, 0.40)), ..Default::default()};
let constrained = allocate_max_sharpe_with(&prices, &opts)?;assert!(constrained.weights.iter().all(|w| *w >= -1e-10));API Reference
Section titled “API Reference”Rust API
Section titled “Rust API”allocate_inverse_varianceallocate_min_volallocate_max_sharpeallocate_efficient_riskAllocationOptions
Implementation Notes
Section titled “Implementation Notes”- Optimizer output is only as good as mean/covariance assumptions; stress-test inputs and rebalance frequency.
- Constraint design (asset caps, sector caps, long/short bounds) is usually more important than small objective tweaks.
- Track turnover, realized slippage, and drift between target and filled weights in production.