How to plot autocorrelation and partial autocorrelation in python

Introduction

We describe how to plot a correlogram with a graph of autocorrelation coefficient and partial autocorrelation coefficient in python.

Plot of autocorrelation coefficient

The function sm.graphics.tsa.plot_acf of statsmodels.api is used.

Plot of self-partial correlation coefficient

The function sm.graphics.tsa.plot_pacf of statsmodels.api is used.

Example

As an example, the autocorrelation coefficient of the following AR (1) model is plotted.

y_t = 1 + 0.5 y_{t-1} + \epsilon_t

However, $ \ epsilon_t $ is the normal white noise with variance 1. Also, let $ y_0 = 2 $.

#A magical spell that makes module capture and graphs look good
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.style.use('seaborn')
mpl.rcParams['font.family'] = 'serif'
%matplotlib inline
import numpy as np
import statsmodels.api as sm

#Creating a column of data to plot
#This time, capture the data at 100 times
y = np.zeros(100)
np.random.seed(42)
epsilon = np.random.standard_normal(100)
y[0] = 2
for t in range(1,100):
    y[t] = 1 + 0.5 * y[t-1] + epsilon[t]

#Take a look at the time series data to plot
plt.plot(y)
plt.xlabel('time')
plt.ylabel('value')
plt.title('time-value plot');

The following graph is plotted. chart.jpg

#Plot of autocorrelation coefficient
sm.graphics.tsa.plot_acf(y, lags=20)
plt.xlabel('lags')
plt.ylabel('corr')

#Partial autocorrelation coefficient plot
sm.graphics.tsa.plot_pacf(y, lags=20)
plt.xlabel('lags')
plt.ylabel('corr')

The correlogram of the autocorrelation coefficient and the correlogram of the partial autocorrelation coefficient are plotted. c1.jpg c2.jpg

Recommended Posts

How to plot autocorrelation and partial autocorrelation in python
How to use is and == in Python
How to generate permutations in Python and C ++
How to develop in Python
[Python] How to sort dict in list and instance in list
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use PubChem in Python
How to handle Japanese in Python
How to swap elements in an array in Python, and how to reverse an array.
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Comparison of how to use higher-order functions in Python 2 and 3
How to execute external shell scripts and commands in python
How to log in to AtCoder with Python and submit automatically
How to package and distribute Python scripts
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to install and use pandas_datareader [Python]
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
python: How to use locals () and globals ()
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to use Python zip and enumerate
How to use regular expressions in Python
How to display Hello world in python
How to write Ruby to_s in Python
How to install OpenCV on Cloud9 and run it in Python
How to use functions in separate files Perl and Python versions
Difference in how to write if statement between ruby ​​and python
[ROS2] How to describe remap and parameter in python format launch
How to display legend marks in one with Python 2D plot
How to display bytes in the same way in Java and Python
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to install Python
How to embed a variable in a python string
How to implement Discord Slash Command in Python
Summary of how to import files in Python 3
How to simplify restricted polynomial fit in python
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
How to install python
[Python] How to read data from CIFAR-10 and CIFAR-100
How to use partial
Summary of how to use MNIST in Python
Send messages to Skype and Chatwork in Python
How to specify TLS version in python requests
How to convert SVG to PDF and PNG [Python]