regit-covariance
Covariance matrix denoising for financial risk validation. Pure Rust.
What it does
regit-covariance takes a returns matrix, estimates the covariance structure, applies denoising using random matrix theory, and produces validated risk metrics that serve as an independent second opinion against prescribed PRIIPs methodology.
For a fund with N = 500 assets and T = 252 trading days, the sample covariance matrix is rank-deficient with at least 248 zero eigenvalues. The remaining eigenvalues are systematically distorted. Risk metrics computed on this matrix inherit that distortion. This crate fixes it.
Mathematical pipeline
Returns (T x N)
-> Sample correlation matrix
-> Eigendecomposition
-> Marchenko-Pastur noise filtering
-> Eigenvalue replacement + reconstruction
-> Denoised correlation matrix (PSD, trace-preserving)
-> Risk metrics (VaR, SRI, divergence report)
Methods implemented
| Method | Reference |
|---|---|
| Sample correlation (equal-weight + EWM) | Standard |
| Marchenko-Pastur density & noise fitting | Marchenko & Pastur (1967) |
| Eigenvalue denoising (constant + target) | Lopez de Prado (2018), Ch. 2 |
| Detoning (market mode removal) | Lopez de Prado (2018), Ch. 2 |
| Linear shrinkage | Ledoit & Wolf (2004) |
| Nonlinear shrinkage (analytical) | Ledoit & Wolf (2020) |
| Parametric VaR (Gaussian) | PRIIPs RTS, Annex II |
| Cornish-Fisher VaR | Cornish & Fisher (1938) |
| PRIIPs SRI (1-7 scale) | EU Regulation 2017/653 |
Quick start
# Clone and build
# Run the server (includes embedded visualization)
# Open http://localhost:3000 in your browser
# Enter tickers or ISINs (e.g. AAPL,MSFT,GOOGL or LU1681043599)
# and click "Run Pipeline"
# Or switch to Synthetic mode for generated data
ISINs are automatically resolved to Yahoo Finance tickers via the search API, with preference for EUR-listed exchanges.
API
REST endpoints
| Method | Path | Description |
|---|---|---|
GET |
/ |
Embedded visualization (Chart.js) |
GET |
/api/health |
Health check |
POST |
/api/compute |
Trigger computation |
GET |
/api/results |
List computation IDs |
GET |
/api/results/:id |
Get full result as JSON |
GET |
/api/stream/:id |
SSE event stream |
Compute request
Live mode (fetches prices from Yahoo Finance):
Synthetic mode (generated data):
The optional prescribed_sri field accepts the SRI (1-7) from a PRIIPs KID document, enabling direct comparison against the kernel's independent estimate. For multi-asset portfolios, per-asset SRI breakdown is included in the risk_metrics event.
SSE events
The stream delivers events progressively:
status- Pipeline stage updateseigenvalues_raw- Raw eigenvalue spectrummp_fit- Marchenko-Pastur fit (noise variance, bounds, signal/noise partition)eigenvalues_denoised- Cleaned eigenvaluescondition_number- Before/after conditioning with health classificationrisk_metrics- VaR, SRI, divergence reportcomplete- Full result payload
Library usage
use ;
let cov = correlation_matrix?;
let eigen = eigendecompose?;
let mp = fit_sigma_sq?;
let cleaned = denoise;
let eigen_clean = eigendecompose?;
let improvement = compare;
println!;
Every function is pure: data in, result out, no side effects. See examples/quickstart.rs for a complete working example.
Architecture
src/
lib.rs # Public API
main.rs # axum server + SSE
math/
sample_covariance.rs # C = (1/T) X'X, EWM
eigen.rs # Eigendecomposition wrapper
marchenko_pastur.rs # MP density, noise fitting
denoise.rs # Eigenvalue replacement
detone.rs # Market mode removal
ledoit_wolf.rs # Linear + nonlinear shrinkage
var.rs # Parametric + Cornish-Fisher VaR
sri.rs # PRIIPs SRI (1-7)
condition.rs # Condition number monitoring
data/
returns.rs # Log returns: r_t = ln(P_t / P_{t-1})
provider/
yahoo.rs # Yahoo Finance v8 chart API
api/
routes.rs # REST endpoints
sse.rs # Server-Sent Events
compute.rs # Pipeline orchestration
state.rs # Shared state
One file, one mathematical operation. Each function is pure and composable.
Testing
87 unit tests covering analytical correctness, trace preservation, PSD, symmetry, eigenvalue ordering, reconstruction accuracy, PRIIPs SRI thresholds, and edge cases.
33 integration tests across 5 test suites:
trace_preservation— proptest: denoised trace = N, symmetry (randomized dimensions)psd_check— positive semi-definiteness across q ratios and seedsdenoise_identity— pure noise → near-identity after denoisingmp_analytical— MP bounds match theoretical formulaspipeline_full— end-to-end: returns → correlation → eigen → MP → denoise → VaR → SRI
Code quality
#![forbid(unsafe_code)]in all math modulesclippy::pedanticwith zero warnings- Every public function documented with mathematical references
- No
unwrap()in library code - Deterministic: same input produces bit-identical output
Dependencies
| Crate | Purpose | License |
|---|---|---|
nalgebra |
Linear algebra, eigendecomposition | Apache-2.0 |
axum |
HTTP server + SSE | MIT |
tokio |
Async runtime | MIT |
serde |
Serialization | Apache-2.0/MIT |
thiserror |
Error types | Apache-2.0/MIT |
reqwest |
HTTP client (Yahoo Finance) | Apache-2.0/MIT |
chrono |
Date handling | Apache-2.0/MIT |
License policy enforced via cargo-deny. No copyleft dependencies.
Data disclaimer
Market data is fetched from Yahoo Finance and is provided for educational and research purposes only. Yahoo Finance data should not be used for production regulatory reporting, live trading decisions, or as the sole basis for investment advice. For production use in regulated environments, source market data from a licensed provider (Bloomberg, Refinitiv, etc.).
References
- Marchenko, V. A., & Pastur, L. A. (1967). Distribution of eigenvalues for some sets of random matrices. Matematicheskii Sbornik, 114(4), 507-536.
- Ledoit, O., & Wolf, M. (2004). A well-conditioned estimator for large-dimensional covariance matrices. Journal of Multivariate Analysis, 88(2), 365-411.
- Ledoit, O., & Wolf, M. (2020). Analytical nonlinear shrinkage of large-dimensional covariance matrices. Annals of Statistics, 48(5), 3043-3065.
- Lopez de Prado, M. (2018). Advances in Financial Machine Learning. Wiley. Chapters 2-3.
- Cornish, E. A., & Fisher, R. A. (1938). Moments and cumulants in the specification of distributions. Revue de l'Institut International de Statistique, 5(4), 307-320.
- PRIIPs Delegated Regulation (EU) 2017/653, Annex II.
Documentation
- MATH.md — Full mathematical derivations (Marchenko-Pastur through SRI classification)
- CHANGELOG.md — Release history
- SECURITY.md — Vulnerability disclosure policy
License
Apache License 2.0. See LICENSE and NOTICE.
Part of Regit OS — the operating system for investment products. From Luxembourg.