cleverly.plotting.bootstrap#

cleverly.plotting.bootstrap(source: Any, *, name: str | None = None, level: float = 0.95, units: str | None = None, 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, ax: Axes | None = None) → Figure[source]#

Plot the bootstrap replicates with a normal curve for comparison.

Use it to see whether the bootstrap distribution is symmetric and bell-shaped on the scale the Wald interval is built on: the log scale for a ratio, the logit scale for the mean of a binary outcome, the transform’s scale for a transformed estimate. When it is skewed or heavy-tailed there, the Wald interval can mislead. The histogram shows the replicates, the curve is a normal density with their mean and standard deviation, and the vertical lines mark the limits. Nothing is resampled here.

From a fit the limits, the spread and their names are the fit’s own, as its summary prints them: the percentile or BCa interval, or a range marked diagnostic when the fit withholds the interval, declares its bootstrap a diagnostic or has too few clusters, and the experimental label when no validation study covers the interval. A bare BootstrapResult draws the percentile limits of its replicates.

Parameters:
sourceBootstrapResult or TMLEResult

The replicates, or a fit that holds them. A fit also marks its reported estimate and takes its confidence level.

namestr or None, default=None

The estimand to draw; the first one with replicates when omitted.

levelfloat, default=0.95

Coverage of the percentile limits, for a bare BootstrapResult.

unitsstr or None, default=None

Units of the outcome, for the axis label.

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.

axAxes or None, default=None

Axes to draw on.

Returns:
Figure

One panel.

Raises:
ValueError

When a fit holds no bootstrap or withheld its bootstrap intervals, the estimand has no replicates, fewer than two replicates are finite, or every replicate is equal.

See also

cleverly.inference.BootstrapResult.interval

The percentile interval 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 the histogram is descriptive, how rows were resampled, and whether the bootstrap interval is experimental.

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
>>> frame, _ = make_linear_ate(n=120, 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,
...     n_bootstrap=40,
...     random_state=0,
... )
>>> fig = result.bootstrap.plot("ate")
>>> fig.texts[0].get_text()
'Bootstrap replicates'
>>> plt.close(fig)