blprs/
lib.rs

1//! Fast Berry–Levinsohn–Pakes (BLP) estimation for random coefficients logit models.
2//!
3//! This crate provides a demand-side estimator that mirrors the public API of
4//! [pyBLP](https://github.com/jeffgortmaker/pyblp) while embracing idiomatic Rust.
5//! It offers tools to
6//!
7//! - manage product-level market data (`data` module),
8//! - describe simulation draws for heterogeneous consumers (`integration` module),
9//! - solve the BLP contraction mapping (`solving` module), and
10//! - assemble a two-step GMM estimator (`estimation` module).
11//!
12//! The implementation focuses on clarity and extensibility. Heavy inline
13//! documentation and unit tests illustrate the essential ingredients of BLP:
14//! market partitioning, heterogeneous consumer integration, fixed-point solving,
15//! and GMM objective evaluation. The goal is to provide a high-quality Rust
16//! alternative to `pyBLP` while maintaining feature parity over time.
17//!
18//! # Quick start
19//!
20//! ```no_run
21//! use blprs::data::{ProductData, ProductDataBuilder};
22//! use blprs::integration::SimulationDraws;
23//! use blprs::{Problem, ProblemOptions};
24//! use nalgebra::{DMatrix, DVector};
25//!
26//! // Assume we have N products and K1 linear, K2 nonlinear characteristics.
27//! let market_ids = vec!["m1".to_string(), "m1".to_string(), "m2".to_string()];
28//! let shares = DVector::from_vec(vec![0.3, 0.2, 0.4]);
29//! let x1 = DMatrix::from_row_slice(3, 2, &[1.0, 10.0, 1.0, 15.0, 1.0, 12.0]);
30//! let x2 = DMatrix::from_row_slice(3, 1, &[10.0, 15.0, 12.0]);
31//! let instruments = x1.clone();
32//!
33//! let products = ProductDataBuilder::new(market_ids, shares)
34//!     .x1(x1)
35//!     .x2(x2)
36//!     .instruments(instruments)
37//!     .build()
38//!     .expect("validated product data");
39//!
40//! let draws = SimulationDraws::standard_normal(200, 1, 1234);
41//!
42//! let problem = Problem::builder()
43//!     .products(products)
44//!     .draws(draws)
45//!     .options(ProblemOptions::default())
46//!     .build()
47//!     .expect("well-formed problem");
48//! let sigma = DMatrix::from_row_slice(1, 1, &[2.0]);
49//!
50//! let result = problem.solve(&sigma).expect("converged");
51//! println!("Estimated betas: {:?}", result.beta);
52//! ```
53//!
54//! The crate is still under heavy development. Supply-side estimation,
55//! optimal instruments, and many advanced `pyBLP` options are tracked in the
56//! public roadmap.
57
58pub mod data;
59pub mod demand;
60pub mod error;
61pub mod estimation;
62pub mod formulation;
63pub mod integration;
64pub mod options;
65pub mod solving;
66
67pub use estimation::{BlpProblem, EstimationResult, Problem, ProblemBuilder, ProblemResults};
68pub use options::{EstimationOptions, GmmOptions, ProblemOptions, WeightingMatrix};
69pub use solving::{ContractionOptions, ContractionSummary};