pytyche.analysis.fit_policy_tree¶
- pytyche.analysis.fit_policy_tree(posterior, *, max_depth=3, min_segment_share=0.1, n_bootstrap=50, bootstrap_seed=0)[source]¶
Discover interpretable segments from the posterior’s per-visitor treatment effects, by fitting a shallow decision tree.
Each visitor is labeled with the arm the posterior expects to be best for them: their posterior-mean contrast vector, reduced to the largest-lift treatment — or control, when no lift is positive. A multiclass
DecisionTreeClassifieris then fit on the visitors’ features (the same feature encoding the fit entry points use), so each leaf groups visitors the model treats alike. Each leaf becomes aDiscoveredSegment:rulereproduces the leaf’s membership exactly throughpytyche.summarize.apply_rule()(verified; mismatch raisesRuntimeError). A root-only tree yields one catch-all segment withclauses == ().arm_best_probabilitiesare per-draw best-arm win frequencies over the leaf’s per-draw mean contrasts — the same code paththompson_allocationuses.gate_estimate/gate_ci(mean and the 80% interval) are computed on the recommended arm’s in-segment contrast draws; for control-recommended leaves, on the maximum-mean contrast (the strongest challenger — non-positive by construction). The interval is the raw 10th/90th percentiles, or the calibrated remap when the posterior carries an attachedCalibrationartifact.stability_scoreis bootstrap-replicability: the fraction ofn_bootstraprow-resampled tree refits in which some leaf has Jaccard overlap >= 0.5 with the segment’s member set.
- Parameters:
posterior (
HurdleBCFResult|ContinuousBCFResult|BinaryBCFResult) – One of the three posterior result types, carrying observed data (raises otherwise).max_depth (
int) – Maximum tree depth.min_segment_share (
float) – Minimum fraction of visitors per leaf (sklearnmin_weight_fraction_leaf).n_bootstrap (
int) – Bootstrap refits behindstability_score;0skips stability — every score is NaN and aUserWarningis emitted.bootstrap_seed (
int) – Seed for the bootstrap resampling RNG.
- Return type:
- Returns:
PolicyTreeResultwith one segment per leaf (ordered by sklearn leaf id);result.observedisposterior.observedby identity.- Raises:
ValueError – When
posterior.observedisNone, or whenn_bootstrapis negative.TypeError – When posterior is not an accepted result type.
RuntimeError – When an extracted segment rule fails to reproduce its leaf’s membership (rule extraction is wrong — fail loud).