-
Notifications
You must be signed in to change notification settings - Fork 1
/
causality_sanctions_forex.R
243 lines (198 loc) · 7.9 KB
/
causality_sanctions_forex.R
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
library(dplyr)
library(readxl)
library(xts)
library(fUnitRoots)
library(urca)
library(vars)
library(aod)
library(zoo)
library(tseries)
library(bootUR)
library(Rbeast)
library(purrr)
library(readr)
# Prevent scientific notation
options(scipen=999)
# Decimals to be saved
n_dec <- 7
# Connect to DB
data_path <- file.path(getwd(), "elaborations")
# Get sanctions data and forex data
sanctions_df <- read_excel("official_data/russia-sanction-timeline.xlsx",
col_types = c("date", "text", "text"))
forex_df <- read_csv("official_data/RUBUSD.csv",
col_types = cols(Date = col_date(format = "%Y-%m-%d"),
Open = col_skip(), High = col_skip(),
Low = col_skip(), `Adj Close` = col_skip(),
Volume = col_skip()))
# Manipulate sanctions data
sanctions_all_df <- sanctions_df %>%
group_by(week = cut(Date, "week")) %>%
mutate(date = as.Date(week) + 6) %>% # Move reference to the end of the week
mutate(total=n()) %>%
ungroup() %>%
distinct(date, total) %>%
filter(date < as.Date("2022-10-10")) # Removing data after CPI cutoff...to be updated over time
sanctions_fin_df <- sanctions_df %>%
filter(`Sanction type` == "financial") %>%
group_by(week = cut(Date, "week")) %>%
mutate(date = as.Date(week) + 6) %>% # Move reference to the end of the week
mutate(total=n()) %>%
ungroup() %>%
distinct(date, total) %>%
filter(date < as.Date("2022-10-10")) # Removing data after CPI cutoff...to be updated over time
sanctions_trade_df <- sanctions_df %>%
filter(!`Sanction type` == "financial") %>%
group_by(week = cut(Date, "week")) %>%
mutate(date = as.Date(week) + 6) %>% # Move reference to the end of the week
mutate(total=n()) %>%
ungroup() %>%
distinct(date, total) %>%
filter(date < as.Date("2022-10-10")) # Removing data after CPI cutoff...to be updated over time
sanctions_all_ts <- as.xts(subset(sanctions_all_df, select=-c(date)), order.by = sanctions_all_df$date)
# add missing dates
sanctions_all_ts <- merge(sanctions_all_ts, seq.Date(as.Date("2021-02-21"), as.Date("2022-10-09"), by = "week"))
sanctions_all_ts <- na.fill(sanctions_all_ts, 0)
sanctions_fin_ts <- as.xts(subset(sanctions_fin_df, select=-c(date)), order.by = sanctions_fin_df$date)
# add missing dates
sanctions_fin_ts <- merge(sanctions_fin_ts, seq.Date(as.Date("2021-02-21"), as.Date("2022-10-09"), by = "week"))
sanctions_fin_ts <- na.fill(sanctions_fin_ts, 0)
sanctions_trade_ts <- as.xts(subset(sanctions_trade_df, select=-c(date)), order.by = sanctions_trade_df$date)
# add missing dates
sanctions_trade_ts <- merge(sanctions_trade_ts, seq.Date(as.Date("2021-02-21"), as.Date("2022-10-09"), by = "week"))
sanctions_trade_ts <- na.fill(sanctions_trade_ts, 0)
# Manipulate Forex data ans get structural break probability via BEAST
forex_grouped_df <- forex_df %>%
filter(Date >= as.Date("2021-02-14")) %>%
filter(Date < as.Date("2022-10-10")) %>% # Removing data after CPI cutoff...to be updated over time
group_by(week = cut(Date, "week")) %>%
mutate(date = as.Date(week) + 6) %>% # Move reference to the end of the week
mutate(forex=mean(Close)) %>%
ungroup() %>%
distinct(date, forex)
forex_ts <- as.xts(forex_grouped_df$forex, order.by = forex_grouped_df$date)
names(forex_ts) <- "forex"
# Compute Beast algorithm
res_beast <- beast(forex_ts, season = "none")
forex_ts$cp_prob <- round(res_beast[["trend"]][["cpOccPr"]], n_dec)
##### ALL SANCTIONS #####
# Check order of integration for the time series
forex_order_integration <- order_integration(
merge(sanctions_all_ts$total, forex_ts$cp_prob),
max_order = 5)
# Select max order of integration
var_forex_select <- VARselect(merge(sanctions_all_ts$total, forex_ts$cp_prob),
lag.max = 23,
type = "both")
# Selecting the lag for VAR
AIC_forex <- var_forex_select$selection[[1]]
forex_max_ord <- max(forex_order_integration$order_int)
total_forex_lag <- AIC_forex + forex_max_ord
# Performing VAR
var_forex <- VAR(
merge(sanctions_all_ts$total, forex_ts$cp_prob),
p=total_forex_lag,
type="both")
forex_serial <- serial.test(var_forex, type="BG", lags.pt = 52, lags.bg=52)
if(1/roots(var_forex)[[1]] > 1 || 1/roots(var_forex)[[2]] > 1){
roots_forex <- "stable"
} else {
roots_forex <- "not stable"
}
# Causality from first column (sanctions) to second (breaks)
wt2_forex<-wald.test(
b=coef(var_forex$varresult[[2]]),
Sigma=vcov(var_forex$varresult[[2]]),
Terms=c(seq(1, AIC_forex*2, 2)))
forex_causality_p <- wt2_forex[["result"]][["chi2"]][["P"]]
# Saving results
results1_df <- data.frame(
item = c("forex_all"),
aic_lag = c(AIC_forex),
max_int_order = c(forex_max_ord),
serial_ac_p = c(forex_serial$serial$p.value),
root = c(roots_forex),
causality_p = c(forex_causality_p)
)
##### FINANCIAL SANCTIONS #####
# Check order of integration for the time series
forex_order_integration <- order_integration(
merge(sanctions_fin_ts$total, forex_ts$cp_prob),
max_order = 5)
# Select max order of integration
var_forex_select <- VARselect(merge(sanctions_fin_ts$total, forex_ts$cp_prob),
lag.max = 12,
type = "both")
# Selecting the lag for VAR
AIC_forex <- var_forex_select$selection[[1]]
forex_max_ord <- max(forex_order_integration$order_int)
total_forex_lag <- AIC_forex + forex_max_ord
# Performing VAR
var_forex <- VAR(
merge(sanctions_fin_ts$total, forex_ts$cp_prob),
p=total_forex_lag,
type="both")
forex_serial <- serial.test(var_forex, type="BG", lags.pt = 52, lags.bg=52)
if(1/roots(var_forex)[[1]] > 1 || 1/roots(var_forex)[[2]] > 1){
roots_forex <- "stable"
} else {
roots_forex <- "not stable"
}
# Causality from first column (sanctions) to second (breaks)
wt2_forex<-wald.test(
b=coef(var_forex$varresult[[2]]),
Sigma=vcov(var_forex$varresult[[2]]),
Terms=c(seq(1, AIC_forex*2, 2)))
forex_causality_p <- wt2_forex[["result"]][["chi2"]][["P"]]
# Saving results
results2_df <- data.frame(
item = c("forex_fin"),
aic_lag = c(AIC_forex),
max_int_order = c(forex_max_ord),
serial_ac_p = c(forex_serial$serial$p.value),
root = c(roots_forex),
causality_p = c(forex_causality_p)
)
##### TRADE SANCTIONS #####
# Check order of integration for the time series
forex_order_integration <- order_integration(
merge(sanctions_trade_ts$total, forex_ts$cp_prob),
max_order = 5)
# Select max order of integration
var_forex_select <- VARselect(merge(sanctions_trade_ts$total, forex_ts$cp_prob),
lag.max = 12,
type = "both")
# Selecting the lag for VAR
AIC_forex <- var_forex_select$selection[[1]]
forex_max_ord <- max(forex_order_integration$order_int)
total_forex_lag <- AIC_forex + forex_max_ord
# Performing VAR
var_forex <- VAR(
merge(sanctions_trade_ts$total, forex_ts$cp_prob),
p=total_forex_lag,
type="both")
forex_serial <- serial.test(var_forex, type="BG", lags.pt = 52, lags.bg=52)
if(1/roots(var_forex)[[1]] > 1 || 1/roots(var_forex)[[2]] > 1){
roots_forex <- "stable"
} else {
roots_forex <- "not stable"
}
# Causality from first column (sanctions) to second (breaks)
wt2_forex<-wald.test(
b=coef(var_forex$varresult[[2]]),
Sigma=vcov(var_forex$varresult[[2]]),
Terms=c(seq(1, AIC_forex*2, 2)))
forex_causality_p <- wt2_forex[["result"]][["chi2"]][["P"]]
# Saving results
results3_df <- data.frame(
item = c("forex_trade"),
aic_lag = c(AIC_forex),
max_int_order = c(forex_max_ord),
serial_ac_p = c(forex_serial$serial$p.value),
root = c(roots_forex),
causality_p = c(forex_causality_p)
)
# Concatenate results and save as csv
results_df <- bind_rows(results1_df, results2_df, results3_df)
write_csv(results_df,
file = file.path(data_path, "sanctions_forex_causality.csv"))