The White Noise Model
By: Sachin Date
Tags: Noise, Programming, Python, Data Science, Time Series Analysis

Example: White noise detection using Python

Let’s illustrate the above procedure using a real world time series of 5000 decibel level measurements taken at a restaurant using the Google Science Journal app.

The data set can be downloaded from here.

We’ll use the pandas library to load the data set from the csv file and plot it:

import pandas as pd

import numpy as np

from matplotlib import pyplot as plt df = pd.read_csv('restaurant_decibel_level.csv', header=0, index_col=[0])

Let’s print the top 10 rows:

df.head(10) Decibel

TimeIndex

0 55.931323

40 57.779260

80 62.956952

140 65.158100

180 60.325242

220 45.411725

262 55.958807

300 62.021807

340 62.222563

380 56.156684

Let’s plot all 5000 values in the series:

Decibel level at a restaurant (Image by Author)

Let’s fetch and plot the auto-correlation coefficients for the first 40 lags. We’ll the statsmodels library to do that.

import statsmodels.graphics.tsaplots as tsa tsa.plot_acf(df['Decibel'], lags=40, alpha=0.05, title='Auto-correlation coefficients for lags 1 through 40')

The alpha=0.05 tells statsmodels to also plot the 95% confidence interval region. We get the following plot:

Auto-correlation plot for the decibel level time series (Image by Author)

As we can see, the time series contains significant auto-correlations up through lags 17. Incidentally, the auto-correlation at lag 0 is always 1.0 as a value is always perfectly correlated with itself.

There is wave-like pattern in the auto-correlation plot that indicates that there could be some seasonality contained in the data. We can try to identify and isolate the seasonality by decomposing the time series into the trend, seasonality and noise components.

For now we’ll focus on the noise portion. The bottom line is that this time series, in its current form, does not appear to be pure white noise.

Next, we’ll two more tests on the time series to confirm this.

The Chi-squared test for white noise detection

The Chi-squared test is based on this powerful result in statistics: the sum of squares of k identical standard normal random variables is a Chi-squared distributed random variable with k degrees of freedom.

Chi-squared distribution with 1 through 9 degrees of freedom. Source: Wikimedia under CC BY 3.0

The actual test is called Box-Pierce test and it’s test statistic is called the Q statistic. Its formula is as follows:

Box-Pierce test statistic (Image by Author)

It can be shown that if the underlying data set is white noise, the expected value of the Q statistic is zero.

For any given time series, one can check if the value of Q deviates from zero in a statistically significant way looking up the p-value of the test statistic in the Chi-square tables for k degrees of freedom. Usually, a p-value of less than 0.05 indicates a significant auto-correlation that cannot be attributed to chance.

The Ljung-Box test for white noise detection

The Ljung-Box test improves upon the Box-Pierce test to obtain a test statistic having a distribution that is closer to the Chi-square distribution than the Q statistic. The test statistic of the Ljung-Box test is calculated as follows, and it is also Chi-square(k) distributed:

Ljung-Box test statistic (Image by Author)

Here, n is the number of data points in the time series and k is the number of time lags to be considered. As with the Box-Pierce test, if the underlying data set is white noise, the expected value of this Chi-square distributed random variable is zero. Again, a p-value of less than 0.05 indicates a significant auto-correlation that cannot be attributed to chance.

Example: Testing for white noise using the Ljung-Box test in Python

Let’s run the Ljung-Box test on the restaurant decibel level data set. We will test upto 40 lags and we’ll ask the test to also run the Box-Pierce test.

import statsmodels.stats.diagnostic as diag

diag.acorr_ljungbox(df['Decibel'], lags=[40], boxpierce=True, model_df=0, period=None, return_df=None)

We get the following output:

(array([13172.80554476]), array([0.]), array([13156.42074648]), array([0.]))

The value 13172.80554476 is the value of the test statistic for the Ljung-Box test and 0.0 is its p-value as per the Chi-square(k=40) table.

The value 13156.42074648 is the test statistic of the Box-Pierce test and 0.0 is its p-value as per the Chi-square(k=40) tables.

As we can see, both p-values are less than 0.01 and so we can say with 99% confidence that the restaurant decibel level time series is not pure white noise.