Building blocks#
For custom pipelines: cross-fit the nuisances, build pseudo-outcomes, then fit
HierarchicalDoseResponse on them.
- cross_fit_nuisances(X, A, Y, outcome_model, gps, groups=None, n_folds=5, n_grid=60, n_ref=1000, max_ref_total=60000, max_levels=30, fold_groups=None, n_jobs=None, nuisance_var=False, random_state=0)[source]#
Cross-fit the outcome model and the dose model.
Returns everything
pseudo_outcomes()needs. Either model may be None, in which case its outputs are NaN.- Parameters:
- Xarray-like
Confounders, numeric. The group should also be (encoded) in X so the nuisance models can use it.
- A, Yarray-like
Dose and outcome.
- outcome_modelsklearn regressor or None
Model for E[Y | X, A].
- gpsGPS, DiscreteGPS, BalancingWeights or None
The weight model.
- groupsarray-like, optional
Finest grouping level, one label per row. Integrals over P(x) are taken within each group, so regressing the pseudo-outcome on (A, group) targets theta_g(a) = E[Y(a) | G = g].
- n_foldsint, default 5
Cross-fitting folds.
- n_gridint, default 60
Dose-grid points for the integrals.
- n_refint, default 1000
Rows per group used for the integrals.
- max_ref_totalint, default 60000
Rows in total used for the integrals.
- max_levelsint, default 30
If A has at most this many distinct values it is treated as discrete, and integrals are evaluated at those values exactly.
- fold_groupsarray-like, optional
Ids kept together in cross-fitting (e.g. customer id).
- n_jobsint, optional
Folds fitted in parallel threads (None = 1, -1 = all cores).
- nuisance_varbool, default False
Also return
q, each row’s outcome model on the dose grid (centred and weighted), used for the variance term that accounts for estimating m(a) (Kennedy et al. 2017).- random_stateint, default 0
Seed for the folds.
- Returns:
- Nuisances
- pseudo_outcomes(nuis, Y, kind='dr', max_weight_quantile=0.99)[source]#
Pseudo-outcomes whose regression on A estimates the dose-response curve.
- Parameters:
- nuisNuisances
Output of
cross_fit_nuisances().- Yarray-like
Outcome.
- kind{“dr”, “ipw”, “plugin”}, default “dr”
"dr":(Y - mu) * varpi / pi + m(Kennedy et al. 2017), right if either model is."ipw":Y * varpi / pi, needs the GPS right."plugin":m, needs the outcome model right.- max_weight_quantilefloat, default 0.99
Cap the weights
varpi / piat this quantile.
- Returns:
- ndarray
One pseudo-outcome per row.
- class Nuisances(mu, pi, varpi, m, a_grid=None, q=None)[source]#
Cross-fitted nuisance values, one per row.
- Attributes:
- mundarray
Outcome model E[Y | X, A] at the row’s own dose.
- pindarray
Density (or probability) of the row’s dose given X.
- varpindarray
Marginal density of the dose.
- mndarray
mu averaged over X at the row’s dose.
- a_gridndarray, optional
Dose grid for the variance term.
- qndarray, optional
Each row’s outcome model on
a_grid(seenuisance_var).