Metadata-Version: 2.4
Name: capre
Version: 0.3.0
Summary: CAPRE: Causal Price-Elastic Recourse via DAG gating, SuperLearner stacking, SCM propagation, and effort-warped surfaces
Author-email: Andrew Wilson <wilson.stats@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://andystats.github.io/capre/
Project-URL: Documentation, https://andystats.github.io/capre/vignettes/
Project-URL: Repository, https://github.com/andystats/capre
Project-URL: Issues, https://github.com/andystats/capre/issues
Keywords: causal-inference,counterfactual-explanations,algorithmic-recourse,superlearner,directed-acyclic-graphs,clinical-decision-support
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Healthcare Industry
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Requires-Dist: pandas>=2.0
Requires-Dist: scikit-learn>=1.3
Provides-Extra: dev
Requires-Dist: pytest>=7.4; extra == "dev"
Requires-Dist: build>=1.0; extra == "dev"
Requires-Dist: twine>=6.1; extra == "dev"
Requires-Dist: packaging>=24.2; extra == "dev"
Dynamic: license-file

# `capre`: Causal Price-Elastic Recourse for Clinical and Tabular Risk Models

`capre` (**Causal Price-Elastic Recourse**) generates actionable, budget-feasible counterfactual recourse plans by combining:

1. **DAG & Partial-Knowledge Tier Gating (`DAGSpec`, `DAGSpec.from_tiers`):** Excludes non-ancestor diagnostic proxies (such as 48-hour laboratory draw counts and missingness indicators) without requiring a complete clinical DAG (supporting sink-only exclusions or coarse `(baseline, levers, mediators, sinks)` tiers), while flagging `confounded_ancestors` sharing unmeasured backdoor paths with the outcome.
2. **Abduct-Act-Predict Propagation & Interventional Causal Shapley (`LinearStructuralModel`):** Propagates upstream interventions through ridge or cubic-spline structural mediator equations and ranks candidate levers by marginal-residual Interventional Causal Shapley efficiency.
3. **Microeconomic Disutility & Effort-Isometric Surface Warping (`LeverSpec`, `PriceTable`):** Models shared domain initiation costs (economies of scope), directional convex marginal effort, Incremental Cost-Effectiveness Ratios (`icer`), Net Clinical Benefit (`net_benefit`), and effort-isometric coordinate maps `z = Psi_x(x')` (`PriceTable.elastic_warp_vector`), while selecting diverse candidate bundles across clinical domains and lever sets.
4. **`SuperLearner` Stacking & Model-Dependence Bounds (`CapreSuperLearner`, `MahalanobisOverlapIndex`):** Pairs cross-validated log-loss ensemble predictions with inter-learner dispersion `sigma_MD(x')` (supporting conservative gating `p_SL(x') + lambda_MD * sigma_MD(x') <= tau*`) and Kish Effective Sample Size (`overlap_ess`) counterfactual support diagnostics.

## Installation

```bash
pip install capre
```

## Quick Example

```python
from capre import CapreExplainer, CapreSuperLearner, make_icu_hapri_demo

demo = make_icu_hapri_demo(n=1000, seed=42)
sl_anc = CapreSuperLearner(feature_columns=demo.ancestor_columns, seed=42).fit(
    demo.df, demo.df[demo.outcome]
)
tau = sl_anc.oof_youden_threshold()

explainer = CapreExplainer.from_superlearner(
    causal_superlearner=sl_anc,
    train_df=demo.df,
    feature_columns=demo.feature_columns,
    outcome=demo.outcome,
    price_table=demo.price_table,
    dag=demo.dag,
)

high_risk_row = demo.df.loc[sl_anc.predict_risk(demo.df) > tau + 0.15].iloc[0]
result = explainer.explain(high_risk_row, threshold=tau, budget=5.0, lambda_md=0.5)
print(result.to_json())
```

## Vignettes and Interactive Companion

- **Python PDF Vignette (LaTeX Mini-Manuscript):** https://andystats.github.io/capre/vignettes/capre-python-guide.pdf
- **Interactive Visual Primer:** https://andystats.github.io/capre/
- **CAPRE for the Classroom (R & Python Workshops):** https://andystats.github.io/capre/classroom/
