Skip to main content

blr_core/
lib.rs

1//! # blr-core: Bayesian Linear Regression with Automatic Relevance Determination
2//!
3//! A performant, pure-Rust implementation of **Bayesian Linear Regression (BLR)** with
4//! **Automatic Relevance Determination (ARD)**, designed for interpretable, sparse modeling
5//! in embedded, edge, and WASM environments.
6//!
7//! ## Part of wamli
8//!
9//! `blr-core` is the mathematical foundation of the
10//! [wamli](https://wamli.github.io/) project, providing portable, interpretable Bayesian
11//! Linear Regression for deployment across edge devices, embedded controllers, and cloud hosts.
12//!
13//! ## Key Features
14//!
15//! - **Interpretable Sparse Models**: ARD automatically discovers and deactivates irrelevant
16//!   features, yielding interpretable coefficients and uncertainty estimates.
17//! - **Uncertainty Quantification**: Posterior distribution over weights with epistemic
18//!   (model) and aleatoric (noise) uncertainty decomposition.
19//! - **Physics-Aware Design**: Sensor calibration and noise estimation modules tailored
20//!   for real-world scientific instruments.
21//! - **Production-Ready**: No unsafe code in core logic, thoroughly tested, suitable for
22//!   WASM compilation and embedded deployment.
23//!
24//! ## Quick Start
25//!
26//! ```rust,no_run
27//! use blr_core::{fit, ArdConfig, PredictiveMarginals};
28//!
29//! // Example: 3 features, 20 training points
30//! let phi = vec![1.0; 60]; // N=20, D=3: feature matrix (row-major)
31//! let y = vec![0.5; 20];    // target observations
32//!
33//! let config = ArdConfig {
34//!     max_iter: 100,
35//!     tol: 1e-5,
36//!     ..ArdConfig::default()
37//! };
38//!
39//! let result = fit(&phi, &y, 20, 3, &config).expect("Fit failed");
40//!
41//! // Predictions with uncertainty
42//! let test_phi = vec![1.0; 30]; // 10 test points × 3 features
43//! let predictions = result.predict(&test_phi, 10, 3);
44//! println!("Mean: {:?}", predictions.mean);
45//! println!("Total std: {:?}", predictions.total_std);
46//! ```
47//!
48//! ## Core Algorithm
49//!
50//! BLR assumes a Gaussian likelihood:
51//!
52//! $$y_n = \\mathbf{\\phi}_n^T \\mathbf{w} + \\epsilon_n, \\quad \\epsilon_n \\sim \\mathcal{N}(0, \\beta^{-1})$$
53//!
54//! ARD places a hierarchical prior on weights:
55//!
56//! $$w_d \\sim \\mathcal{N}(0, \\alpha_d^{-1}), \\quad \\alpha_d \\sim \\text{Gamma}(a_0, b_0)$$
57//!
58//! The algorithm iteratively refines hyperparameters \\(\\{\\alpha_d, \\beta\\}\\) via
59//! empirical Bayes (EM), automatically driving irrelevant \\(\\alpha_d \\to \\infty\\).
60//!
61//! ## Use Cases
62//!
63//! - **Sensor Calibration**: Fit nonlinear response models with automatic feature selection
64//! - **System Identification**: Discover sparse linear relationships in high-dimensional data
65//! - **Edge Inference**: Deploy pretrained models on IoT devices or browser via WASM
66//! - **Scientific Computing**: Physics-informed basis functions with interpretable weights
67//!
68//! ## When NOT to Use blr-core
69//!
70//! - **Large datasets (N > 10 000)**: Matrix inversion scales O(D³); consider gradient-based
71//!   methods for high-feature-count problems.
72//! - **Deep non-linear functions**: BLR is a linear model; complex non-linearities require
73//!   kernel methods or neural networks.
74//! - **Real-time hard-deadline systems**: EM convergence is iterative; worst-case runtime
75//!   is bounded by `max_iter` but not deterministic.
76//! - **Multi-output regression**: The current implementation is single-output only.
77//!
78//! ## Modules
79//!
80//! - [`ard`] — Core BLR+ARD algorithm, model fitting and prediction
81//! - [`noise_estimation`] — Automated noise characterization from residuals
82//! - [`features`] — Polynomial, RBF, and physics-informed basis functions
83//! - [`gaussian`] — Multivariate Gaussian utilities (used internally by `ard`)
84//! - [`synthetic_data`] — Benchmark datasets and ground-truth simulators
85//! - [`error`] — [`BLRError`] error type
86//!
87//! Active learning orchestration, precision tiers, and calibration sessions have
88//! been moved to the `blr-active` crate for better separation of concerns.
89//!
90//! ## Feature Flags
91//!
92//! | Feature | Default | Description |
93//! |---------|---------|-------------|
94//! | `std`   | ✓       | Enable standard library support (required for faer's thread pool) |
95//!
96//! ## Performance
97//!
98//! See the [BENCHMARK_GUIDE.md](https://github.com/wamli/blr-core/blob/main/BENCHMARK_GUIDE.md)
99//! for detailed performance metrics and how to reproduce them on your hardware.
100//!
101//! Quick reference on a single core (Intel i7-12700K, `--release`):
102//!
103//! | Problem | N | D | Fit time |
104//! |---------|---|---|----------|
105//! | Small   | 30 | 6 | ~3 ms |
106//! | Medium  | 60 | 11 | ~12 ms |
107//! | Large   | 500 | 30 | ~240 ms |
108//!
109//! ## Compatibility
110//!
111//! - **Platforms**: Linux, macOS, Windows, WebAssembly (WASI Preview 2)
112//! - **Rust**: 1.70+ (MSRV subject to dependency updates)
113//! - **No unsafe**: Core library is 100% safe Rust
114//! - **Dependencies**: [`faer`](https://crates.io/crates/faer) for linear algebra
115//!
116//! ## References
117//!
118//! - Tipping, M. E. (2001). "Sparse Bayesian learning and the relevance vector machine."
119//!   *Journal of Machine Learning Research*, 1, 211–244.
120//! - Bishop, C. M. (2006). *Pattern Recognition and Machine Learning*. Springer.
121//! - Hennig, P. (2024). *Probabilistic Machine Learning* \[Course\].
122//!   University of Tübingen.
123
124pub mod ard;
125pub mod error;
126pub mod features;
127pub mod gaussian;
128pub mod noise_estimation;
129pub mod synthetic_data;
130
131pub use ard::{fit, fit_with_prior, ArdConfig, BLRPrior, FittedArd, PredictiveMarginals};
132pub use error::BLRError;
133pub use gaussian::Gaussian;