How to Calculate the Bayesian Information Criterion (BIC) in Python

Spread the love

The Bayesian Information Criterion (BIC) is a criterion for model selection among a finite set of models. It’s based on the likelihood function, where it’s derived from a Bayesian point of view. The model with the lowest BIC is preferred. In this article, we will take a comprehensive look at how to calculate the BIC in Python. We will break it down into easy-to-understand steps and also explain the theory behind it.

Part 1: Understanding the Bayesian Information Criterion (BIC)

Before we dive into the computation of BIC in Python, it is important to understand what BIC is and why it is used.

The BIC is a type of model selection criterion, which aims to balance the goodness-of-fit of a model with its complexity. In other words, it discourages overfitting by penalizing models with a high number of parameters. The formula for BIC is:

BIC = ln(n) * k - 2 * ln(L)


  • n is the number of observations.
  • k is the number of parameters estimated by the model.
  • L is the maximized value of the likelihood function for the estimated model.

The model with the lowest BIC is usually considered the best.

Part 2: Computing BIC in Python

Now that we understand what the BIC is, let’s take a look at how we can calculate it using Python. We will use a dataset and fit a model to the data using the Ordinary Least Squares (OLS) regression, and then calculate the BIC for this model.

Step 1: Import Necessary Libraries

First, we need to import the necessary libraries for our task.

import numpy as np
import pandas as pd
import statsmodels.api as sm

Step 2: Load and Preprocess Data

Next, we will load and preprocess our data. The exact steps will depend on your specific dataset and the problem you’re trying to solve. Here, let’s assume that we have a dataset ‘data.csv’ with two independent variables ‘X1’ and ‘X2’ and one dependent variable ‘Y’. We load it using pandas and split the independent and dependent variables.

# Load the dataset
data = pd.read_csv('data.csv')

# Split independent and dependent variables
X = data[['X1', 'X2']]
Y = data['Y']

Step 3: Create and Fit the Model

We will create an OLS model with ‘Y’ as the dependent variable and ‘X1’ and ‘X2’ as the independent variables. We add a constant to the independent variables and fit the model.

# Add constant to independent variables
X = sm.add_constant(X)

# Create the model
model = sm.OLS(Y, X)

# Fit the model
results =

Step 4: Calculate BIC

Statsmodels provides a direct way to compute BIC using the fitted model. The .bic attribute of the result object from the fitted model gives the BIC value.

bic = results.bic
print('BIC: ', bic)

If you want to calculate BIC manually, you can use the formula mentioned earlier. First, you get the number of observations (n), the number of parameters (k), and the maximized log-likelihood (LL).

n = len(Y)
k = len(results.params)
LL = results.llf

# Calculate BIC
bic_manual = np.log(n) * k - 2 * LL
print('Manually computed BIC: ', bic_manual)

The value obtained from the manual calculation should match the one given by the results.bic command.


In this guide, we learned how to compute the Bayesian Information Criterion (BIC) in Python, a criterion used for model selection. Using Python’s statsmodels library, we can calculate BIC directly from a fitted model. By comparing the BIC values of different models, we can select the model that provides the best fit to the data while penalizing those with more parameters to avoid overfitting.

Remember, while the BIC is a powerful tool, it’s just one among many. Other criteria such as the Akaike Information Criterion (AIC) or cross-validation methods may also be suitable depending on your particular situation. Always consider the assumptions and conditions under which you’re working to choose the most appropriate method for your analysis. Python and its extensive scientific computing libraries provide the tools you need to perform these evaluations and build robust statistical models.

Leave a Reply