Models and weights#

default_model(categorical_features=None, n_knots=5, max_iter=500, learning_rate=0.05, cv=2, n_jobs=None, random_state=0)[source]#

Spline-ridge and boosting stack, the default for every nuisance model.

Used for the outcome model and for the dose mean and scale models. The spline member imputes missing values with the column mean; boosting handles NaN natively. Boosting stops early once a 10% validation split stops improving, so max_iter is a ceiling.

Parameters:
categorical_featureslist of int, optional

Column indices to one-hot encode in the spline member and treat as categorical in boosting (e.g. group codes). Other columns get cubic B-splines.

n_knotsint, default 5

Knots per spline.

max_iterint, default 500

Maximum boosting iterations.

learning_ratefloat, default 0.05

Boosting learning rate.

cvint, default 2

Folds used to choose the blend. 2 was as accurate as 3 in our simulations and a third faster.

n_jobsint, optional

Fit the two members in parallel (passed to StackingRegressor).

random_stateint, default 0

Seed for boosting.

Returns:
sklearn.ensemble.StackingRegressor
class GPS(mean_model=None, scale_model=None, heteroscedastic=True, residual='kde', categorical_features=None, n_jobs=None, random_state=0)[source]#

Location-scale model for the density of a continuous dose given X.

Models A = m(X) + s(X) * e, with the density of e estimated by KDE or assumed Gaussian. Gives density-based weights (Kennedy et al. 2017).

Parameters:
mean_modelsklearn regressor, optional

Model for E[A | X]. Default default_model() (boosting alone gave unstable weights in our simulations).

scale_modelsklearn regressor, optional

Model for E[|A - m(X)| | X]. Ignored if heteroscedastic=False. Absolute residuals are far less noisy than log squared residuals, which produce near-zero scales and exploding weights.

heteroscedasticbool, default True

Model s(X) (True) or use a constant scale (False).

residual{“kde”, “gaussian”}, default “kde”

Non-parametric shape for e, or Gaussian.

categorical_featureslist of int, optional

Column indices treated as categorical by the default models.

n_jobsint, optional

Parallel jobs for the default models.

random_stateint, default 0

Seed for the default models.

fit(X, A)[source]#

Fit the dose mean and scale models and the residual density.

Parameters:
Xarray-like

Confounders (numeric).

Aarray-like

Dose.

Returns:
GPS

self.

density(X, a)[source]#

Density of the dose given X, row by row.

Parameters:
Xarray-like

Confounders (numeric).

aarray-like

Dose for each row.

Returns:
ndarray

pi(a_i | x_i) for each row.

class DiscreteGPS(classifier=None, defensive=0.05, min_count=20, floor=0.0001, n_jobs=None, random_state=0)[source]#

Generalised propensity score for a dose with a few distinct values.

For tiers, or counts such as notifications per week: a classifier’s predicted probability of each level. With a discrete dose, varpi/pi is a ratio of probabilities, which is the right weight (a continuous density is not). fit_dose_response() uses it automatically when the dose has at most max_levels distinct values.

Parameters:
classifiersklearn classifier, optional

Default stacks a spline logistic regression with boosting, which gives far better-calibrated probabilities for rare levels than boosting alone.

defensivefloat, default 0.05

Share of the marginal level probabilities mixed into every prediction, p = (1 - defensive) * p(a | x) + defensive * p(a). This bounds each weight by 1 / defensive (20 by default). Without it, rare top or bottom levels got near-zero probabilities and their weights blew the ends of the curve up in smaller samples.

min_countint, default 20

Levels with fewer rows are merged into a neighbour.

floorfloat, default 1e-4

Lower bound on the returned probabilities.

n_jobsint, optional

Parallel jobs for the default classifier.

random_stateint, default 0

Seed for the default classifier.

fit(X, A)[source]#

Fit the level classifier, merging levels with fewer than min_count rows.

Parameters:
Xarray-like

Confounders (numeric).

Aarray-like

Dose.

Returns:
DiscreteGPS

self.

density(X, a)[source]#

Probability of each row’s dose level given X.

Parameters:
Xarray-like

Confounders (numeric).

aarray-like

Dose for each row, snapped to the nearest observed level.

Returns:
ndarray

p(a_i | x_i) for each row.

class BalancingWeights(basis='quadratic', dose_powers=(1, 2), ridge=0.01, max_weight=50.0, scores=False, score_model=None, n_knots=4)[source]#

Entropy-balancing weights for a continuous dose (Tübbicke 2022).

Chooses weights close to uniform such that, after weighting, the dose is uncorrelated with functions of the confounders. An alternative to GPS for a population curve (groups=None); it helped with many confounders in our simulations.

Parameters:
basis{“quadratic”, “linear”, “spline”}, default “quadratic”

Balance terms h(X): standardised X, plus X^2 (quadratic) or splines.

dose_powerstuple of int, default (1, 2)

Powers of the standardised dose used in g(A).

ridgefloat, default 0.01

Penalty on the dual variables; larger means smoother weights and looser balance.

max_weightfloat, default 50.0

Cap on any single weight.

scoresbool, default False

Also balance cross-fitted predictions of the dose (and of the outcome, if given) and their squares: the directions along which confounding acts.

score_modelsklearn regressor, optional

Model for those predictions.

n_knotsint, default 4

Knots for basis="spline".

fit_weights(X, A, groups=None, Y=None)[source]#

Weights that make the weighted dose uncorrelated with the balance terms.

Parameters:
Xarray-like

Confounders (numeric).

Aarray-like

Dose.

groupsNone

Must be None: balancing weights support one population only.

Yarray-like, optional

Outcome, used only with scores=True.

Returns:
ndarray

One weight per row.