-
Notifications
You must be signed in to change notification settings - Fork 3
/
XX_nonNormal_fatTails(1).R
382 lines (276 loc) · 11 KB
/
XX_nonNormal_fatTails(1).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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# This script is for modeling tax revenue of stylized governments
#**********************************************************************
# Packages ####
#**********************************************************************
library(markovchain) # Markov chain object
library(MASS) # multivariate normal generator, must be loaded before tidyverse, otherwise 'select' will be masked
library(tidyverse)
library(broom)
library(readxl)
library(magrittr)
library(ggrepel)
library(stringr)
library(forcats)
library(grid)
library(gridExtra)
library(scales)
library(knitr)
library(xlsx)
# packages for econometric and time series modeling
library(plm)
library(astsa) # companion package
library(TSA) # companion package; arimax: flexible transfer function model
library(tseries) #
library(forecast) # Arima
library(MSwM)
library(TTR)
library(dynlm)
library(broom)
#library(MSBVAR)
# packages for ts
library(zoo)
library(xts)
library(timetk)
library(tidyquant)
library(lubridate)
library(feather)
library(psych) # describe
options(tibble.print_max = 60, tibble.print_min = 60)
# check tidyquant, timetk, sweep (broom ), tibbletime
# Intro to zoo cran.r-project.org/web/packages/zoo/vignettes/zoo-quickref.pdf
# sweep: http://www.business-science.io/code-tools/2017/07/09/sweep-0-1-0.html
#**********************************************************************
# Global settings and tools ####
#**********************************************************************
dir_data_raw <- "data_raw/"
dir_data_out <- "data_out/"
dir_fig_out <- "policyBrief_out/"
# NBER recession periods, post-WWII
recessionPeriods <-
matrix(c(
1953+2/4, 1954+2/4,
1957+3/4, 1958+2/4,
1960+2/4, 1961+1/4,
1969+4/4, 1970+4/4,
1973+4/4, 1975+1/4,
1980+1/4, 1980+3/4,
1981+3/4, 1982+4/4,
1990+3/4, 1991+1/4,
2001+1/4, 2001+4/4,
2007+4/4, 2009+2/4
) , ncol = 2, byrow = T) %>%
as.data.frame() %>%
rename(peak = V1,
trough = V2) %>%
mutate(peak = peak - 1/4,
trough = trough - 1/4)
get_logReturn <- function(x){
if(any(x <= 0, na.rm = TRUE)) stop("Nagative value(s)")
log(x/lag(x))
}
# RIG colors and theme
RIG.blue <- "#003598"
RIG.red <- "#A50021"
RIG.green <- "#009900"
RIG.yellow <- "#FFFF66"
RIG.purple <- "#9966FF"
RIG.yellow.dark <- "#ffc829"
RIG.orange <- "#fc9272"
demo.color6 <- c(RIG.red,
RIG.orange,
RIG.purple,
RIG.green ,
RIG.blue,
RIG.yellow.dark)
RIG.theme <- function() {
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line(size = 0.5, color = "gray80"),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
plot.caption = element_text(hjust = 0, size = 9)
)
}
RIG.themeLite <- function() {
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
plot.caption = element_text(hjust = 0, size = 9)
)
}
#**********************************************************************
# Outline ####
#**********************************************************************
# Goals:
# Path of state tax revenue derived from simulated GDP, stock returns, and estimated elasticities.
# What to have in the results
# 1. a deterministic simulation with constant GDP growth and stock return
# 2. A single stochastic simulation, how different stylized governments respond to GDP and stocks differently
# 3. A scenario, if hard to find a stochastic simulation that makes sense, construct a scenario that is similar to history.
# 4. Distribution of 2000 simulations (quanitles)
# 5. Risk measures: probability of sharp decline in tax revenue, and difference stylized governments.
#**********************************************************************
# Importing simulations of GDP growth and stock return ####
#**********************************************************************
# Notes
# 1. Simulations are generated by Model_simulation(3).R
# 2. What inputs to include:
# - simulated path of real GDP growth
# - Recession and expansion periods in each simulation
# - simulated path stock return
# - simulated path of bond return
# Loading simulation outputs:
load("policyBrief_out/simulation_MS1.RData")
# dfs to use:
# df_sim_gdp_y
# df_sim_gdp_regimes_y
# df_sim_stockreturn_y
# df_sim_bondreturn_y
df_sim <-
df_sim_gdp_regimes_y %>%
left_join(df_sim_gdp_y %>% rename(gdp_chg = return_y)) %>%
left_join(df_sim_stockreturn_y %>% rename(stockreturn = return_y)) %>%
left_join(df_sim_bondreturn_y %>% rename(bondreturn = return_y)) %>%
ungroup() %>%
mutate(sim = str_extract(sim, "\\d+") %>% as.numeric)
df_sim %>% head
# This module create investment return series.
ecdf_fun <- function(x,perc) ecdf(x)(perc)
load("policyBrief_out/simulation_MS1.RData")
load("Data_out/dataAll.RData")
# #**************************************************************************************
# # Fat-tails in simulated returns ####
# #**************************************************************************************
#
# ## simulated data
# df_sim_gdp_y %<>% as.data.frame() %>% rename(gdp_chg = return_y)
# df_sim_stockreturn_y %<>% as.data.frame() %>% rename(stock_return = return_y)
# df_sim_bondreturn_y %<>% as.data.frame() %>% rename(bond_return = return_y)
#
# df_sim <- df_sim_gdp_y %>%
# left_join(df_sim_stockreturn_y) %>%
# left_join(df_sim_bondreturn_y) %>%
# mutate(port60_40_return = 0.6*stock_return + 0.4*bond_return)
#
#
# mean_sim_stock <- df_sim$stock_return %>% mean
# sd_sim_stock <- df_sim$stock_return %>% sd
#
# mean_port60_40 <- df_sim$port60_40_return %>% mean
# sd_port60_40 <- df_sim$port60_40_return %>% sd
#
#
# df_sim %<>% mutate(port_norm = rnorm(nrow(df_sim), mean_port60_40, sd_port60_40),
# stock_norm = rnorm(nrow(df_sim), mean_sim_stock, sd_sim_stock))
#
#
# qts <- c(0.005, 0.01, 0.05, 0.25, 0.5, 0.75, 0.95, 0.99, 0.995)
#
# # comparing percentiles
# quantile(df_sim$stock_return, qts)
# quantile(df_sim$stock_norm, qts)
#
# quantile(df_sim$port60_40_return, qts)
# quantile(df_sim$port_norm, qts)
#
#
# df_probSim <-
# df_sim %>%
# select(stock_return, stock_norm, port60_40_return, port_norm) %>%
# gather(var, value) %>%
# group_by(var) %>%
# summarise(prob40 = ecdf_fun(value, -0.4),
# prob30 = ecdf_fun(value, -0.3),
# prob20 = ecdf_fun(value, -0.2),
# prob10 = ecdf_fun(value, -0.1))
#**************************************************************************************
# Fat tails in bootstrapped returns ####
#**************************************************************************************
nsim <- 5000
# Historical data
df_hist <-
df_dataAll_y %>%
select(year, LCapStock_TRI, LTGBond_TRI, CBond_TRI) %>%
mutate_at(vars(-year), funs(log(./lag(.)))) %>%
mutate(mix_70_30_hist = 0.7 * LCapStock_TRI + 0.3 * LTGBond_TRI) %>%
filter(year %in% 1955:2015)
{
set.seed(123)
df_boot <- df_hist[c("LCapStock_TRI", "LTGBond_TRI")][sample(1:nrow(df_hist), nsim, replace = TRUE), ] %>%
rename(stock_boot = LCapStock_TRI,
GBond_boot = LTGBond_TRI) %>%
mutate(mix70_30_boot = 0.7*stock_boot + 0.3*GBond_boot)
# stock_boot <- sample(df_hist$LCapStock_TRI, nrow(df_sim), replace = TRUE)
# GBond_boot <- sample(df_hist$LTGBond_TRI, nrow(df_sim), replace = TRUE)
# boot_port60_40 <- 0.6*boot_stock + 0.4*boot_GBond
# Mean, sd, and correlation coefficient from strapped sample
mean_boot_stock <- mean(df_boot$stock_boot); mean_boot_stock
sd_boot_stock <- sd(df_boot$stock_boot); sd_boot_stock
mean_boot_GBond <- mean(df_boot$GBond_boot); mean_boot_GBond
sd_boot_GBond <- sd(df_boot$GBond_boot); sd_boot_GBond
cov_stockBond <- cov(df_boot$stock_boot, df_boot$GBond_boot); cov_stockBond
cor_stockBond <- cor(df_boot$stock_boot, df_boot$GBond_boot); cor_stockBond
# Generating stock and bond returns with Multivariate Normal distribution
sim_normal <- mvrnorm(nsim,
mu = c(mean_boot_stock, mean_boot_GBond),
Sigma = matrix(c(sd_boot_stock^2, cov_stockBond, cov_stockBond, sd_boot_GBond^2 ), 2))
}
sim_normal[,1] %>% mean; sim_normal[,1] %>% sd
sim_normal[,2] %>% mean; sim_normal[,2] %>% sd
cor(sim_normal[,1], sim_normal[,2])
df_boot %<>%
mutate(stock_norm = sim_normal[,1],
GBond_norm = sim_normal[,2],
mix_70_30_norm = 0.7*stock_norm + 0.3*GBond_norm)
df_probBoot <-
df_boot %>%
select(stock_boot, stock_norm, mix70_30_boot, mix_70_30_norm) %>%
gather(var, value) %>%
group_by(var) %>%
summarise(prob40 = ecdf_fun(value, -0.4),
prob30 = ecdf_fun(value, -0.3),
prob20 = ecdf_fun(value, -0.2),
prob10 = ecdf_fun(value, -0.1))
df_probBoot
df_boot %>%
select(stock_boot, stock_norm) %>%
gather(var, value) %>%
ggplot(aes(x = value, color = var)) +
stat_ecdf()
df_boot %>%
select(stock_boot, stock_norm) %>%
gather(var, value) %>%
ggplot(aes(x = value, color = var)) +
geom_density()
#df_hist$LCapStock_TRI %>% sd
df_stock <- data.frame(x = seq(-0.7, 0.7, 0.01), pdf_norm = dnorm(seq(-0.7, 0.7, 0.01), mean = mean_boot_stock, sd = sd_boot_stock))
df_hist %>%
ggplot(aes(x = LCapStock_TRI)) + theme_bw() + RIG.themeLite() +
geom_density(color = "blue") +
geom_line(aes(x = x, y = pdf_norm), data = df_stock, color = "red") +
scale_x_continuous(breaks = seq(-1, 1, 0.1)) +
labs(x = "Rate of return",
title = "Comparing empirical distribution of annual stock return \nand normal distribution")
df_GBond <- data.frame(x = seq(-0.7, 0.7, 0.01), pdf_norm = dnorm(seq(-0.7, 0.7, 0.01), mean = mean_boot_GBond, sd = sd_boot_GBond))
df_hist %>%
ggplot(aes(x = LTGBond_TRI)) + theme_bw() + RIG.themeLite() +
geom_density(color = "blue") +
geom_line(aes(x = x, y = pdf_norm), data = df_GBond, color = "red") +
scale_x_continuous(breaks = seq(-1, 1, 0.1)) +
labs(x = "Rate of return") +
labs(x = "Rate of return",
title = "Comparing empirical distribution of annual long-term government bond return \nand normal distribution")
mean_hist_mix <- mean(df_hist$mix_70_30_hist); mean_hist_mix
sd_hist_mix <- sd(df_hist$mix_70_30_hist); sd_hist_mix
df_mix <- data.frame(x = seq(-0.7, 0.7, 0.01), pdf_norm = dnorm(seq(-0.7, 0.7, 0.01), mean = mean_hist_mix, sd = sd_hist_mix))
df_hist %>%
ggplot(aes(x = mix_70_30_hist)) + theme_bw() + RIG.themeLite() +
geom_density(color = "blue") +
geom_line(aes(x = x, y = pdf_norm), data = df_mix, color = "red") +
scale_x_continuous(breaks = seq(-1, 1, 0.1)) +
labs(x = "Rate of return",
title = "Comparing empirical distribution of return of a 70/30 portfolio \nand normal distribution")
write.xlsx2(df_probSim, "Data_SimMacro/Table_probsFatTail.xlsx", sheet = "sim")
write.xlsx2(df_probBoot, "Data_SimMacro/Table_probsFatTail.xlsx", sheet = "boot", append = T)