-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRdemonstration.tex
2074 lines (1825 loc) · 87.3 KB
/
Rdemonstration.tex
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
\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\else % if luatex or xelatex
\ifxetex
\usepackage{mathspec}
\else
\usepackage{fontspec}
\fi
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage[margin=1in]{geometry}
\usepackage{hyperref}
\hypersetup{unicode=true,
pdftitle={R demonstration},
pdfauthor={Wolfgang Scherrer and Manfred Deistler},
pdfborder={0 0 0},
breaklinks=true}
\urlstyle{same} % don't use monospace font for urls
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
%%% Use protect on footnotes to avoid problems with footnotes in titles
\let\rmarkdownfootnote\footnote%
\def\footnote{\protect\rmarkdownfootnote}
%%% Change title format to be more compact
\usepackage{titling}
% Create subtitle command for use in maketitle
\newcommand{\subtitle}[1]{
\posttitle{
\begin{center}\large#1\end{center}
}
}
\setlength{\droptitle}{-2em}
\title{R demonstration}
\pretitle{\vspace{\droptitle}\centering\huge}
\posttitle{\par}
\author{Wolfgang Scherrer and Manfred Deistler}
\preauthor{\centering\large\emph}
\postauthor{\par}
\predate{\centering\large\emph}
\postdate{\par}
\date{Juli 11, 2018}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}
\begin{document}
\maketitle
{
\setcounter{tocdepth}{2}
\tableofcontents
}
\section{Introduction}\label{introduction}
This document is intended as a short guide to the modeling of
multivariate time series with VARMA models or state space models. We
mainly use two packages:
\begin{itemize}
\tightlist
\item
the MTS package, which is kind of companion toolbox for the text book
Tsay (2014), Multivariate Time Series Analysis, John Wiley \& Sons.
\item
and the dse package by Paul Gilbert, see e.g.~Gilbert, P., 2015. Brief
User's Guide: Dynamic Systems Estimation.
\url{http://cran.r-project.org/web/packages/dse/vignettes/Guide.pdf}.
\end{itemize}
We only discuss and use some parts of these packages. Both of them
include many more models and methods, e.g.~the MTS package also supports
multivariate volatility models, factor models and error-correction VAR
models for co-integrated time series.
Some utilities (in particular tools to convert MTS/dse objects) are
collected in \texttt{tools.R} A short description/manual of these tools
may be found at the end of this document.
This code is not thoroughly tested and thus should be used with some
care. In particular there are almost no input checks, so be sure to use
correctly specified parameters. Please feel free to use this code and to
change the code according to your own needs and preferences.
Often we use a syntax like \texttt{MTS::function} or
\texttt{dse::function} in order to make clear which package is used.
\section{VARMA Models}\label{varma-models}
We consider vector autoregressive moving average (VARMA) models of the
form \[
a_0 y_t = a_1 y_{t-1} + \cdots + a_p y_{t-p} +
b_0 \epsilon_t + b_1 \epsilon_{t-1} + \cdots + b_q \epsilon_{t-q}
\] where \((\epsilon_t\,|\,t\in\mathbb{Z})\) is \(n\)-dimensional white
noise with variance
\(\Sigma=\mathbb{E}\epsilon_t\epsilon_t'=I\in\mathbb{R}^{n\times n}\)
and \(a_j, b_j\in\mathbb{R}^{n\times n}\) are parameter matrices. It is
assumed that \(a_0=b_0\) is non singular and we most often consider the
case\footnote{Of course we can easily reparametrize the model
(\(a_j\rightarrow a_0^{-1}a_j\) and \(b_j\rightarrow a_0^{-1}b_j\)) in
order to satisfy the normalization constraint \(a_0=b_0=I\).}
\(a_0=b_0=I\in \mathbb{R}^{n\times n}\).
The AR/MA polynomials associated with this model are \[
\begin{array}{rcl}
a(z) &=& a_0 - a_1z-\cdots - a_pz^p\\
b(z) &=& b_0 + b_1z + \cdots + b_qz^q
\end{array}
\]
\subsection{\texorpdfstring{\texttt{MTS} implementation and
tools}{MTS implementation and tools}}\label{mts-implementation-and-tools}
First load the \texttt{MTS} library and construct an example VARMA(2,2)
model. Note that Tsay uses a different notation,
\[
\phi_0 y_t = \phi_1 y_{t-1} + \cdots + \phi_p y_{t-p} +
\phi_0 \epsilon_t - \theta_1 \epsilon_{t-1} - \cdots - \theta_q \epsilon_{t-q}
\] The AR and MA coefficients are collected in two matrices
\(\phi=(\phi_1,\ldots,\phi_p)\in\mathbb{R}^{n\times np}\) and
\(\theta=(\theta_1,\ldots,\theta_q)\in\mathbb{R}^{n\times nq}\):
\begin{verbatim}
> library(MTS) # load package
> source('tools.R') # load utility functions
>
> phi0 = matrix(c( 1.0, 0, 0,
+ -1.199, 1, 0,
+ -0.638, 0, 1), byrow = TRUE, nrow= 3)
> phi = matrix(c(0.762, 0, 0, -0.074, 0.137,-0.313,
+ -0.142,-0.470, 0.543, 0, 0, 0,
+ 0.920,-0.775, 0.064, 0, 0, 0),
+ byrow = TRUE, nrow= 3, ncol=6)
> theta = matrix(c( 0.694,-0.116,-0.150,-0.216, 0.269,-0.231,
+ -0.540,-0.253, 0.708, 0, 0, 0,
+ 0.748,-0.760, 0.242, 0, 0, 0),
+ byrow = TRUE, nrow= 3, ncol=6)
> sigma = matrix(c(0.815, 0.154, 0.411,
+ 0.154, 0.716, 0.202,
+ 0.411, 0.202, 0.813), nrow=3)
\end{verbatim}
\begin{verbatim}
> phi0
[,1] [,2] [,3]
[1,] 1.000 0 0
[2,] -1.199 1 0
[3,] -0.638 0 1
\end{verbatim}
\begin{verbatim}
> phi
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.762 0.000 0.000 -0.074 0.137 -0.313
[2,] -0.142 -0.470 0.543 0.000 0.000 0.000
[3,] 0.920 -0.775 0.064 0.000 0.000 0.000
\end{verbatim}
\begin{verbatim}
> theta
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.694 -0.116 -0.150 -0.216 0.269 -0.231
[2,] -0.540 -0.253 0.708 0.000 0.000 0.000
[3,] 0.748 -0.760 0.242 0.000 0.000 0.000
\end{verbatim}
\begin{verbatim}
> sigma
[,1] [,2] [,3]
[1,] 0.815 0.154 0.411
[2,] 0.154 0.716 0.202
[3,] 0.411 0.202 0.813
\end{verbatim}
If the stability condition \[
\det a(z) \neq 0 \quad \forall |z|\leq 1
\] holds, then the unique stationary solution of the above VARMA system
has a causal MA(\(\infty\)) representation \[
y_t = \sum_{j\geq 0} k_j \epsilon_{t-j}
\] The sequence \((k_j\in\mathbb{R}^{n\times n}\,|\, j\geq 0)\) is
called the \emph{impulse response function} of the VARMA system. Note
that \(k_0=I\) since \(a_0=b_0\). If \(\Sigma=HH'\), i.e.~if
\(\bar{\epsilon}_t = H^{-1}\epsilon_t\) has a unit variance, then
\((\bar{k}_j=k_jH\,|\, j\geq 0)\) is a so called \emph{orthogonalized
impulse response function}.
The (orthogonalized) impulse response function may be computed with the
function \texttt{VARMAirf}. Note that \texttt{VARMAirf} assumes
\(\phi_0=I\) (\(a_0=I\)) and hence we reparametrize the model by
\(\phi \rightarrow \phi_0^{-1}\phi\) and
\(\theta \rightarrow \phi_0^{-1}\theta\).
The function \texttt{VARMAirf} returns a list with components
\texttt{psi} and \texttt{irf}. The component \texttt{psi} is the matrix
\((k_0,k_1,\ldots,k_l)\in\mathbb{R}^{n\times n(l+1)}\). The component
\texttt{irf} is the matrix\footnote{\(\mbox{vec}\) denotes the
vectorization operator, i.e. \(\mbox{vec}(X)\) is the
\(mn\)-dimensional column vector obtained by stacking the columns of
the \((m,n)\)-dimensional matrix \(X\).}
\((\mbox{vec}(\bar{k}_0),\mbox{vec}(\bar{k}_1),\ldots,\mbox{vec}(\bar{k}_l))\in\mathbb{R}^{n^2\times (l+1)}\),
i.e. \texttt{irf} contains the desired orthogonalized impulse response
coefficients. Note that the \texttt{MTS} package here uses two different
methods to represent a \(3\)-dimensional array by a (\(2\)-dimensional)
matrix!
\texttt{VARMAirf} uses the symmetric square root of \(\Sigma\) (computed
via an eigenvalue decomposition of \(\Sigma\)) and hence the lag zero
coefficient \(\bar{k}_0=k_0 H=H=H'\) is symmetric.
\texttt{VARMAirf} always produces two plots (one for the
(orthogonalized) impulse response function and one for the cumulative
(orthogonalized) impulse response function). In order to be somewhat
more flexible we have implemented a simple function (\texttt{plot3d})
for plotting \(3\)-dimensional arrays like the impulse response function
or the autocovariance function. The \((i,j)\)-th panel plots the
respective \((i,j)\)-component of \(\bar{k}_l\) as a function of the lag
\(l=0,1,2\ldots\) and hence shows influence of the \(j\)-th
``orthogonalized shock'' \(\bar{\epsilon}_{jt}\) on the \(i\)-th
component \(y_{i,t+l}\).
\begin{verbatim}
> k = MTS::VARMAirf(Phi = solve(phi0,phi),
+ Theta = solve(phi0, theta),
+ Sigma = sigma, lag = 12, orth = TRUE)
\end{verbatim}
\includegraphics{Rdemonstration_files/figure-latex/VARMAirf-1.pdf}
\begin{verbatim}
Press return to continue
\end{verbatim}
\includegraphics{Rdemonstration_files/figure-latex/VARMAirf-2.pdf}
\begin{verbatim}
>
> plot3d(k$irf, dim = c(3,3,13),
+ main='orthogonalized impulse response function',
+ labels.ij="partialdiff*y[i_*k]/partialdiff*epsilon[j_*0]",
+ type='o', lty='solid', col='brown4', pch=19, cex=0.5)
\end{verbatim}
\includegraphics{Rdemonstration_files/figure-latex/VARMAirf-3.pdf}
The autocovariance function \[
\gamma_j = \mathbb{E}y_{t+j} y_t' = \sum_{l\geq 0} k_{j+l}\Sigma k_j' \quad j\geq 0
\] of the VARMA process \((y_t)\) may be easily computed with the
function \texttt{ARMAcov}. This function returns a list with components
\texttt{autocov} and \texttt{ccm}, where \texttt{autocov} stores the
autocovariances, i.e.~the matrix
\((\gamma_0,\gamma_1,\ldots,\gamma_l)\in\mathbb{R}^{n\times n(l+1)}\)
and the \((n\times n(l+1))\) matrix \texttt{ccm} contains the
autocorrelations\footnote{Here \(\mbox{diag}(\gamma_0)^{-1/2}\) denotes
the diagonal matrix with diagonal elements \((\gamma_{0,ii})^{-1/2}\),
i.e.~the reciprocals of the standard deviations of the components
\(y_{it}\).}
\(\rho_j = \mbox{diag}(\gamma_0)^{-1/2} \gamma_j \mbox{diag}(\gamma_0)^{-1/2}\),
\(j=0,1,\ldots,l\).
To be precise \texttt{VARMAcov} computes an approximation of the
autocovariance function by the finite sum
\(\sum_{l=0}^{m} k_{j+l}\Sigma k_j'\), where the number \(m\) of lags
used corresponds to the optional parameter \texttt{trun}.
Note that \texttt{VARMAcov} always prints the computed autocovariances
and autocorrelations (called cross correlation matrices) and hence here
we suppress the output of the next \texttt{R} block.
\begin{verbatim}
> g = MTS::VARMAcov(Phi = solve(phi0,phi),
+ Theta = solve(phi0,theta),
+ Sigma = sigma, lag = 12)
>
> plot3d(g$ccm, dim = c(3,3,13),
+ main = 'auto correlation function',
+ labels.ij = "corr(list(y[i_*k],y[j_*0]))",
+ type = 'h', col = 'blue4', lwd = 5, lend = 1)
\end{verbatim}
\includegraphics{Rdemonstration_files/figure-latex/VARMAcov-1.pdf}
If in addition the miniphase assumption \[
\det b(z) \neq 0 \quad \forall |z| < 1
\] holds, then the \(\epsilon_t\)'s are the \emph{innovations} of the
process \((y_t)\) and the above MA(\(\infty\)) representation is the
Wold representation of the process. The variance of the \(h\)-step ahead
prediction errors from the infinite past then is given by \[
\Sigma_h = \mathbb{E}(y_{t+h}-\hat{y}_{t,h})(y_{t+h}-\hat{y}_{t,h})' = \sum_{l=0}^{h-1} k_l \Sigma k_l' = \sum_{l=0}^{h-1} \bar{k}_l \bar{k}_l'
\] For the variance of the \(i\)-th component of the forecast errors we
obtain \[
\mathbb{E}(y_{i,t+h}-\hat{y}_{i,t,h})^2 = \sum_{l=0}^{h-1} \sum_{j=1}^{n} \bar{k}_{l,ij}^2 = \sum_{j=1}^{n} \sigma^h_{ij}
\quad \mbox{ where } \sigma^h_{ij} = \sum_{l=0}^{h-1} \bar{k}_{l,ij}^2.
\] and hence the ratio \[
c^h_{ij} = \frac{\sigma^h_{ij}}{\sum_{m=1}^n \sigma^h_{im}}
\] is the fraction of the forecast error variance of the \(i\)-th
component due to the \(j\)-th component of the (orthogonalized) shocks
\(\bar{\epsilon}_t\). This is the so called \emph{forecast error
variance decomposition} which may be computed by the utility function
\texttt{fevd}. This function takes as a main argument an arbitrary
orthogonal impulse response function (e.g.~computed by \texttt{ARMAirf})
and returns a list with two components. The first element \texttt{vd} is
an \((n,n,h_{\max})\)-dimensional array where the \((i,j,h)\)-th entry
is equal to \(c^h_{ij}\) and the second element \texttt{v} is an
\((n,h_{\max})\)-dimensional matrix where the \((i,h)\)-th element is
the variance of the \(i\)-th component of the \(h\)-step ahead forecast
error, i.e. \(\sum_{m=1}^n \sigma_{im}^h\). The maximum forecast horizon
\(h_{\max}\) is determined by the length of the input.
A plot\footnote{The choice for the \texttt{series.names} will become
clear later on.} of this decomposition may be obtained by
\texttt{plotfevd}.
It seems that the \texttt{MTS} function \texttt{FEVdec} has a bug.
Furthermore the orthogonalization scheme is ``hardwired'' (a Cholesky
decomposition of \(\Sigma\)). Therefore we have implemented an own
version.
\begin{verbatim}
> out = fevd(k$irf, dim = c(3,3,13))
> plotfevd(out$vd,
+ series.names = c('consumption','investment','income'))
\end{verbatim}
\includegraphics{Rdemonstration_files/figure-latex/FEVD-1.pdf}
The stability and the miniphase assumptions may be checked with the
utility function \texttt{is.stable}. In the next subsection we will also
discuss how to check these assumption with \texttt{dse} package tools.
\begin{verbatim}
> #check stability assumption
> is.stable(solve(phi0,phi))
[1] TRUE
attr(,"z")
[1] -7.072110e-01-1.402435e+00i -7.072110e-01+1.402435e+00i
[3] 1.459938e+00-1.005212e+00i 1.459938e+00+1.005212e+00i
[5] 9.596578e+15+0.000000e+00i 2.477278e+17+0.000000e+00i
\end{verbatim}
\begin{verbatim}
> #check miniphase assumption
> is.stable(solve(phi0,theta))
[1] TRUE
attr(,"z")
[1] 8.050519e-01-1.478711e+00i 8.050519e-01+1.478711e+00i
[3] -3.306052e-01-2.326104e+00i -3.306052e-01+2.326104e+00i
[5] 3.396897e+14+0.000000e+00i -7.608720e+15+0.000000e+00i
\end{verbatim}
The VARMA model, i.e.~the degrees \(p,q\) and the parameters
\(a_j,b_j\), are not unique for a given (VARMA) process \((y_t)\)
without additional restrictions. However, note that (due to the
stability and miniphase assumption) the causal MA(\(\infty\))
representation is the Wold representation of the process and hence is
unique. This means that the impulse response coefficients
\((k_j\,|\,j\geq 0)\) are unique. One possibility to get a unique
(identifiable) representation is to use the \emph{echelon canonical
form}. The construction of this canonical form is based on the Hankel
matrix of the impulse response coefficients \[
H = \begin{pmatrix}
k_1 & k_2 & k_3 & \cdots \\
k_2 & k_3 & k_4 & \cdots \\
k_3 & k_4 & k_5 & \cdots \\
\vdots & \vdots & \vdots &
\end{pmatrix}.
\] For VARMA models this matrix has a finite rank. The so called
\emph{Kronecker indices} \((\nu_1,\nu_2,\ldots,\nu_n)\) describe a basis
for the row space of \(H\). The rows with indices \[
j \in \bigcup_{\begin{smallmatrix}1\leq i\leq n \\ \nu_i>0 \end{smallmatrix}} \{i, n+i, \ldots, n(\nu_i-1)+i\}
\] form a basis for the row space of \(H\). Clearly the rank of \(H\) is
equal to the sum of the Kronecker indices
(\(\mbox{rk}(H)=\sum_{i=1}^n \nu_i\)). The echelon canonical form
restricts certain elements of the AR/MA parameter matrices to zero or
one. The position and the number of these restriction depends on the
corresponding Kronecker indices.
The utility function \texttt{impresp2PhiTheta} computes for a given
impulse response the Kronecker indices and the corresponding VARMA model
in echelon canonical form. The computations are based on a finite sub
matrix \(H_{f,p}\) of the infinite dimensional Hankel matrix with \(f\)
block rows and \(p\) block columns. The numbers \(f,p\) are determined
from the length of the input sequence. The core computation is to
determine a basis for the row space. This is done via a QR decomposition
of the transpose \(H_{f,p}'\) with the R function \texttt{qr}. The
output of \texttt{impresp2PhiTheta} is a list with components
\texttt{Phi}, \texttt{Theta}, \texttt{Phi0} (these matrices contain the
AR/MA parameters), \texttt{kidx} (the vector of Kronecker indices),
\texttt{Hrank} (the (computed) rank of the Hankel matrix \(H\)) and
\texttt{Hpivot} (as returned by \texttt{qr()}). Note that the first
\texttt{Hrank} elements of the vector \texttt{Hpivot} contain the
indices of the basis rows of \(H_{f,p}\).
The function \texttt{MTS::Kronspec} determines the zero/one restrictions
imposed by the echelon canonical for given Kronecker indices
(\texttt{kdx}) and prints a nice representation of these restrictions
(for \texttt{output\ =\ TRUE}).
\begin{verbatim}
> out = impresp2PhiTheta(k$psi)
> out$kidx # Kronecker indices
[1] 2 1 1
\end{verbatim}
\begin{verbatim}
> # display the corresponding AR/MA restrictions
> junk = MTS::Kronspec(out$kidx)
Kronecker indices: 2 1 1
Dimension: 3
Notation:
0: fixed to 0
1: fixed to 1
2: estimation
AR coefficient matrices:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 0 0 2 0 0 2 2 2
[2,] 2 1 0 2 2 2 0 0 0
[3,] 2 0 1 2 2 2 0 0 0
MA coefficient matrices:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 0 0 2 2 2 2 2 2
[2,] 2 1 0 2 2 2 0 0 0
[3,] 2 0 1 2 2 2 0 0 0
\end{verbatim}
\begin{verbatim}
>
> all.equal(cbind(out$Phi0, out$Phi, out$Theta),
+ cbind(phi0, phi, theta))
[1] TRUE
\end{verbatim}
The Kronecker indices are \((2,1,1)\) and hence the rank of the Hankel
matrix is \(2+1+1=4\) and the rows \(1,2,3,4\) (i.e.~the first 4 rows)
of \(H\) form a basis. The number of ``free parameters'' is 24. The last
statement of the above \texttt{R} code shows that the VARMA model, we
have started with, is in Echelon canonical form.
\subsection{\texorpdfstring{\texttt{dse} implementation and
tools}{dse implementation and tools}}\label{dse-implementation-and-tools}
The package \texttt{dse} uses an object oriented approach (with the S3
class system) and implements object classes for models, data sets and
estimated models. VARMA models\footnote{The \texttt{dse} package uses
yet another convention for the sign of the AR/MA parameters!}
\[
a_0 y_t + a_1 y_{t-1} + \cdots + a_p y_{t-p} = b_0 \epsilon_t + b_1 \epsilon_{t-1} + \cdots + b_q \epsilon_{t-q}
\] are represented by \texttt{ARMA} objects (which are special
\texttt{TSmodel} objects). Note that the \texttt{ARMA} model class may
represent more general models, in particular models with exogenous
inputs (i.e.~VARMAX models) and models with a trend component. In
addition note that \(a_0\) and \(b_0\) may be different. However, here
we will stick to the simple model above and assume that \(a_0=b_0=I\).
The AR parameters \(a_j\in \mathbb{R}^{n\times n}\) are stored in the
\((p+1,n,n)\)-dimensional array \texttt{A}, where the \(i\)-th slot
\texttt{A{[}i,,{]}} corresponds to the matrix
\(a_{i-1}\in \mathbb{R}^{n\times n}\). Analogously the MA parameters
\(b_j\) are stored in the \((q+1,n,n)\)-dimensional array \texttt{B}.
In order to be able to easily switch between \texttt{MTS} and
\texttt{dse} models and tools we have implemented two utility functions
\texttt{PhiTheta2ARMA} and \texttt{ARMA2PhiTheta}.
Load the \texttt{dse} library and convert the above VARMA model to an
\texttt{dse::ARMA} object. In addition we check that we can reconstruct
the \texttt{Theta}, \texttt{Phi} parameters:
\begin{verbatim}
> library(dse)
Loading required package: tfplot
Loading required package: tframe
Attaching package: 'dse'
The following objects are masked from 'package:stats':
acf, simulate
\end{verbatim}
\begin{verbatim}
>
> arma = PhiTheta2ARMA(Phi = phi, Theta = theta, Phi0 = phi0,
+ output.names = c('consumption','investment','income'))
> arma
A(L) =
1-0.762L1+0.074L2 0-0.137L2 0+0.313L2
-1.199+0.142L1 1+0.47L1 0-0.543L1
-0.638-0.92L1 0+0.775L1 1-0.064L1
B(L) =
1-0.694L1+0.216L2 0+0.116L1-0.269L2 0+0.15L1+0.231L2
-1.199+0.54L1 1+0.253L1 0-0.708L1
-0.638-0.748L1 0+0.76L1 1-0.242L1
\end{verbatim}
\begin{verbatim}
>
> junk = ARMA2PhiTheta(arma, normalizePhi0 = FALSE)
> all.equal(cbind(phi, theta, phi0),
+ cbind(junk$Phi, junk$Theta, junk$Phi0))
[1] TRUE
\end{verbatim}
The stability and the miniphase assumption now may be checked with the
function \texttt{polyrootDet(a))} which computes the roots of the
determinant of a polynomial matrix
\(a(z) = a_0 + a_1 z +\cdots + a_p z^p\) with coefficients which are
stored in the 3-dimensional array \texttt{a}.
\begin{verbatim}
> # check the stability assumption
> min(abs(polyrootDet(arma$A)))>1
[1] TRUE
\end{verbatim}
\begin{verbatim}
> # check the (strict) miniphase assumption
> min(abs(polyrootDet(arma$B)))>1
[1] TRUE
\end{verbatim}
The \texttt{dse} package contains a number of useful utilities for
polynomial matrices (e.g. \texttt{characteristicPoly},
\texttt{companionMatrix}, \texttt{polydet}, \ldots{}).
\section{State Space Models}\label{state-space-models}
State space models are an alternative way to describe processes with a
rational spectral density. We consider models of the form \[
\begin{array}{rcl}
x_{t+1} &=& A x_t + B \epsilon_t \\
y_t &=& C x_t + \epsilon_t
\end{array}
\] where \((\epsilon_t)\) is \(n\)-dimensional white noise with a
variance \(\Sigma =\mathbb{E}\epsilon_t \epsilon_t '\), \(x_t\) is an
observed \(s\)-dimensional random vector called state and
\(A\in\mathbb{R}^{s\times s}\), \(B\in\mathbb{R}^{s\times n}\) and
\(C\in\mathbb{R}^{n\times s}\) are parameter matrices. We always impose
the stability assumption \[
\lambda_{\max} (A) <1
\] and the miniphase assumption \[
\lambda_{\max} (A-BC) \leq 1.
\] Here \(\lambda_{\max}(X)\) denotes the spectral radius of \(X\),
i.e.~the maximum of the moduli of the eigenvalues of \(X\). Given these
assumption there exists a unique stationary solution \((y_t)\) and this
solution is of the form \[
y_t = \sum_{j\geq 0} k_j \epsilon_{t-j}
\] Furthermore the \(\epsilon_t\)'s are the innovations of the process
\((y_t )\) and the above MA representation is the Wold representation of
the process. Therefore one says that the above state model is in
\emph{innovation form}.
State space models are implemented as \texttt{SS} objects in
\texttt{dse}. However, \texttt{dse} uses a different naming convention,
i.e. \(A \rightarrow F\), \(B \rightarrow K\) and \(C \rightarrow H\).
The \texttt{dse} package also handles state space models which are not
in innovation form. To convert the above VARMA model to a state space
model (in innovation form), we may use the function\footnote{Note that
the function \texttt{toSS} does not work for ARMA(\(p,q\)) models with
\(q>p\)!} \texttt{toSS}. The function \texttt{is.innovSS} checks
whether the input is an ``innovation form state space'' object.
\begin{verbatim}
> ss = dse::toSS(arma)
> ss
F =
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0 0 0 -0.074000 0.137000 -0.313000
[2,] 0 0 0 -0.088726 0.164263 -0.375287
[3,] 0 0 0 -0.047212 0.087406 -0.199694
[4,] 1 0 0 0.762000 0.000000 0.000000
[5,] 0 1 0 0.771638 -0.470000 0.543000
[6,] 0 0 1 1.406156 -0.775000 0.064000
H =
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0 0 0 1 0 0
[2,] 0 0 0 0 1 0
[3,] 0 0 0 0 0 1
K =
[,1] [,2] [,3]
[1,] 0.142000 -0.132000 -0.082000
[2,] 0.170258 -0.158268 -0.098318
[3,] 0.090596 -0.084216 -0.052316
[4,] 0.068000 0.116000 0.150000
[5,] 0.479532 -0.077916 0.014850
[6,] 0.215384 0.059008 -0.082300
\end{verbatim}
\begin{verbatim}
> dse::is.innov.SS(ss)
[1] TRUE
\end{verbatim}
We here get a model with a state space dimension \(s=6\).
The impulse response function and the autocovariance function of a
process described by a state state space model in innovation form my be
easily computed as follows: \[
\begin{array}{rcll}
k_0 &=& I \\
k_j &=& C A^{j-1} B & \mbox{ for } j>0
\end{array}
\] The variance of the state \(x_t\) is the solution of a so called
Lyapunov equation \[
P = \mathbb{E} x_t x_t' = APA' + B\Sigma B'
\] The autocovariance function then is \[
\begin{array}{rcll}
\gamma_0 = \mathbb{E} y_t y_t' &=& C P C' + \Sigma \\
\gamma_j = \mathbb{E} y_{t+j} y_t' &=& C A^{j-1} (APC' + B\Sigma) & \mbox{ for } j>0
\end{array}
\]
The utility function \texttt{SSirf} and \texttt{SScov} implement the
above scheme to compute the impulse response and autocovariance
function. The outputs returned by these function have the same structure
as the output of the corresponding \texttt{MTS} function, e.g.
\texttt{SScov} returns a list with components \texttt{autocov} and
\texttt{ccm} where both of them are matrices of dimension
\((n,n(l+1))\). To compute the state variance \(P\) here the
function\footnote{Alternatively one may use the function
\texttt{dse::Riccati}. However for the model we use here for testing
purposes the (non iterative version) of \texttt{Riccati} stops with an
error message. The function \texttt{lyap} first computes a Schur
decomposition of the state transition matrix \(A\) (respectively
\(F\)). To this end the \texttt{QZ} package has to be installed!}
\texttt{lyap} is used.
The following code computes the impulse response function and the ACF of
the state space model. Of course, since this state space model and the
VARMA model above describe the same process the output must be identical
to the output we have computed above.
\begin{verbatim}
> k.ss = SSirf(ss, Sigma = sigma, lag.max = 12, orth = TRUE)
> all.equal(k, k.ss, check.attributes = FALSE)
[1] TRUE
\end{verbatim}
\begin{verbatim}
>
> g.ss = SScov(ss, Sigma = sigma, lag.max = 12)
> all.equal(g, g.ss)
[1] TRUE
\end{verbatim}
State space models (like VARMA models) are by no means unique. Even the
state space dimension \(s\) is not unique. A model is called
\emph{minimal} if its state space dimension is minimal among all state
space models which describe the process. A state space model is called
\emph{observable} (respectively \emph{reachable}) if the observability
matrix \(O = (C',A'C',\ldots,(A')^{s-1}C')'\in \mathbb{R}^{ns\times s}\)
has rank \(s\) (respectively if the reachability matrix
\((B,AB,\ldots,A^{s-1}B)\in\mathbb{R}^{s\times ns}\) has rank \(s\)). It
is a fundamental result in the theory of state space models that a state
space model is minimal if and only the model is both observable and
reachable. The minimal state dimension is equal to the rank of the
Hankel matrix \(H\) of the impulse response coefficients \((k_j)\).
The \texttt{dse} tools \texttt{observability} and \texttt{reachability}
compute the singular values of the observability respectively of the
reachability matrices.
\begin{verbatim}
> svO = dse::observability(ss)
> svR = dse::reachability(ss)
Singular values of reachability matrix for noise: 0.7443343 0.4589284 0.3712107 0.2409465 2.989818e-17 1.082585e-18
\end{verbatim}
\begin{verbatim}
> signif(rbind(svO,svR),4)
[,1] [,2] [,3] [,4] [,5] [,6]
svO 2.9990 2.6280 1.7220 0.9879 9.206e-01 4.450e-01
svR 0.7443 0.4589 0.3712 0.2409 2.990e-17 1.083e-18
\end{verbatim}
Inspecting these singular values shows that the model is not reachable
and hence is \emph{not minimal}. This observation is also verified by
the fact that the state variance \(P\) is not regular. The eigenvalues
of \(P\) are
\begin{verbatim}
> P = lyap(ss$F, ss$K %*% sigma %*% t(ss$K)) # compute the state variance P
> eigen(P, only.values = TRUE)$values
[1] 4.211826e-01 1.233055e-01 7.788831e-02 3.473577e-02
[5] -7.009378e-19 -1.415094e-17
\end{verbatim}
One possibility to achieve a minimal model is to use a ``balancing and
truncation'' scheme. The \texttt{dse} package offers the function
\texttt{balanceMittnik(model,n)} to this end. The (optional) parameter
\texttt{n} is the desired state dimension. Here we use \texttt{n=4}
based on the fact that only \(4\) of the reachability singular values
are significantly greater than zero. The following code computes a
(minimal) state space model with a state space dimension \(4\) and
checks that this model really is an equivalent description of the
process \((y_t)\):
\begin{verbatim}
> ssb = dse::balanceMittnik(ss, n=4)
> ssb
F =
[,1] [,2] [,3] [,4]
[1,] -0.1327857 0.1813460 0.3120305 0.3311117
[2,] -0.7805031 0.1090193 0.2904374 -0.1461123
[3,] 0.0715883 -0.6736422 0.6819328 0.2285732
[4,] -0.2674132 -0.4954136 -0.3930425 -0.2987692
H =
[,1] [,2] [,3] [,4]
[1,] 0.02484203 0.07963300 -0.4383435 0.184895023
[2,] -0.56526277 0.02998341 -0.2627269 0.183926049
[3,] -0.19050836 -0.45709445 -0.3611779 -0.001188339
K =
[,1] [,2] [,3]
[1,] -0.76035926 0.2878963 0.1622077
[2,] 0.01509112 -0.1126601 0.2372431
[3,] -0.21419099 -0.1738510 -0.1596715
[4,] -0.04079621 0.2276468 0.3100379
\end{verbatim}
\begin{verbatim}
> k.ss = SSirf(ss, Sigma = sigma, lag.max = 12, orth = TRUE)
> all.equal(k, k.ss, check.attributes = FALSE)
[1] TRUE
\end{verbatim}
To check the stability assumption we may use the function
\texttt{stability}. For the miniphase assumption there is no
corresponding tool. However, it is not difficult to check this
assumption ``manually'' by computing the eigenvalues of \((A-BC)\)
(respectively using the \texttt{dse} notation of \((F-KH)\)):
\begin{verbatim}
> dse::stability(ssb)
The system is stable.
[1] TRUE
attr(,"roots")
Eigenvalues of F moduli
[1,] -0.2859941+0.5749215i 0.6421272+0i
[2,] -0.2859941-0.5749215i 0.6421272+0i
[3,] 0.4656927+0.3115639i 0.5603051+0i
[4,] 0.4656927-0.3115639i 0.5603051+0i
\end{verbatim}
\begin{verbatim}
> lambda = eigen(ssb$F - ssb$K %*% ssb$H)$values
> cat(ifelse((max(abs(lambda))<1),
+ 'The system is strictly miniphase\n',
+ 'The system is not strictly miniphase\n'))
The system is strictly miniphase
\end{verbatim}
Even minimal systems are not identifiable. There is still the freedom to
apply a state space transformation \(x_t \longrightarrow T x_t\), where
\(T\in \mathbb{R}^{s\times s}\) is non singular. The state parameter
matrices then are transformed as \[
\begin{array}{rcl}
A &\rightarrow& T A T^{-1} \\
B &\rightarrow& T B \\
C &\rightarrow& C T^{-1}
\end{array}
\] See also the function \texttt{dse::gmap}.
Analogously to the VARMA case one may define an echelon canonical form
for state space models. The parameter matrices have certain elements
which are restricted to be one or zero and the position of these
restricted elements again are determined by the Kronecker indices of the
Hankel matrix of the impulse response coefficients. The utility function
\texttt{impresp2SS} computes the state space model in echelon form for a
given impulse response function. (This provides an alternative to
construct a (unique, minimal) state space model which is equivalent to a
given VARMA model.)
\begin{verbatim}
> sse = impresp2SS(k$psi, type = 'echelon')$ss
> sse
F =
[,1] [,2] [,3] [,4]
[1,] 0.000 0.000 0.000 1.000
[2,] -0.142 -0.470 0.543 1.199
[3,] 0.920 -0.775 0.064 0.638
[4,] -0.074 0.137 -0.313 0.762
H =
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 0 0 1 0
K =
[,1] [,2] [,3]
[1,] 0.068000 0.116000 0.15000
[2,] 0.479532 -0.077916 0.01485
[3,] 0.215384 0.059008 -0.08230
[4,] 0.193816 -0.043608 0.03230
\end{verbatim}
\begin{verbatim}
> k.ss = SSirf(sse, Sigma = sigma, lag.max = 12, orth = TRUE)
> all.equal(k, k.ss, check.attributes = FALSE)
[1] TRUE
\end{verbatim}
For this model the number of ``free parameters'' is 24.
\section{Estimation}\label{estimation}
\subsection{Data}\label{data}
In order to illustrate the estimation of VARMA and state space models
here we use a data set from the ``FRED (Federal Reserve Economic Data)''
database (\url{https://fred.stlouisfed.org}). We use the following three
quarterly time series series:
\begin{itemize}
\tightlist
\item
\texttt{DPIC96} Real Disposable Personal Income
\item
\texttt{GPDIC1} Real Gross Private Domestic Investment
\item
\texttt{PCECC96} Real Personal Consumption Expenditures
\end{itemize}
The variables are measured in Billions of Chained 2009 Dollars.
We consider the quarterly growth rates (i.e.~the differences of the log
values) and demean and scale the growth rates such that the sample
variance is equal to one. The whole date set is split into two parts:
The data from 1985 to 1991 is used for the estimation of the models and
the data from 1992 to the end of 2017 is used for the comparison of the
models (in terms of their predictive power).
We use a matrix \texttt{y} to store the data (for estimation with the
\texttt{MTS} tools) and a \texttt{TSdata} object which is used for the
estimation by \texttt{dse} tools.
\begin{verbatim}
> # read the data into an R data.frame object
> data = read.delim(file = 'prCoIn_Quarterly.txt',header=TRUE)
> data$DATE = as.Date(data$DATE)
>
> # compute the quarterly growth rates
> data$consumption = c(NA,diff(log(data$PCECC96)))
> data$investment = c(NA,diff(log(data$GPDIC1)))
> data$income = c(NA,diff(log(data$DPIC96)))
>
> # skip the data before 1958
> d = as.POSIXlt(data$DATE)
> data = data[d >= as.POSIXlt('1958-01-01'), ]
> d = d[d >= as.POSIXlt('1958-01-01')]
>
> # collect the time series to be analyzed in the matrix "y"
> y = cbind(data$consumption,data$investment,data$income)
> colnames(y) = c('consumption','investment','income')
> rownames(y) = paste(format(data$DATE,'%Y'),
+ quarters(data$DATE),sep=' ')
>
> n = ncol(y) # number of variables
> T.obs = nrow(y) # total sample size
> T.est = sum(d < as.POSIXlt('1992-01-01')) # estimation sample
>
> # demean and scale the data
> y = scale(y, center = colMeans(y[1:T.est,]),
+ scale = sqrt((T.est-1)/T.est)*apply(y[1:T.est,],
+ MARGIN = 2, FUN = sd))
>
> data.start = c(1900 + d[1]$year, d[1]$mon %/% 3 +1)
> data.end = c(1900 + d[T.obs]$year, d[T.obs]$mon %/% 3 +1)
> est.end = c(1900 + d[T.est]$year, d[T.est]$mon %/% 3 +1)
>
> # construct "dse" TSdata objects
> sample = dse::TSdata(output = y)
> sample = tframed(sample, list(start=data.start, frequency=4))
> estimation.sample = tfwindow(sample, end = est.end)
>
> # plot the data
> par(oma = c(0,0,0,0), mar = c(2,2,1,0)+0.1, tcl = -0.2,
+ mgp = c(1.25, 0.15, 0), cex.main = 1, cex.axis = 0.75)
> tfplot(sample)
\end{verbatim}
\includegraphics{Rdemonstration_files/figure-latex/data-1.pdf}
\begin{verbatim}
>
> cat('(quarterly) data: ', paste(colnames(y), collapse=','), '\n',
+ 'total sample: ', rownames(y)[1],'-',rownames(y)[T.obs], ' ',
+ T.obs, ' observations\n',
+ 'estimation sample: ', rownames(y)[1],'-',rownames(y)[T.est], ' ',
+ T.est, ' observations\n',
+ 'validation sample: ', rownames(y)[T.est+1],'-',rownames(y)[T.obs], ' ',
+ T.obs-T.est, ' observations\n',sep='')
(quarterly) data: consumption,investment,income
total sample: 1958 Q1-2017 Q4 240 observations
estimation sample: 1958 Q1-1991 Q4 136 observations
validation sample: 1992 Q1-2017 Q4 104 observations
\end{verbatim}
\subsection{Evaluate and Forecast}\label{evaluate-and-forecast}
In order to evaluate the ``fit'' of a given model on a data set we may
use the \texttt{dse::l()} function which in particular computes the
(log) likelihood of the model. The output of this command is a
\texttt{TSestModel} which is an object which stores the model, the data
and the estimation results.
\begin{verbatim}
> arma = l(arma, estimation.sample)
> summary(arma)
neg. log likelihood = 506.1354 sample length = 136
consumption investment income
RMSE 0.9304949 1.232342 0.7825009
ARMA:
inputs :
outputs: consumption investment income
input dimension = 0 output dimension = 3
order A = 2 order B = 2 order C =
26 actual parameters 6 non-zero constants
trend not estimated.
\end{verbatim}
Forecasts may be computed by the \texttt{dse} functions
\texttt{forecast}, \texttt{horizonForecasts} and
\texttt{featherForecasts}. The function \texttt{dse::forecast} computes
the out-of-sample forecasts for a given model and sample. In the example
below we compute the forecasts for 2018-2020, i.e.~for forecast horizons
\(h=1,2,\ldots,12\). The corresponding \texttt{MTS} function (for
estimated VARMA models) is \texttt{MTS::VARMApred}.
The function \texttt{dse::horizonForecasts} computes the \(h\)-step
ahead forecasts for all time points within a sample. The forecasts are
``aligned'' with the original data, such that it is easy to compute the
forecast errors. In the code below we use the state space model
\texttt{sse} (which is equivalent to the VARMA model \texttt{arma}) in
order to show that the syntax is independent of the object class. The
optional parameter \texttt{discard.before} means that predictions based