Time Series_2_Multivariate_TBC!!!!
1. Cointegration
n×1 vector yt of time series is cointegrated if each series individually integrated in the order d (d = 1, nonstationary unit-root processes, i.e., random walks; d = 0, stationary process).
1.1 Simulation for Cointegration, based on Hamilton (1994)


#generate the two time series of length 1000
set.seed(20200229) #fix the random seed
N <- 100 #define length of simulation
x <- cumsum(rnorm(N)) #simulate a normal random walk
gamma <- 0.7 #set an initial parameter value
y <- gamma * x + rnorm(N) #simulate the cointegrating series
plot(x, col="black", type='l', lwd=2, ylab='simulated values')
lines(y,col="skyblue", lwd=2)

Both series are individually random walks.
In real world we don't know gamma, so need estimation based on raw data, running linear regression of one series on the other, i.e., Engle-Granger method of testing cointegration.
1.2 Statistical Tests (Augmented Dickey Fuller test)
install.packages('urca')
library('urca')
summary(ur.df(x,type="none"))
summary(ur.df(y,type="none"))
NULL: Unit root exists in the process
Reject NULL if test-statistic is smaller than critical value. Result shows test-statistic is larger than critical value at three usual sig.levels. We can't reject NULL.
Conclusion: Both series are individually unit root process.

1.3 Linear Combination
z <- y - gamma * x
plot(z,type='l')
summary(ur.df(z,type="none"))

zt seems to be a white noise process and rejection of unit root if confirmed by ADF tests:

1.4 Estimate the Cointegrating Relationship using Engle-Granger method
Step 1: Run linear regression yt on xt (simple OLS estimation);
Step 2: Test residuals for the presence of a unit root.
coin <- lm(y ~ x -1) #regression without constant
coin$resid #obtain the residuals
summary(ur.df(coin$resid)) #ADF test of residuals

Reject NULL.
Pair trading (statistical arbitrage) exploits such cointegrating relationship, aiming to set up strategy based on the spread between two time series. If two series are cointegrated, we expect their stationary linear combination to revert to 0. By selling relatively expensive and buying cheaper one, wait for the reversion to make profit.
Intuition: Linear combination of time series (which form cointegrating relationship) is determined by underlying (long-term) economic forces, e.g., same industry companies may grow similarly; spot and forward price of financial product are bound together by no-arbitrage principle; FX rates of interlinked countries tend to move together.
2. Vector Autoregressive Model
2.1 Theory Bgs

Ai are n×n coefficient matrices for all i = 1...p;
ut is a vector white noise process (assumes lack of auto correlation, while allow contemporaneous (同时发生的) correlation between components, i.e., ut has non-diagonal covariance matrix, meaning that contemporaneous dependencies aren't modeled) with positive definite covariance matrix. Thus we can use OLS to estimate equation by equation. (叫做 reduced form VAR models)
另一种叫做 structural VAR form model, SVAR models the contemporaneous effects among variables:

Where: 
Disturbance terms are defined to be uncorrelated, thus are referred to as structural shocks.
2.2 Three-component VAR model (equity return, stock index, US Treasury bond interest rates)
2.2.1 Task: Forecast stock market index by using additional variables and identify impluse responses.
Assume: There exists a hidden long-term relationship between these three components.
2.2.2 Process:
rm(list = ls()) #clear the whole workspace
install.packages('xts');library(xts)
install.packages('vars');library('vars')
install.packages('quantmod');library('quantmod')
getSymbols('SNP', from='2012-01-02', to='2020-02-29')
getSymbols('MSFT', from='2012-01-02', to='2020-02-29')
getSymbols('DTB3', src='FRED') #3-month T-Bill interest rates
chartSeries(MSFT, theme=chartTheme('white'))

Cl(MSFT) #closing prices
Op(MSFT) #open prices
Hi(MSFT) #daily highest price
Lo(MSFT) #daily lowest price
ClCl(MSFT) #close-to-close daily return
Ad(MSFT) #daily adjusted closing price
Subset to obtain period of interests ,while the downloaded prices are supposed to be nonstationary series and should be transformed into stationary series:
#indexing time series data
DTB3.sub <- DTB3['2012-01-02/2020-02-29']
#Calculate returns from Adjusted log series
SNP.ret <- diff(log(Ad(SNP)))
MSFT.ret <- diff(log(Ad(MSFT)))
#replace NA values
DTB3.sub[is.na(DTB3.sub)] <- 0
DTB3.sub <- na.omit(DTB3.sub) # return with incomplete removed
# merge the three databases to get the same length
# innerjoin returns only rows in which one set have matching keys in the other
dataDaily <- na.omit(merge(SNP.ret,MSFT.ret,DTB3.sub), join='inner')
VAR modeling usually deals with lower frequency data, so transform data to monthly (or quarterly) frequency.
#obtain monthly data, package xts
SNP.M <- to.monthly(SNP.ret)$SNP.ret.Close
MSFT.M <- to.monthly(MSFT.ret)$MSFT.ret.Close
DTB3.M <- to.monthly(DTB3.sub)$DTB3.sub.Close
# allow a max of 4 lags
# choose the model with best(lowest Akaike Information Criterion value)
var1 <- VAR(dataDaily, lag.max=4, ic="AIC")
Or see multiple information criteria:
VARselect(dataDaily,lag.max=4)

summary(var1)



coef(var1) #concise summary of the estimated variables
residuals(var1) #list of residuals (of the corresponding ~lm)
fitted(var1) #list of fitted values
Phi(var1) #coefficient matrices of VMA representation
plot(var1, plot.type='multiple') #Diagram of fit and residuals for each variables



#confidence interval for bootstrapped error bands
var.irf <- irf(var1, ci=0.9)
plot(var.irf)



Number of required restrictions for SVAR model is K(K-1)/2, so in our case is 3.
Time Series_2_Multivariate_TBC!!!!的更多相关文章
随机推荐
- 201771010135杨蓉庆《面向对象程序设计(java)》第四周学习总结
学习目标 1.掌握类与对象的基础概念,理解类与对象的关系: 2.掌握对象与对象变量的关系: 3.掌握预定义类的基本使用方法,熟悉Math类.String类.math类.Scanner类.LocalDa ...
- pta谁先倒
传送门 #include <stdio.h> int main() { int x,y;//酒量 scanf("%d%d",&x,&y); int n; ...
- 吴裕雄 python 神经网络——TensorFlow variables_to_restore函数的使用样例
import tensorflow as tf v = tf.Variable(0, dtype=tf.float32, name="v") ema = tf.train.Expo ...
- 关于雷达(Radar)信道
有些时候,我们在实际的无线网络中,会遇到无线信道一致flapping的情况,即便我们自定义了信道的,发现也会出现flapping.如果这种情况,可能需要确认是否你使用的信道上检测到了雷达. 这里记录一 ...
- ANSYS 非线性材料模型简介1 ---常用弹塑性模型
目录 1. 材料非线性 2. 三个准则 2.1 屈服准则 2.2 流动准则 2.3 强化准则 3. 常用弹塑性模型 3.1 双线性等向强化 3.2 多线性等向强化 3.3 非线性等向强化 3.4 双线 ...
- 使用docker构建第一个spring boot项目
在看了一些简单的docker命令之后 打算自己尝试整合一下docker+spring boot项目本文是自己使用docker+spring boot 发布一个项目1.docker介绍 docke是提供 ...
- centos解决bash: telnet: command not found...&& telnet: connect to address 127.0.0.1: Connection refused拒绝连接
检查telnet是否已安装: [root@hostuser src]# rpm -q telnet-serverpackage telnet-server is not installed[root@ ...
- 什么是redis事务
一.什么是redis事务? 可以一次性执行多条命令,本质上是一组命令的集合.一个事务中的所有命令都会序列化,然后按顺序地串行化执行,而不会被插入其他命令 二.Redis 事务可以做什么? 一个队列中, ...
- np.ndarray与PIL.Image对象相互转换
Image对象有crop功能,也就是图像切割功能,但是使用opencv读取图像的时候,图像转换为了np.adarray类型,该类型无法使用crop功能,需要进行类型转换,所以使用下面的转换方式进行转换 ...
- JS闭包(3)
在将内部函数作为函数的返回值的时候,由于闭包的存在会携带上内部函数所使用的外部函数的变量,如果这些变量很多或者很大,那么在使用完返回的内部函数后最好将其置为null以便释放闭包中的携带变量,一面造成内 ...