Time Series Analysis With Python
Time Series Analysis With Python
PYTHON
Aileen Nielsen
July, 13, 2016
aileen.a.nielsen@gmail.com
INSTALLATION INSTRUCTIONS
http://www.amstat.org/publications/jse/v21n1/witt.pdf
http://www.forbes.com/sites/neilhowe/2015/05/28/whats-behind-the-decline-in-
crime/#3e1e6be07733
SPEECH RECOGNITION
http://www.amstat.org/publications/jse/v21n1/witt.pdf
http://www.forbes.com/sites/neilhowe/2015/05/28/whats-behind-the-decline-in-
crime/#3e1e6be07733
PHYSICS EXPERIMENTS
E CONOMICS , GOVE RNME NT, POLICY
http://freakonomics.com/2011/05/25/mining-for-correlations-it-works/
IN THE NEWS
http://www.forbes.com/sites/jasonevangelho/2016/07/10/pokemon-
go-about-to-surpass-twitter-in-daily-active-users/#5c27d4825174
BE C ARE FUL! IT’S E S PE CIALLY TRUE
FOR TIME S E RIE S THAT YOU NE E D
TO KNOW S OME THING ABOUT
YOUR DATA
http://twentytwowords.com/funny-graphs-show-correlation-
between-completely-unrelated-stats-9-pictures/
10 MINUTES TO PANDAS
PANDAS US E S ‘DATA FRAME S ’ TO
PUT DATA IN AN E AS Y-TO-US E
FORMAT
http://pandas.pydata.org/pandas-docs/stable/10min.html
DEALING WITH TIME
PANDAS FUNCTIONALITY
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
DATEOFFSET COMPONENTS
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html
LAG FUNCTIONS
http://stackoverflow.com/questions/18300270/lag-in-time-series-regression-using-libsvm
http://www.spss-tutorials.com/spss-lag-function/
WINDOW FUNCTIONS
Rolling window Expanding window
Why use a rolling window function?
http://datascience.stackexchange.com/questions/6787/is-decision-tree-algorithm-a-linear-or-nonlinear-algorithm
SPECTRAL ANALYSIS
INTUITION
• Decompose a time series into a sum of many many sine or cosine functions
• The coefficients of these functions should have uncorrelated values
• Regression on sinusoids
https://en.wikipedia.org/wiki/Fourier_transform
EXAMPLES
https://faculty.washington.edu/dbp/PDFFILES/GHS-AP-Stat-talk.pdf
SPECTRA-B ASED FIT CAN BE
SURPRISINGLY GOOD
SPECTRAL ANALYSIS TURNS UP
EVERYWHERE
https://www.otexts.org/fpp/8/1
DIFFERENCING TO CREATE
STATIONARY TIME SERIES
https://www.otexts.org/fpp/8/1
YOU NE E D TO RE MOVE THE TRE ND
AND S E AS ONAL E LE ME NTS BE FORE
FORE C AS TING
https://onlinecourses.science.psu.edu/stat510/node/57
REMOVE SEASONALITY
• The test gives back several values to help you assess significance with standard p-
value reasoning.
• Basic intuition: ρ should not have unit value
SELF-CORRELATION, SELF-
EXPLANATION, AND SELF-
PREDICTION
AUTOCORRELATION FUNCTION
https://en.wikipedia.org/wiki/Autocorrelation
PARTIAL AUTOCORRELATION FUNCTION
https://en.wikipedia.org/wiki/Partial_autocorrelation_function &
https://www.mathworks.com/matlabcentral/answers/160904-interpretation-of-autocorrelation-function-to-
determine-number-of-lags-in-ar-model?requestedDomain=www.mathworks.com
FORECASTING
MOVING AVERAGE PROCESS
(MA)
Xt = µ + εt + θ1εt-1 + … + θqεt-q
• µ is the mean of the series, θ are parameters, θq not 0
• This is a stationary process regardless of values of θ
• Consider an MA(1) process (centered at 0):
Xt = εt + θ1εt-1
θ1 = +.5 θ1 = -.5
Time Series Analysis and Applications, Robert H. Shumway and David S. Stoffer
https://en.wikipedia.org/wiki/Moving-average_model
AUTOREGRESSIVE PROCESS
(AR)
Xt = φ1Xt-1 + … + φpXt-p + εt
• This is a stationary process if abs(φ) < 1
• Consider an AR(1) process:
Xt = φ1Xt-1 + εt
φ 1 = +.9 φ 1 = -.9
Time Series Analysis and Applications, Robert H. Shumway and David S. Stoffer
https://en.wikipedia.org/wiki/Moving-average_model
ARIMA MODEL (A.K.A. BOX-
JENKINS)
• AR = autoregressive terms
• I = differencing
• MA = moving average
• Hence specified as (autoregressive terms, differencing
terms, moving average terms)
ARIMA MODE: ‘THE MOST GENERAL
CLASS OF MODELS FOR FORECASTING
A TIME SERIES WHICH CAN BE
MADE TO BE STATIONARY
http://people.duke.edu/~rnau/411arim.htm
APPLYING THE APPROPRIATE
ARIMA MODEL
http://macromarketmusings.blogspot.com/2013/12/taking-model-to-data.html
CLUSTERING &
CLASSIFICATION
(YET ANOTHER ROUTE TO
PREDICTION)
NE E D TO THINK C ARE FULLY ABOUT
DIS TANCE ME TRIC
http://www.psb.ugent.be/cbd/papers/gentxwarper/DTWalgorithm.htm &
http://alexminnaar.com/time-series-classification-and-clustering-with-python.html
DYNAMIC TIME WARPING
http://www.psb.ugent.be/cbd/papers/gentxwarper/DTWalgorithm.htm &
http://alexminnaar.com/time-series-classification-and-clustering-with-python.html
APPLICATIONS
DTW-based clustering
https://en.wikipedia.org/wiki/Hidden_Markov_model
HOW IT WORKS
http://web.mit.edu/9.29/www/neville_jen/hmm/
GET
MORE
PRACTICE