Theory-based inference

Alongside the simulation grammar, moderndive ships tidy wrappers for the classical (formula-based) tests and a way to draw the theoretical distributions — mirroring R infer’s t_test, prop_test, chisq_test, and assume().

One-line tests

import moderndive as md
from moderndive import t_test, prop_test, chisq_test

# One-sample t-test
age = md.load_age_at_marriage()
t_test(age, response="age", mu=23)
# → statistic, t_df, p_value, alternative, estimate, lower_ci, upper_ci

# Two-sample (Welch) t-test
movies = md.load_movies_sample()
t_test(movies, formula="rating ~ genre", order=("Action", "Romance"))

# Two-proportion z-test
yawn = md.load_mythbusters_yawn()
prop_test(yawn, formula="yawn ~ group", success="yes", order=("seed", "control"))

# Chi-squared test of independence
chisq_test(yawn, formula="yawn ~ group")
# → statistic, chisq_df, p_value
shape: (1, 3)
statisticchisq_dfp_value
f64i64f64
0.10504210.745861

t_stat and chisq_stat return just the test statistic if that’s all you need.

Theoretical distributions with assume()

assume() defines a theoretical sampling distribution you can visualize and use for p-values without simulating:

from moderndive import assume

# t-distribution with 10 degrees of freedom
t_dist = assume("t", df=10)
t_dist.get_p_value(2.0, direction="right")     # one-sided p-value

# plotly by default; engine="plotnine" for ggplot output
t_dist.visualize()
t_dist.visualize(engine="plotnine")

Supported distributions: "t", "z", "F" (pass df=(df1, df2)), and "Chisq".

Overlaying theory on a simulation

visualize(..., method=...) can show the simulation histogram, the theoretical curve, or both:

from moderndive import visualize

boot = (
    age.specify(response="age")
    .generate(reps=1000, type="bootstrap", seed=1)
    .calculate(stat="mean")
)

visualize(boot, method="both")          # histogram + normal-approximation curve
../_images/e5087a5c5e55c1dde4180291b98223048efa4a39a4ab8b15c03f09b4d79d8a12.png
visualize(boot, method="theoretical")   # just the curve
../_images/639aa9ec9e155e3fd81f1daedcadbad32246ccbb967fba5bad23241f70a6b9b6.png

Population standard deviation

A small helper that divides by n (not n 1):

from moderndive import pop_sd

pop_sd([1, 2, 3, 4, 5])   # → 1.414…
1.4142135623730951