0% found this document useful (0 votes)
21 views

Time Series Formulas and Python Functions

Formulas sndpython functions
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Time Series Formulas and Python Functions

Formulas sndpython functions
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Cheat Sheet Time Series Analysis

Created by Daniel Zaldaña @ZaldanaDaniel

Time Series Analysis


Cheat Sheet

1. Basic Concepts 3. Autocorrelation

Time Series Components Correlation Measures

• Trend (Tt ): Long-term direction ACF:



Pn
Seasonal (St ): Regular patterns
t=k+1 (Xt − X̄)(Xt−k − X̄)
• Cyclical (Ct ): Irregular fluctuations ρk = Pn 2
t=1 (Xt − X̄)
• Random (Rt ): Unexplained variation

PACF:

ϕkk = Corr(Xt , Xt−k |Xt−1 , ..., Xt−k+1 )


Decomposition Models

Additive: Yt = Tt + St + Ct + Rt

Multiplicative: Yt = Tt × St × Ct × Rt 4. Time Series Models

Model Specifications
AR(p):
p
2. Statistical Measures Xt = c +
X
ϕi Xt−i + εt
i=1

Core Statistics MA(q):


Mean: q
n
1X X
X̄ = Xt Xt = µ + εt + θi εt−i
n t=1 i=1

Moving Average: ARMA(p,q):


k−1 p q
1X X X
M A(k) = Xt−i Xt = c + ϕi Xt−i + θi εt−i + εt
k i=0
i=1 i=1

Variance: ARIMA(p,d,q):
n
1 X p q
σ2 = (Xt − X̄)2 X X
n−1 (1 − B)d (Xt − µ) = ϕi Xt−i + θi εt−i + εt
t=1
i=1 i=1
Cheat Sheet Time Series Analysis

5. Forecasting Metrics 8. Time Series Patterns

Error Measures Pattern Analysis


MAE: Exponential Growth: Yt = Y0 (1 + r)t
n K
1X Logistic Growth: Yt =
|Yt − Yˆt |
K−Y0 −rt
M AE = 1+( Y0 )e
n t=1 Holt-Winters Multiplicative:
Yt
RMSE: Level: Lt = α + (1 − α)(Lt−1 + Tt−1 )
St−s
v
u n
u1 X Trend: Tt = β(Lt − Lt−1 ) + (1 − β)Tt−1
RM SE = t (Yt − Yˆt )2 Yt
n t=1 Seasonal: St = γ + (1 − γ)St−s
Lt

MAPE:
n
100% X Yt − Yˆt 9. Spectral Analysis
M AP E = | |
n t=1 Yt
Frequency Domain
1
Pn −iωt 2
Periodogram: I(ω) = 2πn | t=1
P∞Xt e |
Spectral Density: f (ω) = 2π k=−∞ γk e−ikω
1

6. Stationarity Tests Coherence: Cxy (ω) =


|fxy (ω)|2
fxx (ω)fyy (ω)

Stationarity Conditions
Weak Stationarity: 10. Model Diagnostics
• E[Xt ] = µ (constant mean)
• V ar(Xt ) = σ 2 (constant variance) Diagnostic Tests
• Cov(Xt , Xt+k ) = γk (covariance depends only Pn
(e −et−1 )2
Durbin-Watson: DW = Pnt
t=2
2
on k) t=1 et
Breusch-Godfrey: LM = (n − p)R2
Augmented Forecast Error Decomposition:
Pp Dickey-Fuller: ∆yt = α + βt +
γyt−1 + i=1 δi ∆yt−i + εt
1X
PT
St2 Pt M SE = (Yt − Yˆt )2
KPSS Test: ηc = Tt=1 where n
2 σ̂ 2
PpSt = i i=1 ei ¯
Unit Root Condition: |1 − i=1 ϕi z | ̸= 0 for = (Ȳ − Ŷ )2 + (sY − rsŶ )2 + 2(1 − r)sY sŶ
|z| ≤ 1

11. Seasonal Analysis


7. Advanced Statistical Measures
Seasonal Metrics
Additional Metrics Seasonal Index:
Cross-Correlation Function (CCF): ρxy (k) = Average for season i
SIi = × 100
Pn
t=k+1 (Xt −X̄)(Yt−k −Ȳ )
√Pn 2
√Pn 2
Overall average
t=1 (Xt −X̄) t=1 (Yt −Ȳ )
Ljung-Box Q-statistic: Q(m) = n(n +
Pm ρ̂2k
2) k=1 n−k SARIMA Model:
Information Criteria: AIC = −2 ln(L) + 2k
BIC = −2 ln(L) + k ln(n) ϕp (B)ΦP (B s )(1−B)d (1−B s )D yt = θq (B)ΘQ (B s )εt
Cheat Sheet Time Series Analysis

1. Basic Statistics
Statistical Components

1 import numpy as np
2 import pandas as pd
3
4 def c a l c u l a t e _ b a s i c _ s t a t s ( data ) :
5 """ Calculate basic statistics """
6 return {
7 ’ mean ’: np . mean ( data ) ,
8 ’ variance ’: np . var ( data , ddof =1) ,
9 ’ std ’: np . std ( data , ddof =1)
10 }
11
12 def moving_average ( data , window ) :
13 """ Calculate moving average """
14 return pd . Series ( data ) . rolling (
15 window = window
16 ) . mean () . values

2. Correlation Analysis
ACF and PACF

1 from statsmodels . tsa . stattools import (
2 acf , pacf
3 )
4
5 def c o r r e l a t i o n _ a n a l y s i s ( data , lags ) :
6 """ Calculate ACF and PACF """
7 acf_vals = acf (
8 data ,
9 nlags = lags ,
10 fft = True
11 )
12 pacf_vals = pacf (
13 data ,
14 nlags = lags ,
15 method = ’ yw ’
16 )
17 return {
18 ’ acf ’: acf_vals ,
19 ’ pacf ’: pacf_vals
20 }
Cheat Sheet Time Series Analysis

3. Time Series Models


Model Implementations

1 from statsmodels . tsa . arima . model import (
2 ARIMA
3 )
4
5 def fit_arima_models ( data ) :
6 """ Fit ARIMA and variants """
7 models = {}
8

9 # AR (1) model
10 models [ ’ ar ’] = ARIMA (
11 data ,
12 order =(1 , 0 , 0)
13 ) . fit ()
14

15 # MA (1) model
16 models [ ’ ma ’] = ARIMA (
17 data ,
18 order =(0 , 0 , 1)
19 ) . fit ()
20

21 # ARMA (1 ,1)
22 models [ ’ arma ’] = ARIMA (
23 data ,
24 order =(1 , 0 , 1)
25 ) . fit ()
26

27 # ARIMA (1 ,1 ,1)
28 models [ ’ arima ’] = ARIMA (
29 data ,
30 order =(1 , 1 , 1)
31 ) . fit ()
32

33 return models
Cheat Sheet Time Series Analysis

4. Forecasting Metrics
Error Measures

1 def c alcula te_met rics ( actual , pred ) :
2 """ Calculate forecast metrics """
3

4 # Mean Absolute Error


5 mae = np . mean ( np . abs (
6 actual - pred
7 ))
8

9 # Root Mean Square Error


10 rmse = np . sqrt ( np . mean (
11 ( actual - pred ) ** 2
12 ))
13
14 # Mean Absolute Percentage Error
15 mape = np . mean ( np . abs (
16 ( actual - pred ) / actual
17 ) ) * 100
18
19 return {
20 ’ mae ’: mae ,
21 ’ rmse ’: rmse ,
22 ’ mape ’: mape
23 }
Cheat Sheet Time Series Analysis

5. Stationarity Tests
Stationarity Analysis

1 from statsmodels . tsa . stattools import (
2 adfuller , kpss
3 )
4
5 def ch ec k_ st ati on ar it y ( data ) :
6 """ Perform stationarity tests """
7
8 # ADF Test
9 adf_result = adfuller ( data )
10
11 # KPSS Test
12 kpss_result = kpss ( data )
13
14 # Rolling statistics
15 roll_mean = pd . Series ( data
16 ) . rolling ( window =12) . mean ()
17 roll_std = pd . Series ( data
18 ) . rolling ( window =12) . std ()
19
20 return {
21 ’ adf_stat ’: adf_result [0] ,
22 ’ adf_pval ’: adf_result [1] ,
23 ’ kpss_stat ’: kpss_result [0] ,
24 ’ kpss_pval ’: kpss_result [1] ,
25 ’ roll_mean ’: roll_mean ,
26 ’ roll_std ’: roll_std
27 }
Cheat Sheet Time Series Analysis

6. Advanced Measures
Advanced Statistics

1 from statsmodels . stats . diagnostic import (
2 acorr_ljungbox
3 )
4
5 def advanced_stats ( data ) :
6 """ Calculate advanced metrics """
7
8 # Ljung - Box Test
9 lb_test = acorr_ljungbox (
10 data ,
11 lags =10
12 )
13
14 # Information Criteria
15 model = ARIMA (
16 data ,
17 order =(1 ,0 ,0)
18 ) . fit ()
19
20 return {
21 ’ lb_stat ’: lb_test . lb_stat ,
22 ’ lb_pval ’: lb_test . lb_pvalue ,
23 ’ aic ’: model . aic ,
24 ’ bic ’: model . bic
25 }
Cheat Sheet Time Series Analysis

7. Seasonal Components
Seasonal Analysis

1 from statsmodels . tsa . seasonal import (
2 se as on al _d ec om po se
3 )
4
5 def a na l y z e_ s e as o n al i t y ( data , period ) :
6 """ Analyze seasonal patterns """
7
8 # Decomposition
9 decomp = s ea so na l_ de co mp os e (
10 data ,
11 period = period ,
12 model = ’ multiplicative ’
13 )
14

15 # Seasonal Indices
16 indices = pd . Series (
17 decomp . seasonal
18 ) . unique ()
19
20 return {
21 ’ trend ’: decomp . trend ,
22 ’ seasonal ’: decomp . seasonal ,
23 ’ residual ’: decomp . resid ,
24 ’ indices ’: indices
25 }
Cheat Sheet Time Series Analysis

8. Growth Models
Growth Patterns

1 from scipy . optimize import curve_fit
2

3 def f it_gro wth_mo dels ( data , time ) :


4 """ Fit growth models """
5
6 def exp_growth (t , y0 , r ) :
7 return y0 * (1 + r ) ** t
8

9 def logistic (t , K , y0 , r ) :
10 return K / (1 + (
11 ( K - y0 ) / y0
12 ) * np . exp ( - r * t ) )
13
14 # Fit models
15 exp_params = curve_fit (
16 exp_growth ,
17 time ,
18 data
19 ) [0]
20

21 log_params = curve_fit (
22 logistic ,
23 time ,
24 data
25 ) [0]
26

27 return {
28 ’ exp ’: exp_params ,
29 ’ logistic ’: log_params
30 }
Cheat Sheet Time Series Analysis

9. Spectral Analysis
Frequency Domain

1 from scipy import signal
2

3 def s pectra l_anal ysis ( data , fs =1.0) :


4 """ Perform spectral analysis """
5
6 # Periodogram
7 freqs , psd = signal . periodogram (
8 data ,
9 fs = fs
10 )
11
12 # Spectral Density
13 f_welch , psd_welch = signal . welch (
14 data ,
15 fs = fs
16 )
17
18 return {
19 ’ freq ’: freqs ,
20 ’ psd ’: psd ,
21 ’ welch_freq ’: f_welch ,
22 ’ welch_psd ’: psd_welch
23 }

10. Model Diagnostics


Diagnostic Tests

1 from statsmodels . stats . diagnostic import (
2 durbin_watson ,
3 acorr_breusch_godfrey
4 )
5
6 def m odel_d iagnos tics ( model , resid ) :
7 """ Perform model diagnostics """
8

9 # Durbin - Watson
10 dw = durbin_watson ( resid )
11
12 # Breusch - Godfrey
13 bg = a c or r _ b r e u s c h _ g o d f r e y (
14 model ,
15 nlags =5
16 )
17
18 return {
19 ’ dw_stat ’: dw ,
20 ’ bg_stat ’: bg [0] ,
21 ’ bg_pval ’: bg [1]
22 }

You might also like