cleverly.sensitivity.CovariateBalance#

class cleverly.sensitivity.CovariateBalance(rows: tuple[BalanceRow, ...], treatment: str, contrasts: tuple[str, ...], n: int, n_repeats: int, limits: tuple[float, float] | None, weighted_data: bool, estimand: Literal['ate', 'att', 'atc'] = 'ate', cross_fitted: bool = False, n_arms: int = 2, threshold: float = 0.1)[source]#

The standardized mean differences of a fit’s covariates, before and after weighting.

Parameters:
rowstuple of BalanceRow

One row per contrast and covariate.

treatmentstr

The treatment’s name.

contraststuple of str

The contrasts, in order. For the ATE: the treated group against the untreated group for a two-arm treatment, each arm against the rest otherwise. For the ATT or ATC: each non-reference arm against the reference arm.

nint

The units with a recorded treatment.

n_repeatsint

The cross-fitting repeats the fit holds; the first one’s propensities are used.

limitstuple of float or None

The limits the fit put on the propensities for this estimand, which the weights respect.

weighted_databool

Whether the data carried observation weights, which both columns use.

estimand{“ate”, “att”, “atc”}

Whose weights and spread the differences use. "ate": each unit weighted by the inverse of the probability of its own arm, divided by the pooled standard deviation of every arm. "att": the arm the effect is among keeps its units’ weights and the reference arm is weighted by the odds of that arm, divided by that arm’s standard deviation. "atc": the mirror image, among the reference arm.

cross_fittedbool

Whether the propensities are out-of-fold predictions; in-sample otherwise.

n_armsint

The treatment’s arms.

thresholdfloat

The size an absolute difference is held against.

Methods#

CovariateBalance.plot(**kwargs: Any) → Any[source]#

Plot each covariate’s absolute difference before and after weighting.

Parameters:
**kwargs

Passed to cleverly.plotting.balance(): max_covariates=, treatment_label=, labels= and title=.

Returns:
Figure

One panel per contrast.

Examples

>>> import matplotlib
>>> matplotlib.use("Agg")
>>> import matplotlib.pyplot as plt
>>> from sklearn.linear_model import LinearRegression, LogisticRegression
>>> from cleverly import ATE, CausalStudy, PointTreatment
>>> from cleverly.datasets import make_linear_ate
>>> from cleverly.sensitivity import covariate_balance
>>> frame, _ = make_linear_ate(n=200, seed=0)
>>> study = CausalStudy(
...     frame,
...     design=PointTreatment(
...         outcome="Y", treatment="A", adjustment=("W1", "W2", "W3", "W4")
...     ),
... )
>>> result = study.identify(ATE()).estimate(
...     outcome_learner=LinearRegression(),
...     treatment_learner=LogisticRegression(max_iter=1000),
...     cross_fit=False,
...     random_state=0,
... )
>>> fig = covariate_balance(result).plot()
>>> fig.axes[1].get_xlabel()
'Absolute standardized mean difference'
>>> plt.close(fig)
CovariateBalance.rows_of(contrast: str) → tuple[BalanceRow, ...][source]#

The rows of one contrast.

Parameters:
contraststr

One of contrasts.

Returns:
tuple of BalanceRow

Its covariates, in the covariates’ order.

CovariateBalance.summary(detail: Detail = 'short') → Summary[source]#

Return the printed summary of the balance.

Parameters:
detail{“short”, “full”}, default=”short”

"short" prints each covariate’s difference before and after weighting. "full" adds the four group means and the spread each difference divides by.

Returns:
Summary

A str whose value is the printed text. Its facts, tables, notes and reading hold the parts as data.

CovariateBalance.to_frame(backend: str | None = None) → Any[source]#

Return one row per contrast and covariate.

Parameters:
backend{“pandas”, “polars”, “pyarrow”} or None, default=None

Dataframe backend to return. None uses pandas when installed, then the first available backend.

Returns:
dataframe

The columns contrast, covariate, the four group means, spread, smd_unweighted and smd_weighted.

Attributes#

CovariateBalance.cross_fitted: bool = False#
CovariateBalance.estimand: Literal['ate', 'att', 'atc'] = 'ate'#
CovariateBalance.n_arms: int = 2#
CovariateBalance.threshold: float = 0.1#
CovariateBalance.rows: tuple[BalanceRow, ...]#
CovariateBalance.treatment: str#
CovariateBalance.contrasts: tuple[str, ...]#
CovariateBalance.n: int#
CovariateBalance.n_repeats: int#
CovariateBalance.limits: tuple[float, float] | None#
CovariateBalance.weighted_data: bool#