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//! - [`StaggeredDid`]: DID with staggered adoption, group-time effects `ATT(g, t)` and their
14//! aggregation by cohort, calendar time or event time (Callaway & Sant'Anna, 2021)
15//!
16//! All of them have scores that are linear in θ and share one engine
17//! ([`fit_model`]): repeated cross-fitting, DML2 estimation, standard errors,
18//! confidence intervals, a multiplier [`Bootstrap`] for joint inference and
19//! multiple-testing adjustment. New scores plug in via [`LinearScoreModel`].
20//!
21//! ## Sensitivity analysis
22//!
23//! [`DmlFit::sensitivity_analysis`] bounds the effect of omitted confounders (PLR, IRM, DID)
24//! and reports robustness values; `sensitivity_benchmark` calibrates the scenario against
25//! observed covariates.
26//!
27//! ## Heterogeneous effects
28//!
29//! - [`SLearner`], [`TLearner`], [`XLearner`], [`RLearner`], [`DrLearner`]: CATE meta-learners
30//! - [`Blp`]: best linear predictor of the CATE and group average effects (GATE) with
31//! robust inference, from [`DrLearner`] fits or [`DmlFit::cate`] / [`DmlFit::gate`]
32//!
33//! ## Building blocks
34//!
35//! - [`DmlData`]: outcome `y`, one or more treatments `d`, covariates `x`, optional instrument `z`
36//! - [`Regressor`] / [`Classifier`]: plug-in nuisance learners
37//! ([`Ols`], [`Lasso`], [`LogisticRegression`], [`RandomForestRegressor`],
38//! [`RandomForestClassifier`], [`GradientBoostingRegressor`], [`GradientBoostingClassifier`]
39//! built in)
40//! - [`KFold`] / [`SampleSplitting`]: reproducible sample splitting
41//!
42//! ```
43//! use dml_core::{DmlData, Ols, Plr};
44//! use ndarray::{Array1, Array2};
45//!
46//! let n = 200;
47//! let x = Array2::from_shape_fn((n, 2), |(i, j)| ((i * 7 + j * 3) % 11) as f64);
48//! let d: Array1<f64> = (0..n).map(|i| x[[i, 0]] + ((i * 13) % 5) as f64).collect();
49//! let y: Array1<f64> = (0..n).map(|i| 0.5 * d[i] - x[[i, 1]] + ((i * 17) % 3) as f64).collect();
50//!
51//! let fit = Plr::new(Ols::new(), Ols::new()).n_folds(5).seed(42).fit(&DmlData::new(y, d, x)?)?;
52//! assert!((fit.coef()[0] - 0.5).abs() < 0.1);
53//! let ci = fit.confint(0.95)?; // shape (n_treat, 2)
54//! # let _ = ci;
55//! # Ok::<(), dml_core::DmlError>(())
56//! ```
57
58pub mod blp;
59pub mod bootstrap;
60pub mod cate;
61pub mod crossfit;
62pub mod data;
63pub mod error;
64pub mod fit;
65pub mod learner;
66pub(crate) mod linalg;
67pub mod model;
68pub mod score;
69pub mod sensitivity;
70pub mod staggered;
71pub mod stats;
72
73pub use blp::Blp;
74pub use bootstrap::{Bootstrap, BootstrapMethod};
75pub use cate::{CateFit, DrLearner, RLearner, SLearner, TLearner, XLearner};
76pub use crossfit::{
77 cross_fit_predict, cross_fit_predict_proba, cross_fit_predict_proba_subset,
78 cross_fit_predict_subset, Fold, KFold, SampleSplitting,
79};
80pub use data::DmlData;
81pub use error::{DmlError, Result};
82pub use fit::{fit_model, DmlFit, PAdjust};
83pub use learner::{
84 Classifier, GradientBoostingClassifier, GradientBoostingRegressor, Lasso, LogisticRegression,
85 MaxFeatures, Ols, RandomForestClassifier, RandomForestRegressor, Regressor,
86};
87pub use model::{
88 Did, DidCs, DidScore, Iivm, Irm, IrmScore, LinearScoreModel, Pliv, Plr, PlrScore,
89 ScoreEstimate, ScoreInput,
90};
91pub use score::LinearScore;
92pub use sensitivity::{
93 Nu2Parts, SensitivityBenchmark, SensitivityParams, SensitivityParts, SensitivityResult,
94};
95pub use staggered::{
96 Aggregation, ControlGroup, DidAggregation, GroupTime, GtCombinations, PanelData,
97 RepeatedCrossSections, StaggeredDid, StaggeredDidFit, StaggeredInput,
98};