-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWilkie_explore.R
1352 lines (986 loc) · 47.9 KB
/
Wilkie_explore.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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
# packages for
library(zoo)
library(xts)
library(timetk)
library(tidyquant)
library(lubridate)
# 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/"
#**********************************************************************
# Loading Data ####
#**********************************************************************
# Loading saved data
load(paste0(dir_data_out, "dataAll.RData"))
#**********************************************************************
# Selecting data for Wilkie-like models ####
#**********************************************************************
# Data needed:
# 1. Inflation:
# - SBBI inflation index
# - FRED CPI-U, seasonally adjusted
# - FRED CPI_U, not seasonally adjusted
# 2. Equity total return index (large cap)
# - SBBI total return index
# - SP500TR index
# 3. Equity price index
# - SBBI capital appreciation index
# - SP500 index
# 4. Long-term bond yield index
# - x SBBI long-term corb bond yield
# - x SBBI long-term gov bond yield
# - Moody's long-term bond yield
# 5. Long-term bond total return
# - SBBI long-term corb bond total return index
# - SBBI long-term gov bond total return index
# 6. GDP
# - FRED quarterly GDP
# - x Stock-Watson monthly GDP
# - x MA monthly GDP
vars_wilkie <- c("Inflation_Index", # SBBI price index, based on CPI-U, not seasonally adjusted
"CPIU_SA_FRED", # CPI-U, seasonally adjusted
"CPIU_NA_FRED", # CPI-U, not seasonally adjusted
"LCap_TRI", # SBBI Large Cap total return
"LCap_CAI", # SBBI Large Cap capital appreciation
"CBond_TRI", # SBBI corporate bond total return index
"LTGBond_TRI", # SBBI government bond total return index
"CBond_Yield_AAA", # Moody's AAA long-term bond yield
"GDP_FRED" # GDP
)
# df_dataWilkie <- df_dataAll %>% select(year, month, yearMon, one_of(vars_wilkie))
#**********************************************************************
# GDP growth ####
#**********************************************************************
# Quarterly GDP
df_GDP_q <-
df_dataAll_q %>%
select(yearMon,
year, month,
GDP = GDP_FRED) %>%
mutate(dl_GDP = log(GDP/lag(GDP)),
l_GDP = log(GDP)) %>%
tk_xts(date_var = yearMon)
# Annual GDP
df_GDP_y <-
df_dataAll_y %>%
select(yearMon,
year, month,
GDP = GDP_FRED) %>%
mutate(l_GDP = log(GDP),
dl_GDP = log(GDP/lag(GDP))) %>%
tk_xts(date_var = yearMon)
df_GDP_q["1945/", "l_GDP"] %>% plot
df_GDP_q["1945/", "dl_GDP"] %>% plot
df_GDP_y["1945/", "l_GDP"] %>% plot
df_GDP_y["1945/", "dl_GDP"] %>% plot
df_GDP_q["1945/", "dl_GDP"] %>% mean(na.rm = TRUE)
df_GDP_y["1945/", "dl_GDP"] %>% mean(na.rm = TRUE)
## ACF and PACF
acf2(df_GDP_q["1945/2015", "dl_GDP"]) # Looks like AR1
acf2(df_GDP_y["1945/2015", "dl_GDP"]) # Looks like random walk
## Auto ARIMA modeling
# Quarterly data
sample_period <- "1950/"
auto.arima(df_GDP_q[sample_period, "l_GDP"], max.d = 1, seasonal = FALSE) # ARIMA(2, 1, 0) with drift
sarima(df_GDP_q[sample_period , "dl_GDP"], 1, 0, 0)
# AR1 (or AR2) looks fine for quarterly GDP growth
# Annual data
sample_period <- "1950/"
auto.arima(df_GDP_y[sample_period, "l_GDP"], max.d = 1, seasonal = FALSE) # ARIMA(0, 1, 0) with drift
sarima(df_GDP_y[sample_period , "dl_GDP"], 0, 0, 0)
# Annual data looks random walk with a mean around 3.1%
#**********************************************************************
# Inflation ####
#**********************************************************************
## Variable "Inflation_Index"
df_inflation_q <-
df_dataAll_q %>%
select(year, month, yearMon, infl_index = CPIU_SA_FRED, infl_index_NA = Inflation_Index) %>%
mutate(dl_inflation = log(infl_index / lag(infl_index)),
l_inflation = log(infl_index),
dl_inflation_NA = log(infl_index_NA / lag(infl_index_NA)),
l_inflation_NA = log(infl_index_NA)) %>%
filter(!is.na(dl_inflation_NA)) %>%
tk_xts(date_var = yearMon)
range(df_inflation_q %>% index) # 1926Q1 - 2015Q4 (SBBI data range, FRED data starts in 1948)
df_inflation_y <-
df_dataAll_y %>%
select(year, month, yearMon, infl_index = CPIU_SA_FRED, infl_index_NA = Inflation_Index) %>%
mutate(dl_inflation = log(infl_index / lag(infl_index)),
l_inflation = log(infl_index),
dl_inflation_NA = log(infl_index_NA / lag(infl_index_NA)),
l_inflation_NA = log(infl_index_NA)) %>%
filter(!is.na(dl_inflation_NA)) %>%
tk_xts(date_var = yearMon)
range(df_inflation_y %>% index) #1927 - 2015
# Plotting series
sample_period <- "1950/"
df_inflation_q[sample_period, "l_inflation"] %>% plot
df_inflation_q[sample_period, "dl_inflation"] %>% plot # potential issue: inflation rate does not look stationary
df_inflation_q[sample_period, "dl_inflation"] %>% diff %>% plot # differenced inflation rate has stable mean, but time varying variance
df_inflation_y[sample_period, "l_inflation"] %>% plot
df_inflation_y[sample_period, "dl_inflation"] %>% plot
df_inflation_y[sample_period, "dl_inflation"] %>% diff %>% plot # annual data look more like stationary
df_inflation_q[sample_period, "dl_inflation"] %>% mean(na.rm = TRUE)
df_inflation_y[sample_period, "dl_inflation"] %>% mean(na.rm = TRUE)
df_inflation_y[sample_period, "dl_inflation"] %>% diff %>% mean(na.rm = TRUE)
## ACF and PACF
acf2(df_inflation_q[sample_period, "dl_inflation"]) # Look non-stationary
acf2(df_inflation_y[sample_period, "dl_inflation"]) # Look AR(1)
acf2(df_inflation_q[sample_period, "dl_inflation"] %>% diff) # AR?
acf2(df_inflation_y[sample_period, "dl_inflation"] %>% diff) # AR?
df_inflation_q
## Auto ARIMA modeling
# Quarterly data
sample_period <- "1950/"
# Auto ARIMA
auto.arima(df_inflation_q[sample_period, "l_inflation"], max.d = 1, seasonal = FALSE) # ARIMA(5, 1, 1) with drift
auto.arima(df_inflation_q[sample_period, "l_inflation"], max.d = 2, seasonal = FALSE) # estimation issue with second order difference
# Try manually
# assume inflation rate is stationary
sarima(df_inflation_q[sample_period , "dl_inflation"], 3, 0, 0) # AR(3), or even larger AR orders, with non-zero mean
# assume differenced inflation rate is stationary (changes in quarterly inflation rate)
sarima(df_inflation_q[sample_period , "dl_inflation"], 3, 1, 1) # AR(3), or even larger AR orders, with zero mean
# Annual data
sample_period <- "1950/"
# Auto ARIMA: log inflation index
auto.arima(df_inflation_y[sample_period, "l_inflation"], max.d = 1, seasonal = FALSE) # ARIMA(1, 1, 0) with drift
auto.arima(df_inflation_y[sample_period, "l_inflation"], max.d = 2, seasonal = FALSE) # ARIMA(0, 2, 0) annual changes in infl rate looks like random walk
# Try manually: inflation rate
sarima(df_inflation_y[sample_period , "dl_inflation"], 1, 0, 0) # AR(1)
sarima(df_inflation_y[sample_period , "dl_inflation"], 0, 1, 0) # random walk in changes of infl rate, worse than AR(1)
# AR(1) looks fine for annual inflation rate, which is consistent with the Wikie approach
## Outlier at 2008Q4
df_inflation_q1 <- df_inflation_q
df_inflation_q1["200812", "l_inflation"] <-
(as.numeric(df_inflation_q["200809","l_inflation"]) + as.numeric(df_inflation_q["200903","l_inflation"]))/2
df_inflation_q1["200812", "dl_inflation"] <- as.numeric(df_inflation_q1["200812", "l_inflation"]) - as.numeric(df_inflation_q1["200809", "l_inflation"])
sample_period <- "1950/"
auto.arima(df_inflation_q1[sample_period, "l_inflation"], max.d = 2, seasonal = FALSE) # ARIMA(5, 1, 4) with drift
sarima(df_inflation_q1[sample_period , "dl_inflation"], 4, 0, 0)
## Wilkie Model: Inflation rate modeled as AR(1)
# mod_inflation_q <- Arima(df_inflation_q$dl_inflation, order = c(1, 0, 0))
Arima(df_inflation_y["1927/1989","dl_inflation" ], order = c(1, 0, 0))
Arima(df_inflation_y["1927/2015","dl_inflation" ], order = c(1, 0, 0))
Arima(df_inflation_y["1950/2015","dl_inflation" ], order = c(1, 0, 0))
mod_inflation_y <- Arima(df_inflation_y["1927/2015", "dl_inflation" ], order = c(1, 0, 0))
df_inflation_y <- cbind(df_inflation_y["1927/2015",], infl_residual = xts(mod_inflation_y$residuals, index(df_inflation_y["1927/2015",])))
## diagnostic of AR(1)
df_inflation_y[,"infl_residual"] %>% acf
df_inflation_y[,"infl_residual"] %>% pacf
# AR(1) seems not sufficient for yearly inflation
#**********************************************************************
# Wilkie Dividend Yield ####
#**********************************************************************
df_dividend_y <-
df_dataAll_y %>%
select(year, month, yearMon, LCap_TRI, LCap_CAI) %>%
mutate(LCap_DivI = (LCap_TRI/lag(LCap_TRI)) * lag(LCap_CAI) - LCap_CAI,
LCap_DivY = LCap_DivI / LCap_CAI,
l_LCap_DivY = log(LCap_DivY),
dl_LCap_DivY = l_LCap_DivY - lag(l_LCap_DivY),
l_LCap_DivI = log(LCap_DivI),
dl_LCap_DivI = l_LCap_DivI - lag(l_LCap_DivI)) %>%
filter(!is.na(l_LCap_DivY)) %>%
tk_xts(date_var = yearMon)
range(df_dividend_y %>% index)
df_dividend_y <- cbind(df_inflation_y, df_dividend_y)
df_dividend_q <-
df_dataAll_q %>%
select(year, month, yearMon, LCap_TRI, LCap_CAI) %>%
mutate(LCap_DivI = (LCap_TRI/lag(LCap_TRI)) * lag(LCap_CAI) - LCap_CAI,
LCap_DivY = LCap_DivI / LCap_CAI,
l_LCap_DivY = log(LCap_DivY),
dl_LCap_DivY = l_LCap_DivY - lag(l_LCap_DivY),
l_LCap_DivI = log(LCap_DivI),
dl_LCap_DivI = l_LCap_DivI - lag(l_LCap_DivI)) %>%
filter(!is.na(l_LCap_DivY)) %>%
tk_xts(date_var = yearMon)
range(df_dividend_y %>% index)
df_dividend_q <- cbind(df_inflation_q, df_dividend_q)
## Plotting series
sample_period <- "1927/"
df_dividend_q[sample_period, "l_LCap_DivY"] %>% plot
df_dividend_q[sample_period, "dl_LCap_DivY"] %>% plot
df_dividend_y[sample_period, "l_LCap_DivY"] %>% plot
df_dividend_y[sample_period, "dl_LCap_DivY"] %>% plot
df_dividend_q[sample_period, "dl_LCap_DivY"] %>% mean(na.rm = TRUE)
df_dividend_y[sample_period, "dl_LCap_DivY"] %>% mean(na.rm = TRUE)
## ACF and PACF
acf2(df_dividend_q[sample_period, "dl_LCap_DivY"]) # looks like AR1 or AR3
acf2(df_dividend_y[sample_period, "dl_LCap_DivY"]) # looks like random walk
## Auto ARIMA modeling
# Quarterly data
sample_period <- "1950/"
# Auto ARIMA
auto.arima(df_dividend_q[sample_period, "l_LCap_DivY"], max.d = 0, seasonal = FALSE) # AR(2) is selected if assuming Div Yield is stationary
auto.arima(df_dividend_q[sample_period, "l_LCap_DivY"], max.d = 1, seasonal = FALSE) # ARIMA(5, 1, 2)
# Try manually
# assume log dividend yield is stationary
sarima(df_dividend_q[sample_period , "l_LCap_DivY"], 2, 0, 2) # No satisfactory spec
# assume differenced dividend yield is stationary (change in dividend yield)
sarima(df_dividend_q[sample_period , "l_LCap_DivY"], 5, 1, 2) # ARIMA(5, 1, 2) (selected by auto arima) is not satisfactory
# Annual data
sample_period <- "1950/"
# Auto ARIMA: log inflation index
auto.arima(df_dividend_y[sample_period, "l_LCap_DivY"], max.d = 1, seasonal = FALSE) # Looks random walk
# Try manually: inflation rate
sarima(df_dividend_y[sample_period , "l_LCap_DivY"], 1, 0, 0) # Level: AR(1) or AR(2)
sarima(df_dividend_y[sample_period , "l_LCap_DivY"], 1, 1, 0) # differenced: AR(1) par is not significant
sarima(df_dividend_y[sample_period , "l_LCap_DivY"], 0, 1, 0) # differentced:Looks random walk
# Dividend yield does not look stationary
# No satisfactory model spec found for quarterly dividend yield (for neither level nor difference)
# Annual level dividend yield: AR(1) or AR(2), but the problem is that the level data do not look stationary
# Annual differenced dividend yield: look like random walk.
## Wilkie model:
# Dividend yield level is modeled as AR(1)
Arima(df_dividend_y["1927/2014", "l_LCap_DivY" ], xreg = df_dividend_y["1927/2014", "dl_inflation"], order = c(1, 0, 0))
# As in HSZ2016, parameter for inflation rate is not significant. (while we have very different point estiamte)
mod_divY_y <- Arima(df_dividend_y["1927/2014", "l_LCap_DivY" ], order = c(1, 0, 0))
mod_divY_y
mod_divY_y$sigma2^0.5
Arima(df_dividend_y["1927/1989", "l_LCap_DivY" ], order = c(1, 0, 0))
# Model AR(1)+transfer function of inflation
#**********************************************************************
# Wilkie Dividend Index ####
#**********************************************************************
## Plotting series
sample_period <- "1927/"
df_dividend_q[sample_period, "l_LCap_DivI"] %>% plot
df_dividend_q[sample_period, "dl_LCap_DivI"] %>% plot
# Great volatility before 1955. Data after 1955 look stationary
df_dividend_y[sample_period, "LCap_DivI"] %>% plot
df_dividend_y[sample_period, "dl_LCap_DivI"] %>% plot
df_dividend_q[sample_period, "dl_LCap_DivI"] %>% mean(na.rm = TRUE)
df_dividend_y[sample_period, "dl_LCap_DivI"] %>% mean(na.rm = TRUE)
## ACF and PACF
acf2(df_dividend_q[sample_period, "dl_LCap_DivI"]) # looks like MA2
acf2(df_dividend_y[sample_period, "dl_LCap_DivI"]) # looks like AR1 or MA1
## Auto ARIMA modeling
# Quarterly data
sample_period <- "1950/"
# Auto ARIMA
auto.arima(df_dividend_q[sample_period, "l_LCap_DivI"], max.d = 1, seasonal = FALSE) # ARIMA(3, 1, 3) with drift
# model spec selected by auto.arima is sensitive to sample period
# Try manually
# assume log differenced dividend yield is stationary
sarima(df_dividend_q[sample_period , "dl_LCap_DivI"], 4, 0, 3) #
# auto-selected model spec cannot pass Ljung-Box tests
# Annual data
sample_period <- "1960/"
# Auto ARIMA: log inflation index
auto.arima(df_dividend_y[sample_period, "l_LCap_DivI"], max.d = 1, seasonal = FALSE) # MA1 looks fine
# Try manually: inflation rate
sarima(df_dividend_y[sample_period , "dl_LCap_DivI"], 0, 0, 1) # MA1, but bad performance in normaltiy and L-B test.
## Wilkie model:
dd <- 0.38
wd <- 1
get_ewa <- function(x, d){
# compute exponentially weighted average
# x: time series in xts
# d: weight of current period
#x <- df_inflation_y[, "dl_inflation"]
#d <- 0.38
n <- length(x)
ewa <- numeric(n)
ewa[1] <- x[1]
for(i in 2:n) ewa[i] <- (1 - d) * ewa[i - 1] + d * x[i]
ewa <- xts(ewa, index(x))
}
# add exponentially weighted inflation
df_dividend_y <- cbind(df_dividend_y, dl_inflation_NA_ewa = get_ewa(df_inflation_y[, "dl_inflation_NA"], dd))
df_dividend_y[sample_period, "dl_inflation_NA_ewa"] %>% plot
# reduced Wilkie specification: MA(1) + exponentially weighted inflation
sample_period <- "1926/1989"
mod_divI_y <- Arima(df_dividend_y[sample_period, "dl_LCap_DivI"], xreg = df_dividend_y[sample_period, "dl_inflation_NA_ewa"], order = c(0, 0, 1),
fixed = c(NA, NA, wd))
mod_divI_y
mod_divI_y$sigma2^0.5
sample_period <- "1926/2014"
mod_divI_y <- Arima(df_dividend_y[sample_period, "dl_LCap_DivI"], xreg = df_dividend_y[sample_period, "dl_inflation_NA_ewa"], order = c(0, 0, 1),
fixed = c(NA, NA, wd))
mod_divI_y
mod_divI_y$sigma2^0.5
# Pure MA1
Arima(df_dividend_y[sample_period, "dl_LCap_DivI"], order = c(0, 0, 1),
fixed = c(NA, NA))
## Issues:
# Model parameter estimates are sensitive to sample period. Including data after 2010 significantly increases the estiamte of intercept
## Further examine the diffrence between dividend growth and inflation rate
dfx <- df_dividend_y[sample_period, "dl_LCap_DivI"] - df_dividend_y[sample_period, "dl_inflation_ewa"]
dfx %>% plot
dfx %>% acf2
auto.arima(dfx) # MA(1) model selected
#**********************************************************************
# Long term corporate bond yield ####
#**********************************************************************
dc <- 0.058
wc <- 1
c_min <- 0.005
df_cbond_y <-
df_dataAll_y %>%
select(year, month, yearMon,
infl_index = CPIU_SA_FRED,
infl_index_NA = Inflation_Index,
CBond_Yield_AAA) %>%
mutate(CBond_Yield_AAA = CBond_Yield_AAA/100,
dl_inflation = log(infl_index / lag(infl_index)),
dl_inflation_NA = log(infl_index_NA / lag(infl_index_NA))
#CBond_Yield_real = CBond_Yield_AAA - dl_inflation,
#l_CBond_Yield_real = log(CBond_Yield_AAA),
#dl_CBond_Yield_real = l_CBond_Yield_real - lag(l_CBond_Yield_real)
) %>% tk_xts(date_var = yearMon)
range(df_cbond_y %>% index)
Index_cbond_y <- index(df_cbond_y)
df_cbond_q <-
df_dataAll_q %>%
select(year, month, yearMon,
infl_index = CPIU_SA_FRED,
infl_index_NA = Inflation_Index,
CBond_Yield_AAA) %>%
mutate(CBond_Yield_AAA = CBond_Yield_AAA/100,
dl_inflation = log(infl_index / lag(infl_index)),
dl_inflation_NA = log(infl_index_NA / lag(infl_index_NA))
#CBond_Yield_real = CBond_Yield_AAA - dl_inflation,
#l_CBond_Yield_real = log(CBond_Yield_AAA),
#dl_CBond_Yield_real = l_CBond_Yield_real - lag(l_CBond_Yield_real)
) %>% tk_xts(date_var = yearMon)
range(df_cbond_y %>% index)
Index_cbond_q <- index(df_cbond_q)
# Inflationary and real parts of long-term corp bond rate.
# Notes:
# set c_min, so that
# if the real component of long-term bond yield is smaller than c_min, then
# forces the real component to be c_min, and the inflationary component to be
# the total rate minus c_min, where the total rate is the sum of unadjusted real
# and inflationary components
# This ensures that simulated the real part is always positive and can take log.
decomp_cbondY <- function(infl, yield_tot, dc, c_min){
## compute inflationary and real components of corp bond yield based on p9 HSZ2016
# infl: inflation in xts
# yield_tot: weight of current period
# dc: weight
# c_min: min of real component of yield
# infl <- df_cbond_y[, "dl_inflation"]
# yield_tot <- df_cbond_y[, "CBond_Yield_AAA"]
# dc <- 0.058
# c_min <- 0.005
Index <- index(infl)
non_NA <- (!is.na(infl) & !is.na(yield_tot)) %>% as.vector
infl <- infl[non_NA, ]
yield_tot <- yield_tot[non_NA, ]
n <- length(infl)
yield_infl <- numeric(n)
yield_real <- numeric(n)
yield_infl[1] <- min(infl[1], yield_tot[1] - c_min)
for(i in 2:n) yield_infl[i] <- min((1 - dc) * yield_infl[i - 1] + dc * infl[i], yield_tot[i] - c_min)
yield_infl <- xts(yield_infl, index(yield_tot))
yield_real <- yield_tot - yield_infl
colnames(yield_real) <- "yield_real"
yield_decomp <- merge(merge(yield_real, yield_infl), Index)
yield_decomp
}
cbondY_comp_y <- decomp_cbondY(df_cbond_y[, "dl_inflation"], df_cbond_y[, "CBond_Yield_AAA"], dc, c_min)
cbondY_comp_y_NA <- decomp_cbondY(df_cbond_y[, "dl_inflation_NA"], df_cbond_y[, "CBond_Yield_AAA"], dc, c_min)
colnames(cbondY_comp_y_NA) <- paste(colnames(cbondY_comp_y_NA), "NA", sep = "_")
cbondY_comp_q <- decomp_cbondY(df_cbond_q[, "dl_inflation"], df_cbond_q[, "CBond_Yield_AAA"], dc, c_min)
cbondY_comp_q_NA <- decomp_cbondY(df_cbond_q[, "dl_inflation_NA"], df_cbond_q[, "CBond_Yield_AAA"], dc, c_min)
colnames(cbondY_comp_q_NA) <- paste(colnames(cbondY_comp_q_NA), "NA", sep = "_")
df_cbond_y <- merge(df_cbond_y, cbondY_comp_y, cbondY_comp_y_NA)
df_cbond_q <- merge(df_cbond_q, cbondY_comp_q, cbondY_comp_q_NA)
cbondY_comp_q_NA %>% plot
cbondY_comp_y_NA %>% plot
df_cbond_y[, c("dl_inflation_NA", "CBond_Yield_AAA")] %>% plot
df_cbond_y %<>%
as.data.frame() %>%
mutate(l_yield_real = log(yield_real),
l_yield_real_NA = log(yield_real_NA),
dl_yield_real = l_yield_real - lag(l_yield_real),
dl_yield_real_NA = l_yield_real_NA - lag(l_yield_real_NA)) %>%
tk_xts(order.by = Index_cbond_y)
df_cbond_q %<>%
as.data.frame() %>%
mutate(l_yield_real = log(yield_real),
l_yield_real_NA = log(yield_real_NA),
dl_yield_real = l_yield_real - lag(l_yield_real),
dl_yield_real_NA = l_yield_real_NA - lag(l_yield_real_NA)) %>%
tk_xts(order.by = Index_cbond_q)
df_cbond_y %>% head
## Plotting series
sample_period <- "1927/"
df_cbond_q[sample_period, "CBond_Yield_AAA"] %>% plot
df_cbond_q[sample_period, "yield_real_NA"] %>% plot
df_cbond_q[sample_period, "dl_yield_real_NA"] %>% plot
df_cbond_y[sample_period, "CBond_Yield_AAA"] %>% plot
df_cbond_y[sample_period, "yield_real_NA"] %>% plot
df_cbond_y[sample_period, "yield_infl_NA"] %>% plot
df_cbond_y[sample_period, "dl_inflation_NA"] %>% plot
df_cbond_y[sample_period, "dl_yield_real_NA"] %>% plot
df_cbond_q[sample_period, "yield_real"] %>% mean(na.rm = TRUE)
df_cbond_y[sample_period, "yield_real"] %>% mean(na.rm = TRUE)
## ACF and PACF
acf2(df_cbond_q[sample_period, "dl_yield_real"]) #
acf2(df_cbond_y[sample_period, "dl_yield_real"]) #
## ARIMA modeling
# Quarterly data
sample_period <- "1950/"
# Auto ARIMA
auto.arima(df_cbond_q["1927/2014", "l_yield_real_NA"], max.d = 1, seasonal = FALSE) # ARIMA(0, 1, 0) starting from 1927 ?
auto.arima(df_cbond_q["1950/2014", "l_yield_real_NA"], max.d = 1, seasonal = FALSE) # ARIMA(0, 1, 0) starting from 1950 ?
# Try manually
# assume log differenced interest yield is stationary
sarima(df_cbond_q["1927/" , "l_yield_real_NA"], 0, 1, 0) # Not so good
sarima(df_cbond_q["1950/" , "dl_yield_real_NA"], 1, 0, 0) # higher orders are needed for quarterly data
sarima(df_cbond_q["1950/" , "dl_yield_real_NA"], 4, 0, 0) #
# residuals do not look normally distributed
# annual data:
sample_period <- "1927/"
# Auto ARIMA: log inflation index
auto.arima(df_cbond_y["1927/2014", "l_yield_real_NA"], max.d = 1, seasonal = FALSE) # ARIMA(1, 0, 2)
auto.arima(df_cbond_y["1951/2014", "l_yield_real_NA"], max.d = 1, seasonal = FALSE) # ARIMA(1, 1, 0)
auto.arima(df_cbond_y["1951/2014", "l_yield_real_NA"], max.d = 0, seasonal = FALSE) # ARIMA(2, 0, 0)
# Try manually: inflation rate
sarima(df_cbond_y["1927/2014" , "l_yield_real_NA"], 1, 0, 1) #
sarima(df_cbond_y["1951/2014" , "l_yield_real_NA"], 1, 0, 1) #
## Wilkie model for real part: log yield modeled as AR(1) (reduced model)
mod_cbond_real <- Arima(df_cbond_y["1927/1989", "l_yield_real_NA"], order = c(1, 0, 0))
mod_cbond_real
mod_cbond_real$coef[2] %>% exp; mod_cbond_real$sigma2^0.5
mod_cbond_real <- Arima(df_cbond_y["1926/2014", "l_yield_real_NA"], order = c(1, 0, 0))
mod_cbond_real
mod_cbond_real$coef[2] %>% exp; mod_cbond_real$sigma2^0.5
mod_cbond_real <- Arima(df_cbond_y["1951/2014", "l_yield_real_NA"], order = c(1, 0, 0))
mod_cbond_real
mod_cbond_real$coef[2] %>% exp; mod_cbond_real$sigma2^0.5
mod_cbond_real <- Arima(df_cbond_y["1991/2014", "l_yield_real_NA"], order = c(1, 0, 0))
mod_cbond_real
mod_cbond_real$coef[2] %>% exp; mod_cbond_real$sigma2^0.5 # much lower than the estimates in HSZ2016
#**********************************************************************
# Try Simulatio: Inflation ####
#**********************************************************************
# Models:
#1a. AR(1) of inflation rate on 1951-2014
#1b. AR(1) of inflation rate on 1991-2014
#2a. HSZ2016 (Hardy, Saunders, Zhang, 2016) estimates on 1951-2014
#2b. HSZ2016 (Hardy, Saunders, Zhang, 2016) estimates on 1991-2014
mdl_infl_y1a <- Arima(df_inflation_y["1951/2014" , "dl_inflation"], c(1, 0, 0)) # AR(1)
mdl_infl_y1b <- Arima(df_inflation_y["1991/2014" , "dl_inflation"], c(1, 0, 0)) # AR(1)
mdl_infl_y1a
mdl_infl_y1b
# HSZ2016 estimate on 1951-2014
mdl_infl_y2a <- mdl_infl_y1a
mdl_infl_y2a$coef <- c(ar1 = 0.7575, intercept = 0.0338)
mdl_infl_y2a$sigma2 <- 0.0171^2 # 0.00029241
# HSZ2016 estimate on 1991-2014
mdl_infl_y2b <- mdl_infl_y1a
mdl_infl_y2b$coef <- c(ar1 = 0, intercept = 0.0244 )
mdl_infl_y2b$sigma2 <- 0.0111^2 # 0.00035721
# Compare estimates
extract_est <- function(fit, mdl){
#names_col <- c(names(mdl_infl_y2b$coef), "sigma")
est <- c(fit$coef, fit$sigma2^0.5) %>% matrix( nrow = 1) %>% as.data.frame
names(est) <- c(names(fit$coef), "sigma")
est$mdl <- mdl
est %<>% select(mdl, everything())
est
}
bind_rows(
extract_est(mdl_infl_y1a, "y1a"),
extract_est(mdl_infl_y1b, "y1b"), # AR1 estimate is -0.1011, forced to 0 in simulation
extract_est(mdl_infl_y2a, "y2a"),
extract_est(mdl_infl_y2b, "y2b")
) %>% kable(digits = 4)
# Check simulated series
Arima(simulate(mdl_infl_y2a, 100), order = c(1, 0, 0))
Arima(simulate(mdl_infl_y2b, 100), order = c(0, 0, 0))
# OK
# Simulation
nsim <- 2000
nyear_sim <- 100
set.seed(1234); sim_infl_y1a <- replicate(nsim,simulate(mdl_infl_y1a, nyear_sim, future = TRUE)) %>% as.tibble() %>% mutate(mdl = "y1a", year = seq_len(nyear_sim))
set.seed(1234); sim_infl_y1b <- replicate(nsim,simulate(mdl_infl_y1b, nyear_sim, future = TRUE)) %>% as.tibble() %>% mutate(mdl = "y1b", year = seq_len(nyear_sim))
set.seed(1234); sim_infl_y2a <- replicate(nsim,simulate(mdl_infl_y2a, nyear_sim, future = TRUE)) %>% as.tibble() %>% mutate(mdl = "y2a", year = seq_len(nyear_sim))
set.seed(1234); sim_infl_y2b <- replicate(nsim,simulate(mdl_infl_y2b, nyear_sim, future = TRUE)) %>% as.tibble() %>% mutate(mdl = "y2b", year = seq_len(nyear_sim))
fn_plot <- function(data){
df <- data %>%
gather(sim, value, -year, -mdl) %>%
group_by(year) %>%
summarize(
q10 = quantile(value, 0.10),
q25 = quantile(value, 0.25),
q50 = quantile(value, 0.50),
q75 = quantile(value, 0.75),
q90 = quantile(value, 0.90),
mdl = unique(mdl))
# df %>% head()
fig <-
df %>% gather(percentile, value, -year, - mdl) %>%
mutate(percentile = as.factor(percentile) %>% fct_rev) %>%
ggplot(aes(x = year, y = value, color = percentile)) + theme_bw() +
geom_line() +
geom_point() +
coord_cartesian(ylim = c(0, 0.08)) +
scale_y_continuous(breaks = seq(0, 1, 0.01))
}
fig_infl_y1a <- fn_plot(sim_infl_y1a)
fig_infl_y1b <- fn_plot(sim_infl_y1b)
fig_infl_y2a <- fn_plot(sim_infl_y2a)
fig_infl_y2b <- fn_plot(sim_infl_y2b)
fig_infl_y1a
fig_infl_y1b
fig_infl_y2a
fig_infl_y2b
sim_infl <- bind_rows(sim_infl_mdl_infl_y1a,
sim_infl_mdl_infl_y1b,
sim_infl_mdl_infl_y2a,
sim_infl_mdl_infl_y2b)
df <- sim_infl %>%
gather(sim, value, -year, -mdl) %>%
group_by(year, mdl) %>%
summarize(
q10 = quantile(value, 0.10),
q25 = quantile(value, 0.25),
q50 = quantile(value, 0.50),
q75 = quantile(value, 0.75),
q90 = quantile(value, 0.90))
# df %>% head()
fig_infl <-
df %>% gather(percentile, value, -year, -mdl) %>%
mutate(percentile = as.factor(percentile) %>% fct_rev,
mdl = factor(mdl, labels = c("AR(1) 1951-2014", "AR(1) 1991-2014",
"AR(1) HSZ 1951-2014", "AR(1) HSZ 1991-2014"))) %>%
ggplot(aes(x = year, y = value, color = percentile)) + theme_bw() +
facet_grid(. ~ mdl) +
geom_line() +
geom_point() +
coord_cartesian(ylim = c(0, 0.08)) +
scale_y_continuous(breaks = seq(0, 1, 0.01))
fig_infl
# Inflation sims based on more recent sample period (1991-2014)
# have much lower mean and variance
# There are differences between our estimate and HSZ estimate, but the
# the magnitude is not large.
#**********************************************************************
# Try Simulation: Dividend Yield ####
#**********************************************************************
# Models
# 1a. AR(1) of log div yield on 1951-1984, with inflation as regressor
# 1b. AR(1) of log div yield on 1991-2014, with inflation as regressor
# 2a. HSZ2016 par values: AR(1) of log div yield on 1951-2014, with inflation as regressor
# 2b. HSZ2016 par values: AR(1) of log div yield on 1991-2014, with inflation as regressor
df_dividend_y %>% head
mdl_divY_y1a <- Arima(df_dividend_y["1951/2014", "l_LCap_DivY"], xreg = df_dividend_y["1951/2014", "dl_inflation"], order = c(1, 0, 0))
mdl_divY_y1a
mdl_divY_y1b <- Arima(df_dividend_y["1991/2014", "l_LCap_DivY"], xreg = df_dividend_y["1991/2014", "dl_inflation"], order = c(1, 0, 0))
mdl_divY_y1b
mdl_divY_y2a <- mdl_divY_y1a
mdl_divY_y2a$coef <- c(ar1 = 0.9582, intercept = log(0.0331), dl_inflation = 0.0504)
mdl_divY_y2a$sigma2 <- 0.131^2
mdl_divY_y2b <- mdl_divY_y1a
mdl_divY_y2b$coef <- c(ar1 = 0.9112, intercept = log(0.0252), dl_inflation = -4.5762 )
mdl_divY_y2b$sigma2 <- 0.1159^2
extract_est <- function(fit, mdl){
#names_col <- c(names(mdl_infl_y2b$coef), "sigma")
est <- c(fit$coef, fit$sigma2^0.5) %>% matrix( nrow = 1) %>% as.data.frame
names(est) <- c(names(fit$coef), "sigma")
est$mdl <- mdl
est %<>% select(mdl, everything())
est
}
bind_rows(
extract_est(mdl_divY_y1a, "y1a"),
extract_est(mdl_divY_y1b, "y1b"), # AR1 estimate is -0.1011, forced to 0 in simulation
extract_est(mdl_divY_y2a, "y2a"),
extract_est(mdl_divY_y2b, "y2b")
) %>% kable(digits = 4)
# Notes
# 1. log dividend yield does not look stationary over time.
# 2. Our estimates of AR term, intercept, and sd are not very different from HSZ
# 3. Estimation on inflation does not match HSZ well
# - 0.62 vs 0.05 for 1951-2014
# - -3.4 vs -4.5 for 1991-2014
# 4. inflation parameter for 1991-2014 is negative, which contradicts theoretical relationship between div yield and infl
# 5. The figure shows that inflation and dividend yield may have a long-term positive relationship, which we may want
# to capture in the model. Perhaps transfer model is not a appropriate way to model inflation and div yield jointly?
# For simulation:
# Because of the unreasonable sign of estimated parameter on inflation,
# we should just use AR(1) for simulating dividend yield
# Simulation
nsim <- 2000
nyear_sim <- 100
mdl_divY_y1a_sim <- mdl_divY_y1a
mdl_divY_y1b_sim <- mdl_divY_y1b
mdl_divY_y2a_sim <- mdl_divY_y2a
mdl_divY_y1a_sim$coef["dl_inflation"] <- 0
mdl_divY_y1b_sim$coef["dl_inflation"] <- 0
mdl_divY_y2a_sim$coef["dl_inflation"] <- 0
set.seed(1234); sim_divY_y1a <- sapply(1:2000, function(x) {simulate(mdl_divY_y1a_sim, nyear_sim, xreg = sim_infl_y1a[, x], future = TRUE) %>% exp %>% as.numeric}) %>% as.tibble %>% mutate(mdl = "y1a", year = seq_len(nyear_sim))
set.seed(1234); sim_divY_y1b <- sapply(1:2000, function(x) {simulate(mdl_divY_y1b_sim, nyear_sim, xreg = sim_infl_y1b[, x], future = TRUE) %>% exp %>% as.numeric}) %>% as.tibble %>% mutate(mdl = "y1b", year = seq_len(nyear_sim))
set.seed(1234); sim_divY_y2a <- sapply(1:2000, function(x) {simulate(mdl_divY_y2a_sim, nyear_sim, xreg = sim_infl_y2a[, x], future = TRUE) %>% exp %>% as.numeric}) %>% as.tibble %>% mutate(mdl = "y2a", year = seq_len(nyear_sim))
set.seed(1234); sim_divY_y2b <- sapply(1:2000, function(x) {simulate(mdl_divY_y2b, nyear_sim, xreg = sim_infl_y2b[, x], future = TRUE) %>% exp %>% as.numeric}) %>% as.tibble %>% mutate(mdl = "y2b", year = seq_len(nyear_sim))
sim_divY <- bind_rows(sim_divY_y1a,
sim_divY_y1b,
sim_divY_y2a,
sim_divY_y2b)
df <- sim_divY %>%
gather(sim, value, -year, -mdl) %>%
group_by(year, mdl) %>%
summarize(
q10 = quantile(value, 0.10),
q25 = quantile(value, 0.25),
q50 = quantile(value, 0.50),
q75 = quantile(value, 0.75),
q90 = quantile(value, 0.90))
# df %>% head()
fig_infl <-
df %>% gather(percentile, value, -year, -mdl) %>%
mutate(percentile = as.factor(percentile) %>% fct_rev,
mdl = factor(mdl, labels = c("AR(1) 1951-2014", "AR(1) 1991-2014",
"AR(1) HSZ 1951-2014", "AR(1) HSZ 1991-2014"))) %>%
ggplot(aes(x = year, y = value, color = percentile)) + theme_bw() +
facet_grid(. ~ mdl) +
geom_line() +
geom_point() +
coord_cartesian(ylim = c(0, 0.08)) +
scale_y_continuous(breaks = seq(0, 1, 0.01))
fig_infl
# set.seed(1234); sim_infl_y1a <- replicate(nsim,simulate(mdl_infl_y1a, nyear_sim, future = TRUE)) %>% as.tibble() %>% mutate(mdl = "y1a", year = seq_len(nyear_sim))
#**********************************************************************
# Try Simulation: Dividend growth ####
#**********************************************************************
# Models
# 1a. MA(1) of dividend growth on 1951-1984, with div yield shock as regressor
# 1b. MA(1) of dividend growth on 1991-2014, without div yield shock as regressor
# 2a. HSZ2016 par values for 1a
# 2b. HSZ2016 par values for 1b
mdl_divY_y1a$residuals
mdl_divY_y1b$residuals
## Wilkie model:
dd <- 0.38
wd <- 1
get_ewa <- function(x, d){
# compute exponentially weighted average
# x: time series in xts
# d: weight of current period
#x <- df_inflation_y[, "dl_inflation"]
#d <- 0.38
n <- length(x)
ewa <- numeric(n)
ewa[1] <- x[1]
for(i in 2:n) ewa[i] <- (1 - d) * ewa[i - 1] + d * x[i]
ewa <- xts(ewa, index(x))
}
# regressors
infl_ewa_1a <- df_dividend_y["1951/2014", "dl_inflation_NA_ewa"]
divY_res_1a <- xts(mdl_divY_y1a$residuals, index(infl_ewa)) %>% lag; colnames(divY_res_1a) <- "divY_res"
xreg_1a <- cbind(infl_ewa_1a, divY_res_1a)[-1, ]
infl_ewa_1b <- df_dividend_y["1991/2014", "dl_inflation_NA_ewa"]
divY_res_1b <- xts(mdl_divY_y1b$residuals, index(infl_ewa_1b)) %>% lag; colnames(divY_res_1b) <- "divY_res"
xreg_1b <- cbind(infl_ewa_1b, divY_res_1b)[-1, ]
# Wilkie specification: MA(1) + regressors
mdl_divI_y1a <- Arima(df_dividend_y["1952/2014", "dl_LCap_DivI"], xreg = xreg_1a, order = c(0, 0, 1),
fixed = c(NA, NA, wd, NA))
mdl_divI_y1a; mdl_divI_y1a$sigma2^0.5
mdl_divI_y1b <- Arima(df_dividend_y["1992/2014", "dl_LCap_DivI"], xreg = xreg_1a["1992/2014"], order = c(0, 0, 1),
fixed = c(NA, NA, wd, NA))
mdl_divI_y1b; mdl_divI_y1b$sigma2^0.5
mdl_divI_y2a <- mdl_divI_y1a
mdl_divI_y2a$coef <- c(ma1 = -.3468, intercept = 0.0202 , dl_inflation_NA_ewa = 1, divY_res = 0.2138 )
mdl_divI_y2a$sigma2 <- 0.1146^2
mdl_divI_y2b <- mdl_divI_y1b
mdl_divI_y2b$coef <- c(ma1 = 0.1215, intercept = 0.0306 , dl_inflation_NA_ewa = 1, divY_res = 0.1567)
mdl_divI_y2b$sigma2 <- 0.1097^2
extract_est <- function(fit, mdl){
#names_col <- c(names(mdl_infl_y2b$coef), "sigma")
est <- c(fit$coef, fit$sigma2^0.5) %>% matrix( nrow = 1) %>% as.data.frame
names(est) <- c(names(fit$coef), "sigma")
est$mdl <- mdl
est %<>% select(mdl, everything())
est
}
bind_rows(
extract_est(mdl_divI_y1a, "y1a"),
extract_est(mdl_divI_y2a, "y2a"),