6.7.24
Explore autocorrelation with a lag plot To detect autocorrelation in a time series, plot the previous value versus the current value. An upward slope means strong autocorrelation and a readable pattern.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import numpy as np
import matplotlib.pyplot as plt
rng = np . random . default_rng ( 11 )
series = np . cumsum ( rng . normal ( 0 , 1.2 , size = 120 )) + 50
lag = 1
x_prev = series [: - lag ]
x_curr = series [ lag :]
fig , ax = plt . subplots ( figsize = ( 4.4 , 4.4 ))
ax . scatter ( x_prev , x_curr , color = "#38bdf8" , alpha = 0.7 )
coef = np . corrcoef ( x_prev , x_curr )[ 0 , 1 ]
ax . set_xlabel ( "Value at t-1" )
ax . set_ylabel ( "Current value" )
ax . set_title ( f "Lag { lag } scatter (corr { coef : .2f } )" )
ax . grid ( alpha = 0.2 )
lims = [ min ( series ) - 2 , max ( series ) + 2 ]
ax . plot ( lims , lims , color = "#475569" , linestyle = "--" , linewidth = 1 )
ax . set_xlim ( lims )
ax . set_ylim ( lims )
fig . tight_layout ()
plt . show ()
Reading tips
# Points aligned in an upward line suggest strong autocorrelation and persistent trends. A circular cloud implies weak autocorrelation, close to a random walk. Small multiples across lags help decide which lag features to use.