Time series analysis part 4 VAR

1. Overview

2. What is a VAR model?

3. Granger causality

Definition

Test

Analysis example

data

Preprocessing
#Read data.
data = pd.read_csv('Train_Dst_Auction_DecPre_CF_1.txt', header=None, delim_whitespace=True)
#The first 4 lines are the best ASK/It is the price and quantity data for BID.
#In addition, the first 3900 columns are the data for the first issue.
pr = data.iloc[:4,:3900].T
pr.columns = ['ask_p','ask_v','bid_p','bid_v']
#Calculate the medium price from the best ASK and best BID.
pr['mid_p'] = (pr['ask_p'] + pr['bid_p']) / 2
#Calculate the rate of change of the medium price.
pr['p_chg'] = pr['mid_p'].pct_change()
#Calculate the degree of imbalance between the quantities of ASK and BID.
pr['v_imb'] = (pr['ask_v'] / pr['bid_v']).apply(np.log)
pr = pr.dropna()
#Cases where there are more sellers at the previous time
print('sell > buy ', pr.loc[pr['v_imb'].shift(-1)>1, 'p_chg'].sum())
#Cases where there are more buyers at the previous time
print('sell < buy ', pr.loc[pr['v_imb'].shift(-1)<1, 'p_chg'].sum())
# sell > buy  -0.0060484707428217765
# sell < buy  0.027729879129729684

On average, if there are many sells, the medium price is falling, and if there are many buys, the medium price is rising.

Granger causality test

#First, load the library and feed the data.
from statsmodels.tsa.vector_ar.var_model import VAR
model = VAR(pr[['v_imb','p_chg']].values)
model.select_order(10).summary()
AIC BIC FPE HQIC
0 -15.49 -15.48 1.880e-07 -15.49
1 -16.29 -16.28 8.405e-08 -16.29
2 -16.31 -16.30 8.217e-08 -16.31
3 -16.32 -16.30 8.173e-08 -16.31
4 -16.33* -16.30* 8.101e-08* -16.32*
5 -16.33 -16.29 8.103e-08 -16.32
6 -16.33 -16.29 8.108e-08 -16.31
7 -16.33 -16.28 8.112e-08 -16.31
8 -16.33 -16.27 8.116e-08 -16.31
9 -16.33 -16.27 8.111e-08 -16.31
10 -16.33 -16.26 8.120e-08 -16.30
#Create a model with an order of 4.
var_model = model.fit(4)
#Granger causality test. causing causing=0('v_imb')From used=1('p_chg')Test causality to.
Granger = var_model.test_causality(causing=0, caused=1)
Granger.summary()
Test statistic Critical value p-value df
9.531 2.373 0.000 (4, 7772)
Granger = var_model.test_causality(causing=1, caused=0)
Granger.summary()
Test statistic Critical value p-value df
0.9424 2.373 0.438 (4, 7772)

4. Impulse Response Function

Non-orthogonalized impulse response function

Orthogonalized impulse response function

Analysis example

#Create a model with an order of 4.
var_model = model.fit(4)
# k=Calculate impulse responses up to 10.
IRF = var_model.irf(10)
#Plot the results. orth=False means non-orthogonalization.
IRF.plot(orth=False)
plt.show()

save.png

5. ANOVA

Definition

Analysis example

#Create a model with an order of 4.
var_model = model.fit(4)
# k=Calculate the variance contribution ratio up to 10.
FEVD = var_model.fevd(10)
#Plot the results.
FEVD.plot()
plt.show()

save.png

FEVD.summary()
FEVD for v_imb v_imb p_chg
0 1.000000 0.000000
1 0.999994 0.000006
2 0.999969 0.000031
3 0.999971 0.000029
4 0.999572 0.000428
5 0.999511 0.000489
6 0.999478 0.000522
7 0.999452 0.000548
8 0.999418 0.000582
9 0.999397 0.000603
FEVD for p_chg v_imb p_chg
0 0.012342 0.987658
1 0.018310 0.981690
2 0.018871 0.981129
3 0.019158 0.980842
4 0.019791 0.980209
5 0.020477 0.979523
6 0.020889 0.979111
7 0.021208 0.978792
8 0.021472 0.978528
9 0.021678 0.978322

Recommended Posts

Time series analysis part 4 VAR
Time series analysis Part 3 Forecast
Time series analysis Part 1 Autocorrelation
Python: Time Series Analysis
Time series analysis Part 2 AR / MA / ARMA
RNN_LSTM1 Time series analysis
Time series analysis 1 Basics
Time series analysis related memo
Python: Time Series Analysis: Preprocessing Time Series Data
Time series analysis practice sales forecast
Time series analysis 3 Preprocessing of time series data
Time series analysis 2 Stationary, ARMA / ARIMA model
I tried time series analysis! (AR model)
Time Series Decomposition
Time series analysis 4 Construction of SARIMA model
Time series analysis # 6 Spurious regression and cointegration
pandas series part 1
Python: Time Series Analysis: Building a SARIMA Model
Python: Time Series Analysis: Stationarity, ARMA / ARIMA Model
Kaggle ~ Housing Analysis ③ ~ Part1
Python time series question
Display TOPIX time series
Time series plot / Matplotlib
Python 3.4 Create Windows7-64bit environment (for financial time series analysis)
Python application: Pandas Part 2: Series
Challenge to future sales forecast: ② Time series analysis using PyFlux
A study method for beginners to learn time series analysis
[Python] Plot time series data
Wrap analysis part1 (data preparation)
Challenge to future sales forecast: ⑤ Time series analysis by Prophet
Challenges for future sales forecasts: (1) What is time series analysis?
[Statistics] [Time series analysis] Plot the ARMA model and grasp the tendency.
Easy time series prediction with Prophet
Time series plot started ~ python edition ~
About time series data and overfitting
Japanese analysis processing using Janome part1
Differentiation of time series data (discrete)
Movement statistics for time series forecasting
LSTM (1) for time series forecasting (for beginners)
Multidimensional data analysis library xarray Part 2
Power of forecasting methods in time series data analysis Semi-optimization (SARIMA) [Memo]
Instantly illustrate the predominant period in time series data using spectrum analysis