Skip to main content

dml_core/
lib.rs

1//! # dml-core
2//!
3//! Double / debiased machine learning (Chernozhukov et al., 2018) in Rust.
4//!
5//! ## Models
6//!
7//! - [`Plr`]: partially linear regression (partialling-out or IV-type score)
8//! - [`Irm`]: interactive regression model for a binary treatment (ATE / ATTE)
9//! - [`Pliv`]: partially linear IV regression
10//! - [`Iivm`]: interactive IV model, local average treatment effect (LATE) with a binary instrument
11//! - [`Did`] / [`DidCs`]: doubly robust difference-in-differences for panel data and
12//!   repeated cross-sections (Sant'Anna & Zhao, 2020)
13//!
14//! All of them have scores that are linear in θ and share one engine
15//! ([`fit_model`]): repeated cross-fitting, DML2 estimation, standard errors,
16//! confidence intervals, a multiplier [`Bootstrap`] for joint inference and
17//! multiple-testing adjustment. New scores plug in via [`LinearScoreModel`].
18//!
19//! ## Heterogeneous effects
20//!
21//! - [`SLearner`], [`TLearner`], [`XLearner`], [`RLearner`], [`DrLearner`]: CATE meta-learners
22//! - [`Blp`]: best linear predictor of the CATE and group average effects (GATE) with
23//!   robust inference, from [`DrLearner`] fits or [`DmlFit::cate`] / [`DmlFit::gate`]
24//!
25//! ## Building blocks
26//!
27//! - [`DmlData`]: outcome `y`, one or more treatments `d`, covariates `x`, optional instrument `z`
28//! - [`Regressor`] / [`Classifier`]: plug-in nuisance learners
29//!   ([`Ols`], [`Lasso`], [`LogisticRegression`], [`RandomForestRegressor`],
30//!   [`RandomForestClassifier`] built in)
31//! - [`KFold`] / [`SampleSplitting`]: reproducible sample splitting
32//!
33//! ```
34//! use dml_core::{DmlData, Ols, Plr};
35//! use ndarray::{Array1, Array2};
36//!
37//! let n = 200;
38//! let x = Array2::from_shape_fn((n, 2), |(i, j)| ((i * 7 + j * 3) % 11) as f64);
39//! let d: Array1<f64> = (0..n).map(|i| x[[i, 0]] + ((i * 13) % 5) as f64).collect();
40//! let y: Array1<f64> = (0..n).map(|i| 0.5 * d[i] - x[[i, 1]] + ((i * 17) % 3) as f64).collect();
41//!
42//! let fit = Plr::new(Ols::new(), Ols::new()).n_folds(5).seed(42).fit(&DmlData::new(y, d, x)?)?;
43//! assert!((fit.coef()[0] - 0.5).abs() < 0.1);
44//! let ci = fit.confint(0.95)?; // shape (n_treat, 2)
45//! # let _ = ci;
46//! # Ok::<(), dml_core::DmlError>(())
47//! ```
48
49pub mod blp;
50pub mod bootstrap;
51pub mod cate;
52pub mod crossfit;
53pub mod data;
54pub mod error;
55pub mod fit;
56pub mod learner;
57pub(crate) mod linalg;
58pub mod model;
59pub mod score;
60pub mod stats;
61
62pub use blp::Blp;
63pub use bootstrap::{Bootstrap, BootstrapMethod};
64pub use cate::{CateFit, DrLearner, RLearner, SLearner, TLearner, XLearner};
65pub use crossfit::{
66    cross_fit_predict, cross_fit_predict_proba, cross_fit_predict_proba_subset,
67    cross_fit_predict_subset, Fold, KFold, SampleSplitting,
68};
69pub use data::DmlData;
70pub use error::{DmlError, Result};
71pub use fit::{fit_model, DmlFit, PAdjust};
72pub use learner::{
73    Classifier, Lasso, LogisticRegression, MaxFeatures, Ols, RandomForestClassifier,
74    RandomForestRegressor, Regressor,
75};
76pub use model::{
77    Did, DidCs, DidScore, Iivm, Irm, IrmScore, LinearScoreModel, Pliv, Plr, PlrScore,
78    ScoreEstimate, ScoreInput,
79};
80pub use score::LinearScore;