cleverly.plotting.overlap#

cleverly.plotting.overlap(result: Any, *, treatment_label: str | None = None, labels: dict[Any, str] | None = None, regimen: str | None = None, nodes: Sequence[int] | None = None, censoring: bool = False, horizon: float | None = None, cause: str | None = None, time_label: str = 'Time point', title: str | Literal[False] | None = None, subtitle: str | Literal[False] | None = None, xlabel: str | Sequence[str] | None = None, ylabel: str | Sequence[str] | None = None, legend: str | Literal[False] = 'top', figsize: tuple[float, float] | None = None) → Figure[source]#

Plot the predicted probability of treatment for treated and untreated units.

Use it to check overlap: whether every kind of unit had a real chance of each treatment. Where one group’s histogram has mass and the other’s has none, the comparison rests on extrapolation, and units near 0 or 1 get large weights. The limits the fit put on the probabilities are drawn as dotted lines, with the share of units beyond each. Bars are shares of all units, weighted by the data’s weights.

The probabilities are the out-of-fold predictions before the limits were applied. A fit with several cross-fitting repeats draws the first repeat, and notes() says so.

Fits beyond one point treatment draw the probabilities they hold:

  • A survival fit of a treatment given once at baseline draws the baseline treatment probability per arm. A collaborative fit draws, per arm, the treatment model its search selected for that arm at the last horizon. The fit’s g_bounds limit the product of the treatment probability and the probability of staying under observation, not the treatment probability alone, so censoring=True adds that product for each regimen at the chosen horizon, with its limit.

  • A fit with a treatment at several time points draws one panel per time point: the probability of the regimen’s treatment among the units whose history up to that time point is observed and followed the regimen, those who then took the regimen’s treatment above and those who did not below. censoring=True adds each time point’s probability of staying under observation among those who took it. The limits apply to the cumulative product, which weights() draws, so no limit line is drawn here.

  • A structural nested mean fit draws each time point’s estimated probability of each treatment level given the history, with the margin from 0 and 1 its positivity summary counts.

  • An adaptive TMLE fit with external data draws the treatment probability on the trial rows, and the probability of being in the trial for trial against external rows, with the floor its positivity warning checks.

  • A continuous-time survival fit draws the predicted probability of the treated arm, units given it above and units given the reference arm below. Its floor bounds the product of the treatment probability and the probability of staying uncensored, so censoring=True adds that product just before the chosen target time for the units given each arm, with the floor.

Parameters:
resultTMLEResult or another fitted result

A fitted result with an estimated treatment mechanism: a TMLEResult, LongitudinalResult, StructuralNestedMeanResult, AdaptiveTMLEResult or ContinuousSurvivalResult.

treatment_labelstr or None, default=None

Words for the treatment in the axis label: "Predicted probability of smoking = 1", or with labels= "Predicted probability of smoking: smoking mothers". The column name when omitted, or "being in the group" beside labels=.

labelsdict or None, default=None

Words for each arm, keyed by the arm’s level as in the data, such as {1: "offered", 0: "not offered"}. Each names its group of units on its own. On a fit with a treatment at several time points, words for each regimen keyed by its label. On a continuous-time survival fit, words for each arm keyed by its label.

regimenstr or None, default=None

On a fit with a treatment at several time points, the one regimen to draw; every regimen of the result, one block of panels each, when omitted. A result with more than three regimens needs it.

nodessequence of int or None, default=None

On a fit with a treatment at several time points, the time points to draw, counted from one; every one when omitted.

censoringbool, default=False

On a longitudinal or survival fit with censoring, also draw the probability of staying under observation: the cumulative product with the treatment probability for a baseline treatment, each time point’s probability for a treatment over time.

horizonfloat or None, default=None

The horizon of the cumulative product censoring=True draws for a baseline treatment, or the target time on a continuous-time survival fit; the largest when omitted.

causestr or None, default=None

The cause of that product under competing events; the first when omitted.

time_labelstr, default=”Time point”

Words for a time point in the panel titles.

titlestr, False or None, default=None

The title; a short description of the figure when omitted, and none when False.

subtitlestr, False or None, default=None

The line under the title; the plot’s own, which names the estimate and its headline number, when omitted, and none when False.

xlabelstr, sequence of str or None, default=None

The x-axis label. A string replaces the first labelled panel’s label and the same label wherever it repeats; a sequence gives each labelled panel its own, in reading order.

ylabelstr, sequence of str or None, default=None

The y-axis label, given as for xlabel.

legend{“top”, “right”, “bottom”} or False, default=”top”

Where the key goes: above the axes, right of them, or under the figure; False draws no key.

figsizetuple of float or None, default=None

Width and height in inches of a figure the plot creates.

Returns:
Figure

One panel for a two-arm treatment, one per arm for more arms; one per time point, or per time point and level, for a treatment over time.

Raises:
CapabilityError

When the fit estimated no treatment mechanism; when a time point’s treatment is a dose or a policy whose weight is a density ratio, which has no probability per arm.

DataError

When the treatment is continuous, which has no probability per arm; when the regimen, a time point, the horizon, the target time or the cause is not in the result; or when a fit over time holds more than three regimens and regimen is omitted.

ValueError

When censoring=True and the fit has no censoring, a keyword for a fit over time is given for one that is not, or a continuous-time survival fit is given horizon without censoring=True.

See also

cleverly.sensitivity.positivity_report

The same overlap as numbers.

Notes

The figure carries no note text. It does not mark an experimental fit or band, one whose interval has no checked coverage because no registered study covers it or a stated condition applies, such as too few clusters: read notes() on the figure before you publish it. For this plot the notes say which cross-fitting repeat is drawn and that the share of units beyond each limit is printed at its foot. For a fit over time they also say which regimen and horizon are drawn, which units each panel holds, and where the fit’s limits apply instead.

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.plotting import overlap
>>> 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 = overlap(result)
>>> fig.axes[0].get_xlabel()
'Predicted probability of A = 1'
>>> plt.close(fig)