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:
- rows
tupleofBalanceRow One row per contrast and covariate.
- treatment
str The treatment’s name.
- contrasts
tupleofstr 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.
- n
int The units with a recorded treatment.
- n_repeats
int The cross-fitting repeats the fit holds; the first one’s propensities are used.
- limits
tupleoffloatorNone 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_arms
int The treatment’s arms.
- threshold
float The size an absolute difference is held against.
- rows
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=andtitle=.
- Returns:
FigureOne 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:
- Returns:
tupleofBalanceRowIts 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:
SummaryA
strwhose value is the printed text. Itsfacts,tables,notesandreadinghold 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”}
orNone, default=None Dataframe backend to return.
Noneuses pandas when installed, then the first available backend.
- backend{“pandas”, “polars”, “pyarrow”}
- Returns:
dataframeThe columns
contrast,covariate, the four group means,spread,smd_unweightedandsmd_weighted.
Attributes#
- CovariateBalance.rows: tuple[BalanceRow, ...]#