cleverly.survival.ContinuousSurvivalTMLE#

class cleverly.survival.ContinuousSurvivalTMLE(horizons: Sequence[float], *, restricted_horizons: Sequence[float] = (), arms: Mapping[str, Any] | None = None, reference: str | None = None, causes: Mapping[int, str] | None = None, cross_fit: bool = True, n_folds: int = 10, learner_folds: int = 5, hazard_library: Sequence[Any] | None = None, censoring_library: Sequence[Any] | None = None, treatment_learner: Any = None, norm: Literal['covariance', 'variance', 'euclidean'] = 'covariance', g_bounds: Literal['auto'] | float = 'auto', max_iter: int = 500, step_size: float = 0.1, tol: float | None = None, alpha: float = 0.05, random_state: int | None = None, tilt: Literal['all', 'factual'] = 'all', simultaneous: bool = False, n_multiplier: int = 10000, multiplier_kind: str = 'mammen', band_scale: str = 'logit', variance: Literal['switched', 'influence_curve'] = 'switched', max_memory_bytes: int | None = None, n_jobs: int | None = 1, n_bootstrap: int = 0, bootstrap_interval: Literal['percentile', 'bca'] = 'percentile')[source]#

One-step TMLE of cause-specific absolute risks in continuous time.

Parameters:
horizonssequence of float

The target times \(t_1 < \dots < t_K\).

restricted_horizonssequence of float

The restriction times \(\tau_1 < \dots < \tau_R\) of the restricted means. Each \(\tau\) adds the restricted mean time lost to each cause, \(E \int_0^\tau F_j(u \mid a, L)\, du\), of both arms to the targets the walk solves, so ContinuousSurvivalResult.rmtl() and rmst() can read them. The extra targets change the walk, so the risks of such a fit differ slightly from those of a fit without them.

armsmapping of str to level, or None

The label of each treatment level, two levels. None labels the two observed levels by str(level).

referencestr or None

The label of the comparison arm of the contrasts. None takes the first label.

causesmapping of int to str, or None

The label of each nonzero event code. None reads one code 1 as one event, and several codes as competing causes labelled str(code).

cross_fitbool

Fit every nuisance out of fold on n_folds folds, selecting each model inside the training folds. False fits each on every row, the sources’ setting.

n_foldsint

Cross-fitting folds.

learner_foldsint

Folds of the cross-validation that selects each nuisance model.

hazard_librarysequence or None

Hazard learners for every cause. None takes three unstratified Cox models.

censoring_librarysequence or None

Hazard learners for the censoring hazard. None takes the same three.

treatment_learnerclassifier or None

The treatment model. None selects between a main-terms logistic model and an L1 logistic model by cross-validated log loss. A user learner needs cross_fit=True.

norm{“covariance”, “variance”, “euclidean”}

The inner product of the universal submodel, with the covariance held at the initial fit.

g_bounds“auto” or float

The lower bound on \(\pi(a \mid L)\, S_c(u^- \mid a, L)\). "auto" is \(5 / (\sqrt n \log n)\).

max_iterint

The cap on accepted targeting steps.

step_sizefloat

The first step size.

tolfloat or None

Stop when the largest \(|P_n D^*| / \hat\sigma\) is at or below tol. None uses \(1 / (\sqrt n \log n)\) (Rytgaard and van der Laan 2024, Sec 4.3).

alphafloat

Significance level of the intervals.

random_stateint or None

Seed of the folds, the inner selection folds, the lasso members, any unseeded user learner, the off-grid diagnostic and the band. None draws one seed per fit, which the result’s stored estimator records.

tilt{“all”, “factual”}

"factual" reproduces the concrete 1.0.5 update, for the mutation control only.

simultaneousbool

Add a sup-t band over every risk and risk difference.

n_multiplierint

Multiplier draws of the band.

multiplier_kindstr

The multiplier distribution, as simultaneous_bands() takes.

band_scale{“logit”, “identity”}

The scale of the band of a bounded estimate.

variance{“switched”, “influence_curve”}

The variance every estimate reports. "switched" reads the influence-curve variance, raised to the predictable variation of the influence curve’s martingale part where the curve is sparse: where the relative standard deviation of the influence-curve variance exceeds SPARSE_INFLUENCE_THRESHOLD (0.125), the variance is the larger of the two (cleverly.inference.switched). It is never below the influence-curve variance and equals it with probability tending to one. "influence_curve" reads the influence-curve variance alone. A band takes its critical value from the influence curves and its half-widths from the reported standard errors.

max_memory_bytesint or None

The memory budget of a fit, in bytes. The walk works through blocks of units; before fitting any nuisance, the fit predicts the largest number of bytes it holds at once with no block kept between passes, which grows with n times the number of targets, and refuses with MethodConfigurationError when the prediction exceeds the budget. The walk then keeps as many blocks as the budget allows and rebuilds the others on each pass, so the budget sets how long a fit runs and never what it returns. None reads the memory available at that moment, keeps blocks up to three quarters of it, and checks nothing (keeping every block) on a platform that does not report it. A value overrides the reading either way. Every refit of a result, and so every refutation and truncation curve, uses the value of the fit.

n_jobsint or None

The threads the targeting walk runs its blocks of units on. -1 takes the CPUs the process may use (its affinity, capped by a control group quota) and None one. The result is bit for bit the same at any value; each thread holds one block’s working arrays, which the memory budget counts. With n_bootstrap, the bootstrap refits run on this many worker processes, each walking on one thread.

n_bootstrapint

Full-refit bootstrap replicates, 0 for none and otherwise at least 2. Each replicate resamples units with replacement and reruns the whole fit: the folds, every nuisance selection and the walk. Copies of a unit share every fold, the cross-fitting folds and the selections’ inner folds, so no copy is validated on its own twin. Each estimate then carries a BootstrapSummary beside its Wald interval, and with simultaneous=True the result adds a studentized bootstrap band over every risk and risk difference. When more than 1% of the refits fail, every bootstrap interval is withheld with a ConvergenceWarning, and the Wald intervals stand. No bootstrap interval of this engine reads supported.

bootstrap_interval{“percentile”, “bca”}

The bootstrap interval each estimate carries: the percentile interval of the replicate estimates, or the bias-corrected and accelerated interval, with the acceleration read from the estimate’s own influence curve.

Methods#

ContinuousSurvivalTMLE.fit(data: Any, *, time: str, event: str, treatment: str, baseline: Sequence[str]) → ContinuousSurvivalResult[source]#

Fit the nuisances and target every risk.

Parameters:
dataDataFrame

A pandas or polars frame with one row per unit.

timestr

The follow-up time column: positive finite numbers.

eventstr

The event-code column: 0 censored, a positive integer per cause.

treatmentstr

The binary baseline treatment column.

baselinesequence of str

The baseline covariates.

Returns:
ContinuousSurvivalResult

The targeted risks, their contrasts and the record.

ContinuousSurvivalTMLE.fit_data(data: Any) → ContinuousSurvivalResult[source]#

Fit a ContinuousSurvivalData container.

Parameters:
dataContinuousSurvivalData

The validated container.

Returns:
ContinuousSurvivalResult

The targeted risks, their contrasts and the record.

ContinuousSurvivalTMLE.refit(data: Any, *, random_state: int | None, _frozen_folds: Any = None, _frozen_plan: FoldPlan | None = None) → ContinuousSurvivalResult[source]#

Fit a copy of this estimator, at the seed random_state, to data.

result.estimator.refit(result.data, random_state=result.estimator.random_state) reproduces the fit bit for bit in one process: the stored estimator carries the fit’s resolved seed, and every learner, the folds, the off-grid draws and the band read that seed.

Parameters:
dataContinuousSurvivalData

The rows to fit.

random_stateint or None

The seed of the copy. None draws one, which the new result’s estimator records.

_frozen_foldsarray_like or None

Private: one outer cross-fitting label per row of data, used instead of a fresh draw. A subset refutation passes each drawn unit’s original label, so the refits keep the fit’s split. When omitted, a fit that was itself produced with frozen labels reuses them on data of the same size.

_frozen_planFoldPlan or None

Private: the fold plan of a fit of data of the same size, used instead of every fold draw. The outer labels, each outer fold’s inner selection labels and the generator state after the nuisance phase are the plan’s, so a refit of the same rows at the same seed reproduces that fit bit for bit, and a refit of changed event codes keeps every fold. A simulated-confounding replay passes it. When omitted, a fit that was itself produced under a plan reuses it on data of the same size.

Returns:
ContinuousSurvivalResult

The fit of the copy.

Raises:
ValueError

If both _frozen_folds and _frozen_plan are passed, or the plan does not fit the data.