How to do portmanteau test with python

Introduction

Describe how to perform portmanteau test in python

What is a portmanteau test?

A method of testing whether there is a correlation in a series of correlation functions.

For more information, see [wikipedia](https://en.wikipedia.org/wiki/%E3%81%8B%E3%81%B0%E3%82%93%E6%A4%9C%E5% AE% 9A)

Functions used in the portmanteau test

For example, when performing the Ljung-Box test statsmodels.stats.diagnostic.acorr_ljungbox Is used. Click here for details (https://www.statsmodels.org/stable/generated/statsmodels.stats.diagnostic.acorr_ljungbox.html)

Example 1

For example, the test is performed using randomly generated noise (white Gaussian noise). Of course, there should be no correlation, so the null hypothesis should not be rejected.

import matplotlib as mpl
import matplotlib.pyplot as plt
plt.style.use('seaborn')
mpl.rcParams['font.family'] = 'serif'
%matplotlib inline
import numpy as np
from statsmodels.stats.diagnostic import acorr_ljungbox
p = print

#The data points are 1000 points
np.random.seed(42)
data = np.random.standard_normal(1000)

#First plot the data
plt.figure(figsize=(10,6))
plt.plot(data,lw = 1.5)
plt.xlabel('time')
plt.ylabel('value')
plt.xlim([0,100])
plt.title('time vs. value plot');

Naturally, the time series data of white Gaussian noise is plotted. img.png

Now, let's do a portmanteau test.

result = acorr_ljungbox(data,lags = 5)
p(result)

The result is as follows.

(array([0.05608493, 0.05613943, 0.31898424, 3.27785331, 3.94903872]), array([0.81279444, 0.97232058, 0.9564194 , 0.51244884, 0.55677627]))

It is output in tuple format with two elements, the first is the test statistic and the second is the p-value. Let's make it tabular so that it looks beautiful.

result_table = pd.DataFrame(data = result, index=['static value', 'P value'],columns=[str(i) for i in range(1,6)])
result_table

The following result is output. The column direction is the size of the lug. キャプチャ.PNG

Example 2

Next, let's test the MA (2) process. Assume the following formula.

y_t = 1 + \epsilon_t + 0.5 \epsilon_{t-3}

However, $ \ epsilon_t $ is white Gaussian noise. As you can see from the form of the equation, it seems that there is a correlation when the time difference is 3 (for example, $ y_5 $ and $ y_8 $). Of course, it can be confirmed mathematically, but this is confirmed by the portmanteau test.

#Creating model data
data = np.zeros(1000)
np.random.seed(42)
err = np.random.standard_normal(1000)
for i in range(1000):
    if i-3 < 0:
        data[i] = 1 + err[i]
    else:
        data[i] = 1 + err[i] + 0.5 * err[i-3]

#First plot the data
plt.figure(figsize=(10,6))
plt.plot(data,lw = 1.5)
plt.xlabel('time')
plt.ylabel('value')
plt.title('time vs. value plot (MA(3) model)')
plt.xlim([0,100])

img.png

result = acorr_ljungbox(data,lags = 5)
result_table = pd.DataFrame(data = result, index=['static value', 'P value'],columns=[str(i) for i in range(1,6)])
result_table

キャプチャ.PNG

For example, when P is tested at 0.05, there is no significant difference when the lag is 2 or less, but it is found that there is a significant difference when it is 3 or more (that is, when $ \ rho_3 $ is included). I will.

Recommended Posts

How to do portmanteau test with python
How to do multi-core parallel processing with python
How to do hash calculation with salt in Python
[Python] How to do PCA in Python
To do tail recursion with Python2
How to get started with Python
What to do with PYTHON release?
How to use FTP with Python
How to calculate date with python
How to do Bulk Update with PyMySQL and notes [Python]
How to do arithmetic with Django template
How to do R chartr () in Python
How to work with BigQuery in Python
How to display python Japanese with lolipop
How to enter Japanese with Python curses
[Python] How to deal with module errors
How to install python3 with docker centos
Primality test with Python
How to install Python
Primality test with python
Do Houdini with Python3! !! !!
How to upload with Heroku, Flask, Python, Git (4)
[Python] How to test command line parser click
How to enjoy programming with Minecraft (Ruby, Python)
[REAPER] How to play with Reascript in Python
[Python] What I did to do Unit Test
How to crop an image with Python + OpenCV
How to specify attributes with Mock of python
How to measure execution time with Python Part 1
How to use tkinter with python in pyenv
[Python] How to handle Japanese characters with openCV
[Python] How to compare datetime with timezone added
How to measure execution time with Python Part 2
Connect to BigQuery with Python
How to convert / restore a string with [] in python
How to install Python [Windows]
python3: How to use bottle (2)
How to scrape image data from flickr with python
[Introduction to Python] How to iterate with the range function?
Do Django with CodeStar (Python3.8, Django2.1.15)
Connect to Wikipedia with Python
How to update Python Tkinter to 8.6
Post to slack with Python 3
How to upload with Heroku, Flask, Python, Git (Part 3)
How to do zero-padding in one line with OpenCV
How to run tests in bulk with Python unittest
[Python] How to specify the download location with youtube-dl
How to measure mp3 file playback time with python
How to use python interactive mode with git bash
How to use Python argparse
How to update with SQLAlchemy?
How to convert JSON file to CSV file with Python Pandas
How to cast with Theano
How to upload with Heroku, Flask, Python, Git (Part 1)
Python: How to use pydub
[Python] How to deal with pandas read_html read error
[Python] How to use checkio
How to run Notepad ++ Python
How to upload with Heroku, Flask, Python, Git (Part 2)
How to Alter with SQLAlchemy?
Switch python to 2.7 with alternatives