whyflow 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- whyflow-0.2.0/.gitattributes +4 -0
- whyflow-0.2.0/.github/workflows/demo.yml +44 -0
- whyflow-0.2.0/.github/workflows/publish.yml +25 -0
- whyflow-0.2.0/.github/workflows/tests.yml +23 -0
- whyflow-0.2.0/.gitignore +8 -0
- whyflow-0.2.0/CHANGELOG.md +37 -0
- whyflow-0.2.0/CITATION.cff +22 -0
- whyflow-0.2.0/CONTRIBUTING.md +38 -0
- whyflow-0.2.0/LICENSE +21 -0
- whyflow-0.2.0/PKG-INFO +229 -0
- whyflow-0.2.0/README.md +199 -0
- whyflow-0.2.0/example.py +49 -0
- whyflow-0.2.0/examples/haunted_dag.py +56 -0
- whyflow-0.2.0/examples/howell_children.py +87 -0
- whyflow-0.2.0/examples/howell_children_pymc.py +88 -0
- whyflow-0.2.0/examples/mediator_mistake.py +51 -0
- whyflow-0.2.0/pyproject.toml +51 -0
- whyflow-0.2.0/tests/test_adjustment.py +144 -0
- whyflow-0.2.0/tests/test_bayes.py +58 -0
- whyflow-0.2.0/tests/test_charts.py +20 -0
- whyflow-0.2.0/tests/test_checks.py +142 -0
- whyflow-0.2.0/tests/test_dag.py +66 -0
- whyflow-0.2.0/tests/test_report.py +45 -0
- whyflow-0.2.0/tests/test_sensitivity.py +85 -0
- whyflow-0.2.0/tests/test_workflow.py +100 -0
- whyflow-0.2.0/whyflow/__init__.py +16 -0
- whyflow-0.2.0/whyflow/adjustment.py +222 -0
- whyflow-0.2.0/whyflow/bayes.py +32 -0
- whyflow-0.2.0/whyflow/charts.py +148 -0
- whyflow-0.2.0/whyflow/checks.py +92 -0
- whyflow-0.2.0/whyflow/dag.py +122 -0
- whyflow-0.2.0/whyflow/report.py +365 -0
- whyflow-0.2.0/whyflow/sensitivity.py +70 -0
- whyflow-0.2.0/whyflow/workflow.py +240 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
name: demo site
|
|
2
|
+
|
|
3
|
+
# Builds the demo website from the examples. On pull requests it only checks that the site builds;
|
|
4
|
+
# on main it also publishes it to GitHub Pages.
|
|
5
|
+
on:
|
|
6
|
+
push:
|
|
7
|
+
branches: [main]
|
|
8
|
+
pull_request:
|
|
9
|
+
workflow_dispatch:
|
|
10
|
+
|
|
11
|
+
permissions:
|
|
12
|
+
contents: read
|
|
13
|
+
|
|
14
|
+
concurrency:
|
|
15
|
+
group: demo-${{ github.ref }}
|
|
16
|
+
cancel-in-progress: true
|
|
17
|
+
|
|
18
|
+
jobs:
|
|
19
|
+
build:
|
|
20
|
+
runs-on: ubuntu-latest
|
|
21
|
+
steps:
|
|
22
|
+
- uses: actions/checkout@v6
|
|
23
|
+
- uses: actions/setup-python@v6
|
|
24
|
+
with:
|
|
25
|
+
python-version: "3.12"
|
|
26
|
+
- run: pip install -e ".[dev]"
|
|
27
|
+
- run: python demo/build.py
|
|
28
|
+
- uses: actions/upload-pages-artifact@v5
|
|
29
|
+
with:
|
|
30
|
+
path: _site
|
|
31
|
+
|
|
32
|
+
deploy:
|
|
33
|
+
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
|
|
34
|
+
needs: build
|
|
35
|
+
runs-on: ubuntu-latest
|
|
36
|
+
permissions:
|
|
37
|
+
pages: write
|
|
38
|
+
id-token: write
|
|
39
|
+
environment:
|
|
40
|
+
name: github-pages
|
|
41
|
+
url: ${{ steps.deployment.outputs.page_url }}
|
|
42
|
+
steps:
|
|
43
|
+
- id: deployment
|
|
44
|
+
uses: actions/deploy-pages@v5
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
name: publish
|
|
2
|
+
|
|
3
|
+
# Runs when you publish a GitHub Release: builds whyflow and uploads it to PyPI.
|
|
4
|
+
# Uses Trusted Publishing, so no passwords or tokens are stored anywhere.
|
|
5
|
+
on:
|
|
6
|
+
release:
|
|
7
|
+
types: [published]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
pypi:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
environment:
|
|
13
|
+
name: pypi
|
|
14
|
+
url: https://pypi.org/p/whyflow
|
|
15
|
+
permissions:
|
|
16
|
+
id-token: write
|
|
17
|
+
contents: read
|
|
18
|
+
steps:
|
|
19
|
+
- uses: actions/checkout@v6
|
|
20
|
+
- uses: actions/setup-python@v6
|
|
21
|
+
with:
|
|
22
|
+
python-version: "3.12"
|
|
23
|
+
- run: python -m pip install build
|
|
24
|
+
- run: python -m build
|
|
25
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
name: tests
|
|
2
|
+
name: tests
|
|
3
|
+
|
|
4
|
+
on:
|
|
5
|
+
push:
|
|
6
|
+
branches: [main]
|
|
7
|
+
pull_request:
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
fail-fast: false
|
|
14
|
+
matrix:
|
|
15
|
+
python-version: ["3.11", "3.12", "3.13"]
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v6
|
|
18
|
+
- uses: actions/setup-python@v6
|
|
19
|
+
with:
|
|
20
|
+
python-version: ${{ matrix.python-version }}
|
|
21
|
+
- run: pip install -e ".[dev]"
|
|
22
|
+
- run: ruff check .
|
|
23
|
+
- run: pytest
|
whyflow-0.2.0/.gitignore
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to whyflow. The format follows [Keep a Changelog](https://keepachangelog.com/).
|
|
4
|
+
|
|
5
|
+
## [0.2.0] - 2026-12-04
|
|
6
|
+
|
|
7
|
+
First release on PyPI: `pip install whyflow`.
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- **Diagram check** (`wf.add_adjustment_check()`): estimators declare `adjusts_for=[...]`, and whyflow checks it
|
|
12
|
+
against the causal diagram: mediators, colliders, things caused by the outcome, open back doors. It suggests
|
|
13
|
+
the smallest valid adjustment sets, or says none exists. The report lights up the paths: green for the effect,
|
|
14
|
+
moving red for bias.
|
|
15
|
+
- **Reliable recovery test**: `wf.add_recovery_test()` now runs 100 simulations and checks coverage and average
|
|
16
|
+
error, with a pass mark that allows for the luck of a finite number of tries. Two charts in the report.
|
|
17
|
+
- **Sensitivity analysis** (`wf.add_sensitivity_check()`): adds an unmeasured confounder at growing strengths
|
|
18
|
+
and finds the tipping point where the estimator breaks. Chart and slider in the report.
|
|
19
|
+
- **Unmeasured variables**: `DAG().add("U", observed=False)`.
|
|
20
|
+
- **Needs from parameter names**: `def estimand(generative_model):` receives that step's result.
|
|
21
|
+
- **Jupyter display**: end a cell with `wf`.
|
|
22
|
+
- **Playground**: run and edit whyflow workflows in the browser, no installation (Python runs locally via
|
|
23
|
+
Pyodide), at https://koopticon.github.io/whyflow/playground.html.
|
|
24
|
+
- **Live demo site**, rebuilt from the examples on every change, and two examples built to fail:
|
|
25
|
+
`mediator_mistake.py` and `haunted_dag.py`.
|
|
26
|
+
- Logo, a rebuilt README, `CITATION.cff`, this changelog and `CONTRIBUTING.md`.
|
|
27
|
+
|
|
28
|
+
### Fixed
|
|
29
|
+
|
|
30
|
+
- A step whose code can't be read (typed at a Python prompt) no longer crashes the report.
|
|
31
|
+
- A failed result box in the report no longer renders as a small pill.
|
|
32
|
+
- One line-ending style in every file, which had broken CI.
|
|
33
|
+
|
|
34
|
+
## [0.1.0] - 2026-12-02
|
|
35
|
+
|
|
36
|
+
- Steps with required reasons, a simulatable causal diagram, a single-simulation recovery test, "test before you
|
|
37
|
+
invest" warnings, Bayesian estimators via posterior samples, and the HTML report.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
cff-version: 1.2.0
|
|
2
|
+
message: "If whyflow helps your work, please cite it as below."
|
|
3
|
+
title: "whyflow: statistical workflows that carry their reasons, code and tests"
|
|
4
|
+
abstract: >-
|
|
5
|
+
A Python tool for the statistical workflow taught in Statistical Rethinking: every step carries its code and
|
|
6
|
+
the reason it exists, estimators are checked against the causal diagram, on simulated data and for
|
|
7
|
+
sensitivity to unmeasured confounding, and the analysis becomes one shareable report.
|
|
8
|
+
authors:
|
|
9
|
+
- family-names: Konialee
|
|
10
|
+
given-names: Jawad
|
|
11
|
+
version: 0.2.0
|
|
12
|
+
date-released: 2026-12-04
|
|
13
|
+
license: MIT
|
|
14
|
+
repository-code: "https://github.com/koopticon/whyflow"
|
|
15
|
+
url: "https://koopticon.github.io/whyflow/"
|
|
16
|
+
keywords:
|
|
17
|
+
- statistics
|
|
18
|
+
- causal inference
|
|
19
|
+
- bayesian
|
|
20
|
+
- workflow
|
|
21
|
+
- simulation
|
|
22
|
+
- statistical rethinking
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Contributing to whyflow
|
|
2
|
+
|
|
3
|
+
Thanks for wanting to help. Bug reports, ideas and pull requests are all welcome. If you're unsure whether
|
|
4
|
+
something fits, open an [issue](https://github.com/koopticon/whyflow/issues) first and ask.
|
|
5
|
+
|
|
6
|
+
## Set up
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
git clone https://github.com/koopticon/whyflow
|
|
10
|
+
cd whyflow
|
|
11
|
+
python -m venv .venv
|
|
12
|
+
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
|
13
|
+
pip install -e ".[dev]"
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Before you open a pull request
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
ruff check . # style and common mistakes
|
|
20
|
+
pytest # all tests should pass
|
|
21
|
+
python demo/build.py # the examples still run (optional, CI does it too)
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Work on a branch, one change per pull request, and add a test for anything new. CI runs the tests on Python
|
|
25
|
+
3.11 to 3.13 and builds the demo site.
|
|
26
|
+
|
|
27
|
+
## Style
|
|
28
|
+
|
|
29
|
+
- **Messages a person can act on.** Every check explains its verdict in plain words and says what to do next.
|
|
30
|
+
If an error message would confuse someone in week three of Statistical Rethinking, rewrite it.
|
|
31
|
+
- **The report is one file.** No new runtime dependencies for drawing; charts are SVG written in Python.
|
|
32
|
+
- **Honest limits.** If something isn't supported, say so in the README's "Current limits".
|
|
33
|
+
|
|
34
|
+
## Releasing (maintainers)
|
|
35
|
+
|
|
36
|
+
1. Bump `version` in `pyproject.toml` and `CITATION.cff`, and add a section to `CHANGELOG.md`.
|
|
37
|
+
2. Merge to `main`, then create a GitHub Release with a tag like `v0.2.0`.
|
|
38
|
+
3. The `publish` workflow builds the package and uploads it to PyPI with Trusted Publishing (no tokens).
|
whyflow-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jawad Konialee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
whyflow-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: whyflow
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Statistical workflows that carry their reasons, their code and their tests.
|
|
5
|
+
Project-URL: Homepage, https://koopticon.github.io/whyflow/
|
|
6
|
+
Project-URL: Repository, https://github.com/koopticon/whyflow
|
|
7
|
+
Project-URL: Issues, https://github.com/koopticon/whyflow/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/koopticon/whyflow/blob/main/CHANGELOG.md
|
|
9
|
+
Author: Jawad Konialee
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: bayesian,causal-inference,dag,simulation,statistical-rethinking,statistics,workflow
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Requires-Dist: numpy
|
|
22
|
+
Requires-Dist: pandas
|
|
23
|
+
Provides-Extra: bayes
|
|
24
|
+
Requires-Dist: pymc; extra == 'bayes'
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
27
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
28
|
+
Requires-Dist: statsmodels; extra == 'dev'
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
<p align="center">
|
|
32
|
+
<a href="https://koopticon.github.io/whyflow/">
|
|
33
|
+
<picture>
|
|
34
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/logo-dark.svg">
|
|
35
|
+
<img alt="whyflow" src="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/logo.svg" height="72">
|
|
36
|
+
</picture>
|
|
37
|
+
</a>
|
|
38
|
+
</p>
|
|
39
|
+
|
|
40
|
+
<p align="center"><em>Statistical workflows that carry their reasons, their code and their tests.</em></p>
|
|
41
|
+
|
|
42
|
+
<p align="center">
|
|
43
|
+
<a href="https://github.com/koopticon/whyflow/actions/workflows/tests.yml"><img alt="tests" src="https://github.com/koopticon/whyflow/actions/workflows/tests.yml/badge.svg"></a>
|
|
44
|
+
<a href="https://pypi.org/project/whyflow/"><img alt="PyPI" src="https://img.shields.io/pypi/v/whyflow?color=0d9488"></a>
|
|
45
|
+
<a href="https://pypi.org/project/whyflow/"><img alt="Python versions" src="https://img.shields.io/pypi/pyversions/whyflow"></a>
|
|
46
|
+
<a href="https://github.com/koopticon/whyflow/blob/main/LICENSE"><img alt="MIT license" src="https://img.shields.io/badge/license-MIT-blue"></a>
|
|
47
|
+
</p>
|
|
48
|
+
|
|
49
|
+
<p align="center">
|
|
50
|
+
<a href="https://koopticon.github.io/whyflow/"><strong>Live demo</strong></a> ·
|
|
51
|
+
<a href="https://koopticon.github.io/whyflow/playground.html"><strong>Try it in your browser</strong></a> ·
|
|
52
|
+
<a href="#quickstart">Quickstart</a> ·
|
|
53
|
+
<a href="#what-it-checks">What it checks</a> ·
|
|
54
|
+
<a href="#how-it-compares">How it compares</a> ·
|
|
55
|
+
<a href="#examples">Examples</a> ·
|
|
56
|
+
<a href="#cite">Cite</a>
|
|
57
|
+
</p>
|
|
58
|
+
|
|
59
|
+
<p align="center">
|
|
60
|
+
<img alt="Clicking through a whyflow report: the workflow diagram, a recovery test's charts, and the sensitivity slider" src="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/hero.gif" width="820">
|
|
61
|
+
</p>
|
|
62
|
+
|
|
63
|
+
whyflow turns the statistical workflow Richard McElreath teaches in *Statistical Rethinking* into code you can
|
|
64
|
+
run and a report you can share. You write down how you think the data came about, what you want to know, and
|
|
65
|
+
how you'll estimate it, and every step has to say **why** it's there. Before you touch real data, whyflow
|
|
66
|
+
checks the estimator three ways, and the whole analysis becomes one page where each box in the diagram opens
|
|
67
|
+
the code behind it.
|
|
68
|
+
|
|
69
|
+
In his 2026 lectures McElreath said he knew of no tool that attaches each step's code to the workflow diagram,
|
|
70
|
+
and asked if anyone would build one. This is one answer.
|
|
71
|
+
|
|
72
|
+
<p align="center">
|
|
73
|
+
<img alt="The workflow: generative model, estimand, estimator, test before you invest (diagram check, recovery test, sensitivity), then real data" src="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/workflow.png" width="900">
|
|
74
|
+
</p>
|
|
75
|
+
|
|
76
|
+
**No install needed to try it:** the [playground](https://koopticon.github.io/whyflow/playground.html) runs
|
|
77
|
+
whyflow in your browser. Edit the estimator, press Run, and the report updates.
|
|
78
|
+
|
|
79
|
+
## Install
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
pip install whyflow
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Python 3.11+. The examples also use `statsmodels`; the Bayesian one uses PyMC (`pip install "whyflow[bayes]"`).
|
|
86
|
+
|
|
87
|
+
## Quickstart
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
import statsmodels.formula.api as smf
|
|
91
|
+
|
|
92
|
+
from whyflow import DAG, Workflow
|
|
93
|
+
|
|
94
|
+
wf = Workflow("Total effect of age on weight")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@wf.step(kind="generative_model", why="Age makes children taller; age and height both make them heavier.")
|
|
98
|
+
def generative_model():
|
|
99
|
+
return DAG().add("age").add("height", causes={"age": 2.0}).add("weight", causes={"age": 0.5, "height": 0.4})
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
# A parameter named after a step receives that step's result.
|
|
103
|
+
@wf.step(kind="estimand", why="The TOTAL effect of age, including the part that works through height.")
|
|
104
|
+
def estimand(generative_model):
|
|
105
|
+
return {"cause": "age", "outcome": "weight", "true_value": generative_model.total_effect("age", "weight")}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@wf.step(kind="estimator", adjusts_for=[], why="Weight on age only: height is a mediator, so leave it out.")
|
|
109
|
+
def estimator():
|
|
110
|
+
def fit(data):
|
|
111
|
+
model = smf.ols("weight ~ age", data=data).fit()
|
|
112
|
+
low, high = model.conf_int().loc["age"]
|
|
113
|
+
return {"estimate": model.params["age"], "low": low, "high": high}
|
|
114
|
+
|
|
115
|
+
return fit
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
wf.add_adjustment_check() # does the causal diagram agree with what the estimator adjusts for?
|
|
119
|
+
wf.add_recovery_test() # 100 simulated datasets: does it find the true effect, 1.3?
|
|
120
|
+
wf.add_sensitivity_check() # how strong would an unmeasured confounder have to be to break it?
|
|
121
|
+
|
|
122
|
+
wf.run()
|
|
123
|
+
wf.report("report.html") # or, in a Jupyter notebook, end a cell with: wf
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Now change the estimator to `weight ~ age + height` with `adjusts_for=["height"]` and run it again. The
|
|
127
|
+
diagram check flags height as a mediator before any data exists, and the recovery test shows the cost: an
|
|
128
|
+
estimate of 0.5 instead of 1.3.
|
|
129
|
+
|
|
130
|
+
## What it checks
|
|
131
|
+
|
|
132
|
+
### Good and bad controls, from the diagram alone
|
|
133
|
+
|
|
134
|
+
Declare what the estimator adjusts for, and whyflow reads the causal diagram: mediators, colliders, things
|
|
135
|
+
caused by the outcome, back doors left open. It suggests the smallest sets that work, and says plainly when an
|
|
136
|
+
unmeasured variable means none does. The paths that carry the effect you want are green; bias leaking in moves
|
|
137
|
+
along its path in red.
|
|
138
|
+
|
|
139
|
+
<p align="center"><img alt="The haunted DAG: adjusting for G leaves the path through the unmeasured U open, drawn as moving red arrows" src="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/controls.gif" width="720"></p>
|
|
140
|
+
|
|
141
|
+
### Recovery on simulated data
|
|
142
|
+
|
|
143
|
+
100 datasets where the truth is known. Does the estimator find it, and do its ranges catch it as often as they
|
|
144
|
+
promise? The pass mark allows for the luck of 100 tries, so a good estimator doesn't fail by chance.
|
|
145
|
+
|
|
146
|
+
<p align="center"><img alt="Recovery test: where the 100 estimates landed, and every simulation's range in green or red" src="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/recovery.png" width="720"></p>
|
|
147
|
+
|
|
148
|
+
### Sensitivity: what would break it?
|
|
149
|
+
|
|
150
|
+
The diagram only holds the confounders you thought of. This step adds one you didn't, at growing strengths,
|
|
151
|
+
and finds the tipping point where the estimator stops finding the truth. Drag the slider in the report to see
|
|
152
|
+
each strength.
|
|
153
|
+
|
|
154
|
+
<p align="center"><img alt="Sensitivity analysis: the estimate drifting away from the truth as a hidden confounder gets stronger, with the tipping point marked" src="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/sensitivity.png" width="720"></p>
|
|
155
|
+
|
|
156
|
+
And one rule ties it together: an `analysis` step whose estimator hasn't passed a test gets a warning, and a
|
|
157
|
+
⚠️ in the report. Test before you invest.
|
|
158
|
+
|
|
159
|
+
## How it compares
|
|
160
|
+
|
|
161
|
+
whyflow doesn't replace these tools; it sits between them. Draw your diagram in dagitty, borrow estimators
|
|
162
|
+
from DoWhy, run big pipelines in targets or Hamilton. whyflow is for keeping one analysis's reasoning, code
|
|
163
|
+
and tests together.
|
|
164
|
+
|
|
165
|
+
| | whyflow | [dagitty](https://www.dagitty.net) | [DoWhy](https://github.com/py-why/dowhy) | [estiplan](https://github.com/rsmeyerlorey/estiplan) | [targets](https://github.com/ropensci/targets) / [Hamilton](https://github.com/apache/hamilton) |
|
|
166
|
+
|---|:---:|:---:|:---:|:---:|:---:|
|
|
167
|
+
| Analysis drawn as a diagram of steps | ✅ | | | ✅ | ✅ |
|
|
168
|
+
| Each step's code attached to the diagram | ✅ | | | | partly |
|
|
169
|
+
| Every step must say why | ✅ | | | | |
|
|
170
|
+
| Adjustment sets from a causal DAG | ✅ | ✅ | ✅ | ✅ | |
|
|
171
|
+
| Estimator tested on simulated data with a known truth | ✅ | | partly | | |
|
|
172
|
+
| Sensitivity to an unmeasured confounder | ✅ | | ✅ | | |
|
|
173
|
+
| Built-in estimation methods | bring your own | | ✅ | brms code | |
|
|
174
|
+
| Caching, big pipelines | | | | | ✅ |
|
|
175
|
+
| Language | Python | web, R | Python | web app | R / Python |
|
|
176
|
+
|
|
177
|
+
## More
|
|
178
|
+
|
|
179
|
+
- **Bayesian estimators.** Fit with PyMC (or anything), then `summarize_samples(posterior)` gives the mean and
|
|
180
|
+
an 89% interval; the recovery test then expects 89% coverage.
|
|
181
|
+
- **Unmeasured variables.** `DAG().add("U", observed=False)` shapes the simulated data but is left out of it,
|
|
182
|
+
like in real life.
|
|
183
|
+
- **Jupyter.** End a cell with `wf` and the report appears inline.
|
|
184
|
+
- **Steps without boilerplate.** Name a parameter after a step to receive its result, or list them with
|
|
185
|
+
`needs=[...]` and name the parameters as you like.
|
|
186
|
+
|
|
187
|
+
## Examples
|
|
188
|
+
|
|
189
|
+
Every example also runs on the [live demo](https://koopticon.github.io/whyflow/), rebuilt on each change.
|
|
190
|
+
|
|
191
|
+
| Example | What it shows |
|
|
192
|
+
|---|---|
|
|
193
|
+
| [`example.py`](example.py) | The quickstart: all three checks pass; a hidden confounder of 0.3 would break it. |
|
|
194
|
+
| [`examples/mediator_mistake.py`](examples/mediator_mistake.py) | The most common mistake, "controlling for" a mediator. Both tests fail: 0.5 instead of 1.3. |
|
|
195
|
+
| [`examples/haunted_dag.py`](examples/haunted_dag.py) | McElreath's haunted DAG: an unmeasured confounder no adjustment can fix. |
|
|
196
|
+
| [`examples/howell_children.py`](examples/howell_children.py) | Real data, the !Kung San census from the book: about **1.34 kg per year** of age. |
|
|
197
|
+
| [`examples/howell_children_pymc.py`](examples/howell_children_pymc.py) | The same analysis with a Bayesian model in PyMC. |
|
|
198
|
+
|
|
199
|
+
The Howell data is downloaded from the [rethinking](https://github.com/rmcelreath/rethinking) repository when
|
|
200
|
+
an example runs; it isn't included here. `python demo/build.py` builds the demo site into `_site/`.
|
|
201
|
+
|
|
202
|
+
## Current limits
|
|
203
|
+
|
|
204
|
+
- Simulation covers continuous variables with straight-line relationships, and the target is a **total**
|
|
205
|
+
effect of one variable on another.
|
|
206
|
+
- The diagram check trusts what `adjusts_for` says; it can't read your model formula. The recovery test is what
|
|
207
|
+
catches an estimator that does something different from what it declares.
|
|
208
|
+
- The diagram check lists every path between cause and outcome: fine for a few dozen variables, slow for very
|
|
209
|
+
large diagrams.
|
|
210
|
+
- Every run re-runs every step (no caching yet).
|
|
211
|
+
|
|
212
|
+
## Cite
|
|
213
|
+
|
|
214
|
+
If whyflow helps your work, please cite it. GitHub's **Cite this repository** button (from
|
|
215
|
+
[`CITATION.cff`](CITATION.cff)) gives APA and BibTeX.
|
|
216
|
+
|
|
217
|
+
## Contributing and feedback
|
|
218
|
+
|
|
219
|
+
whyflow is young. If you try it, especially while working through Statistical Rethinking in Python, tell me
|
|
220
|
+
what worked and what didn't in an [issue](https://github.com/koopticon/whyflow/issues). To work on the code,
|
|
221
|
+
see [CONTRIBUTING.md](CONTRIBUTING.md). Changes are listed in the [changelog](CHANGELOG.md).
|
|
222
|
+
|
|
223
|
+
Inspired by Richard McElreath's *Statistical Rethinking* and its *estiplans*, and by
|
|
224
|
+
[YesWorkflow](https://github.com/yesworkflow-org/yw-prototypes), which draws workflow diagrams from comments in
|
|
225
|
+
scripts.
|
|
226
|
+
|
|
227
|
+
## License
|
|
228
|
+
|
|
229
|
+
[MIT](LICENSE)
|
whyflow-0.2.0/README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://koopticon.github.io/whyflow/">
|
|
3
|
+
<picture>
|
|
4
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/logo-dark.svg">
|
|
5
|
+
<img alt="whyflow" src="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/logo.svg" height="72">
|
|
6
|
+
</picture>
|
|
7
|
+
</a>
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
<p align="center"><em>Statistical workflows that carry their reasons, their code and their tests.</em></p>
|
|
11
|
+
|
|
12
|
+
<p align="center">
|
|
13
|
+
<a href="https://github.com/koopticon/whyflow/actions/workflows/tests.yml"><img alt="tests" src="https://github.com/koopticon/whyflow/actions/workflows/tests.yml/badge.svg"></a>
|
|
14
|
+
<a href="https://pypi.org/project/whyflow/"><img alt="PyPI" src="https://img.shields.io/pypi/v/whyflow?color=0d9488"></a>
|
|
15
|
+
<a href="https://pypi.org/project/whyflow/"><img alt="Python versions" src="https://img.shields.io/pypi/pyversions/whyflow"></a>
|
|
16
|
+
<a href="https://github.com/koopticon/whyflow/blob/main/LICENSE"><img alt="MIT license" src="https://img.shields.io/badge/license-MIT-blue"></a>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
<p align="center">
|
|
20
|
+
<a href="https://koopticon.github.io/whyflow/"><strong>Live demo</strong></a> ·
|
|
21
|
+
<a href="https://koopticon.github.io/whyflow/playground.html"><strong>Try it in your browser</strong></a> ·
|
|
22
|
+
<a href="#quickstart">Quickstart</a> ·
|
|
23
|
+
<a href="#what-it-checks">What it checks</a> ·
|
|
24
|
+
<a href="#how-it-compares">How it compares</a> ·
|
|
25
|
+
<a href="#examples">Examples</a> ·
|
|
26
|
+
<a href="#cite">Cite</a>
|
|
27
|
+
</p>
|
|
28
|
+
|
|
29
|
+
<p align="center">
|
|
30
|
+
<img alt="Clicking through a whyflow report: the workflow diagram, a recovery test's charts, and the sensitivity slider" src="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/hero.gif" width="820">
|
|
31
|
+
</p>
|
|
32
|
+
|
|
33
|
+
whyflow turns the statistical workflow Richard McElreath teaches in *Statistical Rethinking* into code you can
|
|
34
|
+
run and a report you can share. You write down how you think the data came about, what you want to know, and
|
|
35
|
+
how you'll estimate it, and every step has to say **why** it's there. Before you touch real data, whyflow
|
|
36
|
+
checks the estimator three ways, and the whole analysis becomes one page where each box in the diagram opens
|
|
37
|
+
the code behind it.
|
|
38
|
+
|
|
39
|
+
In his 2026 lectures McElreath said he knew of no tool that attaches each step's code to the workflow diagram,
|
|
40
|
+
and asked if anyone would build one. This is one answer.
|
|
41
|
+
|
|
42
|
+
<p align="center">
|
|
43
|
+
<img alt="The workflow: generative model, estimand, estimator, test before you invest (diagram check, recovery test, sensitivity), then real data" src="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/workflow.png" width="900">
|
|
44
|
+
</p>
|
|
45
|
+
|
|
46
|
+
**No install needed to try it:** the [playground](https://koopticon.github.io/whyflow/playground.html) runs
|
|
47
|
+
whyflow in your browser. Edit the estimator, press Run, and the report updates.
|
|
48
|
+
|
|
49
|
+
## Install
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install whyflow
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Python 3.11+. The examples also use `statsmodels`; the Bayesian one uses PyMC (`pip install "whyflow[bayes]"`).
|
|
56
|
+
|
|
57
|
+
## Quickstart
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
import statsmodels.formula.api as smf
|
|
61
|
+
|
|
62
|
+
from whyflow import DAG, Workflow
|
|
63
|
+
|
|
64
|
+
wf = Workflow("Total effect of age on weight")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@wf.step(kind="generative_model", why="Age makes children taller; age and height both make them heavier.")
|
|
68
|
+
def generative_model():
|
|
69
|
+
return DAG().add("age").add("height", causes={"age": 2.0}).add("weight", causes={"age": 0.5, "height": 0.4})
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
# A parameter named after a step receives that step's result.
|
|
73
|
+
@wf.step(kind="estimand", why="The TOTAL effect of age, including the part that works through height.")
|
|
74
|
+
def estimand(generative_model):
|
|
75
|
+
return {"cause": "age", "outcome": "weight", "true_value": generative_model.total_effect("age", "weight")}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@wf.step(kind="estimator", adjusts_for=[], why="Weight on age only: height is a mediator, so leave it out.")
|
|
79
|
+
def estimator():
|
|
80
|
+
def fit(data):
|
|
81
|
+
model = smf.ols("weight ~ age", data=data).fit()
|
|
82
|
+
low, high = model.conf_int().loc["age"]
|
|
83
|
+
return {"estimate": model.params["age"], "low": low, "high": high}
|
|
84
|
+
|
|
85
|
+
return fit
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
wf.add_adjustment_check() # does the causal diagram agree with what the estimator adjusts for?
|
|
89
|
+
wf.add_recovery_test() # 100 simulated datasets: does it find the true effect, 1.3?
|
|
90
|
+
wf.add_sensitivity_check() # how strong would an unmeasured confounder have to be to break it?
|
|
91
|
+
|
|
92
|
+
wf.run()
|
|
93
|
+
wf.report("report.html") # or, in a Jupyter notebook, end a cell with: wf
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Now change the estimator to `weight ~ age + height` with `adjusts_for=["height"]` and run it again. The
|
|
97
|
+
diagram check flags height as a mediator before any data exists, and the recovery test shows the cost: an
|
|
98
|
+
estimate of 0.5 instead of 1.3.
|
|
99
|
+
|
|
100
|
+
## What it checks
|
|
101
|
+
|
|
102
|
+
### Good and bad controls, from the diagram alone
|
|
103
|
+
|
|
104
|
+
Declare what the estimator adjusts for, and whyflow reads the causal diagram: mediators, colliders, things
|
|
105
|
+
caused by the outcome, back doors left open. It suggests the smallest sets that work, and says plainly when an
|
|
106
|
+
unmeasured variable means none does. The paths that carry the effect you want are green; bias leaking in moves
|
|
107
|
+
along its path in red.
|
|
108
|
+
|
|
109
|
+
<p align="center"><img alt="The haunted DAG: adjusting for G leaves the path through the unmeasured U open, drawn as moving red arrows" src="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/controls.gif" width="720"></p>
|
|
110
|
+
|
|
111
|
+
### Recovery on simulated data
|
|
112
|
+
|
|
113
|
+
100 datasets where the truth is known. Does the estimator find it, and do its ranges catch it as often as they
|
|
114
|
+
promise? The pass mark allows for the luck of 100 tries, so a good estimator doesn't fail by chance.
|
|
115
|
+
|
|
116
|
+
<p align="center"><img alt="Recovery test: where the 100 estimates landed, and every simulation's range in green or red" src="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/recovery.png" width="720"></p>
|
|
117
|
+
|
|
118
|
+
### Sensitivity: what would break it?
|
|
119
|
+
|
|
120
|
+
The diagram only holds the confounders you thought of. This step adds one you didn't, at growing strengths,
|
|
121
|
+
and finds the tipping point where the estimator stops finding the truth. Drag the slider in the report to see
|
|
122
|
+
each strength.
|
|
123
|
+
|
|
124
|
+
<p align="center"><img alt="Sensitivity analysis: the estimate drifting away from the truth as a hidden confounder gets stronger, with the tipping point marked" src="https://raw.githubusercontent.com/koopticon/whyflow/main/docs/sensitivity.png" width="720"></p>
|
|
125
|
+
|
|
126
|
+
And one rule ties it together: an `analysis` step whose estimator hasn't passed a test gets a warning, and a
|
|
127
|
+
⚠️ in the report. Test before you invest.
|
|
128
|
+
|
|
129
|
+
## How it compares
|
|
130
|
+
|
|
131
|
+
whyflow doesn't replace these tools; it sits between them. Draw your diagram in dagitty, borrow estimators
|
|
132
|
+
from DoWhy, run big pipelines in targets or Hamilton. whyflow is for keeping one analysis's reasoning, code
|
|
133
|
+
and tests together.
|
|
134
|
+
|
|
135
|
+
| | whyflow | [dagitty](https://www.dagitty.net) | [DoWhy](https://github.com/py-why/dowhy) | [estiplan](https://github.com/rsmeyerlorey/estiplan) | [targets](https://github.com/ropensci/targets) / [Hamilton](https://github.com/apache/hamilton) |
|
|
136
|
+
|---|:---:|:---:|:---:|:---:|:---:|
|
|
137
|
+
| Analysis drawn as a diagram of steps | ✅ | | | ✅ | ✅ |
|
|
138
|
+
| Each step's code attached to the diagram | ✅ | | | | partly |
|
|
139
|
+
| Every step must say why | ✅ | | | | |
|
|
140
|
+
| Adjustment sets from a causal DAG | ✅ | ✅ | ✅ | ✅ | |
|
|
141
|
+
| Estimator tested on simulated data with a known truth | ✅ | | partly | | |
|
|
142
|
+
| Sensitivity to an unmeasured confounder | ✅ | | ✅ | | |
|
|
143
|
+
| Built-in estimation methods | bring your own | | ✅ | brms code | |
|
|
144
|
+
| Caching, big pipelines | | | | | ✅ |
|
|
145
|
+
| Language | Python | web, R | Python | web app | R / Python |
|
|
146
|
+
|
|
147
|
+
## More
|
|
148
|
+
|
|
149
|
+
- **Bayesian estimators.** Fit with PyMC (or anything), then `summarize_samples(posterior)` gives the mean and
|
|
150
|
+
an 89% interval; the recovery test then expects 89% coverage.
|
|
151
|
+
- **Unmeasured variables.** `DAG().add("U", observed=False)` shapes the simulated data but is left out of it,
|
|
152
|
+
like in real life.
|
|
153
|
+
- **Jupyter.** End a cell with `wf` and the report appears inline.
|
|
154
|
+
- **Steps without boilerplate.** Name a parameter after a step to receive its result, or list them with
|
|
155
|
+
`needs=[...]` and name the parameters as you like.
|
|
156
|
+
|
|
157
|
+
## Examples
|
|
158
|
+
|
|
159
|
+
Every example also runs on the [live demo](https://koopticon.github.io/whyflow/), rebuilt on each change.
|
|
160
|
+
|
|
161
|
+
| Example | What it shows |
|
|
162
|
+
|---|---|
|
|
163
|
+
| [`example.py`](example.py) | The quickstart: all three checks pass; a hidden confounder of 0.3 would break it. |
|
|
164
|
+
| [`examples/mediator_mistake.py`](examples/mediator_mistake.py) | The most common mistake, "controlling for" a mediator. Both tests fail: 0.5 instead of 1.3. |
|
|
165
|
+
| [`examples/haunted_dag.py`](examples/haunted_dag.py) | McElreath's haunted DAG: an unmeasured confounder no adjustment can fix. |
|
|
166
|
+
| [`examples/howell_children.py`](examples/howell_children.py) | Real data, the !Kung San census from the book: about **1.34 kg per year** of age. |
|
|
167
|
+
| [`examples/howell_children_pymc.py`](examples/howell_children_pymc.py) | The same analysis with a Bayesian model in PyMC. |
|
|
168
|
+
|
|
169
|
+
The Howell data is downloaded from the [rethinking](https://github.com/rmcelreath/rethinking) repository when
|
|
170
|
+
an example runs; it isn't included here. `python demo/build.py` builds the demo site into `_site/`.
|
|
171
|
+
|
|
172
|
+
## Current limits
|
|
173
|
+
|
|
174
|
+
- Simulation covers continuous variables with straight-line relationships, and the target is a **total**
|
|
175
|
+
effect of one variable on another.
|
|
176
|
+
- The diagram check trusts what `adjusts_for` says; it can't read your model formula. The recovery test is what
|
|
177
|
+
catches an estimator that does something different from what it declares.
|
|
178
|
+
- The diagram check lists every path between cause and outcome: fine for a few dozen variables, slow for very
|
|
179
|
+
large diagrams.
|
|
180
|
+
- Every run re-runs every step (no caching yet).
|
|
181
|
+
|
|
182
|
+
## Cite
|
|
183
|
+
|
|
184
|
+
If whyflow helps your work, please cite it. GitHub's **Cite this repository** button (from
|
|
185
|
+
[`CITATION.cff`](CITATION.cff)) gives APA and BibTeX.
|
|
186
|
+
|
|
187
|
+
## Contributing and feedback
|
|
188
|
+
|
|
189
|
+
whyflow is young. If you try it, especially while working through Statistical Rethinking in Python, tell me
|
|
190
|
+
what worked and what didn't in an [issue](https://github.com/koopticon/whyflow/issues). To work on the code,
|
|
191
|
+
see [CONTRIBUTING.md](CONTRIBUTING.md). Changes are listed in the [changelog](CHANGELOG.md).
|
|
192
|
+
|
|
193
|
+
Inspired by Richard McElreath's *Statistical Rethinking* and its *estiplans*, and by
|
|
194
|
+
[YesWorkflow](https://github.com/yesworkflow-org/yw-prototypes), which draws workflow diagrams from comments in
|
|
195
|
+
scripts.
|
|
196
|
+
|
|
197
|
+
## License
|
|
198
|
+
|
|
199
|
+
[MIT](LICENSE)
|