-
Notifications
You must be signed in to change notification settings - Fork 38
/
README.Rmd
209 lines (150 loc) · 8.96 KB
/
README.Rmd
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
---
title: "frequencyConnectedness"
output: github_document
---
[![Build Status](https://travis-ci.org/tomaskrehlik/frequencyConnectedness.svg?branch=master)](https://travis-ci.org/tomaskrehlik/frequencyConnectedness.svg?branch=master)
A package implementing frequency dependent connectedness due to [Barunik, Krehlik (2018)][BK2018] as well as the traditional definitions of [Diebold, Yilmaz (2009, 2012)][DY09]. See the papers for detailed description.
## Installation
The stable version can be installed from `CRAN` by the standard means of using `install.packages("frequencyConnectedness")`. If there is any other development version, you can install it using the following instructions.
Be sure to have installed the `devtools` package that allows you to install packages from Github directly. To install the version from branch `dev` do
````{r, eval = F}
# install.packages("devtools")
library(devtools)
install_github("tomaskrehlik/frequencyConnectedness", tag = "dev")
````
## Usage
Currently the package works in close cooperation with the `vars`, `urca`, and `BigVAR` packages. In general, if you have any model that can produce the forecast error variance decomposition, it can be relatively easily made to work with this package. Let me know by filing an issue, if that is the case and I will try to incorporate it.
For the time being the following is available:
- Traditional estimation of VAR
- Fitting of the VECM model
- Using `BigVAR` to fit VAR models with various penalization schemes
For the illustration purposes we include some simulated data and volatilities data from the [Ox-Man institute](http://realized.oxford-man.ox.ac.uk/).
Let's walk through some basics. First load packages and get some data.
````{r}
library(frequencyConnectedness)
data(exampleSim)
# Shorten the data, rolling estimation takes quite some time
exampleSim <- exampleSim[1:600,]
````
Then compute a system estimate on which the computation of connectedness is based:
````{r}
# Compute the VAR(2) estimate with constant and save results
est <- VAR(exampleSim, p = 2, type = "const")
# Alternatively, you could use VECM
# est <- vec2var(ca.jo(exampleSim, ecdet = "trend", K = 2), r = 1)
````
Then use the estimate to compute the connectedness measures.
First, the traditional overall measures that are not frequency dependent as in Diebold and Yilmaz, also with the possibility of nullifying the cross correlation elements. These commands print out the table and all the relevant measures.
````{r}
# Compute traditional spillovers
spilloverDY09(est, n.ahead = 100, no.corr = F)
spilloverDY12(est, n.ahead = 100, no.corr = F)
spilloverDY09(est, n.ahead = 100, no.corr = T)
spilloverDY12(est, n.ahead = 100, no.corr = T)
````
If you save them, you can use the functions `overall, to, from, net, pairwise` to extract the spillovers in numeric form
````{r}
sp <- spilloverDY12(est, n.ahead = 100, no.corr = T)
overall(sp)
to(sp)
from(sp)
net(sp)
pairwise(sp)
````
Next, we can decompose the measure on desired frequencies and get the frequency dependent measures.
````{r}
# Get the frequency connectedness on partition (pi,pi/4), (pi/4,0), roughly
# corresponding to movements of 1 to 4 days and 4 to longer.
bounds <- c(pi+0.00001, pi/4, 0)
spilloverBK09(est, n.ahead = 100, no.corr = F, partition = bounds)
spilloverBK12(est, n.ahead = 100, no.corr = F, partition = bounds)
spilloverBK09(est, n.ahead = 100, no.corr = T, partition = bounds)
spilloverBK12(est, n.ahead = 100, no.corr = T, partition = bounds)
````
Note that the bounds should cover the range `(1.001, 0)*pi`, because the overall variance of the system is computed over these frequencies. (So if you wanted to remove the trend from computations, you could use `(1.001, 0.01)*pi` and the computation will ignore the variance created around the zero frequency.) Again, if you save the outputs from the `spillover....` function, you can evaluate the `overall, to, from, net, pairwise` to get the relevant tables.
Moreover, if you want to aggregate the behaviour of some of the bands, you can do:
````{r}
# Get the frequency connectedness on partition (pi,pi/4), (pi/4,0), roughly
# corresponding to movements of 1 to 4 days and 4 to longer.
bounds <- c(pi+0.00001, pi/4, pi/10, 0)
spilloverBK12(est, n.ahead = 100, no.corr = F, partition = bounds)
collapseBounds(spilloverBK12(est, n.ahead = 100, no.corr = F, partition = bounds), 1:2)
````
In many cases, one is interested in the dynamics of the connectedness. This can be achieved within the package by the following commands.
````{r}
# Get the rolling window estimates
params_est = list(p = 2, type = "const")
sp <- spilloverRollingDY09(exampleSim, n.ahead = 100, no.corr = F, "VAR", params_est = params_est, window = 100)
# alternatively for co-integration you could do
# coint_est <- function(data, r) {
# return(vec2var(ca.jo(data, ecdet = "trend", K = 2), r = r))
# }
# params_est = list(r = 1)
# sp <- spilloverRollingDY09(exampleSim, n.ahead = 100, no.corr = F, "coint_est", params_est = params_est, window = 100)
````
In general, the `spilloverRolling....` function takes the following arguments:
- data, as `exampleSim`
- the arguments for relevant spillover function, as `n.ahead, no.corr`, and alternatively `partition` in case of the `BK` variant.
- window, what window you should roll
- name of function used for estimates, in this case `"VAR"`, and list of parameters for this function called `params_est`
Using this, one can plot the resulting spillover measures.
````{r}
plotOverall(sp)
plotTo(sp)
plotFrom(sp)
plotNet(sp)
plotPairwise(sp)
````
It is generally not a good idea to print all the spillover tables as they are not informative.
To make your own rolling estimate, let's follow this example. First, we start with construction of unconditional estimate and then use the same function for the rolling estimate. We perform VAR-LASSO estimation on a big system of log-volatilities of financial indices with automatic selection of the LASSO penalty using cross-validation.
````{r}
# Example of usage of BigVAR package on the volatilities data that are included
library(BigVAR)
data(volatilities)
big_var_est <- function(data) {
Model1 = constructModel(as.matrix(data), p = 4, struct = "Basic", gran = c(50, 50), VARX = list(), verbose = F)
Model1Results = cv.BigVAR(Model1)
}
# Perform the estimation
oo <- big_var_est(log(volatilities[apply(volatilities>0, 1, all),]))
spilloverDY12(oo, n.ahead = 100, no.corr = F)
spilloverBK12(oo, n.ahead = 100, no.corr = F, partition = bounds)
# Now use the same function to perform the rolling estimation.
# The original estimation call was:
# big_var_est(log(volatilities[apply(volatilities>0, 1, all),]))
# so our data are:
# log(volatilities[apply(volatilities>0, 1, all),]) (we only use 1:150) because it takes a lot of time to fit
# n.ahead, no.corr, and window are self explanatory.
# name of the function to use for estimation is the big_var_est.
sp <- spilloverRollingBK12(log(volatilities[apply(volatilities>0, 1, all),])[1:150, ], n.ahead = 100, no.corr = F, func_est = "big_var_est", params_est = list(), window = 100, partition = bounds)
plotOverall(sp)
# I only plot 5 of the To indicators as plotting all of them is not nice
plotTo(sp, which = 1:5)
# You can extract the to spillovers
head(to(sp)[[1]])
````
If you have more cores at your disposal as is usual in the computers nowadays, it is beneficial to use them through `parallel` package especially in case of rolling estimation. If you use two cores it usually almost doubles the speed. For example
````{r, eval = F}
library(parallel)
library(frequencyConnectedness)
exampleSim <- exampleSim[1:600,]
params_est = list(p = 2, type = "const")
# Export the relevant variables to the cluster so that it can use them
cl <- makeCluster(16)
clusterExport(cl, c("params_est", "exampleSim"))
sp <- spilloverRollingDY09(exampleSim, n.ahead = 100, no.corr = F, "VAR", params_est = params_est, window = 100, cluster=cl)
stopCluster(cl)
````
## Replication of paper and tests
I will release later some codes that replicat papers that we wrote using this package and the methodology.
If you would be interested in having your script included, write me an e-mail, or create an issue.
Because the package might change in the future, there is a set of test to always preserve the integrity of the original functions. You can read what is tested in the [testfile](tests/testthat/test-basic.r). Also provided that you have the `testthat` package installed, you can run the tests yourself.
````{r, eval=F}
library(frequencyConnectedness)
library(testthat)
test_package("frequencyConnectedness")
````
## License
This package is free and open source software, licensed under GPL (>= 2).
[BK2018]: https://academic.oup.com/jfec/article-abstract/16/2/271/4868603?redirectedFrom=fulltext "Barunik, J., Krehlik, T., Measuring the Frequency Dynamics of Financial Connectedness and Systemic Risk"
[DY09]: http://www.sciencedirect.com/science/article/pii/S016920701100032X "Diebold, F. X., Yilmaz, K., Better to give than to receive: Predictive directional measurement of volatility spillovers"