cleverly.plotting.targeting#

cleverly.plotting.targeting(curve: Any, *, view: str = 'toc', score: str | None = None, ax: Axes | 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, outcome_label: str | None = None, labels: Mapping[str, str] | None = None) → Figure[source]#

Plot how much treating by a score first gains over treating at random.

Parameters:
curveTargetingCurve

From cleverly.study.IdentifiedEffect.targeting_curve() or curve.diagnostics.targeting_curve().

view{“toc”, “gain”, “difference”}, default=”toc”

"toc": the TOC against 0 with pointwise intervals and the band at the grid points. "gain": the cumulative gain against the random-targeting line. "difference": the TOC of the first score minus the second’s, of a curve fitted with compare=.

scorestr or None, default=None

Which score to draw on the TOC and gain views; the primary score when omitted.

axAxes or None, default=None

The axes to draw on; a new figure when omitted.

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.

outcome_labelstr or None, default=None

The outcome’s quantity in your words, such as "order share", for the y axis; the axis says “the mean outcome” or “effect” when omitted.

labelsmapping of str to str or None, default=None

Words for each score in the title and the y axis, keyed by the score’s column name, such as {"S": "the predicted effect"}; the column name when omitted.

Returns:
Figure

The figure drawn on.

Raises:
ValueError

For an unknown view or score, or the difference view of a curve with no comparison.

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 mark the experimental top fractions, say that each interval is for the gap to the random line, and say where the band holds.

Examples

>>> import numpy as np
>>> import pandas as pd
>>> from sklearn.linear_model import LinearRegression, LogisticRegression
>>> from cleverly import CausalStudy, ConditionalContrast, PointTreatment
>>> from cleverly.bases import Modifiers
>>> from cleverly.plotting import targeting
>>> rng = np.random.default_rng(0)
>>> n = 500
>>> w1, w2 = rng.uniform(-1, 1, n), rng.uniform(-1, 1, n)
>>> a = rng.binomial(1, 0.5, n)
>>> y = w2 + a * (1 + w1) + rng.normal(0, 0.5, n)
>>> frame = pd.DataFrame({"Y": y, "A": a, "W1": w1, "W2": w2, "S": w1})
>>> study = CausalStudy(
...     frame, design=PointTreatment(outcome="Y", treatment="A", adjustment=("W1", "W2"))
... )
>>> effect = study.identify(ConditionalContrast(Modifiers(["W1"])))
>>> curve = effect.targeting_curve(
...     "S",
...     computed_from=["W1"],
...     score_kind="external",
...     n_half_samples=200,
...     random_state=0,
...     outcome_learner=LinearRegression(),
...     treatment_learner=LogisticRegression(),
...     outcome_by_arm=True,
...     q_bounds=(-5.0, 6.0),
... )
>>> fig = targeting(curve, view="gain")
>>> fig.axes[0].get_xlabel()
'Fraction treated, highest scores first'
>>> fig = targeting(curve, view="gain", outcome_label="Y", labels={"S": "the score"})
>>> fig.axes[0].get_ylabel()
'Change in Y from treating the top fraction'