14.4 C
London
Monday, July 7, 2025
HomePythonHypothesis Tests in PythonHow to Perform a Ljung-Box Test in Python

How to Perform a Ljung-Box Test in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The Ljung-Box test is a statistical test that checks if autocorrelation exists in a time series.

It uses the following hypotheses:

H0: The residuals are independently distributed.

HA: The residuals are not independently distributed; they exhibit serial correlation.

Ideally, we would like to fail to reject the null hypothesis. That is, we would like to see the p-value of the test be greater than 0.05 because this means the residuals for our time series model are independent, which is often an assumption we make when creating a model.

This tutorial explains how to perform a Ljung-Box test in Python.

Example: Ljung-Box Test in Python

To perform the Ljung-Box test on a data series in Python, we can use the acorr_ljungbox() function from the statsmodels library which uses the following syntax:

acorr_ljungbox(x, lags=None)

where:

  • x: The data series
  • lags: Number of lags to test

This function returns a test statistic and a corresponding p-value. If the p-value is less than some threshold (e.g. α = .05), you can reject the null hypothesis and conclude that the residuals are not independently distributed.

The following code shows how to use this function to perform the Ljung-Box test on the built-in statsmodels dataset called “SUNACTIVITY”:

import statsmodels.api as sm

#load data series
data = sm.datasets.sunspots.load_pandas().data

#view first ten rows of data series 
data[:5]

YEAR	SUNACTIVITY
0	1700.0	5.0
1	1701.0	11.0
2	1702.0	16.0
3	1703.0	23.0
4	1704.0	36.0

#fit ARMA model to dataset
res = sm.tsa.ARMA(data["SUNACTIVITY"], (1,1)).fit(disp=-1)

#perform Ljung-Box test on residuals with lag=5
sm.stats.acorr_ljungbox(res.resid, lags=[5], return_df=True)

          lb_stat	lb_pvalue
5	107.86488	1.157710e-21

The test statistic of the test is 107.86488 and the p-value of the test is 1.157710e-21, which is much less than 0.05. Thus, we reject the null hypothesis of the test and conclude that the residuals are not independent.

Note that we chose to use a lag value of 5 in this example, but you can choose any value that you would like to use for the lag. For example, we could instead use a value of 20:

#perform Ljung-Box test on residuals with lag=20
sm.stats.acorr_ljungbox(res.resid, lags=[20], return_df=True)

           lb_stat	lb_pvalue
20	343.634016	9.117477e-61

The test statistic of the test is 343.634016 and the p-value of the test is 9.117477e-61, which is much less than 0.05. Thus, we reject the null hypothesis of the test once again and conclude that the residuals are not independent.

Depending on your particular situation you may choose a lower or higher value to use for the lag. 

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories