A Python utility for Bayesian model selection in cosmological MCMC analyses. It reads the likelihood statistics produced by getLikeStats() (e.g., from CosmoMC/Cobaya pipelines), computes the Akaike Information Criterion (AIC) and the Bayesian Information Criterion (BIC), and interprets the results using two established statistical scales.
When comparing competing cosmological models (e.g., ΛCDM vs. a model with extra parameters), maximising the likelihood alone always favours the more complex model. Information criteria penalise this complexity by adding a term proportional to the number of free parameters k, thereby implementing Occam's razor statistically.
where:
-
$k$ — number of free parameters in the model -
$\hat{L}$ — maximum likelihood of the model
The penalty term
where:
-
$N$ — total number of data points -
$k$ — number of free parameters -
$\hat{L}$ — maximum likelihood of the model
The penalty term
Note on the input value.
getLikeStats()reports-log(Like)=$-\ln\hat{L}$ . The code recovers$\ln\hat{L} = -(-\ln\hat{L})$ before substituting into the formulae.
The model with the lower AIC is preferred. Its competitors are ranked by
| Interpretation | |
|---|---|
| Substantial support | |
| Less support (intermediate) | |
| Considerably less support | |
| Marginal support | |
| Essentially no support |
| Strength of evidence | |
|---|---|
| Weak / Inconclusive | |
| 2-6 | Positive |
| 6-10 | Strong |
| Very strong (decisive) |
- Python ≥ 3.7
- NumPy
Install dependencies with:
pip install numpyThe script expects a plain-text statistics file containing a line of the form:
Best fit sample -log(Like) = <value>
This is the standard output of getLikeStats() in CosmoMC/Cobaya. Example:
Best fit sample -log(Like) = 1328.142200
The parser locates this line with str.startswith() and splits on = to extract the numerical value.
Edit the __main__ block in AIC_&_BIC.py to point to your statistics files:
N_data_points = 2509 # Total number of data points
model_1 = {
'name': 'LCDM',
'path': '/path/to/LCDM/Like_statistics.txt',
'k': 6 # Number of free parameters
}
model_2 = {
'name': 'MyModel',
'path': '/path/to/MyModel/Like_statistics.txt',
'k': 10
}
output_file = '/path/to/output/AIC_BIC_results.txt'
perform_model_comparison(model_1, model_2, N_data_points, output_file)Then run:
python "AIC_&_BIC.py"The script prints results to the terminal and writes them to output_file. A typical output looks like:
==========================================================
MODEL COMPARISON ANALYSIS
==========================================================
Total number of data points (N): 2509
--- TABULAR RESULTS ---
Model | k | Max ln(L) | AIC | BIC
-----------------------------------------------------------------------
LCDM | 6 | -1328.14 | 2668.28 | 2703.77
TPM | 10 | -1320.05 | 2660.10 | 2719.07
--- DIFFERENCES (Model 2 - Model 1) ---
Delta AIC: 8.18
Delta BIC: 15.30
--- CONCLUSIONS ---
AIC Analysis (Burnham & Anderson):
-> Model 'LCDM' has ESSENTIALLY NO support relative to the best model 'TPM' (Δ_AIC = 8.18)
BIC Analysis (Jeffreys' Scale):
-> Very strong (decisive) evidence favoring LCDM over TPM (|Delta BIC| = 15.30)
==========================================================
This example illustrates a typical tension between AIC and BIC: AIC rewards the extra likelihood gained by the more complex model, while BIC's stronger complexity penalty favours the simpler one.
This project is licensed under the MIT License — see the LICENSE file for details.