In this post I will try to show some different visualizations for ACF and PACF. There are already lots of nice tutorials, but visualization is not sufficient I think. What I mean is, you cannot see different ACF and PACF combinations to get an idea about how they look for different data.
The code is at github. Link
Pictures in post could be hard to see, if you cannot see clearly check github for original sized images.
First lets see how a series diverges with a correlation value. So if every member of a series is correlated with 0.99 of previous,
x(t) = x(t-1) * 0.99
in 10 steps 1 shrinks to 0.9. If we make correlation number 0.91 ,
x(t) = x(t-1) * 0.91
in 10 steps it becomes 0.39. This way you can have an idea of fast divergence will happen according to correlation coefficient.
I will generate different data and check ACF ,PACF graphs.
The simple code is above. I give AR and MA parameters, generate data, and draw ACF and PACF. I also check if series is stationary.
Null Hypothesis (H0): If failed to be rejected, it suggests the time series has a unit root, meaning it is non-stationary. It has some time dependent structure.
Alternate Hypothesis (H1): The null hypothesis is rejected; it suggests the time series does not have a unit root, meaning it is stationary. It does not have time-dependent structure.
p-value > 0.05: Fail to reject the null hypothesis (H0), the data has a unit root and is non-stationary.
p-value <= 0.05: Reject the null hypothesis (H0), the data does not have a unit root and is stationary.
First let’s create a weekly data. Think I have a cookie shop at a major station. So sales of this Monday is similar to next Monday. So I can create a fake data in which every day is correlated with the same day 1 week ago.
With get_weekly_data method I can create data which is correlated with same day 1 week before with a correlation coefficient.
If I say today is %90 correlated with previous week, I get a table as below.
In ACF you can see that most correlated date is 7 days before. (Nearly %90 of before) And you can see day 14 is %90 of day 7. In PACF you can see a similar pattern. Play with source code for other ratios.
At weekly data I created data manually. We can also create data with ArmaProcess, as I showed in the 1st piece of code. I will call these method as below.
Details of ARMA data generation can be seen in documentation.
https://www.statsmodels.org/stable/generated/statsmodels.tsa.arima_process.ArmaProcess.html
“due to using the lag-polynomial representation, the AR parameters should have the opposite sign of what one would write in the ARMA representation.”
So if next term AR coefficient array [1,-1.1] is ( first 1 is for X(t) )
X(t) = -( -1.1 ) X(t-1)
X(t) is 1.1 positively correlated with X(t-1)
When we generate a ARMA DATA as above, we are saying 2nd term is +1.1 correlated with 1st term. So if initial value is 1, subsequent values can be seen as below:
X(t) = X(t-1) * 1.1
X(2) =X(1) * 1.1 = 1.1
X(3) = X(2) * 1.1 = 1.21
X(4) =X(3) * 1.1 = 1.32
If initial value negative (by random) the change will be in other direction. You can see even with same parameters one goes to positive 1e8 the other goes to negative 1e8.
We can also make AR term positive and bigger than 1. Then sign of result oscillates and we values explore. Check below graph to see values are in range “1e8”.
X(t) = -( 1.1 ) X(t-1)
X(t) = X(t-1) * -1.1
X(2) =X(1) * -1.1 = -1.1
X(3) = X(2) * -1.1 = 1.21
X(4) =X(3) * -1.1 = -1.32
since coefficient is minus, value of series oscillate between positive and negative as in below.
For making a cheat sheet I will show different AR and MA coefficients and draw 3 graphs. Graphs are self explanatory. You can play with code and obtain different results.
Below graphs are for strong correlation 0.99. As you see effect of ACF shrink gradually when MA is small. So a strong auto correlated series will have a graphs as in 1st row below.
Below we can see effect of 3 high correlated series. [-0.99,-1,-1.1].
[-0.99 and -1] shows nearly copying former value. [-1.1] is a exploding pattern.(multiplying something over 1 multiple times make value too big)
Multi Step lags
For simplicity in above graphs I only give parameter for 1 step lag. We can have different patterns, below I give multi step lags as parameter. If some of lag coefficients is more than 1, you can see an exploding pattern as in 3rd row.
Sin/Cos Graphs
We can check the correlation for sin/cos shape functions.(Here we do not give AR or MA). Here I am creating a sin data, and showing ACF and PACF.
As you see in below, ACF makes a wave.
0–25 values increases as a declining positive correlation.
25–75values decreases, so correlation is also negative but acceleration of this declining changes near 50, so ACF becomes more until near 50 then begin to approach 0 again.
75–100 again have positive correlation.
Here I tried to give a cheat sheet for different patterns with ACF and PACF. For simplicity I will keep this post as it is. And at next posts I will try to comment on these graphs.