# py-why/econml architecture

> Architecture diagram of the GitHub repository py-why/econml, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/py-why/econml
- Repository: https://github.com/py-why/econml

## Overview

##### Project Type and Purpose
- This is an open-source Python library (EconML) for estimating heterogeneous treatment effects via machine learning.
- It provides a unified API to implement causal inference methods (DML, DR, IV, forests, metalearners, policy learning, DeepIV, interpretability, inference).
- Designed as a backbone for other Python applications and notebooks to import and use.

##### File Structure Analysis
- Top-level:
- `.github/workflows/` – CI/CD pipelines (linting, testing, publishing docs & package)
- `econml/` – main Python package, subdivided into modules and sub-packages:
– `_cate_estimator.py`, `_ensemble/`, `_ortho_learner.py`, `_shap.py`, `_tree_exporter.py`, etc.
– Functional sub-packages: `dml`, `dr`, `iv`, `metalearners`, `orf`, `panel`, `policy`, `score`, `inference`, `cate_interpreter`, `data`, `grf`, `sklearn_extensions`, `tree`, `validate`.
- `doc/` – Sphinx documentation source and user guide PDF.
- `notebooks/`, `monte_carlo_tests/`, `prototypes/` – example workflows, experiments, demos.
- `econml/tests/` – pytest-based unit and integration tests mirroring the package structure.
- Config & packaging: `pyproject.toml`, `setup.py`, `.pre-commit-config.yaml`, `.gitignore`.

##### README Insights
- Emphasizes package capabilities, target audience (econometrics + ML practitioners), and supported methods.
- Usage examples for each method category highlight the core API (`fit`, `effect`, `effect_interval`, `summaries`, `interpret`).
- Developer instructions: testing, building docs with Sphinx, release process (GitHub Actions).

a. Main Components
- User/Application Layer
- Python client code / Jupyter notebooks that import EconML estimators.
- EconML Core
- Packages: `dml`, `dr`, `iv`, `metalearners`, `orf`, `panel`, `policy`, `inference`, `cate_interpreter`, `data`, `utilities`, `tree`, `sklearn_extensions`, `validate`.
- External Dependencies
- Core: `numpy`, `scipy`, `scikit-learn`, `pandas`, `joblib`
- Optional: `keras`/`tensorflow` (DeepIV), `xgboost` (notebooks), `graphviz` (docs), `shap`, `matplotlib`.
- Development & CI/CD
- GitHub Actions workflows: `ci.yml`, `publish-package.yml`, `publish-documentation.yml`, doc generation script.
- Pre-commit, pytest, Sphinx.
- Distribution
- PyPI package, documentation website hosted via GitHub Pages/Azure.

b. Relationships and Interactions
- EconML Core modules import and coordinate with one another (e.g., `dml` uses `inference`, `data`, `utilities`).
- Core modules call into external dependencies for numeric computation, ML modeling, and graphing.
- Development pipelines trigger on commits/tags: lint & test → build & publish package → build & publish docs.
- Notebooks and prototypes consume the installed package to demonstrate functionality.

c. Architectural Patterns and Principles
- Modular layering: each causal method category in its own sub-package.
- Shared utilities: cross-cutting concerns (data generation, model selection, parallelization).
- Cross-fitting & dependency injection: estimators accept arbitrary scikit-learn estimators.
- Separation of concerns: core logic vs. examples vs. docs vs. tests.

d. Key Technologies, Frameworks, Libraries
- Language: Python 3.7+
- Testing: pytest, unittest
- Documentation: Sphinx, reStructuredText, Graphviz
- CI/CD: GitHub Actions
- ML: scikit-learn, numpy, scipy, pandas, keras
- Packaging: setuptools, wheel, PyPI

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User/Application Layer
    subgraph "User/Application Layer" 
        style UserApps fill:#fff2cc,stroke:#333,stroke-width:1px
        Notebooks["Notebooks & Examples"]:::example
        Prototypes["Prototypes"]:::example
        MonteCarlo["Monte Carlo Tests & Demos"]:::example
    end

    %% EconML Core
    subgraph "EconML Core Package" 
        style EconMLCore fill:#cfe2f3,stroke:#333,stroke-width:1px
        CoreRoot["econml/"]:::core
        DML["dml"]:::core
        DR["dr"]:::core
        IV["iv"]:::core
        Meta["metalearners"]:::core
        ORF["orf"]:::core
        Panel["panel"]:::core
        Policy["policy"]:::core
        Inference["inference"]:::core
        Interpreter["cate_interpreter"]:::core
        DataGen["data"]:::core
        Utils["utilities.py"]:::core
        TreePkg["tree"]:::core
        SKExt["sklearn_extensions"]:::core
        Validate["validate"]:::core
        Ensemble["_ensemble"]:::core
        CateEst["_cate_estimator.py"]:::core
        OrthoLearn["_ortho_learner.py"]:::core
        SHAP["_shap.py"]:::core
        TreeExp["_tree_exporter.py"]:::core
        AutoML["automated_ml"]:::core
        Score["score"]:::core
        GRF["grf"]:::core
        FedLearn["federated_learning.py"]:::core
    end

    %% External Dependencies
    subgraph "External Dependencies"
        style ExtDeps fill:#d9ead3,stroke:#333,stroke-width:1px
        Numpy["numpy"]:::ext
        Scipy["scipy"]:::ext
        SKLearn["scikit-learn"]:::ext
        Pandas["pandas"]:::ext
        Joblib["joblib"]:::ext
        Keras["keras/tensorflow"]:::ext
        XGBoost["xgboost"]:::ext
        SHAPLib["shap"]:::ext
        MatPlot["matplotlib"]:::ext
        Graphviz["graphviz"]:::ext
    end

    %% CI/CD & Distribution
    subgraph "CI/CD & Distribution Infrastructure"
        style Infra fill:#f4cccc,stroke:#333,stroke-width:1px
        Commit["GitHub Commit/Tag"]:::infra
        CIWorkflows["CI/CD Workflows"]:::infra
        PreCommit[".pre-commit-config.yaml"]:::infra
        Config["setup.py & pyproject.toml"]:::infra
        DocsSource["doc/"]:::infra
        Tests["econml/tests/"]:::infra
        PyPI["PyPI Package"]:::infra
        GHPages["Documentation Site"]:::infra
    end

    %% Relationships
    Notebooks -->|"imports API"| CoreRoot
    Prototypes -->|"imports API"| CoreRoot
    MonteCarlo -->|"imports API"| CoreRoot

    CoreRoot --> DML
    CoreRoot --> DR
    CoreRoot --> IV
    CoreRoot --> Meta
    CoreRoot --> ORF
    CoreRoot --> Panel
    CoreRoot --> Policy
    CoreRoot --> Inference
    CoreRoot --> Interpreter
    CoreRoot --> DataGen
    CoreRoot --> Utils
    CoreRoot --> TreePkg
    CoreRoot --> SKExt
    CoreRoot --> Validate
    CoreRoot --> Ensemble
    CoreRoot --> CateEst
    CoreRoot --> OrthoLearn
    CoreRoot --> SHAP
    CoreRoot --> TreeExp
    CoreRoot --> AutoML
    CoreRoot --> Score
    CoreRoot --> GRF
    CoreRoot --> FedLearn

    CoreRoot --> Numpy
    CoreRoot --> Scipy
    CoreRoot --> SKLearn
    CoreRoot --> Pandas
    CoreRoot --> Joblib
    CoreRoot -.->|optional| Keras
    CoreRoot -.->|optional| XGBoost
    CoreRoot -.->|optional| SHAPLib
    CoreRoot --> MatPlot
    CoreRoot --> Graphviz

    Commit --> CIWorkflows
    CIWorkflows --> PreCommit
    CIWorkflows --> Tests
    CIWorkflows --> DocsSource
    CIWorkflows --> Config
    CIWorkflows --> PyPI
    CIWorkflows --> GHPages

    %% Click Events
    click Notebooks "https://github.com/py-why/econml/tree/main/notebooks/"
    click Prototypes "https://github.com/py-why/econml/tree/main/prototypes/"
    click MonteCarlo "https://github.com/py-why/econml/tree/main/monte_carlo_tests/"
    click CoreRoot "https://github.com/py-why/econml/tree/main/econml/"
    click DML "https://github.com/py-why/econml/tree/main/econml/dml/"
    click DR "https://github.com/py-why/econml/tree/main/econml/dr/"
    click IV "https://github.com/py-why/econml/tree/main/econml/iv/"
    click Meta "https://github.com/py-why/econml/tree/main/econml/metalearners/"
    click ORF "https://github.com/py-why/econml/tree/main/econml/orf/"
    click Panel "https://github.com/py-why/econml/tree/main/econml/panel/"
    click Policy "https://github.com/py-why/econml/tree/main/econml/policy/"
    click Inference "https://github.com/py-why/econml/tree/main/econml/inference/"
    click Interpreter "https://github.com/py-why/econml/tree/main/econml/cate_interpreter/"
    click DataGen "https://github.com/py-why/econml/tree/main/econml/data/"
    click Utils "https://github.com/py-why/econml/blob/main/econml/utilities.py"
    click TreePkg "https://github.com/py-why/econml/tree/main/econml/tree/"
    click SKExt "https://github.com/py-why/econml/tree/main/econml/sklearn_extensions/"
    click Validate "https://github.com/py-why/econml/tree/main/econml/validate/"
    click Ensemble "https://github.com/py-why/econml/tree/main/econml/_ensemble/"
    click CateEst "https://github.com/py-why/econml/blob/main/econml/_cate_estimator.py"
    click OrthoLearn "https://github.com/py-why/econml/blob/main/econml/_ortho_learner.py"
    click SHAP "https://github.com/py-why/econml/blob/main/econml/_shap.py"
    click TreeExp "https://github.com/py-why/econml/blob/main/econml/_tree_exporter.py"
    click AutoML "https://github.com/py-why/econml/tree/main/econml/automated_ml/"
    click Score "https://github.com/py-why/econml/tree/main/econml/score/"
    click GRF "https://github.com/py-why/econml/tree/main/econml/grf/"
    click FedLearn "https://github.com/py-why/econml/blob/main/econml/federated_learning.py"
    click CIWorkflows "https://github.com/py-why/econml/blob/main/.github/workflows/ci.yml"
    click PreCommit "https://github.com/py-why/econml/blob/main/.pre-commit-config.yaml"
    click Config "https://github.com/py-why/econml/blob/main/setup.py"
    click DocsSource "https://github.com/py-why/econml/tree/main/doc/"
    click Tests "https://github.com/py-why/econml/tree/main/econml/tests/"

    %% Styles
    classDef core fill:#cfe2f3,stroke:#333,stroke-width:1px
    classDef ext fill:#d9ead3,stroke:#333,stroke-width:1px
    classDef infra fill:#f4cccc,stroke:#333,stroke-width:1px
    classDef example fill:#fff2cc,stroke:#333,stroke-width:1px
```
