A u tocorrelation F u nction TIME SE R IE S AN ALYSIS IN P YTH ON Rob Reider Adj u nct Professor , NYU - Co u rant Cons u ltant , Q u antopian
A u tocorrelation F u nction A u tocorrelation F u nction ( ACF ): The a u tocorrelation as a f u nction of the lag Eq u als one at lag -z ero Interesting information be y ond lag - one TIME SERIES ANALYSIS IN PYTHON
ACF E x ample 1: Simple A u tocorrelation F u nction Can u se last t w o v al u es in series for forecasting TIME SERIES ANALYSIS IN PYTHON
ACF E x ample 2: Seasonal Earnings Earnings for H & R Block ACF for H & R Block TIME SERIES ANALYSIS IN PYTHON
ACF E x ample 3: Usef u l for Model Selection Model selection TIME SERIES ANALYSIS IN PYTHON
Plot ACF in P y thon Import mod u le : from statsmodels.graphics.tsaplots import plot_acf Plot the ACF : plot_acf(x, lags= 20, alpha=0.05) TIME SERIES ANALYSIS IN PYTHON
Confidence Inter v al of ACF TIME SERIES ANALYSIS IN PYTHON
Confidence Inter v al of ACF Arg u ment alpha sets the w idth of con � dence inter v al E x ample : alpha=0.05 5% chance that if tr u e a u tocorrelation is z ero , it w ill fall o u tside bl u e band Con � dence bands are w ider if : Alpha lo w er Fe w er obser v ations Under some simplif y ing ass u mptions , 95% con � dence bands √ N are ±2/ If y o u w ant no bands on plot , set alpha=1 TIME SERIES ANALYSIS IN PYTHON
ACF Val u es Instead of Plot from statsmodels.tsa.stattools import acf print(acf(x)) [ 1. -0.6765505 0.34989905 -0.01629415 -0.0250701 -0.03186545 0.01399904 -0.03518128 0.02063168 -0.0262064 ... 0.07191516 -0.12211912 0.14514481 -0.09644228 0.0521588 TIME SERIES ANALYSIS IN PYTHON
Let ' s practice ! TIME SE R IE S AN ALYSIS IN P YTH ON
White Noise TIME SE R IE S AN ALYSIS IN P YTH ON Rob Reider Adj u nct Professor , NYU - Co u rant Cons u ltant , Q u antopian
What is White Noise ? White Noise is a series w ith : Constant mean Constant v ariance Zero a u tocorrelations at all lags Special Case : if data has normal distrib u tion , then Ga u ssian White Noise TIME SERIES ANALYSIS IN PYTHON
Sim u lating White Noise It ' s v er y eas y to generate w hite noise import numpy as np noise = np.random.normal(loc=0, scale=1, size=500) TIME SERIES ANALYSIS IN PYTHON
What Does White Noise Look Like ? plt.plot(noise) TIME SERIES ANALYSIS IN PYTHON
A u tocorrelation of White Noise plot_acf(noise, lags=50) TIME SERIES ANALYSIS IN PYTHON
Stock Market Ret u rns : Close to White Noise A u tocorrelation F u nction for the S & P 500 TIME SERIES ANALYSIS IN PYTHON
Let ' s practice ! TIME SE R IE S AN ALYSIS IN P YTH ON
Random Walk TIME SE R IE S AN ALYSIS IN P YTH ON Rob Reider Adj u nct Professor , NYU - Co u rant Cons u ltant , Q u antopian
What is a Random Walk ? Toda y' s Price = Yesterda y' s Price + Noise = + ϵ P P t −1 t t Plot of sim u lated data TIME SERIES ANALYSIS IN PYTHON
What is a Random Walk ? Toda y' s Price = Yesterda y' s Price + Noise = + ϵ P P t −1 t t Change in price is w hite noise P − P = ϵ t −1 t t Can ' t forecast a random w alk Best forecast for tomorro w' s price is toda y' s price TIME SERIES ANALYSIS IN PYTHON
What is a Random Walk ? Toda y' s Price = Yesterda y' s Price + Noise = + ϵ P P t −1 t t Random w alk w ith dri �: = μ + P + ϵ P t −1 t t Change in price is w hite noise w ith non -z ero mean : P − P = μ + ϵ t −1 t t TIME SERIES ANALYSIS IN PYTHON
Statistical Test for Random Walk Random w alk w ith dri � = μ + P + ϵ P t −1 t t Regression test for random w alk = α + β P + ϵ P t −1 t t Test : H : β = 1 ( random w alk ) H : β < 1 ( not 0 1 random w alk ) TIME SERIES ANALYSIS IN PYTHON
Statistical Test for Random Walk Regression test for random w alk = α + β P + ϵ P t −1 t t Eq u i v alent to P − P = α + β P + ϵ t −1 t −1 t t Test : H : β = 0 ( random w alk ) H : β < 0 ( not 0 1 random w alk ) TIME SERIES ANALYSIS IN PYTHON
Statistical Test for Random Walk Regression test for random w alk P − P = α + β P + ϵ t −1 t −1 t t Test : H : β = 0 ( random w alk ) H : β < 0 ( not 0 1 random w alk ) This test is called the Dicke y- F u ller test If y o u add more lagged changes on the right hand side , it ' s the A u gmented Dicke y- F u ller test TIME SERIES ANALYSIS IN PYTHON
ADF Test in P y thon Import mod u le from statsmodels from statsmodels.tsa.stattools import adfulle R u n A u gmented Dicke y- Test adfuller(x) TIME SERIES ANALYSIS IN PYTHON
E x ample : Is the S & P 500 a Random Walk ? # Run Augmented Dickey-Fuller Test on SPX data results = adfuller(df['SPX']) # Print p-value print(results[1]) 0.782253808587 # Print full results print(results) (-0.91720490331127869, 0.78225380858668414, 0, 1257, {'1%': -3.4355629707955395, '10%': -2.567995644141416, '5%': -2.8638420633876671}, 10161.888789598503) TIME SERIES ANALYSIS IN PYTHON
Let ' s practice ! TIME SE R IE S AN ALYSIS IN P YTH ON
Stationarit y TIME SE R IE S AN ALYSIS IN P YTH ON Rob Reider Adj u nct Professor , NYU - Co u rant Cons u ltant , Q u antopian
What is Stationarit y? Strong stationarit y : entire distrib u tion of data is time - in v ariant Weak stationarit y : mean , v ariance and a u tocorrelation are time - in v ariant ( i . e ., for a u tocorrelation , corr ( X , X ) is t − τ t onl y a f u nction of τ ) TIME SERIES ANALYSIS IN PYTHON
Wh y Do We Care ? If parameters v ar y w ith time , too man y parameters to estimate Can onl y estimate a parsimonio u s model w ith a fe w parameters TIME SERIES ANALYSIS IN PYTHON
E x amples of Nonstationar y Series Random Walk TIME SERIES ANALYSIS IN PYTHON
E x amples of Nonstationar y Series Seasonalit y in series TIME SERIES ANALYSIS IN PYTHON
E x amples of Nonstationar y Series Change in Mean or Standard De v iation o v er time TIME SERIES ANALYSIS IN PYTHON
Transforming Nonstationar y Series Into Stationar y Series Random Walk First di � erence plot.plot(SPY) plot.plot(SPY.diff()) TIME SERIES ANALYSIS IN PYTHON
Transforming Nonstationar y Series Into Stationar y Series Seasonalit y Seasonal di � erence plot.plot(HRB) plot.plot(HRB.diff(4)) TIME SERIES ANALYSIS IN PYTHON
Transforming Nonstationar y Series Into Stationar y Series # Log of AMZN Revenues AMZN Q u arterl y Re v en u es plt.plot(np.log(AMZN)) plt.plot(AMZN) # Log, then seasonal difference plt.plot(np.log(AMZN).diff(4)) TIME SERIES ANALYSIS IN PYTHON
Let ' s practice ! TIME SE R IE S AN ALYSIS IN P YTH ON
Recommend
More recommend