@@ -202,6 +202,42 @@ def lag_plot(series, ax=None, **kwds):
202
202
ax .scatter (y1 , y2 , ** kwds )
203
203
return ax
204
204
205
+ def autocorrelation_plot (series , ax = None ):
206
+ """Autocorrelation plot for time series.
207
+
208
+ Parameters:
209
+ -----------
210
+ series: Time series
211
+ ax: Matplotlib axis object, optional
212
+
213
+ Returns:
214
+ -----------
215
+ ax: Matplotlib axis object
216
+ """
217
+ import matplotlib .pyplot as plt
218
+ n = len (series )
219
+ data = series .values
220
+ if ax == None :
221
+ ax = plt .gca (xlim = (1 , n ), ylim = (- 1.0 , 1.0 ))
222
+ mean = np .mean (data )
223
+ c0 = np .sum ((data - mean ) ** 2 ) / float (n )
224
+ def r (h ):
225
+ return ((data [:n - h ] - mean ) * (data [h :] - mean )).sum () / float (n ) / c0
226
+ x = np .arange (n ) + 1
227
+ y = map (r , x )
228
+ z95 = 1.959963984540054
229
+ z99 = 2.5758293035489004
230
+ ax .axhline (y = z99 / np .sqrt (n ), linestyle = '--' , color = 'grey' )
231
+ ax .axhline (y = z95 / np .sqrt (n ), color = 'grey' )
232
+ ax .axhline (y = 0.0 , color = 'black' )
233
+ ax .axhline (y = - z95 / np .sqrt (n ), color = 'grey' )
234
+ ax .axhline (y = - z99 / np .sqrt (n ), linestyle = '--' , color = 'grey' )
235
+ ax .set_xlabel ("Lag" )
236
+ ax .set_ylabel ("Autocorrelation" )
237
+ ax .plot (x , y )
238
+ ax .grid ()
239
+ return ax
240
+
205
241
def grouped_hist (data , column = None , by = None , ax = None , bins = 50 , log = False ,
206
242
figsize = None , layout = None , sharex = False , sharey = False ,
207
243
rot = 90 ):
0 commit comments