cleverly.sensitivity.SensitivityContour#

class cleverly.sensitivity.SensitivityContour(rows: tuple[tuple[str, tuple[Any, ...]], ...], backend: str | None, estimand: str = '', psi: float = nan, bound: str = 'lower', rho: float = 1.0, scale: str = 'level', max_bias: float = nan, nu2: float = nan, nu2_estimator: str = 'doubly_robust', n_repeats: int = 1, assumption: str | None = None)[source]#

One end of the bias-adjusted range over a grid of confounder strengths.

Returned by contour_data(). to_frame() gives the long frame of cf_d, cf_y and value for a contour plot.

Parameters:
rowstuple

The cf_d, cf_y and value columns, as (name, values) pairs.

backendstr or None

The dataframe backend to_frame() emits.

estimandstr

The estimand the grid bounds.

psifloat

Its reported estimate.

bound{“lower”, “upper”}

Which end of the range each cell holds.

rhofloat

The alignment every cell assumes.

scale{“difference”, “level”}

The scale of the estimate, as the fit reports it. A difference has a no-effect value of zero, and a level (a counterfactual mean) has none.

max_biasfloat

The bias scale sqrt(sigma^2 nu^2) every cell multiplies, the median over the fit’s draws.

nu2float

The Riesz second moment nu^2 behind it, the median over the fit’s draws.

nu2_estimator{“doubly_robust”, “plugin”}

The estimator of nu^2 the cells use.

n_repeatsint

The fit’s cross-fitting draws. Each cell is the median over them.

assumptionstr or None

The sentence a point bound under assume_consistent_mechanism=True carries, else None.

Methods#

classmethod SensitivityContour.build(payload: Mapping[str, Sequence[Any]], *, backend: str | None, estimand: str, psi: float, bound: str, rho: float, scale: str, max_bias: float, nu2: float, nu2_estimator: str, n_repeats: int, assumption: str | None) → SensitivityContour[source]#

Build the grid from its columns.

Parameters:
payloadmapping of str to sequence

The cf_d, cf_y and value columns.

backendstr or None

The backend to_frame() emits.

estimandstr

The estimand the grid bounds.

psifloat

Its reported estimate.

bound{“lower”, “upper”}

Which end of the range each cell holds.

rhofloat

The alignment every cell assumes.

scalestr

The scale of the estimate.

max_biasfloat

The bias scale every cell multiplies.

nu2float

The Riesz second moment behind it.

nu2_estimatorstr

The estimator of nu^2.

n_repeatsint

The fit’s cross-fitting draws.

assumptionstr or None

The consistent-mechanism sentence, or None.

Returns:
SensitivityContour

The grid.

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

Plot the bias-adjusted bound over the two confounder strengths.

Parameters:
**kwargs

Passed to cleverly.plotting.confounding(): robustness=, benchmarks=, multiples=, null=, ax= and the formatting keywords title=, subtitle=, xlabel=, ylabel=, legend= and figsize=.

Returns:
Figure

A filled contour of the cells with the no-effect contour in bold.

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 contour_data, robustness_value
>>> frame, _ = make_linear_ate(n=200, seed=0)
>>> design = PointTreatment(
...     outcome="Y", treatment="A", adjustment=("W1", "W2", "W3", "W4")
... )
>>> result = CausalStudy(frame, design=design).identify(ATE()).estimate(
...     outcome_learner=LinearRegression(),
...     treatment_learner=LogisticRegression(max_iter=1000),
...     cross_fit=False,
...     random_state=0,
... )
>>> contour = contour_data(result, "ate", grid_size=8)
>>> fig = contour.plot(robustness=robustness_value(result, "ate"))
>>> fig.axes[-1].get_ylabel()
'Bias-adjusted lower bound, no sampling error'
>>> plt.close(fig)
SensitivityContour.summary(*, detail: Detail = 'short') → Summary[source]#

Return the printed summary.

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

"full" adds the definitions of the strengths.

Returns:
Summary

A str that prints part of the grid and how to read it.

Attributes#

SensitivityContour.assumption: str | None = None#
SensitivityContour.bound: str = 'lower'#
SensitivityContour.estimand: str = ''#
SensitivityContour.max_bias: float = nan#
SensitivityContour.n_repeats: int = 1#
SensitivityContour.nu2: float = nan#
SensitivityContour.nu2_estimator: str = 'doubly_robust'#
SensitivityContour.psi: float = nan#
SensitivityContour.rho: float = 1.0#
SensitivityContour.scale: str = 'level'#
SensitivityContour.rows: tuple[tuple[str, tuple[Any, ...]], ...]#
SensitivityContour.backend: str | None#