Get Started

Ljung-Box test

Another powerful tool to check autocorrelations in the data is the Ljung-Box test. In this exercise, you will practice detecting autocorrelation in the standardized residuals by performing a Ljung-Box test.

The null hypothesis of Ljung-Box test is: the data is independently distributed. If the p-value is larger than the specified significance level, the null hypothesis cannot be rejected. In other words, there is no clear sign of autocorrelations and the model is valid.

You will use the same GARCH model as the previous exercise. Its standardized residuals are saved in std_resid.

This is a part of the course

“GARCH Models in Python”

View Course

Exercise instructions

  • Import the module needed for Ljung-Box tests from the statsmodels package.
  • Perform a Ljung-Box test up to lag 10, and save the result in lb_test.
  • Print and review p-values from the Ljung-Box test result.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import the Python module
from statsmodels.stats.diagnostic import ____

# Perform the Ljung-Box test
lb_test = ____(std_resid , ____ = ____, return_df = True)

# Print the p-values
print('P-values are: ', ____.iloc[0,1])

This exercise is part of the course

GARCH Models in Python

AdvancedSkill Level
4.8+
4 reviews

Learn about GARCH Models, how to implement them and calibrate them on financial data from stocks to foreign exchange.

This chapter introduces you to the KISS principle of data science modeling. You’ll learn how to use p-values and t-statistics to simplify model configuration, use ACF plot, Ljung-Box test to verify model assumptions and use likelihood and information criteria for model selection.

Exercise 1: Significance testing of model parametersExercise 2: Keep it simple stupidExercise 3: Simplify the model with p-valuesExercise 4: Simplify the model with t-statisticsExercise 5: Validation of GARCH model assumptionsExercise 6: Detect autocorrelationsExercise 7: ACF plotExercise 8: Ljung-Box test
Exercise 9: Goodness of fit measuresExercise 10: Goodness of fit basicsExercise 11: Pick a winner based on log-likelihoodExercise 12: Pick a winner based on AIC/BICExercise 13: GARCH model backtestingExercise 14: Backtesting basicsExercise 15: Backtesting with MAE, MSE

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free