-
Notifications
You must be signed in to change notification settings - Fork 3
/
MacroModel_explore_sim.R
611 lines (443 loc) · 16.6 KB
/
MacroModel_explore_sim.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# This script explore economic and financial data and does exploratory modeling
#**********************************************************************
# Notes ####
#**********************************************************************
# A discussion of ARIMA transfer function models:
# https://robjhyndman.com/hyndsight/arimax/
## Issues:
# Simulate ARIMA process with initial values and regressors
#**********************************************************************
# Packages ####
#**********************************************************************
library(tidyverse)
library(broom)
library(readxl)
library(magrittr)
library(stringr)
library(forcats)
library(grid)
library(gridExtra)
library(scales)
library(knitr)
# packages for time series modeling
library(astsa) # companion package
library(TSA) # companion package; arimax: flexible transfer function model
library(tseries) #
library(forecast) # Arima
library(MSwM)
library(TTR)
#library(MSBVAR)
# packages for ts
library(zoo)
library(xts)
library(timetk)
library(tidyquant)
library(lubridate)
library(feather)
library(psych) # describe
# 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 ####
#**********************************************************************
dir_data_raw <- "data_raw/"
dir_data_out <- "data_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)
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)
)
}
#**********************************************************************
# Loading Data ####
#**********************************************************************
# Loading saved data
load(paste0(dir_data_out, "dataAll.RData"))
#**********************************************************************
# Plotting stock returns and volatility ####
#**********************************************************************
# Stock return and volatility with different data frequencies
# Stock returns:
# log total return (price appreciation + dividend)
# Equity premium calculated with different measures of risk free rate
# Frequencies:
# 1. Monthly
# 2. Quarterly
# 3. Yearly
get_logReturn <- function(x){
if(any(x <= 0, na.rm = TRUE)) stop("Nagative value(s)")
log(x/lag(x))
}
Vars <- c("year", "month", "yearMon",
"TBill3m_FRED",
"Tbond10y_FRED",
"LCapStock_TRI",
"LCapStock_CAI",
"CBond_TRI",
"LTGBond_TRI",
"Inflation_Index",
"GDP_FRED")
fn <- function(df, year_range, rolling_width, freq){
df_stock_m <-
df %>%
select(one_of(Vars)) %>%
filter(year %in% year_range) %>%
mutate(return_tot = (1 + get_logReturn(LCapStock_TRI))^freq - 1,
dl_gdp = (1 + get_logReturn(GDP_FRED))^freq - 1,
dl_cbond = (1 + get_logReturn(CBond_TRI))^freq - 1,
dl_gbond = (1 + get_logReturn(LTGBond_TRI))^freq - 1,
return_tot_o = get_logReturn(LCapStock_TRI),
dl_gdp_o = get_logReturn(GDP_FRED),
dl_cbond_o = get_logReturn(CBond_TRI),
dl_gbond_o = get_logReturn(LTGBond_TRI),
TBill3m_FRED = TBill3m_FRED/100,
Tbond10y_FRED = Tbond10y_FRED/100,
ERP_3m = return_tot - TBill3m_FRED,
ERP_10y = return_tot - Tbond10y_FRED) %>%
mutate(sd_return = rollapply(return_tot, rolling_width, sd, align = "right", fill = NA),
mean_return = rollapply(return_tot, rolling_width, mean, align = "right", fill = NA),
sd_ERP_3m = rollapply(ERP_3m, rolling_width, sd, align = "right", fill = NA),
mean_ERP_3m = rollapply(ERP_3m, rolling_width, mean, align = "right", fill = NA),
sd_ERP_10y = rollapply(ERP_10y, rolling_width, sd, align = "right", fill = NA),
mean_ERP_10y = rollapply(ERP_10y, rolling_width, mean, align = "right", fill = NA),
sd_cbond = rollapply(dl_cbond, rolling_width, sd, align = "right", fill = NA),
mean_cbond = rollapply(dl_cbond, rolling_width, mean, align = "right", fill = NA),
sd_gbond = rollapply(dl_gbond, rolling_width, sd, align = "right", fill = NA),
mean_gbond = rollapply(dl_gbond, rolling_width, mean, align = "right", fill = NA),
mean_gdp = rollapply(dl_gdp, rolling_width, mean, align = "right", fill = NA)
)
}
df_stock_m <- fn(df_dataAll, 1953:2015, 12, 12)
df_stock_q <- fn(df_dataAll_q, 1953:2015, 12, 4)
df_stock_y <- fn(df_dataAll_y, 1953:2015, 5, 1)
# save data in feather format for python use
write_feather(df_stock_q, "data_out/df_stock_q.feather" )
df_stock_q$dl_gdp_o
#***********************************************************************************
# Examine simplified approach to modeling GDP and returns jointly ####
#***********************************************************************************
#' Overview of the simplified modeling approach:
#'
#' 1. GDP (quarterly, core variable):
#' - Markov-switching model (AR or RW-drift)
#' - Transition matrix that determines regime-switching behavior
#' - Regime-dependent mean/std/parameters
#' - mean/std based on NBER definition
#' - mean/std based on stock MS model
#' - mean/std based on GDP MS model
#' - Simulated regimes and paths of GDP growth
#' 2. Total stock returns (for now, we may want to model ERP later in order to incorporate inflation)
#' - Markov-switching model: RW-drift
#' - Return regimes are highly aligned with GDP regimes
#' - For simulation: different mean return and std in GDP different GDP regimes
#' 3. Total cbond returns
#' - Show regime-switching behavior, but very different from those for GDP and stock returns.
#' - high return and high volatility regime
#' - low return and low volatility regime
#' - May not be pure random walk within regimes, may need to model MSAR process.
#' - Correlation with stock changes wildly over time, with no obivous pattern.
#' - Simulation stragegy:
#' - 1. modeled and simulated as a seperate MS-AR/DR process, calibrate
#' correlation with stock to historical value/assumed value by setting
#' the correlation between error terms of stock model and bond model.
#' - 2.
#' 4. Return of Portfolio
#' - Construct portfolio returns
#
#' Summary of quaterly stock mean return and std
#'
#' 1. Based on NBER recession periods 1953 - 2015
#' mean std
#' expansion 0.032 0.0686
#' recession -0.014 0.119
#'
#'
#' 2. Based on MS model of stock return 1954-2015
#' mean std
#' expansion: 0.0419 0.0547
#' recession: -0.0192 0.1126
#'
#'
#' 3. Based on MS RW-drift model of GDP (regimes imported from python program)
#'
#' - estimate seed 123 (?a local optimum, but consistent with hamilton1989)
#' mean std
#' expansion: 0.032 0.0685
#' recession: 0.0049 0.1044
#'
#' - estimate seed 127 (?global optimum, matches historical length of recessions best)
#' mean std
#' expansion: 0.028 0.0723
#' recession: 0.0027 0.122
#'
#' Comparing NBER and model recession/expansion period length
#'
#' US recessions NBER:
#' recession expansion
#' Starting 1953: 3.7Q 20.2Q
#' Starting 1980 3.7Q 23.7Q
#'
#'
#' Model recessions
#' recession expansion
#' MSAR4 1.6 19.9
#' MSAR2 1.7 20.9
#' MSAR1 1.8 20.4
#' RW-drift 4.4 11.4
#' RW-drift 3.1 20.7
#' Hamilton1989 4.1 16.2
#***********************************************************************************
# Implementing a simple 2-state Markov chain ####
#***********************************************************************************
# Defining transition probabilities
p12 <- 0.048
p21 <- 0.32
p11 <- 1 - p12
p22 <- 1 - p21
# Use package markovchain
library(markovchain)
gdpStates <- c('0', '1')
byRow <- TRUE
gdpMatrix <- matrix(c(p11, p12,
p21, p22),
byrow = byRow,
nrow = 2)
gdpMatrix
mc_gdp <- new("markovchain",
states = gdpStates,
transitionMatrix = gdpMatrix,
name = 'gdp'
)
mc_gdp %>% plot
summary(mc_gdp)
# simulating with markov chain
rmarkovchain(n = 4*30, object = mc_gdp, t0 = '0')
replicate(10, rmarkovchain(n = 4*30, object = mc_gdp, t0 = '1'))
#***********************************************************************************
# Simulating quarterly stock returns with regime-switching ####
#***********************************************************************************
s.mean_0 <- 0.03
s.std_0 <- 0.07
s.mean_1 <- 0
s.std_1 <- 0.12
gdp.mean_0 <- 0.0095
gdp.std_0 <- 5.463e-05^0.5 # 0.007391211
gdp.mean_1 <- -0.0055
gdp.std_1 <- 5.463e-05^0.5 # 0.007391211
nyear <- 30
nsim <- 2000
set.seed(11)
{
sim_gdp_regimes <- replicate(nsim, rmarkovchain(n = nyear * 4, object = mc_gdp, t0 = '0') %>% as.numeric())
sim_stockreturn_0 <- replicate(nsim, rnorm(nyear*4, s.mean_0, s.std_0))
sim_stockreturn_1 <- replicate(nsim, rnorm(nyear*4, s.mean_1, s.std_1))
sim_gdp_0 <- replicate(nsim, rnorm(nyear*4, gdp.mean_0, gdp.std_0))
sim_gdp_1 <- replicate(nsim, rnorm(nyear*4, gdp.mean_1, gdp.std_1))
}
sim_gdp_regimes
sim_stockreturn_0
sim_stockreturn_1
sim_gdp_0 %>% mean
sim_gdp_1 %>% mean
sim_stockreturn <- (sim_gdp_regimes == 0) * sim_stockreturn_0 + (sim_gdp_regimes == 1) * sim_stockreturn_1
sim_gdp_growth <- (sim_gdp_regimes == 0) * sim_gdp_0 + (sim_gdp_regimes == 1) * sim_gdp_1
sim_stockreturn
sim_gdp_growth
#***********************************************************************************
# Examine recession regime:number and length in 30-year period ####
#***********************************************************************************
get_regimeLength <- function(x, rgm = 1){
#x <- sim_gdp_regimes[,2]
y <- numeric(length(x))
y[1] <- 1
for(i in 2:length(x)){
if(x[i] != x[i-1]) y[i] <- y[i-1] + 1 else y[i] <- y[i-1]
}
df <- data.frame(idx = y, regime = x)
df %<>% filter(regime == rgm) %>%
group_by(idx) %>%
summarise(regime_len = n())
df
r <- df$regime_len
r
}
get_regimeLength(sim_gdp_regimes[,3], 1)
summary_gdpRegime_0 <- sapply(as.data.frame(sim_gdp_regimes), get_regimeLength, rgm = 0)
summary_gdpRegime_1 <- sapply(as.data.frame(sim_gdp_regimes), get_regimeLength, rgm = 1)
# number of recessions and expected length of recession
sapply(summary_gdpRegime_1, length) %>% mean # 5.35 recessions in 30 years (many are very short)
sapply(summary_gdpRegime_1, mean) # average length is 3.1 quarters.
# distribution of recession length (all sims together)
x <- data.frame(n.rec = unlist(summary_gdpRegime_1))
x %>% count(n.rec)
nrow(x)
unlist(summary_gdpRegime_1) %>% hist()
# distribution of average recession length across sims
sapply(summary_gdpRegime_1, mean) %>% hist()
# distribution of number of recessions across sims
sapply(summary_gdpRegime_1, length) %>% hist()
#***********************************************************************************
# Examine simulation results of stock returns ####
#***********************************************************************************
# simulated quarterly stock returns
sim_stockreturn %>% mean
sim_stockreturn %>% sd
sim_stockreturn %>% as.vector() %>% describe
sim_gdp_growth %>% mean
sim_gdp_growth %>% sd
# Convert quarterly returns / growth into annual values
df_sim_stockreturn <- as.data.frame(sim_stockreturn) %>%
mutate(year = rep(1:nyear, each = 4))
df_sim_stockreturn_y <-
df_sim_stockreturn %>%
gather(sim, return, -year) %>%
group_by(sim, year) %>%
summarise(return_y = prod(1+return) - 1 )
df_sim_stockreturn_y$return_y %>% mean # 10.8% mean annual return (quarterly return compounded)
df_sim_stockreturn_y$return_y %>% sd # 17.2% std
df_sim_stockreturn_y$return_y %>% describe
df_sim_stockreturn_y$return_y %>% hist(27)
sim_stockreturn %>% as.vector() %>% hist(27)
rnorm(20000,0.02607146, 0.0790173) %>% hist(27)
rnorm(20000,0.02607146, 0.0790173) %>% quantile(0.10)
sim_stockreturn %>% quantile(0.10)
# historical data (quarterly)
#stock quarterly
df_stock_q$return_tot_o[-1] %>% mean #2.55%
df_stock_q$return_tot_o[-1] %>% sd #7.9%
df_stock_q$return_tot_o[-1] %>% describe # skew -0.92, kurtosis 1.72
rnorm(2000,0.0255,0.0792) %>% describe
# stock annual
df_stock_y$return_tot_o[-1] %>% mean # 10.3%
df_stock_y$return_tot_o[-1] %>% sd # 15.7%
df_stock_y$return_tot_o[-1] %>% describe() # skew -0.38, kurtosis 0.03
rnorm(20000,0.0255,0.0792) %>% describe # skew 0.03, kur
df_stock_y$return_tot_o[-1] %>% plot(type = 'l')
df_stock_y$return_tot_o[-1] %>% hist(27)
df_stock_q$return_tot_o[-1] %>% hist(27)
df_stock_q$return_tot_o[-1] %>% qqnorm
df_stock_q$return_tot_o[-1] %>% qqline
df_stock_y$return_tot_o[-1] %>% qqnorm
df_stock_y$return_tot_o[-1] %>% qqline
df_stock_q$return_tot_o[-1] %>% quantile(0.10)
#cbond
df_stock_q$dl_cbond_o[-1] %>% mean
df_stock_q$dl_cbond_o[-1] %>% sd
#gbond
df_stock_q$dl_gbond_o[-1] %>% mean
df_stock_q$dl_gbond_o[-1] %>% sd
shapiro.test(df_stock_q$return_tot_o)
#***********************************************************************************
# Generating variables with multivariate normal distribution ####
#***********************************************************************************
library(MASS)
# Note
# https://www.r-bloggers.com/simulating-from-the-bivariate-normal-distribution-in-r/
#
mean_stock <- 0.10
mean_bond <- 0.035
std_stock <- 0.17
std_bond <- 0.04
rho <- 0.17
mu <- c(mean_stock, mean_bond)
sigma <- matrix(c(std_stock^2, std_stock*std_bond*rho,
std_stock*std_bond*rho, std_bond^2 ), 2)
mu
sigma
nyear = 30
nsim = 1000
mvrnorm(nyear, mu = mu, Sigma = sigma) # from MASS package
set.seed(10)
sim_errorTerms <- replicate(nsim, mvrnorm(nyear, mu = mu, Sigma = sigma))
sim_error_stock <- sim_errorTerms[, 1, ]
sim_error_bond <- sim_errorTerms[, 2, ]
sim_error_stock %>% dim
sim_error_bond %>% dim
#' Summary
#'
#' - Simulated quarterly returns
#' - Simulated mean and annual returns are reasonable and generally consistent with historical data
#' - Skewness and kurtosis are still much smaller than historical
#' - Compare to normal:
#'
#'
#' - Simulated annual returns
#' - Simulated mean annual return and standard deviation looks reasonable
#' - Kurtosis of historical kurtosis is surprisingly low
#' - compare to normal"
#'
#' - Note:
#' - qarterly returns non-normal and fat tail
#' - It seems that annual returns are normal?
#'
#'
#'
#'Type of returns
#' Frequency:
#' 1. quarterly
#' 2. annual
#' How returns are generated:
#' 1. historical data
#' 2. simulations using gdp regimes
#' 2.1 mean/std from historical regimes
#' 2.2 mean/std from gdp model regimes
#' 2.3 mean/std from stock model regimes
#' 2.4 other scenarios
#' 3.Normal distribution
#'
#'Measures and plots
#' mean
#' std
#' skewness
#' kurtosis
#' 1%, 5% percentiles
#'
#' histogram
#' kernal smoothed curve
#' qqplot
#'