Python companion for ModernDive: a tidy simulation-inference grammar, regression helpers, datasets, and dual-engine plotly/plotnine plots
Project description
moderndive (Python)
The Python companion package for ModernDive: Statistical Inference via Data Science — a faithful port of the R moderndive and infer packages to a modern Python data-science stack (polars, plotly, plotnine, statsmodels).
📖 Documentation (with runnable examples): https://moderndive.readthedocs.io
It is intentionally pure-Python (no compiled extensions) so it installs under Pyodide via micropip for in-browser execution.
Installation
pip install moderndive # from PyPI (once published)
# or, from source:
pip install git+https://github.com/moderndive/moderndive-python
What’s inside
- A tidy simulation-inference grammar mirroring R
infer:specify → hypothesize → generate → calculate, plusfit()for multiple regression,observe(), andassume()(theoretical t/z/F/Chisq).specify()is also available as a DataFrame method, so you can writedf.specify(...)just like R’sdf %>% specify(...).calculate(stat=...)takes the full infer vocabulary or any custom callable test statistic. Summaries viaget_p_value/get_confidence_interval(percentile, SE, bias-corrected); British-spelling and short aliases included. - Dual-engine plots:
visualize/shade_p_value/shade_confidence_interval(and every plot helper) takeengine="plotly"(default, interactive) orengine="plotnine"— same code, your choice of output. - Theory-based wrapper tests:
t_test,prop_test,chisq_test,t_stat,chisq_stat, plus themoderndive.theorymodule. - Regression & summary helpers mirroring R
moderndive:get_regression_table,get_regression_points,get_regression_summaries,get_correlation,pop_sd,tidy_summary,count_missing(built onstatsmodelswhere relevant, returningpolarsframes), plus the model plotsgg_parallel_slopes/geom_parallel_slopesandgg_categorical_model/geom_categorical_model, andpairplot(theGGally::ggpairsanalog). - Sampling:
rep_slice_sample/rep_sample_nfor sampling-distribution activities. - 58 datasets:
load_*()loaders returningpolarsDataFrames (themoderndive/infer,nycflights23,gapminder, ISLR2, and FiveThirtyEight datasets used in the book).
Quick start
Are tracks more likely to be popular in metal than in deep house? Compute the observed difference in “popular” rates, then permute the genre labels 1000 times to build a null distribution and read off a p-value.
import moderndive as md
from moderndive import get_p_value, visualize, shade_p_value
spotify = md.load_spotify_metal_deephouse()
# Observed difference in popularity rates (metal − deep house)
obs = (
spotify
.specify(formula="popular_or_not ~ track_genre", success="popular")
.calculate(stat="diff in props", order=("metal", "deep-house"))
)
obs
ObservedStatistic(stat='diff in props', value=0.034)
# Permutation null distribution + p-value
null = (
spotify
.specify(formula="popular_or_not ~ track_genre", success="popular")
.hypothesize(null="independence")
.generate(reps=1000, type="permute", seed=76)
.calculate(stat="diff in props", order=("metal", "deep-house"))
)
print(get_p_value(null, obs_stat=obs, direction="right"))
shape: (1, 1)
┌─────────┐
│ p_value │
│ --- │
│ f64 │
╞═════════╡
│ 0.075 │
└─────────┘
# Visualize — interactive plotly by default; engine="plotnine" for ggplot-style
visualize(null) + shade_p_value(obs_stat=obs, direction="right")
Development
This repo uses uv.
uv sync --extra dev # create the environment
make test # run the test suite (enforces 100% coverage)
make readme # re-render README.md from README.qmd (needs Quarto)
make build-data # rebuild the bundled Parquet datasets (needs R; see tools/)
make build # build the wheel/sdist
The test suite is held at 100% statement coverage (enforced in CI via --cov-fail-under=100). Releases are automated on v* tags — see RELEASING.md.
License
MIT. The ModernDive book content is licensed CC-BY-NC-SA 4.0; this software package is MIT-licensed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file moderndive-0.1.0.tar.gz.
File metadata
- Download URL: moderndive-0.1.0.tar.gz
- Upload date:
- Size: 9.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
727029eb5cbdcf5501bf012f7e7230f9f2dcf747abbc22c09b4ed58557683aa4
|
|
| MD5 |
ab633120b92c010f000c56eb445d9b1a
|
|
| BLAKE2b-256 |
7766304b7dc656a4ad2a6177bdfbe70790e252c0fd150f88a30e53ca687b5602
|
Provenance
The following attestation bundles were made for moderndive-0.1.0.tar.gz:
Publisher:
release.yml on moderndive/moderndive-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
moderndive-0.1.0.tar.gz -
Subject digest:
727029eb5cbdcf5501bf012f7e7230f9f2dcf747abbc22c09b4ed58557683aa4 - Sigstore transparency entry: 1888479872
- Sigstore integration time:
-
Permalink:
moderndive/moderndive-python@68edd77d3163627fb236a78a4a1676d786bdd252 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/moderndive
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@68edd77d3163627fb236a78a4a1676d786bdd252 -
Trigger Event:
push
-
Statement type:
File details
Details for the file moderndive-0.1.0-py3-none-any.whl.
File metadata
- Download URL: moderndive-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.7 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10f15fbed9a3bf4c2f61cc4332a5f0da9d9705cb1f9d145483a4f1eb9769dc93
|
|
| MD5 |
202ec537097d6ad283ce858461079283
|
|
| BLAKE2b-256 |
fd21fed078b920d478aad34e61197658d5e0a2e3a84f02ee7d49fdeb46ff43f9
|
Provenance
The following attestation bundles were made for moderndive-0.1.0-py3-none-any.whl:
Publisher:
release.yml on moderndive/moderndive-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
moderndive-0.1.0-py3-none-any.whl -
Subject digest:
10f15fbed9a3bf4c2f61cc4332a5f0da9d9705cb1f9d145483a4f1eb9769dc93 - Sigstore transparency entry: 1888479955
- Sigstore integration time:
-
Permalink:
moderndive/moderndive-python@68edd77d3163627fb236a78a4a1676d786bdd252 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/moderndive
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@68edd77d3163627fb236a78a4a1676d786bdd252 -
Trigger Event:
push
-
Statement type: