This project implements a Bayesian hierarchical model for estimating baseball batting averages, based on the Efron-Morris methodology. The goal is to address the challenge of small sample sizes by applying shrinkage estimation—pulling extreme observations toward the population mean.
The model uses a Beta-Binomial hierarchical structure:
y_i ~ Binomial(K_i, theta_i)— observed hits given at-batstheta_i ~ Beta(mu * kappa, (1 - mu) * kappa)— player abilitymu ~ Beta(2, 8)— league average priorkappa ~ Gamma(1, 0.1)— concentration parameter
The concentration parameter kappa controls shrinkage strength: larger values mean more shrinkage toward the population mean.
For the Stan implementation with full Bayesian inference, see stan_model/README.md.
For a Python-native implementation using PyMC, see efronmorris_cleaned.ipynb.
Note: The notebook uses ArviZ for visualization of prior/posterior distributions and credible intervals.
The model successfully estimates all parameters:
- Posterior
mu(league average): ~0.269 - Posterior
kappa(concentration): ~24.8
The increase in kappa from prior (~10) to posterior (~25) indicates strong shrinkage—players with extreme batting averages are pulled toward the population mean, with more shrinkage applied to players with smaller sample sizes.
This part of the project is made up of 3 files
- Final_AVG_Model.ipynb - This is the final iteration of our modelling process. The script:
- Calls data from pybaseball
- Constructs and filters the dataset into a training and validation set
- Runs the PyMC model
- Analyzes prior and posterior distributions
- Explores validation using 2025 averages
- Plots visualizations and investigates model performance metrics
- Conducts residual analysis of model predictions
-
2026_Predictive_Model.ipynb - is a modified version of Final_AVG_Model.ipynb. Some unnecessary components were trimmed out. It also contains an additional function "get_AVG(player_name)" which pulls any available player from the dataset and prints their predicted 2026 average. It also contains an analysis of the players with the greatest estimated change in average between 2025 and 2026.
-
FeatureSelection_LOOCV.v2.ipynb - is the final version of our Leave One Out Cross Validation for feature selection. The script:
- Calls data from pybaseball
- Constructs and filters the dataset into a training and validation set
- Checks the correlation between all features of interest
- Defines a large sample of different feature combinations
- Runs the model with each different set of features
- Prints a table and plot indicating which feature sets performed the best using az.compare()