cleverly.ParameterEstimate#
- class cleverly.ParameterEstimate(name: str, psi: float, influence_curve: ndarray[tuple[Any, ...], dtype[float64]], variance: float, n: int, n_clusters: int, scale: Literal['level', 'difference', 'ratio', 'fraction'] = 'difference', alpha: float = 0.05, log_psi: float | None = None, bootstrap: BootstrapSummary | None = None, covariance_rule: Literal['centered', 'second_moment'] = 'centered', inference: Literal['influence_curve', 'working_mechanism_plugin', 'generated_design_plugin', 'estimated_weight_plugin', 'adaptive_baseline_plugin', 'no_cluster_variance_plugin', 'few_cluster_plugin', 'constant_node_plugin', 'stack_failure_plugin'] = 'influence_curve', transform: Transform | None = None, reference_df: int | None = None, support: Support = Support(label='experimental', reason='This estimate was built outside a classified fit.', evidence=(), row=None, limits=()))[source]#
A point estimate with everything needed to do inference on it.
- Parameters:
- name
str Stable alias of the estimand.
- psi
float The estimate, on the outcome’s original scale.
- influence_curve
ndarray Per-observation influence curve on the inference scale – the original outcome scale for levels and differences, the log scale for ratios.
- variance
float Variance on the inference scale.
- n
int Number of observations behind the estimate.
- n_clusters
int Independent clusters behind it, which is what the variance divides by.
- scale{“level”, “difference”, “ratio”, “fraction”}
Scale the estimate is reported on, which decides how the interval is built.
- alpha
float Significance level of
ci.- log_psi
floatorNone Present for ratios only: the estimate on the log scale.
- bootstrap
BootstrapSummaryorNone Bootstrap summary, when
n_bootstrap > 0was requested.- covariance_rule{“centered”, “second_moment”}
How joint covariance and delta-method contrasts are computed from the influence curve.
"centered"uses the sample covariance at the observation or cluster unit, which can differ fromvarianceon a fold-evaluated or repeated fit."second_moment"uses the raw second moment that the stacked cross-fitted natural-course mean reports as its variance.- inference
str Whether the package supplies inference for this estimate. One of
InferenceStatus. The inference reference lists each status and its reason."influence_curve"is the ordinary case, andstd_error,ciandpvalueanswer. A status whose record shows its interval answers the same three accessors, and the estimate carries the experimental label. At any other status those three accessors raise with the status’s reason, andplugin_std_errorandplugin_intervalreport the retained diagnostic.- transform
TransformorNone A monotone map that the interval and the test are computed on, set by
contrast(..., transform=).Nonefor every estimate a fit reports.- reference_df
intorNone Degrees of freedom of the Student t reference of
ci,pvalueandplugin_interval, orNonefor the normal reference. A clustered fit with fewer than 40 clusters with positive weight mass sets it to the cluster count minus 2, per estimate.- support
Support What the package states about the evidence behind this estimate: the label
"supported"or"experimental", the reason, the evidence and the support row. A fit sets it from its classification. An estimate built outside a classified fit is experimental.reprandsummary()print the wordexperimentalfor an experimental estimate.
- name
- Attributes:
std_errorfloatStandard error on the inference scale.
plugin_std_errorfloatPlug-in standard error of the reported curve, at every inference status.
inference_valuefloatReturn the estimate on the scale used by its influence curve.
citupleoffloatConfidence interval at level
1 - alpha.plugin_intervaltupleoffloatWald interval of the plug-in curve, at level
1 - alpha.pvaluefloatTwo-sided p-value against the null of no effect.
supplies_inferenceboolWhether the package supplies inference for this estimate.
See also
cleverly.estimators.TMLEResultThe mapping these are read out of.
cleverly.inference.influence_varianceThe variance an estimate is given.
cleverly.inference.simultaneous_bandsJoint bands over several of these.
cleverly.inference.WaldTestWhat
wald_test()returns at a declared null.
Examples
Read an estimate from a fitted result:
>>> 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=80, seed=1) >>> 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, ... random_state=0, ... ) >>> estimate = result["ate"] >>> estimate.name, estimate.n ('ate', 80) >>> estimate.ci[0] < estimate.ci[1] True
Test the estimate against a declared null value:
>>> test = estimate.wald_test(null=1.0) >>> test.null, test.pvalue < 1.0 (1.0, True)
Methods#
- ParameterEstimate.spread_columns(*, pvalue: bool = True) dict[str, float][source]#
The standard error, interval and p-value, each under its status-appropriate name.
An estimate the package supplies inference for returns
std_err,ci_lower,ci_upperandp_value. A diagnostic estimate returns the same standard error and interval under the namesspread_name()gives them, and no p-value. The numbers are the bodiesplugin_std_errorandplugin_intervalread, so they are identical under either status.
- ParameterEstimate.to_dict() dict[str, Any][source]#
Return a JSON-compatible representation.
supportis always present, and is"supported"or"experimental". An experimental estimate addssupport_reasonandsupport_evidence, the evidence joined by;, and an estimate whose support states limits addssupport_limits.inferenceis present for every status except"influence_curve", and names it. An estimate the package supplies inference for, either at"influence_curve"or at a status that shows its interval, emitsstd_err,ci_lower,ci_upperandp_value. One whosesupplies_inferenceisFalseemitsplugin_std_err,plugin_interval_lowerandplugin_interval_upperin their place, and nop_value. Its bootstrap columns arebootstrap_sd,bootstrap_range_lowerandbootstrap_range_upperrather thanbootstrap_std_err,bootstrap_ci_lowerandbootstrap_ci_upper. The bootstrap columns take the same diagnostic names whenbootstrap_is_diagnostic()isTrue. A fit with a bootstrap addsbootstrap_support, the label of the bootstrap summary. A transformed estimate addstransformandtransform_psi, the estimate on the transform’s scale. An estimate with a Student t reference addsreference_df, and its bootstrap columns take the diagnostic names.std_erris present exactly whensupplies_inferenceisTrue, so a programmatic consumer has a total test.- Returns:
dictA JSON-compatible mapping of every reported field.
- ParameterEstimate.wald_test(null: float | None = None) WaldTest[source]#
Test the estimate against a declared null value.
The statistic is computed on the inference scale. A ratio’s null is mapped to the log scale, and a transformed estimate’s null is mapped through its transform.
- Parameters:
- Returns:
WaldTestThe statistic and its two-sided p-value. At
null=Noneits p-value ispvalue.
- Raises:
CapabilityErrorWhen
supplies_inferenceisFalse. The fit reports no test.ValueErrorWhen a ratio-scale null is not positive, or a transformed estimate’s null has no value under its transform.
Examples
>>> import numpy as np >>> from cleverly.inference import make_estimate >>> estimate = make_estimate("ey1", 0.4, np.array([0.1, -0.1, 0.2, -0.2]), n=4) >>> test = estimate.wald_test(null=0.4) >>> test.statistic, test.pvalue (0.0, 1.0)
- ParameterEstimate.with_bootstrap(summary: BootstrapSummary) ParameterEstimate[source]#
Return a copy with bootstrap inference attached.
- Parameters:
- summary
BootstrapSummary Percentile interval and bootstrap standard error to attach.
- summary
- Returns:
ParameterEstimateA copy carrying that summary. The analytic interval is unchanged.
Attributes#
- ParameterEstimate.ci#
Confidence interval at level
1 - alpha.- Raises:
CapabilityErrorWhen
supplies_inferenceisFalse. Readplugin_intervalfor the retained diagnostic.
- ParameterEstimate.inference: Literal['influence_curve', 'working_mechanism_plugin', 'generated_design_plugin', 'estimated_weight_plugin', 'adaptive_baseline_plugin', 'no_cluster_variance_plugin', 'few_cluster_plugin', 'constant_node_plugin', 'stack_failure_plugin'] = 'influence_curve'#
- ParameterEstimate.inference_value#
Return the estimate on the scale used by its influence curve.
- ParameterEstimate.plugin_interval#
Wald interval of the plug-in curve, at level
1 - alpha.A diagnostic and not a confidence interval. It is the interval
cibuilds, read through a name that makes no coverage claim, so a fit whose inference the package refuses can still report the spread of the curve it computed. At a non-inferential status the coverage of this interval is unestablished, for the reason the inference reference gives. The registered selector studies measure it and report it as a diagnostic.
- ParameterEstimate.plugin_std_error#
Plug-in standard error of the reported curve, at every inference status.
A diagnostic and not a standard error for an estimate whose
supplies_inferenceisFalse. On an ordinary estimate it isstd_errorunder a name that makes no coverage claim.
- ParameterEstimate.pvalue#
Two-sided p-value against the null of no effect.
The null is zero for a level, difference or fraction and one for a ratio (i.e. zero on the log scale). A transformed estimate maps that null through its transform. The value is
wald_test().pvalue, from the same body.- Raises:
CapabilityErrorWhen
supplies_inferenceisFalse. This fit reports no p-value.
- ParameterEstimate.score#
Mean of the influence curve.
Targeting is supposed to drive this to zero;
cleverly.validation.scorecompares it against the standard error to decide whether it did.
- ParameterEstimate.std_error#
Standard error on the inference scale.
- Raises:
CapabilityErrorWhen
supplies_inferenceisFalse. Readplugin_std_errorfor the retained diagnostic.
- ParameterEstimate.supplies_inference#
Whether the package supplies inference for this estimate.
Truewheninferenceis"influence_curve"or a status that shows its interval, which is whenstd_error,ciandpvalueanswer.
- ParameterEstimate.support: Support = Support(label='experimental', reason='This estimate was built outside a classified fit.', evidence=(), row=None, limits=())#