StatsPAI

repository·main·Indexed 43 days ago

https://github.com/brycewang-stanford/statspai

Validation-tiered causal inference and econometrics workflows for Python version 1.28.0. Designed as an Agent & Python-native replacement for Stata and R, it provides tools for OLS, IV, DiD, RD, and high-dimensional fixed effects (HDFE) with machine-readable schemas for AI agents. Includes bundled datasets, performance benchmarks against pyfixest and fixest, and a recommendation engine for econometrically acceptable estimators.

Tokens
241.5K
Snippets
628
Records
1K
Agent score
55%

What's inside StatsPAI

  1. Overview of StatsPAI Test Suites and Capabilities

    main

    StatsPAI's test suite is organized into functional batches that define the library's core capabilities. Developers can use this categorization to understand which econometric and causal inference methods are supported and verified. The suites cover everything from basic OLS to advanced Agentic workflows and Machine Learning-based causal discovery.

    Core Capabilities by Batch:

    • Infrastructure & Core: OLS, diagnostics, postestimation, and model summary tools.
    • Difference-in-Differences (DiD): Advanced DiD, staggered DiD, and time-varying covariates.
    • Instrumental Variables (IV): Standard IV, 2SLS, mediation, and proximal methods.
    • Regression Discontinuity (RD): Standard RD, RDDensity, and bunching.
    • DML & Meta-learners: Double Machine Learning, Causal Forests, and Auto-CATE.
    • Synthetic Control (Synth): Advanced synthetic control and sequential SDID.
    • Matching & Panel Data: Optimal matching, GMM, ARIMA, GARCH, and High-Dimensional Fixed Effects (HDFE).
    • Bayesian Inference: Bayesian DiD, IV, and BCF (Bayesian Causal Forests).
    • ML & Causal Discovery: DAGs, TMLE, Causal Discovery, and Conformal Inference.
    • Agent & Workflow: Agentic reasoning, MCP (Model Context Protocol) integration, and causal workflows.
    • Output & Reporting: Regression tables, publication-ready exports (Quarto, AER style), and journal presets.
  2. Overview of the `sp.did` API family

    main

    The sp.did module provides a comprehensive suite of Difference-in-Differences (DiD) estimators and diagnostic tools for applied causal inference. The API is organized into several specialized functions and utility modules.

    Core Estimators:

    • sp.callaway_santanna: Implements Callaway & Sant'Anna (2021) ATT(g,t) and Doubly Robust DiD (DR-DiD).
    • sp.sun_abraham: Implements the IW event-study approach.
    • sp.did_imputation: Implements Borusyak-Jaravel-Spiess imputation.
    • sp.gardner_did / sp.did_2stage: Implements Gardner (2021) two-stage approach.
    • sp.wooldridge_did: Implements Wooldridge ETWFE, DR-DiD, and TWFE diagnostics.
    • sp.honest_did: Implements Rambachan-Roth (2023) sensitivity analysis.
    • sp.bacon_decomposition: Implements Goodman-Bacon (2021) decomposition.
    • sp.did_multiplegt: Implements dCDH (2020) DID_M.
    • sp.continuous_did: Supports dose-quantile DiD, TWFE, and lpoly dose-response.
    • sp.did_2x2: Implements the canonical 2×2 DiD.
    • sp.cic: Implements Changes-in-Changes.
    • sp.stacked_did: Implements Stacked DiD.
    • sp.cohort_anchored: Implements Cohort-anchored DiD.
    • sp.ddd: Implements Triple Difference.

    Utilities and Diagnostics:

    • sp.did_analysis / DIDAnalysis: A unified workflow for DiD analysis.
    • sp.event_study: General event-study utility.
    • sp.pretrends: Pretrend testing toolkit.
    • sp.parallel_trends_plot, sp.bacon_plot, sp.group_time_plot, sp.did_plot: Specialized plotting functions.
    • sp.cs_report: Generates LaTeX, Word, or Excel reports.
  3. Overview of StatsPAI

    main

    StatsPAI is an open-source Python package designed for causal inference and applied econometrics. It provides a unified interface for estimating, diagnosing, and reporting models that are typically fragmented across specialized packages (like DoWhy, EconML, or DoubleML) or proprietary environments like Stata.

    Key features include:

    • Broad Method Coverage: Includes regression, panel models, instrumental variables (IV), difference-in-differences (DiD), regression discontinuity (RD), synthetic control, matching, and machine-learning estimators for heterogeneous treatment effects.
    • Unified Reporting: Mature estimators share a common reporting surface, allowing users to generate summaries, figures, LaTeX/Word tables, or citations using consistent calls.
    • Agent-Native Design: Every registered function exposes a machine-readable schema (arguments, outputs, and failure metadata), enabling LLM-driven research assistants to discover estimators and validate assumptions programmatically.
    • Extensible Backend: While primarily built on NumPy, SciPy, Pandas, statsmodels, scikit-learn, and linearmodels, it supports optional accelerators like PyTorch, JAX, and a Rust/PyO3 kernel for high-dimensional routines.
  4. Overview of StatsPAI Rust crates

    main

    The Rust components of StatsPAI are designed to provide high-performance kernels for specific statistical operations. Currently, the repository includes:

    • statspai_hdfe: A kernel for High-Dimensional Fixed Effects (HDFE) group-demeaning, implemented using PyO3 for Python bindings and Rayon for parallel execution.
  5. Compare StatsPAI with alternative Python packages

    main

    StatsPAI is optimized for end-to-end AER-style (American Economic Review) empirical workflows and agent-driven analysis. Use it instead of assembling multiple disparate packages in the following scenarios:

    ScenarioUse StatsPAIAlternative
    Full Pipeline (EDA → estimand → DAG → estimate → robustness)✅ single import covers all eight AER sectionsassemble pyfixest + econml + causalml + differences + ...
    Agent-driven analysis (self-describing API)✅ list_functions / describe_function / function_schemastatsmodels / pyfixest (no agent API)
    Estimand decision (DID vs RD vs IV?)✅ sp.causal_question + sp.causalmanual judgement call
    Stata migration (matching API names)✅ sp.regress, sp.estat, sp.sumstats, sp.feols, sp.panellinearmodels (partial)
    Robustness gauntlet (Oster, E-value, etc.)✅ Oster / honest_did / E-value / Conley / 2-way / spec_curve / placebo all in sp.*manually wire 5+ packages
    Epidemiology / Public Health (Target-trial, MR, Survival)✅ sp.target_trial.*, sp.gformula, sp.tmle, sp.mendelian, sp.kaplan_meier, etc.hand-stitched zEpid + lifelines + statsmodels
    ML Causal Inference (DML, Causal Forest, Policy Learning)✅ sp.dml, sp.metalearner, sp.causal_forest, sp.policy_tree, sp.ope.*, etc.EconML + DoWhy + CausalML + GRF + zEpid
  6. Understand the Dynamic Panel GMM development roadmap

    main

    The dynamic_panel_gmm_plan.md document outlines a two-month hardening plan for the Dynamic Panel GMM implementation in StatsPAI. The goal is to move from a basic implementation to a robust, Stata/R-parity-guaranteed suite of estimators.

    Key development milestones include:

    • Correctness Fixes: Addressing listwise deletion bugs (B1) and implementing a proper lag operator (B2).
    • Expanded Estimators: Implementing System GMM (Blundell-Bond), Anderson-Hsiao, and Bias-corrected LSDV.
    • Advanced Features: Adding instrument collapsing (B5), forward orthogonal deviations (B7), and difference-in-Hansen tests (B8).
    • Diagnostics: Implementing Windmeijer-corrected AR tests and estat-style postestimation surfaces.
  7. Handle changes to `anticipation > 0` and `base_period="varying"`

    main

    If you are calling sp.callaway_santanna (or sp.did) with both anticipation > 0 and base_period="varying", the behavior of pre-treatment placebo cells has changed to match R did.

    What changed

    StatsPAI now shifts the base period back by anticipation for post-treatment cells only. For pre-treatment placebo cells, the base period remains the period immediately before the treatment.

    Impact on Results

    • Pre-treatment ATT(g,t): Event-study leads, joint pre-trend tests, and anything built on placebo cells will move. The earliest pre-treatment cells that were previously dropped will now appear in the grid.
    • Post-treatment ATT(g,t): These do not move. Consequently, sp.aggte results (overall, simple, group, or calendar effects) and the headline ATT remain unchanged.

    Other logic changes

    • Cohorts where no period satisfies t + anticipation < g are now dropped with an explicit warning.
    • The period grid now uses strict inequalities and handles irregularly spaced periods (e.g., 1990, 1995, 2000) by resolving to the neighboring observed period.
  8. Understand the limitations of sp.paper()

    main

    When using sp.paper(), be aware of the following behaviors:

    • No Hyperparameter Sweeps: sp.paper() does not perform sweeps; it selects the recommendation from sp.recommend(...). For multiverse analysis, use sp.spec_curve(...) and pass the results to replication_pack via the extra_files= parameter.
    • No LLM by default: To use Large Language Models, you must explicitly pass llm="auto". Otherwise, no network calls are made.
    • No CSL Verification: The function does not check if your CSL file exists. Errors regarding missing CSL files will only appear during the quarto render step. It is recommended to run quarto render locally before final submission.
  9. Understand StatsPAI error taxonomy

    main

    StatsPAI uses a specific error taxonomy to provide machine-readable diagnostics and recovery hints instead of generic Python exceptions. When using fairness diagnostics, decomposition methods, or epidemiology measures, you may encounter the following specialized error types:

    • MethodIncompatibility: Raised when parameters, weights, formulas, or methods are incompatible with the requested operation (e.g., invalid wild-bootstrap weights or unknown distributional statistics).
    • DataInsufficient: Raised when the input data lacks the necessary information to perform a calculation (e.g., empty exposure rows in relative_risk or risk_difference).
    • NumericalInstability: Raised when outputs are non-finite (NaN or Inf) or when numerical methods fail to produce stable results.
    • ConvergenceFailure: Raised when iterative methods (like bootstrap paths) fail to converge, often providing n_ok/n_boot diagnostics to indicate the success rate.

    Note: Many of these are subclasses of standard Python errors like ValueError, TypeError, or RuntimeError to maintain backward compatibility.

  10. Understand Sequential Ignorability for `sp.mediate`

    main

    The sp.mediate function identifies natural effects under the assumption of sequential ignorability. This requires two conditions:

    1. Treatment ignorability: No unobserved confounding of the treatment with either the mediator or the outcome, conditional on pre-treatment covariates X.
    2. Mediator ignorability: Conditional on treatment and covariates, the mediator is as-good-as-randomized with respect to the outcome (no unobserved M–Y confounding).

    Warning: Part 2 is untestable from data. If a mediator-outcome confounder is affected by the treatment, natural effects are not identified. In such cases, use sp.mediate_interventional instead. Always report sp.mediate_sensitivity alongside sp.mediate to account for potential violations of mediator ignorability.

  11. How cross-language parity is verified

    main

    StatsPAI ensures correctness by comparing its implementations against canonical R or Stata references using a byte-for-byte comparison method. This process is designed to catch subtle statistical errors that standard unit tests (which often only check for direction, range, or shape) miss.

    The Verification Workflow:

    1. Source Inspection: Read the reference's actual source code (using deparse() in R or viewsource in Stata) rather than relying on documentation, to avoid naming traps (e.g., where a variable name like e(B) means different things in different languages).
    2. Byte Comparison: Generate a single CSV containing results from StatsPAI, R, and Stata, and compare the values directly.
    3. Bisection: If a divergence is found, bisect the calculation (design matrix, weights, residuals, meat matrix, etc.) to find the exact point of failure.
    4. Classification:
      • T2 Module: A real gap in implementation.
      • Registered Tolerance: A documented convention difference.
      • Grade C: A gap that cannot be located and remains visibly open.
    5. Registration: Finalize via the parity harness contract, tier A fixture lock, and audit scripts.
  12. Verify results with Cross-Software Parity

    main

    To ensure a StatsPAI result is consistent with established Stata or R outputs, you can check the parity status. StatsPAI maintains parity artifacts under tests/r_parity/ and tests/stata_parity/.

    In MCP clients, you can query statspai://parity/track-a-summary to get a compact JSON summary of the Track A 3-way report. This report categorizes agreement into:

    • machine-level agreement: Point estimates match at tight tolerance.
    • iterative/cross-fit agreement: Agreement within wider registered tolerances (e.g., due to random folds/optimizers).
    • convention gap: Backends target different estimands or covariance conventions.
    • unavailable: No committed external artifact exists yet.