Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Section 5.4 — Bayesian difference between means

Notebook setup
Note: you may need to restart the kernel to use updated packages.
<Figure size 640x480 with 0 Axes>
datasets/ directory present and ready.

Introduciton

We’ll now revisit the problem of comparison between two groups from a Bayesian perspective.

Model

The data models for the two groups are based on Student’s tt-distribution:

X∼T(ν,MX,ΣX)andY∼T(ν,MY,ΣY).X \sim \mathcal{T}(\nu, M_X, \Sigma_X) \qquad \text{and} \qquad Y \sim \mathcal{T}(\nu, M_Y, \Sigma_Y).

We’ll use a normal prior for the means, half-tt priors for standard deviations, and a gamma distribution for the degrees of freedom parameter:

MX,MY∼N(μM,σM),ΣX,ΣY∼T+ ⁣(νΣ,σΣ),ν∼Gamma(α,β).M_X, M_Y \sim \mathcal{N}(\mu_{M}, \sigma_{M}), \quad \Sigma_X, \Sigma_Y \sim \mathcal{T}^+\!(\nu_{\Sigma}, \sigma_{\Sigma}), \quad \nu \sim \textrm{Gamma}(\alpha,\beta).

Bambi formula objects

Formula('y ~ 0 + group', 'sigma ~ 0 + group')
('y ~ 0 + group', ('sigma ~ 0 + group',))
['y ~ 0 + group', 'sigma ~ 0 + group']

Choosing priors for log-sigma

Normal distribution for log-sigma
<Figure size 600x220 with 2 Axes>

Choosing priors for the degrees of freedom parameter

<Figure size 600x220 with 2 Axes>

Example 1: comparing electricity prices

Electricity prices from East End and West End

Electricity prices dataset

Loading...
(7.655555555555556, 1.973120020267162)

Bayesian model

XW∼T(ν,MW,ΣW),XE∼T(ν,ME,ΣE),MW,ME ⁣∼ ⁣N(μM,σM),log⁡ΣW,log⁡ΣE ⁣∼ ⁣N(0,1),ν ⁣∼ ⁣Gamma(2,0.1).\begin{align*} X_W \sim \mathcal{T}(\nu, M_W, \Sigma_W), \qquad \qquad X_E \sim \mathcal{T}(\nu, M_E, \Sigma_E), \qquad\qquad \\ M_W, M_E \!\sim\! \mathcal{N}(\mu_{M}, \sigma_{M}), \quad \log\Sigma_W, \log\Sigma_E\!\sim\!\mathcal{N}(0,1), \quad \nu \!\sim\! \text{Gamma}(2,0.1). \end{align*}

Bambi model

Formula: price ~ 0 + loc sigma ~ 0 + loc Family: t Link: mu = identity sigma = log Observations: 18 Priors: target = mu Common-level effects loc ~ Normal(mu: 8.0, sigma: 5.0) Auxiliary parameters nu ~ Gamma(alpha: 2.0, beta: 0.1) target = sigma Common-level effects sigma_loc ~ Normal(mu: 0.0, sigma: 1.0)
Loading...

Prior predictive checks

Model fitting and analysis

Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [nu, loc, sigma_loc]
Loading...
Loading...
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 1 seconds.

Processing the results

Calculate derived quantities used for the analysis plots and summaries.

Loading...
<Figure size 800x1000 with 10 Axes>

Compare to frequentist results

(5.022875513276464, 0.00025703383372176116)
ConfidenceInterval(low=1.9396575883681466, high=4.060342411631854)
2.3678062243290996

Conclusions

Example 2: comparing IQ scores

We’ll look at IQ scores data taken from a the paper Bayesian Estimation Supersedes the t-Test (BEST) by John K. Kruschke.

smart drug administered to treatment group and want to compare to control group. Data contains outliers)

cf. compare_iqs2_many_ways.ipynb

IQ scores dataset

Loading...
<Figure size 500x300 with 1 Axes>

Bayesian model

TODO: add formulas

Bambi model

Formula: iq ~ 0 + group sigma ~ 0 + group Family: t Link: mu = identity sigma = log Observations: 89 Priors: target = mu Common-level effects group ~ Normal(mu: 100.0, sigma: 35.0) Auxiliary parameters nu ~ Gamma(alpha: 2.0, beta: 0.1) target = sigma Common-level effects sigma_group ~ Normal(mu: 1.0, sigma: 2.0)
Loading...

Model fitting and analysis

Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [nu, group, sigma_group]
Loading...
Loading...
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 1 seconds.
Loading...
<Figure size 800x1000 with 10 Axes>

Sensitivity analysis

Loading...

Compare to frequentist results

(1.622190457290228, 0.10975381983712836)
ConfidenceInterval(low=-0.36118477236636837, high=3.4766862921232016)
0.33096545301397307

Conclusions

Explanations

Alternative choices of priors

e.g. prior for sigma as very wiiiiiide, and nu ~ Exp(1/29)+1 → BEST Bayesian estimation supersedes the t-test by John K. Kruschke

If we use a model with common standard deviation, we get equivalent of pooled sigma

Performance tests

Simulated datasets

Simulate datasets with various combinations of nn, Δ\Delta, and outliers.

[{'n': 20, 'Delta': 0, 'outliers': 'no', 'random_seed': 45}, {'n': 20, 'Delta': 0, 'outliers': 'few', 'random_seed': 46}, {'n': 20, 'Delta': 0, 'outliers': 'lots', 'random_seed': 47}, {'n': 20, 'Delta': 0.2, 'outliers': 'no', 'random_seed': 48}]

Model comparisons

Compare results several models:

  • Classical permutation test (from Section 4.5)

  • Classical Welch’s two-sample t-test (from Section 4.5)

  • Bayesian model with N\mathcal{N} data model

  • Robust Bayesian model with T\mathcal{T} data model

  • Bayes factors with JZS prior and standard cutoff

Help on function fit_dmeans_models in module ministats.book.tables:

fit_dmeans_models(dataset, random_seed=42)
    Fit the following models for the difference of two means:
    - Permutation test from Section 3.5
    - Welch's two-sample $t$-test from Section 3.5
    - Bayesian model that uses normal as data model
    - Robust Bayesian model that uses t-distribution as data model
    - Bayes factor using JZS prior (using `pingouin` library)
    For each model, we run the hypothesis test to decide if the populations
    are the same or different based on the conventional cutoff level of 5%.
    We also construct a 90% interval estimates for the unknown `Delta`.

Results

simdata/ directory present and ready.
Loading...

Type I (false positive) error rates

Loading...

Observations:

  • all models are about the same

  • BF model has significantly less false positives

Power analysis

Loading...

Observations:

  • Bayes T\mathcal{T} has higher power than Bayes N\mathcal{N}

  • Bayes T\mathcal{T} outperforms all other models when outliers are present

Interval estimates coverage

Loading...

Observations:

  • All methods have coverage close to the nominal 90%

  • Bayes T\mathcal{T} produces narrower intervals

Discussion

Comparison to the frequentist two-sample t-test

  • results numerically similar

  • note we’re using 𝒯 as data model, not as a sampling distribution

  • conceptually different:

    • p-value vs. decision based on posterior distribution

    • confidence intervals vs. credible intervals

Comparing multiple groups

  • ?Extension to Bayesian ANOVA? Can extend approach to multiple groups: Bayesian ANOVA

FWD reference to hierarchical models for group comparison covered in Section 5.5

Exercises

Exercise 1: small samples

Exercise 2: lecture and debate curriculums

Formula: score ~ 0 + curriculum sigma ~ 0 + curriculum Family: t Link: mu = identity sigma = log Observations: 15 Priors: target = mu Common-level effects curriculum ~ Normal(mu: 70.0, sigma: 30.0) Auxiliary parameters nu ~ Gamma(alpha: 2.0, beta: 0.1) target = sigma Common-level effects sigma_curriculum ~ Normal(mu: 1.0, sigma: 2.0)
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [nu, curriculum, sigma_curriculum]
Loading...
Loading...
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 1 seconds.
Loading...

Exercise 3: redo exercises from Section 3.5 section using Bayesian methods

TODO

BONUS Examples

Example 4: small example form BEST vignette

See http://cran.nexr.com/web/packages/BEST/vignettes/BEST.pdf#page=2

Example 5: comparing morning to evening

examples/paired_samples.py