Time Series for Python with PyFlux

Merry Christmas buddy!

Christmas Lights in Surrey

It's been quite a while without writing anything. Today, we are going to introduce PyFlux for time seriers analysis. Cannonical models are to be directly adopted from PyFlux Documents and tested in this blog.

Get Started

Copy and paste that piece of code from PyFlux Documents with trivial modifications as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import pandas as pd
import numpy as np
from pandas_datareader.data import DataReader
from datetime import datetime
import pyflux as pf
import matplotlib.pyplot as plt


a = DataReader('JPM', 'yahoo', datetime(2006,6,1), datetime(2016,6,1))
a_returns = pd.DataFrame(np.diff(a['Adj Close'].values))
a_returns.index = a.index.values[1:a.index.values.shape[0]]
a_returns.columns = ["JPM Returns"]

print(a_returns.head())

plt.figure(figsize=(15, 5))
plt.ylabel("Returns")
plt.plot(a_returns)
plt.show()

pf.acf_plot(a_returns.values.T[0])
pf.acf_plot(np.square(a_returns.values.T[0]))

After running the above piece of code, we'll get the following time series loaded:

1
2
3
4
5
6
            JPM Returns
2006-06-02 0.167995
2006-06-05 -0.613506
2006-06-06 -0.430914
2006-06-07 -0.094942
2006-06-08 0.073048

and the following resultant figures:

Time Series
Autocorrelation
Squared Autocorrelation

Cannonical Models

ARIMA

We can load and test ARIMA model using the following piece of code:

1
2
3
4
5
6
7
8
9
10
my_model = pf.ARIMA(data=a_returns, ar=4, ma=4, family=pf.Normal())
print(my_model.latent_variables)

result = my_model.fit("MLE")
result.summary()

my_model.plot_z(figsize=(15,5))
my_model.plot_fit(figsize=(15,10))
my_model.plot_predict_is(h=50, figsize=(15,5))
my_model.plot_predict(h=20,past_values=20,figsize=(15,5))

ARIMA's latent variables are printed as:

1
2
3
4
5
6
7
8
9
10
11
12
Index    Latent Variable           Prior           Prior Hyperparameters     V.I. Dist  Transform
======== ========================= =============== ========================= ========== ==========
0 Constant Normal mu0: 0, sigma0: 3 Normal None
1 AR(1) Normal mu0: 0, sigma0: 0.5 Normal None
2 AR(2) Normal mu0: 0, sigma0: 0.5 Normal None
3 AR(3) Normal mu0: 0, sigma0: 0.5 Normal None
4 AR(4) Normal mu0: 0, sigma0: 0.5 Normal None
5 MA(1) Normal mu0: 0, sigma0: 0.5 Normal None
6 MA(2) Normal mu0: 0, sigma0: 0.5 Normal None
7 MA(3) Normal mu0: 0, sigma0: 0.5 Normal None
8 MA(4) Normal mu0: 0, sigma0: 0.5 Normal None
9 Normal Scale Flat n/a (non-informative) Normal exp

And the fitting result is summarized as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Normal ARIMA(4,0,4)
======================================================= ==================================================
Dependent Variable: JPM Returns Method: MLE
Start Date: 2006-06-08 00:00:00 Log Likelihood: -3106.9508
End Date: 2016-06-02 00:00:00 AIC: 6233.9016
Number of observations: 2514 BIC: 6292.1979
==========================================================================================================
Latent Variable Estimate Std Error z P>|z| 95% C.I.
======================================== ========== ========== ======== ======== =========================
Constant 0.0126 0.0136 0.9273 0.3538 (-0.0141 | 0.0394)
AR(1) 0.0596 0.198 0.3008 0.7635 (-0.3284 | 0.4475)
AR(2) -0.3591 0.1858 -1.9331 0.0532 (-0.7231 | 0.005)
AR(3) -0.2205 0.2687 -0.8208 0.4118 (-0.7471 | 0.3061)
AR(4) 0.5272 0.1373 3.8406 0.0001 (0.2581 | 0.7962)
MA(1) -0.1667 0.1995 -0.8354 0.4035 (-0.5578 | 0.2244)
MA(2) 0.3234 0.202 1.6008 0.1094 (-0.0726 | 0.7193)
MA(3) 0.1603 0.2396 0.6692 0.5033 (-0.3093 | 0.63)
MA(4) -0.5817 0.1618 -3.5942 0.0003 (-0.8989 | -0.2645)
Normal Scale 0.8331
==========================================================================================================

And 4 images are to be produced as:

ARIMA Latent Variable Plot
ARIMA Model vs. Data
ARIMA Forcast vs. Data
ARIMA Forcast

GARCH

We can load and test GARCH model using the following piece of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
my_model = pf.GARCH(p=1, q=1, data=a_returns)
print(my_model.latent_variables)

my_model.adjust_prior(1, pf.TruncatedNormal(0.01, 0.5, lower=0.0, upper=1.0))
my_model.adjust_prior(2, pf.TruncatedNormal(0.97, 0.5, lower=0.0, upper=1.0))

result = my_model.fit('M-H', nsims=20000)
result.summary()

my_model.plot_z([1,2])
my_model.plot_fit(figsize=(15,5))
my_model.plot_sample(nsims=10, figsize=(15,7))

from scipy.stats import kurtosis
my_model.plot_ppc(T=kurtosis)
my_model.plot_predict(h=30, figsize=(15,5))

GARCH's latent variables are printed as:

1
2
3
4
5
6
Index    Latent Variable           Prior           Prior Hyperparameters     V.I. Dist  Transform
======== ========================= =============== ========================= ========== ==========
0 Vol Constant Normal mu0: 0, sigma0: 3 Normal exp
1 q(1) Normal mu0: 0, sigma0: 0.5 Normal logit
2 p(1) Normal mu0: 0, sigma0: 0.5 Normal logit
3 Returns Constant Normal mu0: 0, sigma0: 3 Normal None

And the fitting result is summarized as:

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
~/.local/lib/python3.6/site-packages/numdifftools/limits.py:126: UserWarning: All-NaN slice encountered
warnings.warn(str(msg))
Acceptance rate of Metropolis-Hastings is 0.000125
Acceptance rate of Metropolis-Hastings is 0.00075
Acceptance rate of Metropolis-Hastings is 0.105525
Acceptance rate of Metropolis-Hastings is 0.13335
Acceptance rate of Metropolis-Hastings is 0.1907
Acceptance rate of Metropolis-Hastings is 0.232
Acceptance rate of Metropolis-Hastings is 0.299

Tuning complete! Now sampling.
Acceptance rate of Metropolis-Hastings is 0.36655
GARCH(1,1)
======================================================= ==================================================
Dependent Variable: JPM Returns Method: Metropolis Hastings
Start Date: 2006-06-05 00:00:00 Unnormalized Log Posterior: -2671.5492
End Date: 2016-06-02 00:00:00 AIC: 5351.717896880396
Number of observations: 2517 BIC: 5375.041188860938
==========================================================================================================
Latent Variable Median Mean 95% Credibility Interval
======================================== ================== ================== =========================
Vol Constant 0.0059 0.0057 (0.004 | 0.0076)
q(1) 0.0721 0.0728 (0.0556 | 0.0921)
p(1) 0.9202 0.9199 (0.9013 | 0.9373)
Returns Constant 0.0311 0.0311 (0.0109 | 0.0511)
==========================================================================================================

And 5 images are to be produced as:

GARCH Latent Variable Plot
GARCH Volatility
GARCH Volatility Sampling
GARCH Posterior Predictive
GARCH Forcast

GAS

VAR