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
BootstrapResultdraws the percentile limits of its replicates.- Parameters:
- source
BootstrapResultorTMLEResult The replicates, or a fit that holds them. A fit also marks its reported estimate and takes its confidence level.
- name
strorNone, default=None The estimand to draw; the first one with replicates when omitted.
- level
float, default=0.95 Coverage of the percentile limits, for a bare
BootstrapResult.- units
strorNone, default=None Units of the outcome, for the axis label.
- title
str,FalseorNone, default=None The title; a short description of the figure when omitted, and none when
False.- subtitle
str,FalseorNone, 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.- xlabel
str, sequenceofstrorNone, 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.
- ylabel
str, sequenceofstrorNone, default=None The y-axis label, given as for
xlabel.- legend{“top”, “right”, “bottom”}
orFalse, default=”top” Where the key goes: above the axes, right of them, or under the figure;
Falsedraws no key.- figsize
tupleoffloatorNone, default=None Width and height in inches of a figure the plot creates.
- ax
AxesorNone, default=None Axes to draw on.
- source
- Returns:
FigureOne panel.
- Raises:
ValueErrorWhen 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.intervalThe 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)