Source code for cleverly.sensitivity.marginal

r"""Bounds on arm means and the ATE under the marginal sensitivity model.

Tan (2006, Section 4.2, eq. (9)) lets an unobserved ``U`` move the odds of treatment by at
most a factor :math:`\Gamma \ge 1` against the odds given the covariates alone:

.. math::

    \Gamma^{-1} \le \frac{P(A = 1 \mid W, U) / P(A = 0 \mid W, U)}
                          {e(W) / (1 - e(W))} \le \Gamma,

with :math:`(Y(1), Y(0)) \perp A \mid (W, U)`.  :math:`\Gamma = 1` is no unmeasured
confounding.

**The sharp arm bound.**  Under the model :math:`E[Y(1) \mid A = 0, W] = E[w Y \mid A = 1, W]`
for a weight with values in :math:`[\Gamma^{-1}, \Gamma]` and conditional mean one.  The
largest such value is a linear program whose dual is

.. math::

    \rho^+(W) = \min_q \; q + E[\varphi_\Gamma(Y - q) \mid A = 1, W],
    \qquad \varphi_\Gamma(u) = \Gamma u_+ - \Gamma^{-1} u_-.

Any :math:`q` gives an upper bound (weak duality), and the :math:`\tau`-quantile of
:math:`Y \mid A = 1, W` with :math:`\tau = \Gamma / (1 + \Gamma)` attains it, for a
continuous or a discrete outcome.  The arm bound is
:math:`\psi^+_1 = E[A Y + (1 - A) \rho^+(W)]`.  The lower bound applies the same step to
:math:`-Y`, and arm 0 exchanges the roles of the arms.  The ATE bounds are
:math:`[\psi^-_1 - \psi^+_0, \psi^+_1 - \psi^-_0]`.

**The estimator.**  With :math:`R(q) = q + \varphi_\Gamma(Y - q)` the bound is estimated by
the cross-fitted mean of

.. math::

    A Y + (1 - A) \hat\rho(W) + A \frac{1 - \hat e(W)}{\hat e(W)}
    \bigl(R(\hat q) - \hat\rho(W)\bigr),

which is consistent for a valid bound when :math:`\hat e` or :math:`\hat\rho` is consistent,
and for the sharp bound when :math:`\hat q` also converges to the quantile.  The derivative
of :math:`E[R(q) \mid A = 1, W]` in :math:`q` vanishes at the quantile of a continuous
outcome, so the quantile error enters at second order.  :math:`\hat\rho` is
:math:`\hat\mu / \Gamma + (1 - 1/\Gamma)\hat c` with :math:`\hat\mu` the fit's out-of-fold
outcome regression and :math:`\hat c` a regression of the tail pseudo-outcome
:math:`q + (Y - q)_+ / (1 - \tau)`.  For each predicted fold, the quantile trains on one
half of the other folds and the tail regression on the other half, as the authors' code
does, so an overfitted quantile cannot bias a training pseudo-outcome.  On a binary
outcome both nuisances are closed forms of :math:`\hat\mu`:
:math:`\hat q = 1\{\hat\mu > 1 - \tau\}` and
:math:`\hat\rho^+ = \min(\Gamma\hat\mu, 1 - (1 - \hat\mu)/\Gamma)`.  These are the formulas of
the authors' replication code for Dorn, Guo and Kallus,
``CausalML/DoublyValidDoublySharpSensitivity`` (``simplified_dvds.R``).

**Compositions.**  Each arm bound is the mean of a per-row summand, so the other
parameters are means of the same summands.

* More than two arms: the model restricts the odds of ``A = a`` against ``A != a``, so
  ``e`` is ``P(A = a | W)`` and the summand reads the indicator of arm ``a``.
* ``att`` and ``atc`` (two arms): :math:`E[Y(0) \mid A = 1] P(A = 1)` is the mean of the
  arm-0 summand less its observed-arm part, and the mean is divided by the share of the
  conditioning arm (a delta-method curve).
* ``rr`` and ``or``: the ratio map is increasing in the arm and decreasing in the
  reference, so ``RR+ = psi+_1 / psi-_0``. Each end's limits come from its log-scale curve.
  The set is componentwise, with no sharpness claim beyond the arm bounds.
* Sampling weights: every mean is a weighted mean and every regression is weighted.
* Baseline strata: a stratum-conditional parameter is the mean over the stratum's rows,
  divided by the stratum's share.
* Missing outcomes: under :math:`\Delta \perp Y \mid A, W`, with ``U`` acting on ``A``
  only, the summand's residual terms are multiplied by
  :math:`\Delta / P(\Delta = 1 \mid A, W)` from the fit's response model, and the
  quantile and the tail regression train on observed rows.
* A selecting ``CTMLE`` fit stores a mechanism that does not estimate ``P(A | W)``, so the
  bounds refit the fit's treatment learner on every covariate, on the fit's folds, as
  :func:`~cleverly.sensitivity.omitted_variable_bounds` does under
  ``assume_consistent_mechanism=True``.

**The interval.**  ``[lower - z se_lower, upper + z se_upper]`` with
:math:`z = \Phi^{-1}(1 - (1 - \text{level})/2)` covers the identified set with asymptotic
probability at least ``level`` by the union bound.  Each limit is the matching side of the
two-sided interval at ``level`` that the bounded estimate's own rule forms about that end
(:func:`~cleverly.inference.interval_reference`): the range-logit scale of a bounded mean,
the skew-widened t of a continuous outcome and the MOVER multiples of a bounded difference
replace the normal ``z`` where the estimate's interval reads them.  A clustered fit below the
few-cluster threshold replaces :math:`z` with the Student t quantile on the fit's own
``J - 2`` degrees of freedom over the rows the bounded parameter reads.  It is a set
interval: the tighter interval for the parameter alone (Imbens and Manski 2004) is not
shipped.  The binary quantile is a plug-in threshold, so its sharpness needs a margin
condition: the outcome regression puts little mass near :math:`1 - \tau`.  Validity needs no
such condition.

**What the interval may claim.**  It resolves through the registry of
:mod:`cleverly.validation._interpretation`, instrument :data:`MSM_BOUND_INSTRUMENT`.  Study
``marginal-bound-limit-coverage`` (X82 SH) registers the ``continuous`` and ``binary`` rows
for the ATE of a five-fold cross-fitted TMLE with GLM nuisances at ``n >= 8000``.  Every
other fit prints the registry's descriptive sentence.
"""

from __future__ import annotations

import os
import pickle
import sys
import warnings
from collections.abc import Callable, Sequence
from dataclasses import dataclass, field, fields, replace
from typing import TYPE_CHECKING, Any

import numpy as np

from .._continuous_survival_family import refuse_continuous_survival
from .._inference_status import FEW_CLUSTER_ESTIMAND_STATUS, status_record, supplies_inference
from .._refusal import NO_ALTERNATIVE, Refusal, RefusalReason
from .._trial_family import AUGMENTED_TRIAL_FAMILY, augmented_trial_refusal, refuse_augmented_trial
from ..data.known_mechanism import known_score_fit, known_score_reader_refusal
from ..exceptions import CapabilityError
from ..inference.cluster import cluster_reference_df, influence_variance
from ..inference.cluster_jackknife import jackknife_fit
from ..inference.delta import reference_quantile
from ..inference.influence import ParameterEstimate
from ..utils.frames import emit_frame
from ..utils.parallel import map_parallel, resolve_n_jobs
from ._limits import bound_limits

if TYPE_CHECKING:  # pragma: no cover - typing only
    from .._summary import Detail, Summary
    from ..estimators.base import TMLEResult
    from ..validation._interpretation import Resolution

__all__ = [
    "MSM_BOUND_INSTRUMENT",
    "MarginalSensitivityBounds",
    "MarginalSensitivityRow",
    "marginal_sensitivity_bounds",
]

FloatArray = np.ndarray

#: The registry instrument of the bound's confidence limits.  The rule is the outcome kind,
#: ``"continuous"`` or ``"binary"``, followed by one ``"+feature"`` per composition the
#: bound used (:func:`_limit_reading`), then ``"+repeats"`` on a fit with more than one
#: cross-fitting draw and ``"+clustered"`` on a clustered fit.
MSM_BOUND_INSTRUMENT = "msm_bound_limit"

#: The default grid of the sensitivity parameter.
DEFAULT_GAMMA_GRID = (1.0, 1.5, 2.0)

#: The fold count of the bound's own split when the fit was not cross-fitted.
_IN_SAMPLE_FOLDS = 5

#: The roadmap row of the compositions with no known calculation.
_OWNER = "X87"

#: The roadmap row of the compositions X82 part H2 adds.
_PART_H2 = "X82"

#: The fitted methods the bounds read. A selecting C-TMLE has its mechanism refitted.
_BOUNDED_METHODS = frozenset({"tmle", "drtmle", "collaborative_tmle"})

#: The statuses a collaborative fit withholds its interval under because its mechanism is
#: selected. The bounds refit the mechanism on every covariate, so their own curve does not
#: read the selection.
_SELECTION_STATUSES = frozenset({"working_mechanism_plugin", "generated_design_plugin"})


[docs] @dataclass(frozen=True, repr=False) class MarginalSensitivityRow: """The bounds at one value of the sensitivity parameter. Parameters ---------- gamma : float The sensitivity parameter :math:`\\Gamma`. estimand : str The bounded parameter. lower : float The estimated lower bound. upper : float The estimated upper bound. lower_std_error : float The standard error of ``lower``. upper_std_error : float The standard error of ``upper``. lower_limit : float The lower side of the estimate's own interval rule about ``lower``: ``lower`` less ``z`` standard errors under the normal identity rule. upper_limit : float The upper side of that rule about ``upper``. """ gamma: float estimand: str lower: float upper: float lower_std_error: float upper_std_error: float lower_limit: float upper_limit: float def __repr__(self) -> str: from .._summary import short_repr return short_repr( "MarginalSensitivityRow", [ ("gamma", f"{self.gamma:g}"), ("", self.estimand), ("", f"range [{self.lower:.4g}, {self.upper:.4g}]"), ], )
[docs] @dataclass(frozen=True, repr=False) class MarginalSensitivityBounds: """Marginal sensitivity model bounds over a grid of :math:`\\Gamma`. Parameters ---------- rows : tuple of MarginalSensitivityRow One row per :math:`\\Gamma`, in increasing order. estimand : str The bounded parameter. level : float The coverage level of the set interval. null : float or None The null value of the estimand: ``0.0`` for a difference, ``1.0`` for a ratio and ``None`` for an arm mean. outcome_kind : {"continuous", "binary"} Which nuisance route the bounds used. quantile_learner : tuple of str The quantile learner each side and arm selected, or ``()`` for a binary outcome. pseudo_outcome_learner : tuple of str The tail-regression learner each side and arm selected, or ``()``. limit_reading : Resolution or None The registry's reading of the confidence limits. backend : str or None The dataframe backend of the fit's data, which :meth:`to_frame` emits. scale : {"identity", "log"} The scale of the standard errors. A risk ratio or an odds ratio reports its ends and limits on the ratio scale and its standard errors on the log scale. reference_df : int or None The degrees of freedom of the Student t reference of the limits, the fit's own rule over the rows the bounded parameter reads; ``None`` for the normal reference. Attributes ---------- breakdown : float or None limit_breakdown : float or None limit_descriptive : bool """ rows: tuple[MarginalSensitivityRow, ...] estimand: str level: float null: float | None outcome_kind: str quantile_learner: tuple[str, ...] = () pseudo_outcome_learner: tuple[str, ...] = () limit_reading: Resolution | None = field(default=None, compare=False) backend: str | None = None scale: str = "identity" reference_df: int | None = None @property def breakdown(self) -> float | None: """The smallest grid :math:`\\Gamma` whose point bounds contain the null. ``None`` for an arm mean, or when no grid value reaches the null. """ if self.null is None: return None for row in self.rows: if row.lower <= self.null <= row.upper: return row.gamma return None @property def limit_breakdown(self) -> float | None: """The smallest grid :math:`\\Gamma` whose set interval contains the null.""" if self.null is None: return None for row in self.rows: if row.lower_limit <= self.null <= row.upper_limit: return row.gamma return None @property def limit_descriptive(self) -> bool: """Whether the confidence limits print without a licensed interpretation.""" return self.limit_reading is None or not self.limit_reading.licensed
[docs] def to_frame(self) -> Any: """Return one row per :math:`\\Gamma`, in the backend of the fit's data. Returns ------- DataFrame The fields of :class:`MarginalSensitivityRow`. """ names = [item.name for item in fields(MarginalSensitivityRow)] payload = {name: [getattr(row, name) for row in self.rows] for name in names} return emit_frame(payload, backend=self.backend)
[docs] def plot(self, **kwargs: Any) -> Any: """Plot the range of the estimate against Γ, with its interval and no-effect line. Parameters ---------- **kwargs Passed to :func:`cleverly.plotting.sensitivity`: ``outcome_label=``, ``units=`` and ``title=``. Returns ------- Figure One panel over the grid of Γ. Examples -------- >>> import matplotlib >>> matplotlib.use("Agg") >>> import matplotlib.pyplot as plt >>> import numpy as np, pandas as pd >>> from sklearn.linear_model import LogisticRegression >>> from cleverly.estimators import TMLE >>> from cleverly.sensitivity import marginal_sensitivity_bounds >>> rng = np.random.default_rng(0) >>> w = rng.normal(size=300) >>> a = rng.binomial(1, 1 / (1 + np.exp(-w))) >>> y = rng.binomial(1, 1 / (1 + np.exp(-(w + a - 0.5)))) >>> frame = pd.DataFrame({"W": w, "A": a, "Y": y}) >>> fit = TMLE( ... outcome_learner=LogisticRegression(), ... treatment_learner=LogisticRegression(), ... random_state=0, ... ).fit(frame, outcome="Y", treatment="A", covariates=["W"]).single() >>> fig = marginal_sensitivity_bounds(fit, gamma=(1.0, 1.5, 2.0, 3.0)).plot() >>> fig.axes[0].get_ylabel() 'Range of the average effect' >>> plt.close(fig) """ from ..plotting import sensitivity return sensitivity(self, **kwargs)
[docs] def summary(self, *, detail: Detail = "short") -> Summary: """Return the printed summary. Parameters ---------- detail : {"short", "full"} ``"full"`` adds the assumptions and the settings. Returns ------- Summary A ``str`` that prints the question, the numbers and how to read them, and keeps its header, tables and notes as data. """ from ._summaries import marginal_summary return marginal_summary(self, detail)
def __repr__(self) -> str: from ._summaries import marginal_repr return marginal_repr(self) def _repr_html_(self) -> str: return self.summary().as_html()
# --------------------------------------------------------------------------------------- # Refusals def _gap(condition: str, roadmap: str = _OWNER) -> Refusal: return Refusal( RefusalReason.IMPLEMENTATION_GAP, condition, alternative=NO_ALTERNATIVE, roadmap=roadmap ) #: The refusal on a structural nested mean model fit. STRUCTURAL_REFUSAL = Refusal( RefusalReason.DIFFERENT_ESTIMAND, "the marginal sensitivity model bounds an arm mean of a point treatment. A structural " "nested mean model reports a blip parameter, which is a different functional.", alternative=NO_ALTERNATIVE, ) def marginal_bound_refusal(result: Any) -> Refusal | None: """The refusal of the bounds on ``result``, or ``None`` when they apply. Every rule reads the fitted result only, so the facade can declare the refusal without running a learner. Parameters ---------- result : TMLEResult or LongitudinalResult A fitted result. Returns ------- Refusal or None The first failed condition. """ family = getattr(result, "assessment_family", "point") if family == "longitudinal": return _gap( "the marginal sensitivity model bounds are derived for a point treatment. The " "longitudinal analogue of Tan (2025) gives population bounds only, with no sample " "estimator.", "F16", ) if family == "structural": return STRUCTURAL_REFUSAL if family == AUGMENTED_TRIAL_FAMILY: return augmented_trial_refusal("marginal_sensitivity_bounds", "bound") data = result.data config = result.config if getattr(config, "continuous_treatment", False) or getattr(data, "policies", None): return Refusal( RefusalReason.DIFFERENT_ESTIMAND, "the marginal sensitivity model bounds the odds of a discrete treatment. A dose, " "a policy or a regime asks a different question that the model does not state.", alternative=NO_ALTERNATIVE, ) if getattr(config, "parameter_axis", "arm") != "arm": return Refusal( RefusalReason.DIFFERENT_ESTIMAND, "the marginal sensitivity model bounds arm means and their difference. A regime, " "shift, incremental or MSM parameter is a different functional.", alternative=NO_ALTERNATIVE, ) method = getattr(result, "fitted_method", "unknown") if method == "adaptive_iptw": from ..estimators.iptw import OPERATION_REFUSALS return OPERATION_REFUSALS["marginal_bounds"] if method not in _BOUNDED_METHODS: return _gap( f"the bounds read the outcome regression and the treatment mechanism of a TMLE, " f"DR-TMLE or C-TMLE fit, and a {method} fit is none of these.", _PART_H2, ) if method == "collaborative_tmle" and _refits_mechanism(result): refusal = _collaborative_refusal(result) if refusal is not None: return refusal elif _mechanism_unfitted(result): return Refusal( RefusalReason.DIFFERENT_ESTIMAND, "this fit reports the natural-course mean only and fitted no treatment " "mechanism. The model bounds a counterfactual arm mean through the odds of " "treatment, and the observed-treatment mean is not one.", alternative="Fit the arm means ('ey') or a contrast, then call the bounds.", ) if getattr(data, "intermediate", None) is not None: return _gap( "a controlled direct effect also needs a bound on confounding of the intermediate " "and the outcome, which the model does not state." ) if data.has_missing_treatment: return _gap( "a missing treatment needs a joint model of U on the treatment and on its " "observation, which no read source defines." ) if data.family not in ("gaussian", "binomial"): return Refusal( RefusalReason.DIFFERENT_ESTIMAND, f"the bounds read a continuous or a binary outcome; this fit's family is " f"{data.family!r}.", alternative=NO_ALTERNATIVE, ) if jackknife_fit(result): return Refusal( RefusalReason.INFERENCE_GAP, "the bound's interval is built from its influence curve, and this fit reports the " "delete-one-cluster jackknife variance (cluster_variance='jackknife'), which the " "curve does not carry, so at Gamma = 1 the set interval would not be the fit's " "interval.", alternative="Refit with cluster_variance='influence_curve' for the bounds.", roadmap="F28", ) status = result.inference_status status = status() if callable(status) else status if status != "influence_curve" and not ( status in _SELECTION_STATUSES and _refits_mechanism(result) ): record = status_record(status) return Refusal( RefusalReason.INFERENCE_GAP, "the bound's interval is built from its influence curve on a fit whose " "nuisances support one. " + record.reason, alternative=record.alternative or NO_ALTERNATIVE, roadmap=record.reopened_by, ) return None def marginal_estimand_refusal(result: Any, estimand: str | None = None) -> Refusal | None: r"""Why the bounds of ``estimand`` withhold their interval on ``result``, or ``None``. The fit-level reasons are :func:`marginal_bound_refusal`'s; call it first. This one is the bounded estimate's own: its status withholds the interval (:func:`_withheld_status`), and at :math:`\Gamma = 1` the set interval would be a confidence interval for that estimate. ``None`` names the default estimand, which the capability row reads. When the fit leaves the caller a choice of estimand, no estimate is bound yet, so there is nothing to refuse: the row defers and asks for ``estimand=``. """ if estimand is None and len(default_estimand_candidates(result)) != 1: return None try: target = _resolve_target(result, estimand) except CapabilityError as error: # A target the bound is not derived for (a known-score ATT, a multi-arm conditional # effect) refuses in the call; the row declares the same record. if error.refusal is None: raise return error.refusal return _estimand_refusal(result, target) def _estimand_refusal(result: Any, target: _Target) -> Refusal | None: withheld = _withheld_status(result, target) if withheld is None: return None record = status_record(withheld) # A shown parameter status (few_cluster_estimand_plugin) labels the estimate's interval # rather than withholding it; the bound refuses it as the fit-level few_cluster_plugin is. reads = "labels" if record.interval == "shown" else "withholds" return Refusal( RefusalReason.INFERENCE_GAP, f"the bound's interval is built from the influence curve of {target.name}, and that " f"estimate {reads} its interval ({withheld}): {record.reason}", alternative=record.alternative or NO_ALTERNATIVE, roadmap=record.reopened_by, ) def _withheld_status(result: Any, target: _Target) -> str | None: """The status of ``target``'s estimate when the bound refuses it, else ``None``. A reported estimate answers through its own status: one that withholds the interval, or ``"few_cluster_estimand_plugin"``, which shows it below the default cluster floor of an estimate no floor study measured, as the fit-level ``"few_cluster_plugin"`` that :func:`marginal_bound_refusal` refuses does. An unreported one withholds when an arm it reads holds one observed outcome value in its rows, the cause the fit's ``constant_arm_cell_plugin`` status names, because the bound's influence curve leaves out that arm's outcome variance as the estimate's would. """ from ..inference.influence import CONSTANT_ARM_CELL_STATUS, constant_outcome_cells estimate = result.estimates.get(target.name) if estimate is not None: status = estimate.inference if status == FEW_CLUSTER_ESTIMAND_STATUS: return str(status) # A selecting C-TMLE withholds for its selected mechanism, which the bounds refit. if supplies_inference(status) or ( status in _SELECTION_STATUSES and _refits_mechanism(result) ): return None return str(status) data = result.data rows = np.ones(data.n, dtype=bool) if target.stratum is not None: rows = np.asarray(data.strata) == target.stratum constant = constant_outcome_cells( data.outcome, data.treatment, rows, weights=data.weights, observed=data.observed ) arms = {target.arm} if target.versus is None else {target.arm, target.versus} return CONSTANT_ARM_CELL_STATUS if arms & {float(arm) for arm in constant} else None def _mechanism_unfitted(result: Any) -> bool: """Whether a draw of ``result`` kept the never-fitted treatment mechanism.""" from ..estimators._nuisance import UnfittedPropensity return any( isinstance(repeat.nuisance.propensity, UnfittedPropensity) for repeat in getattr(result, "repeats", ()) ) #: The refusal of ``att`` or ``atc`` on a fit with more than two arms. MULTI_ARM_CONDITIONAL_REFUSAL = _gap( "on more than two arms the model bounds the odds of each arm against all the others. " "The effect on the treated also needs E[Y(b) | A = a] for one other arm a, which that " "model does not bound." ) def _collaborative_refusal(result: Any) -> Refusal | None: """Why the mechanism of a selecting C-TMLE fit cannot be refitted, or ``None``.""" from ..estimators.ctmle import is_per_arm_oat if result.estimator is None: return Refusal( RefusalReason.INVALID_DECLARATION, "the bounds refit the treatment mechanism of a selecting CTMLE fit on every " "covariate, which reads the fitted estimator, and this result carries none.", alternative=NO_ALTERNATIVE, ) if is_per_arm_oat(result.estimator): return _gap( "a per-arm outcome-adaptive CTMLE declares a treatment learner that reads one " "column, the outcome prediction at one arm, so no learner for the mechanism on " "every covariate exists to refit.", _PART_H2, ) return None # --------------------------------------------------------------------------------------- # Learners QuantileFactory = Callable[[float], Any] def _default_quantile_candidates(tau: float, seed: int) -> dict[str, Any]: from sklearn.ensemble import HistGradientBoostingRegressor from sklearn.linear_model import QuantileRegressor return { "linear quantile": QuantileRegressor(quantile=tau, alpha=0.0, solver="highs"), "boosted quantile": HistGradientBoostingRegressor( loss="quantile", quantile=tau, max_depth=3, max_iter=200, early_stopping=True, random_state=seed, ), } def _default_mean_candidates(seed: int) -> dict[str, Any]: from sklearn.ensemble import HistGradientBoostingRegressor from sklearn.linear_model import LinearRegression return { "linear": LinearRegression(), "boosted": HistGradientBoostingRegressor( max_depth=3, max_iter=200, early_stopping=True, random_state=seed ), } def _pinball(y: FloatArray, q: FloatArray, tau: float, weights: FloatArray | None = None) -> float: u = y - q return float(np.average(np.maximum(tau * u, (tau - 1.0) * u), weights=weights)) # --------------------------------------------------------------------------------------- # One side of one arm @dataclass(frozen=True) class _Side: """The cross-fitted summand of one arm bound, on the outcome scale. ``summand`` is the per-row estimating function and ``own`` its observed-arm part, ``1{A = a}`` times the arm's augmented outcome. ``value`` and ``curve`` are the unweighted marginal mean and its centred curve. """ value: float curve: FloatArray summand: FloatArray own: FloatArray quantile_learner: str | None = None pseudo_outcome_learner: str | None = None def _side( summand: FloatArray, own: FloatArray, quantile_learner: str | None = None, pseudo_outcome_learner: str | None = None, ) -> _Side: value = float(np.mean(summand)) return _Side(value, summand - value, summand, own, quantile_learner, pseudo_outcome_learner) def _observation( y: FloatArray, observed_weight: FloatArray | None ) -> tuple[FloatArray, FloatArray]: """The outcome with unobserved rows set to zero, and ``Delta / P(Delta = 1 | A, W)``.""" if observed_weight is None: return y, np.ones_like(y) return np.where(observed_weight > 0, y, 0.0), observed_weight def _binary_side( y: FloatArray, treated: FloatArray, e: FloatArray, mu: FloatArray, gamma: float, upper: bool, observed_weight: FloatArray | None = None, ) -> _Side: """The binary-outcome side, with both nuisances closed forms of ``mu``. ``observed_weight`` is ``Delta / P(Delta = 1 | A, W)`` on a fit with missing outcomes. """ y, obs = _observation(y, observed_weight) tau = gamma / (1.0 + gamma) if upper: q = (mu > 1.0 - tau).astype(float) rho = np.minimum(1.0 - 1.0 / gamma + mu / gamma, gamma * mu) residual = y - q r = q + np.where(residual > 0, gamma, 1.0 / gamma) * residual else: q = (mu > tau).astype(float) rho = np.maximum(1.0 - gamma + gamma * mu, mu / gamma) residual = y - q r = q + np.where(residual < 0, gamma, 1.0 / gamma) * residual own = treated * (mu + obs * (y - mu)) summand = own + (1.0 - treated) * rho + treated * obs * (1.0 - e) / e * (r - rho) return _side(summand, own) def _fit(learner: Any, x: FloatArray, y: FloatArray, weights: FloatArray | None) -> Any: from ..learners._fitting import fit_learner return fit_learner(learner, x, y, sample_weight=weights) def _continuous_side( x: FloatArray, y: FloatArray, treated: FloatArray, e: FloatArray, mu: FloatArray, gamma: float, upper: bool, assignment: Any, n_folds: int, quantile_learner: QuantileFactory | None, pseudo_outcome_learner: Any, seed: int, *, weights: FloatArray | None = None, observed_weight: FloatArray | None = None, ) -> _Side: """The continuous-outcome side: the upper bound of ``sign * Y``, read back by sign. ``weights`` are the fit's sampling weights, which every regression and every held-out loss reads. ``observed_weight`` is ``Delta / P(Delta = 1 | A, W)`` on a fit with missing outcomes: the quantile and the tail regression train on observed rows only. """ y, obs = _observation(y, observed_weight) sign = 1.0 if upper else -1.0 y_s, mu_s = sign * y, sign * mu own_s = treated * (mu_s + obs * (y_s - mu_s)) if gamma == 1.0: # phi_1(u) = u, so R = Y and rho = mu: the AIPW mean, with no learner to fit. summand = own_s + (1.0 - treated) * mu_s + treated * obs * (1.0 - e) / e * (y_s - mu_s) return _side(sign * summand, sign * own_s) tau = gamma / (1.0 + gamma) rows = (treated > 0.5) & (obs > 0) n = y.shape[0] w = np.ones(n) if weights is None else np.asarray(weights, dtype=float) def tail(q: FloatArray, target: FloatArray) -> FloatArray: return q + np.maximum(target - q, 0.0) / (1.0 - tau) def train_weights(mask: FloatArray) -> FloatArray | None: return None if weights is None else w[mask] # For fold k the other folds split by parity of their order: the quantile trains on # one half and the tail regression on the other, so no training pseudo-outcome reads a # quantile fitted on its own row. This is the split of the authors' code # (``DVDS.R``, ``semiadaptive = FALSE``). halves = [] for k in range(n_folds): others = assignment[assignment != k] order = np.where(others > k, others - 1, others) first = np.zeros(n, dtype=bool) first[assignment != k] = order % 2 == 0 second = (assignment != k) & ~first halves.append((assignment == k, first, second)) from ..learners._fitting import seed_unset if quantile_learner is None: q_candidates = _default_quantile_candidates(tau, seed) else: q_candidates = {"quantile_learner": seed_unset(quantile_learner(tau), seed)} q_name: str | None = None q_hat = np.empty(n, dtype=float) q_second: list[FloatArray] = [] best_risk = np.inf for name, learner in q_candidates.items(): held_out = np.empty(n, dtype=float) on_second: list[FloatArray] = [] for test, first, second in halves: train = rows & first model = _fit(learner, x[train], y_s[train], train_weights(train)) held_out[test] = model.predict(x[test]) on_second.append(np.asarray(model.predict(x[rows & second]), dtype=float)) risk = _pinball(y_s[rows], held_out[rows], tau, w[rows]) if risk < best_risk: q_name, q_hat, q_second, best_risk = name, held_out, on_second, risk c_candidates = ( _default_mean_candidates(seed) if pseudo_outcome_learner is None else {"pseudo_outcome_learner": seed_unset(pseudo_outcome_learner, seed)} ) c_name: str | None = None c_hat = np.empty(n, dtype=float) best_risk = np.inf for name, learner in c_candidates.items(): predictions = np.empty(n, dtype=float) for (test, _first, second), q_on_second in zip(halves, q_second, strict=True): train = rows & second target = tail(q_on_second, y_s[train]) model = _fit(learner, x[train], target, train_weights(train)) predictions[test] = model.predict(x[test]) # The held-out loss reads each row's pseudo-outcome from the quantile fitted # without its fold, as the evaluation does. loss = (tail(q_hat[rows], y_s[rows]) - predictions[rows]) ** 2 risk = float(np.average(loss, weights=w[rows])) if risk < best_risk: c_name, c_hat, best_risk = name, predictions, risk rho = mu_s / gamma + (1.0 - 1.0 / gamma) * c_hat r = y_s / gamma + (1.0 - 1.0 / gamma) * tail(q_hat, y_s) summand = own_s + (1.0 - treated) * rho + treated * obs * (1.0 - e) / e * (r - rho) return _side(sign * summand, sign * own_s, q_name, c_name) # --------------------------------------------------------------------------------------- # The bounded parameter #: The estimand stems, and the scale each reports on. _LINEAR_STEMS = ("ey", "ate", "att", "atc") _RATIO_STEMS = ("rr", "or") @dataclass(frozen=True) class _Target: """One bounded parameter: its stem, its arms and the stratum it conditions on. ``arm`` is the arm a level reports and the non-reference arm of a contrast; ``versus`` is the reference arm, or ``None`` for a level. ``stratum`` is a code into ``data.strata_levels``, or ``None`` for a marginal parameter. """ name: str stem: str arm: float versus: float | None = None stratum: int | None = None @property def ratio(self) -> bool: return self.stem in _RATIO_STEMS @property def null(self) -> float | None: if self.versus is None: return None return 1.0 if self.ratio else 0.0 def _named_targets(result: TMLEResult) -> dict[str, _Target]: """Every marginal parameter the bounds can take, by its reported name.""" from ..targets.base import arm_alias, parameter_name data = result.data reference = float(result.config.reference_arm) label = data.arm_label binary = bool(data.is_binary_treatment) out: dict[str, _Target] = {} for arm in data.arm_codes: name = parameter_name("ey", arm=label(arm)) out[name] = _Target(name, "ey", float(arm)) if binary: out["ey1"] = _Target("ey1", "ey", 1.0) out["ey0"] = _Target("ey0", "ey", 0.0) for arm in data.arm_codes: if float(arm) == reference: continue for stem in ("ate", "att", "atc", *_RATIO_STEMS): name = arm_alias(stem, arm=label(arm), versus=label(reference), collapse=binary) out[name] = _Target(name, stem, float(arm), reference) # A fit with structured parameter keys may report aliases of its own. from ._parameters import arm_parameters for name, parameter in arm_parameters(result).items(): stem = "ey" if parameter.stem in ("ey1", "ey0") else parameter.stem out.setdefault(name, _Target(name, stem, parameter.arm, parameter.versus)) if data.strata is not None: # The conditional report suffixes the marginal alias with the stratum's label. from ..targets.base import stratum_alias for code in range(data.n_strata): label_s = data.stratum_label(code) for marginal in list(out.values()): if marginal.stratum is None: name = stratum_alias(marginal.name, label_s) out[name] = replace(marginal, name=name, stratum=code) return out def default_estimand_candidates(result: TMLEResult) -> tuple[str, ...]: """The parameters a call without ``estimand`` chooses between. ``("ate",)`` when the fit reports the ATE; else the reported parameters the bounds can take; else, when the fit reports none of them (a PAR or natural-course fit), every parameter the bounds can take. One candidate settles the default; two or more leave a choice the caller makes, which the sensitivity facade reports as a deferral. Parameters ---------- result : TMLEResult The point-treatment fit to bound. Returns ------- tuple of str The candidate names, in the order the fit reports them. """ named = _named_targets(result) if "ate" in result.estimates and "ate" in named: return ("ate",) reported = tuple(name for name in result.estimates if name in named) return reported if reported else tuple(named) def _default_estimand(result: TMLEResult) -> str: """The parameter a call without ``estimand`` bounds: the fit's own reported one.""" candidates = default_estimand_candidates(result) if len(candidates) == 1: return candidates[0] raise ValueError( "marginal_sensitivity_bounds answers for one estimand, and this fit leaves a " f"choice: pass estimand= with one of {list(candidates)}. A call without it bounds " "only the one parameter the fit reported, never a parameter it did not report." ) def _reference_df(result: TMLEResult, target: _Target) -> int | None: """The t reference of the bounds' limits: the fit's own rule over the target's rows. A clustered fit studentizes each estimate on ``J - 2`` degrees of freedom below the few-cluster threshold (:func:`~cleverly.inference.cluster.cluster_reference_df`), over the rows the estimate reads. The bound endpoints are means of influence curves over the same rows, so their limits take the same reference; ``None`` is the normal one. """ data = result.data if data.cluster is None: return None rows = None if target.stratum is not None: rows = np.asarray(data.strata) == target.stratum weights = data.weights if getattr(data, "is_weighted", False) else None return cluster_reference_df(data.cluster, weights, rows=rows) def _resolve_target(result: TMLEResult, estimand: str | None) -> _Target: """The parameter an ``estimand`` name asks for, with ``None`` read as the default.""" from ._parameters import stratum_parameter data = result.data named = _named_targets(result) if estimand is None: estimand = _default_estimand(result) if estimand in named: target = named[estimand] else: conditional = stratum_parameter(result, estimand) if conditional is None: raise ValueError( f"estimand must name an arm mean, a contrast, att, atc, rr or or of this fit " f"({', '.join(sorted(named))}), or a reported stratum-conditional alias; " f"got {estimand!r}" ) stem = "ey" if conditional.stem in ("ey1", "ey0") else conditional.stem target = _Target(estimand, stem, conditional.arm, conditional.versus, conditional.stratum) if target.stem in ("att", "atc") and known_score_fit(result): raise CapabilityError.from_refusal( known_score_reader_refusal("the marginal sensitivity bound") ) if target.stem in ("att", "atc") and not data.is_binary_treatment: raise CapabilityError.from_refusal(MULTI_ARM_CONDITIONAL_REFUSAL) if target.ratio: observed = np.asarray(data.outcome, dtype=float)[np.asarray(data.observed, dtype=bool)] if data.family != "binomial" and target.stem == "or": raise CapabilityError( "the odds ratio bounds read a binary outcome; this fit's family is " f"{data.family!r}.", reason=RefusalReason.INVALID_DECLARATION, alternative="Ask for the risk ratio ('rr') of a nonnegative outcome.", ) if np.any(observed < 0): raise CapabilityError( "the risk ratio bounds take the ratio of two arm bounds, which needs a " "nonnegative outcome; this outcome takes negative values.", reason=RefusalReason.INVALID_DECLARATION, alternative="Bound the difference ('ate') instead.", ) return target def _gammas(gamma: float | Sequence[float]) -> tuple[float, ...]: values = (gamma,) if isinstance(gamma, (int, float)) else tuple(gamma) if not values: raise ValueError("gamma needs at least one value") out = tuple(sorted({float(value) for value in values})) if any(not np.isfinite(value) or value < 1.0 for value in out): raise ValueError(f"every gamma must be a finite number of at least 1; got {values!r}") return out def _refits_mechanism(result: TMLEResult) -> bool: """Whether the bounds refit the treatment mechanism instead of reading the fit's. A selecting C-TMLE conditions its mechanism on a selected function of the covariates, which does not estimate ``P(A | W)``. A derivative C-TMLE (C-TMLE0) selects nothing, so its mechanism is the ordinary one. """ if getattr(result, "fitted_method", None) != "collaborative_tmle": return False from ..estimators.ctmle import CTMLEDerivativeFit return not isinstance(result.extra.get("ctmle"), CTMLEDerivativeFit) def _mechanism(result: TMLEResult, repeat: Any) -> Any: """The treatment mechanism of one draw: the fit's, or one refit on every covariate.""" if not _refits_mechanism(result): return repeat.nuisance.propensity from .omitted_variable import _full_mechanism return _full_mechanism(result, repeat) def _draw_sides( result: TMLEResult, repeat: Any, gamma: float, wanted: Sequence[tuple[float, bool]], quantile_learner: QuantileFactory | None, pseudo_outcome_learner: Any, ) -> dict[tuple[float, bool], _Side]: """Every ``(arm, upper)`` side in ``wanted``, on one cross-fitting draw.""" from ..estimators._nuisance import tilt_missingness data = result.data nuisance = repeat.nuisance folds = nuisance.folds seed = int(repeat.seed) if repeat.seed is not None else 0 if folds.n_folds < 3: # The quantile and the tail regression each train on one half of the folds outside # the predicted fold, which needs at least three folds. from ..learners.crossfit import make_folds folds = make_folds( data.n, _IN_SAMPLE_FOLDS, stratify=np.asarray(data.treatment, dtype=float), cluster=data.cluster, random_state=seed, ) assignment, n_folds = np.asarray(folds.assignment), int(folds.n_folds) propensity = _mechanism(result, repeat) level_index = {float(level): i for i, level in enumerate(propensity.arms)} g = propensity.bounded(result.config.g_bounds) y = np.asarray(data.outcome, dtype=float) x = np.asarray(data.covariates, dtype=float) weights = np.asarray(data.weights, dtype=float) if data.weights_name is not None else None response = None if data.has_missing_outcome: response = tilt_missingness(nuisance, result.config.missingness_bound) observed = np.asarray(data.observed, dtype=bool) binary = data.family == "binomial" treatment = np.asarray(data.treatment, dtype=float) payloads: list[tuple[Callable[..., _Side], tuple[Any, ...], dict[str, Any]]] = [] for level, upper in wanted: treated = (treatment == level).astype(float) e = g[:, level_index[level]] mu = nuisance.scaler.unscale_levels(np.asarray(nuisance.outcome.arms[level], dtype=float)) observed_weight = None if response is not None: column = list(nuisance.arms).index(level) observed_weight = np.where(observed, 1.0 / response[:, column], 0.0) if binary: payloads.append((_binary_side, (y, treated, e, mu, gamma, upper, observed_weight), {})) else: payloads.append( ( _continuous_side, ( x, y, treated, e, mu, gamma, upper, assignment, n_folds, quantile_learner, pseudo_outcome_learner, seed, ), {"weights": weights, "observed_weight": observed_weight}, ) ) # The continuous sides fit their quantile and tail learners independently of one another, # each from the draw's seed, so on a fit made with ``n_jobs > 1`` they run in workers and # come back in order: every side is the one the serial loop computes. A side that runs in # another process has its warnings captured there and raised again here in task order # (:func:`_replay` states where that matches the serial loop). ``n_jobs=1``, the binary # sides and Gamma = 1, which are closed forms, take the serial loop itself. n_jobs = 1 if binary or gamma == 1.0 else resolve_n_jobs(getattr(result.estimator, "n_jobs", 1)) if n_jobs == 1 or len(payloads) <= 1: return { key: function(*arguments, **keywords) for key, (function, arguments, keywords) in zip(wanted, payloads, strict=True) } caller = os.getpid() outcomes = map_parallel( _captured_side, [(caller, function, arguments, keywords) for function, arguments, keywords in payloads], n_jobs=n_jobs, ) sides: dict[tuple[float, bool], _Side] = {} for key, (side, shown) in zip(wanted, outcomes, strict=True): _replay(shown) sides[key] = side return sides #: A warning a worker captured: message, category, file name and line number. _Shown = tuple[Any, type[Warning], str, int] def _captured_side( caller: int, function: Callable[..., _Side], arguments: tuple[Any, ...], keywords: dict[str, Any], ) -> tuple[_Side, list[_Shown]]: """One side, with the warnings it raised in another process, in order, for :func:`_replay`. ``warnings.catch_warnings`` swaps process-wide state, so it is safe only where nothing else runs at the same time: a worker process. A side running in the caller's process (a thread backend, or a call nested in a worker, where joblib uses threads) raises its warnings in place, as the serial loop does, and returns none to replay. """ if os.getpid() == caller: return function(*arguments, **keywords), [] with warnings.catch_warnings(record=True) as caught: warnings.simplefilter("always") side = function(*arguments, **keywords) return side, [_portable(w.message, w.category, w.filename, w.lineno) for w in caught] def _portable(message: Any, category: type[Warning], filename: str, lineno: int) -> _Shown: """A captured warning that survives the trip back to the caller. A warning instance that does not pickle travels as its text, which the caller raises as ``category(text)``. """ try: pickle.dumps((message, category)) except Exception: try: pickle.dumps(category) except Exception: category = Warning return str(message), category, filename, lineno return message, category, filename, lineno def _replay(shown: Sequence[_Shown]) -> None: """Raise a worker's warnings again in the caller, at their original source lines. Each one passes through the caller's filters, under the module name the serial loop would report, so a filter on ``module=`` matches as it would there. No once-per-location registry is passed: the serial loop resets that registry between fits (every learner fit changes the warning filters), so under the ``"default"`` and ``"module"`` actions it shows every repeat, and so does this replay. Under ``"once"`` the serial loop shows a repeat after each reset and the replay shows it once; under ``"always"``, ``"error"`` and ``"ignore"`` they agree. """ for message, category, filename, lineno in shown: module = _module_of(filename) if module is None: # An explicit ``module=None`` drops the warning on CPython 3.13; leaving it out # names the module from the file name. warnings.warn_explicit(message, category, filename, lineno, registry=None) else: warnings.warn_explicit( message, category, filename, lineno, module=module, registry=None ) def _module_of(filename: str) -> str | None: """The name of the loaded module at ``filename``, as ``warnings.warn`` reports it. ``None`` when no loaded module has that file, and :func:`_replay` leaves :func:`warnings.warn_explicit` to name it from the file name. """ for name, module in list(sys.modules.items()): if getattr(module, "__file__", None) == filename: return name return None def _wanted(target: _Target) -> list[tuple[float, bool]]: """The sides the target reads: both sides of its arm and, for a contrast, of ``versus``.""" wanted = [(target.arm, True), (target.arm, False)] if target.versus is not None: wanted += [(target.versus, True), (target.versus, False)] return wanted @dataclass(frozen=True) class _End: """One end of the bound on one draw: its value and influence curve, on the bound's scale.""" value: float curve: FloatArray def _mean(numerator: FloatArray, denominator: FloatArray, mass: FloatArray) -> _End: """``E[mass numerator] / E[mass denominator]`` and its influence curve. ``mass`` is the sampling weight times the stratum indicator. The denominator is one for a marginal mean and the indicator of the conditioning arm for ``att`` and ``atc``. """ share = float(np.mean(mass * denominator)) value = float(np.mean(mass * numerator)) / share return _End(value, mass * (numerator - value * denominator) / share) def _log_end(end: _End, odds: bool) -> _End: """The end on the log scale (of the mean, or of its odds).""" value = end.value if not value > 0.0 or (odds and not value < 1.0): raise CapabilityError( f"an arm bound of {value:.4g} has no log{' odds' if odds else ''} at this gamma, so " "the ratio of the arm bounds is undefined there.", reason=RefusalReason.INVALID_DECLARATION, alternative="Ask for a smaller gamma, or bound the difference ('ate').", ) scale = value * (1.0 - value) if odds else value transformed = np.log(value / (1.0 - value)) if odds else np.log(value) return _End(float(transformed), end.curve / scale) def _target_ends( result: TMLEResult, target: _Target, sides: dict[tuple[float, bool], _Side] ) -> tuple[_End, _End]: """``(lower, upper)`` of the target on one draw, a ratio on the log scale.""" data = result.data n = data.n mass = np.asarray(data.weights, dtype=float).copy() if target.stratum is not None: mass = mass * (np.asarray(data.strata) == target.stratum) ones = np.ones(n) arm_up, arm_low = sides[(target.arm, True)], sides[(target.arm, False)] if target.versus is None: return _mean(arm_low.summand, ones, mass), _mean(arm_up.summand, ones, mass) ref_up, ref_low = sides[(target.versus, True)], sides[(target.versus, False)] if target.stem == "ate": return ( _mean(arm_low.summand - ref_up.summand, ones, mass), _mean(arm_up.summand - ref_low.summand, ones, mass), ) treatment = np.asarray(data.treatment, dtype=float) if target.stem == "att": # E[Y(a) | A = a] is identified; E[Y(ref) | A = a] reads the reference arm's bound # without its observed-arm part. given = (treatment == target.arm).astype(float) own = arm_up.own return ( _mean(own - (ref_up.summand - ref_up.own), given, mass), _mean(own - (ref_low.summand - ref_low.own), given, mass), ) if target.stem == "atc": given = (treatment == target.versus).astype(float) own = ref_up.own return ( _mean((arm_low.summand - arm_low.own) - own, given, mass), _mean((arm_up.summand - arm_up.own) - own, given, mass), ) # A ratio of two arm bounds is monotone in each: the upper end divides the arm's upper # bound by the reference's lower bound. odds = target.stem == "or" arms = {key: _log_end(_mean(side.summand, ones, mass), odds) for key, side in sides.items()} lower = _End( arms[(target.arm, False)].value - arms[(target.versus, True)].value, arms[(target.arm, False)].curve - arms[(target.versus, True)].curve, ) upper = _End( arms[(target.arm, True)].value - arms[(target.versus, False)].value, arms[(target.arm, True)].curve - arms[(target.versus, False)].curve, ) return lower, upper def _shipped_limit( estimate: ParameterEstimate | None, end: float, std_error: float, curve: FloatArray | None, level: float, side: float, *, ratio: bool, df: int | None = None, clustered: bool = False, ) -> float: """One limit of the set interval, under the interval rule the bounded estimate ships. ``end`` is the bound on the reported scale (a ratio's on the ratio scale) and ``std_error`` its standard error on the inference scale. The limit is the side ``side`` (``-1`` lower, ``1`` upper) of the two-sided interval at ``level`` that :func:`~cleverly.inference.interval_reference` forms about the bound: the estimate's t reference over the target's rows, the range-logit scale of a bounded mean, the skew-widened t of a continuous outcome (reading ``curve`` on one draw), and the MOVER multiples of a bounded difference (:func:`~cleverly.sensitivity._limits.bound_limits`). A target the fit reports no estimate of takes the normal reference on the bound's scale, the log scale for a ``ratio``. On a ``clustered`` fit, ``df`` is the few-cluster t reference over the target's rows (:func:`_reference_df`, ``None`` for the normal one): a clustered estimate keeps its own symmetric reference under every shipped rule, so the limit is the normal or t limit on the bound's scale, on the bound's own degrees of freedom, which can differ from the estimate's (a fold-evaluated CV-TMLE pools the cluster variance on ``J - 2``). """ if estimate is None or clustered: # No rule is shipped for an unreported target, and a clustered one keeps its # symmetric reference: the normal or t quantile on the bound's scale. z = reference_quantile(1.0 - level, df) if ratio: return float(np.exp(np.log(end) + side * z * std_error)) return float(end + side * z * std_error) read = bound_limits(estimate, level=level, psi=end, std_error=std_error, influence_curve=curve) return float(read.lower if side < 0 else read.upper) def _median_rule(values: FloatArray, variances: FloatArray) -> tuple[float, float]: """The repeat median and its variance (Chernozhukov et al. 2018, eq. 3.14).""" centre = float(np.median(values)) return centre, float(np.median(variances + (values - centre) ** 2)) def _limit_reading( result: TMLEResult, outcome_kind: str, target: _Target, custom_learners: bool ) -> Resolution: from ..validation._interpretation import fit_configuration, resolve # Study SH covers the default quantile and tail learners and the marginal ATE of an # unweighted, fully observed two-arm fit with one draw. Each other feature names a rule # of its own, which no row covers. data = result.data features = [outcome_kind] if outcome_kind == "continuous" and custom_learners: features.append("custom_learners") if not data.is_binary_treatment: features.append("multi_arm") if target.stem in ("att", "atc"): features.append("conditional") if target.ratio: features.append("ratio") if target.stratum is not None: features.append("stratum") if data.weights_name is not None: features.append("weighted") if data.has_missing_outcome: features.append("missing_outcome") if _refits_mechanism(result): features.append("refit_mechanism") if len(getattr(result, "repeats", ())) > 1: # Study SH measured one cross-fitting draw; the median rule over draws is unmeasured. features.append("repeats") if data.cluster is not None: features.append("clustered") rule = "+".join(features) try: fit = fit_configuration(result, target.name) except (AttributeError, KeyError, TypeError, ValueError): fit = None return resolve(MSM_BOUND_INSTRUMENT, rule, fit)
[docs] def marginal_sensitivity_bounds( result: TMLEResult, gamma: float | Sequence[float] = DEFAULT_GAMMA_GRID, *, estimand: str | None = None, level: float = 0.95, quantile_learner: QuantileFactory | None = None, pseudo_outcome_learner: Any = None, ) -> MarginalSensitivityBounds: """Bound an arm mean or the ATE under the marginal sensitivity model. Parameters ---------- result : TMLEResult A fitted ``TMLE``, ``DRTMLE`` or ``CTMLE`` result with a Gaussian or binary outcome. It may have more than two arms, sampling weights, baseline strata or missing outcomes. gamma : float or sequence of float, default=DEFAULT_GAMMA_GRID The sensitivity parameter :math:`\\Gamma \\ge 1` (Tan's :math:`\\Lambda`), or a grid. :math:`\\Gamma = 1` reproduces the cross-fitted AIPW estimate. On a selecting ``CTMLE`` fit that estimate reads the refitted mechanism, so it is not the C-TMLE estimate. estimand : str or None, default=None The reported name of an arm mean (``"ey1"``), a difference (``"ate"``), an effect on the treated or the controls (``"att"``, ``"atc"``, two arms only), a ratio (``"rr"``, ``"or"``), any of these against a named reference arm, or a stratum-conditional alias such as ``"ate[V=1]"``. The fit need not have reported it. ``None`` bounds the fit's own reported parameter: the ATE when the fit reports it, else the one reported parameter the bounds can take. A fit that reports two or more of them and no ATE (or none of them) raises ``ValueError`` naming the choices. level : float, default=0.95 The coverage level of the set interval. quantile_learner : callable, optional A factory ``tau -> regressor`` for the conditional ``tau``-quantile of a continuous outcome. By default the bounds select, by out-of-fold pinball loss, between a linear quantile regression and a shallow boosted quantile regression. pseudo_outcome_learner : regressor, optional The regression of the tail pseudo-outcome. By default the bounds select, by out-of-fold squared loss, between a linear regression and a shallow booster. A declared learner, and the regressor ``quantile_learner`` returns, take the cross-fitting draw's seed (0 when the draw has none, as the default candidates take) for every ``random_state`` they leave unset. Returns ------- MarginalSensitivityBounds One row per :math:`\\Gamma`. Raises ------ CapabilityError When :func:`marginal_bound_refusal` refuses the fit, when ``att`` or ``atc`` is asked of a fit with more than two arms, or when a ratio's outcome scale does not fit it or an arm bound has no logarithm. ValueError When ``gamma`` holds a value below 1, or ``estimand`` or ``level`` is invalid. See Also -------- cleverly.sensitivity.omitted_variable_bounds : The partial-:math:`R^2` scale bound. cleverly.sensitivity.evalue : The risk-ratio scale summary. Examples -------- >>> import numpy as np, pandas as pd >>> from sklearn.linear_model import LinearRegression, LogisticRegression >>> from cleverly.estimators import TMLE >>> from cleverly.sensitivity import marginal_sensitivity_bounds >>> rng = np.random.default_rng(0) >>> w = rng.normal(size=300) >>> a = rng.binomial(1, 1 / (1 + np.exp(-w))) >>> y = rng.binomial(1, 1 / (1 + np.exp(-(w + a - 0.5)))) >>> frame = pd.DataFrame({"W": w, "A": a, "Y": y}) >>> fit = TMLE( ... outcome_learner=LogisticRegression(), ... treatment_learner=LogisticRegression(), ... random_state=0, ... ).fit(frame, outcome="Y", treatment="A", covariates=["W"]).single() >>> bounds = marginal_sensitivity_bounds(fit, gamma=(1.0, 2.0)) >>> row_1, row_2 = bounds.rows >>> bool(np.isclose(row_1.lower, row_1.upper)) True >>> bool(row_2.lower < row_1.lower and row_2.upper > row_1.upper) True """ refuse_augmented_trial(result, "marginal_sensitivity_bounds", "bound") refuse_continuous_survival(result, "marginal_sensitivity_bounds") refusal = marginal_bound_refusal(result) if refusal is not None: raise CapabilityError.from_refusal(refusal) if not 0.0 < level < 1.0: raise ValueError(f"level must lie in (0, 1); got {level!r}") grid = _gammas(gamma) target = _resolve_target(result, estimand) withheld = _estimand_refusal(result, target) if withheld is not None: raise CapabilityError.from_refusal(withheld) cluster = result.data.cluster reported = result.estimates.get(target.name) df = _reference_df(result, target) outcome_kind = "binary" if result.data.family == "binomial" else "continuous" rows: list[MarginalSensitivityRow] = [] quantile_names: set[str] = set() pseudo_names: set[str] = set() for value in grid: lowers, uppers, lower_vars, upper_vars = [], [], [], [] lower_curves, upper_curves = [], [] for repeat in result.repeats: sides = _draw_sides( result, repeat, value, _wanted(target), quantile_learner, pseudo_outcome_learner ) for side in sides.values(): if side.quantile_learner is not None: quantile_names.add(side.quantile_learner) if side.pseudo_outcome_learner is not None: pseudo_names.add(side.pseudo_outcome_learner) try: low, up = _target_ends(result, target, sides) except CapabilityError as error: if not target.ratio: raise defined = rows[-1].gamma if rows else None reach = ( f"The ratio is defined up to gamma={defined:g}; pass a grid that stops there." if defined is not None else "No gamma of the grid gives a defined ratio." ) raise CapabilityError( f"at gamma={value:g}, {error.refusal.condition} {reach}", reason=RefusalReason.INVALID_DECLARATION, alternative=error.refusal.alternative, ) from error lowers.append(low.value) uppers.append(up.value) lower_vars.append(influence_variance(low.curve, cluster)) upper_vars.append(influence_variance(up.curve, cluster)) lower_curves.append(low.curve) upper_curves.append(up.curve) lower, lower_var = _median_rule(np.asarray(lowers), np.asarray(lower_vars)) upper, upper_var = _median_rule(np.asarray(uppers), np.asarray(upper_vars)) lower_se, upper_se = float(np.sqrt(lower_var)), float(np.sqrt(upper_var)) if target.ratio: lower, upper = float(np.exp(lower)), float(np.exp(upper)) single = len(lower_curves) == 1 lower_limit = _shipped_limit( reported, lower, lower_se, lower_curves[0] if single else None, level, -1.0, ratio=target.ratio, df=df, clustered=cluster is not None, ) upper_limit = _shipped_limit( reported, upper, upper_se, upper_curves[0] if single else None, level, 1.0, ratio=target.ratio, df=df, clustered=cluster is not None, ) rows.append( MarginalSensitivityRow( gamma=value, estimand=target.name, lower=lower, upper=upper, lower_std_error=lower_se, upper_std_error=upper_se, lower_limit=lower_limit, upper_limit=upper_limit, ) ) return MarginalSensitivityBounds( rows=tuple(rows), estimand=target.name, level=level, null=target.null, outcome_kind=outcome_kind, quantile_learner=tuple(sorted(quantile_names)), pseudo_outcome_learner=tuple(sorted(pseudo_names)), limit_reading=_limit_reading( result, outcome_kind, target, custom_learners=quantile_learner is not None or pseudo_outcome_learner is not None, ), backend=result.data.backend, scale="log" if target.ratio else "identity", reference_df=df, )