SKILL.md
Bayesian Statistics Guide
A skill for applying Bayesian statistical methods to research data analysis. Covers prior specification, Markov chain Monte Carlo (MCMC) sampling, posterior interpretation, model comparison, and reporting standards.
Bayesian Framework Overview
Bayes' Theorem in Practice
Posterior = (Likelihood x Prior) / Evidence
P(theta | data) = P(data | theta) * P(theta) / P(data)
In practice:
P(theta | data) is proportional to P(data | theta) * P(theta)
(the denominator is a normalizing constant)
When to Use Bayesian Methods
| Scenario | Bayesian Advantage |
|---|---|
| Small sample sizes | Priors regularize estimates |
| Complex hierarchical models | Natural framework for multilevel data |
| Sequential data collection | Update beliefs as data arrives |
| Prior knowledge available | Formally incorporate existing evidence |
| Model comparison | Bayes factors and posterior model probabilities |
| Prediction | Full posterior predictive distributions |
Prior Specification
Types of Priors
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
def visualize_priors(parameter_name: str, prior_type: str = 'weakly_informative'):
"""
Visualize common prior choices for a parameter.
"""
x = np.linspace(-10, 10, 1000)
priors = {
'flat': {
'dist': stats.uniform(loc=-100, scale=200),
'description': 'Flat/Uniform: minimal prior info (often improper)',
'recommendation': 'Avoid -- can lead to improper posteriors'
},
'weakly_informative': {
'dist': stats.norm(loc=0, scale=2.5),
'description': 'Weakly informative: Normal(0, 2.5)',
'recommendation': 'Good default for regression coefficients'
},
'informative': {
'dist': stats.norm(loc=0.5, scale=0.2),
'description': 'Informative: based on previous studies',
'recommendation': 'Use when strong prior evidence exists'
},
'horseshoe': {
'dist': stats.cauchy(loc=0, scale=1),
'description': 'Horseshoe-like (Cauchy): sparsity-inducing',
'recommendation': 'Good for variable selection problems'
}
}
prior = priors.get(prior_type, priors['weakly_informative'])
return prior
# Recommended default priors (Gelman et al., 2008):
# Intercept: Normal(0, 10)
# Coefficients: Normal(0, 2.5) on standardized predictors
# Standard deviation: Half-Cauchy(0, 2.5) or Exponential(1)
# Correlation: LKJ(2) for correlation matrices
