cleverly.validation.CoverageStudy#
- class cleverly.validation.CoverageStudy(dgp: DGP | Callable[..., tuple[Any, dict[str, float]]], estimator: Callable[[], TMLE | LTMLE], *, n: int = 1000, n_replicates: int = 100, estimands: Sequence[str] | None = None, fit_kwargs: dict[str, Any] | None = None, seed: int | None = None, sample_seeds: Sequence[int] | None = None, n_jobs: int = 1, truth_key: str = 'population', intermediate_value: float | None = None, label: str | None = None, checkpoint: ReplicationCheckpoint | None = None)[source]#
Repeatedly fit an estimator on fresh data and measure bias and coverage.
- Parameters:
- dgp
DGPorcallable() A
DGP, or a callable(n, seed) -> (frame, truth)following the convention of the generators incleverly.datasets.- estimator
callable() A zero-argument factory returning a fresh estimator per replication. A factory rather than an instance, so replications cannot share fitted state.
- n
int Sample size per replication.
- n_replicates
int How many replications to run.
- estimandssequence
ofstrorNone Which estimands to summarise; defaults to whatever the first fit reports.
- fit_kwargs
mappingorNone Passed to
fit. Column names,delta=,id=and so on.- seed
intorNone Root of the distinct per-replication sample seeds.
- sample_seedssequence
ofintorNone Explicit distinct uint32 sample seeds, one per replication. Use these to coordinate separate studies. When supplied, these replace the sample stream from
seed.- n_jobs
int Number of joblib workers across replications.
- truth_key{“population”, “sample”}
"population"(default) compares against the population estimand, fixed across replications;"sample"compares against each replication’s realised sample estimand, which removes one source of variability but changes what coverage means.- intermediate_value
objectorNone The level of the intermediate variable to study, for a controlled direct effect. Required when
fit_kwargscontainsintermediate=, because such a fit returns one result per level and each level is a different parameter: the study picks that level out of the result set and asks the process for the truth at the same level, so the two cannot silently disagree. Seecleverly.estimators.direct_effect.- label
str Name of the study, used in its reports.
- checkpoint
ReplicationCheckpointorNone Storage for finished replications. With one,
run()reuses every stored outcome and fits only the rest, so a stopped study resumes where it stopped.
- dgp
Notes
A replication whose fit raises a numerical or fitting error is recorded as a
ReplicationFailureand the study continues. These are recorded:DataError,LongitudinalError,numpy.linalg.LinAlgError,FloatingPointError,ZeroDivisionError,OverflowError, and a plainValueErrororRuntimeErrorthatnumpy,scipy,scikit-learnorstatsmodelsraised. Every other exception stops the study, includingCapabilityError,MethodConfigurationErrorand programming errors such asTypeError. An error while drawing a sample always stops the study.Examples
>>> from sklearn.linear_model import LinearRegression, LogisticRegression >>> from cleverly.estimators import TMLE >>> from cleverly.datasets import nonlinear_dgp >>> from cleverly.validation import CoverageStudy >>> study = CoverageStudy( ... dgp=nonlinear_dgp(), ... estimator=lambda: TMLE( ... outcome_learner=LinearRegression(), ... treatment_learner=LogisticRegression(max_iter=1000), ... ), ... n=500, ... n_replicates=50, ... seed=0, ... ) >>> study.n_replicates 50
Methods#
- CoverageStudy.run() StudyResult[source]#
Execute the study.
- Returns:
StudyResultPer-replication records, per-estimand summaries, and the failures.
- CoverageStudy.run_replication(replicate_index: int, seed: int) tuple[ReplicationRecord, ...] | ReplicationFailure[source]#
Fit one declared replication from an explicit sample seed.
A fresh estimator is created for every call. A recordable fitting failure (see the class notes) returns an explicit record; any other exception propagates. This method does not aggregate a sampling distribution or change the study budget.
- Parameters:
- Returns:
tupleofReplicationRecordorReplicationFailureOne record per estimand on success, or the fit’s explicit failure record.