-
Notifications
You must be signed in to change notification settings - Fork 13
/
threshold_functions.cpp
165 lines (144 loc) · 4.47 KB
/
threshold_functions.cpp
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
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace RcppArmadillo;
#include <RcppArmadilloExtensions/sample.h>
// [[Rcpp::export]]
NumericVector KF(NumericMatrix y, NumericMatrix Z,NumericMatrix Ht, NumericMatrix Qtt,int m, int p, int t, NumericVector B0, NumericMatrix V0) {
static Rcpp::Function asVector("as.vector");
//Define everything calculated down there
arma::vec bp = Rcpp::as<arma::vec>(B0);
arma::mat Vp = Rcpp::as<arma::mat>(V0);
arma::mat Qt =Rcpp::as<arma::mat>(Qtt);
arma::mat bt(t,m);
arma::mat Vt(pow(m,2),t);
arma::mat R(p,m);
arma::mat H(t*m,p);
arma::mat cfe(p,1);
arma::mat yt(p,1);
arma::mat f(p,p);
arma::mat inv_f(p,p);
arma::mat btt(m,1);
arma::mat Vtt(m,m);
arma::cube test(m,m,t);
for (int i=1;i<(t+1);++i){
arma::mat Qt =diag(Qtt(i-1,_));
//int i=t;
R= Ht(Range((i-1)*p,(i*p)-1),_);
H = Z(Range((i-1)*p,(i*p)-1),_);
yt=y(_,i-1);
cfe= yt-H*bp;
f = H*Vp*trans(H)+R;
inv_f = solve(f.t(),H).t();//inv(f);
//btt = bp+Vp*trans(H)*inv_f*cfe; // bp=btt-1
btt = bp+Vp*inv_f*cfe;
//Vtt = Vp-Vp*trans(H)*inv_f*H*Vp;
Vtt = Vp-Vp*inv_f*H*Vp;
if ((i-1)<t){
bp=btt;
Vp=Vtt+Qt;
}
bt.row(i-1) = trans(btt);
test.slice(i-1)=Vtt;
}
//draw S(T|T) ~ N(S(T|T),P(T|T))
arma::mat bdraw(t,m);
arma::mat Y = arma::randn(m, 1);
arma::mat bmean(m,1);
arma::mat bvar(m,m);
arma::vec eigval;
arma::mat eigvec;
//eig_sym(eigval,eigvec,Vtt,"std");
arma::eig_sym(eigval,eigvec,Vtt);
//eigvec*diagmat(sqrt(pmax(eigvec,0)))
NumericVector epsilon(m);
arma::vec arma_epsilon(epsilon.begin(),epsilon.length(),false);
epsilon = rnorm(m);
bdraw.row(t-1)=(btt+eigvec*diagmat(arma::sqrt(arma::max(eigval,arma::zeros<arma::vec>(m))))*arma_epsilon).t();
//backward recurssions
arma::mat bf(1,m);
for (int i=1;i<t;++i){
arma::mat Qt =diag(Qtt(t-i,_));
bf=trans(bdraw.row(t-i));
btt=trans(bt.row(t-i-1));
Vtt=test.slice(t-i-1);
f=Vtt+Qt;
inv_f=solve(f.t(),Vtt.t()).t();
cfe=bf-btt;
//bmean=btt+Vtt*inv_f*cfe;
bmean=btt+inv_f*cfe;
//bvar=Vtt-Vtt*inv_f*Vtt;
bvar=Vtt-inv_f*Vtt;
//eig_sym(eigval,eigvec,bvar,"std");
arma::eig_sym(eigval,eigvec,bvar);
//eigvec*diagmat(sqrt(pmax(eigvec,0)))
epsilon = rnorm(m);
bdraw.row(t-i-1)= (bmean+eigvec*diagmat(arma::sqrt(arma::max(eigval,arma::zeros<arma::vec>(m))))*arma_epsilon).t();
}
bdraw=trans(bdraw);
return wrap(bdraw);
}
// [[Rcpp::export]]
NumericVector KF_fast(NumericMatrix y, NumericMatrix Z,NumericMatrix Ht, NumericMatrix Qtt,int m, int p, int t, NumericVector B0, NumericMatrix V0) {
static Rcpp::Function asVector("as.vector");
//Define everything calculated down there
arma::vec bp = Rcpp::as<arma::vec>(B0);
arma::mat Vp = Rcpp::as<arma::mat>(V0);
arma::mat Qt =Rcpp::as<arma::mat>(Qtt);
arma::mat bt(t,m);
arma::mat Vt(pow(m,2),t);
arma::mat R(p,m);
arma::mat H(t*m,p);
arma::mat cfe(p,1);
arma::mat yt(p,1);
arma::mat f(p,p);
arma::mat inv_f(p,p);
arma::mat btt(m,1);
arma::mat Vtt(m,m);
arma::cube test(m,m,t);
for (int i=1;i<(t+1);++i){
arma::mat Qt =diag(Qtt(i-1,_));
//int i=t;
R= Ht(Range((i-1)*p,(i*p)-1),_);
H = Z(Range((i-1)*p,(i*p)-1),_);
yt=y(_,i-1);
cfe= yt-H*bp;
f = H*Vp*trans(H)+R;
inv_f = inv(f);
btt = bp+Vp*trans(H)*inv_f*cfe; // bp=btt-1
//btt = bp+Vp*inv_f*cfe;
Vtt = Vp-Vp*trans(H)*inv_f*H*Vp;
//Vtt = Vp-Vp*trans(H)*inv_f*H*Vp;
if ((i-1)<t){
bp=btt;
Vp=Vtt+Qt;
}
bt.row(i-1) = trans(btt);
test.slice(i-1)=Vtt;
}
//draw S(T|T) ~ N(S(T|T),P(T|T))
arma::mat bdraw(t,m);
arma::mat Y = arma::randn(m, 1);
arma::mat bmean(m,1);
arma::mat bvar(m,m);
bdraw.row(t-1)=trans(btt+ trans(arma::chol(Vtt))*Y);
//backward recurssions
arma::mat bf(1,m);
for (int i=1;i<t;++i){
arma::mat Qt =diag(Qtt(t-i,_));
bf=trans(bdraw.row(t-i));
btt=trans(bt.row(t-i-1));
Vtt=test.slice(t-i-1);
f=Vtt+Qt;
inv_f=inv(f);
cfe=bf-btt;
bmean=btt+Vtt*inv_f*cfe;
//bmean=btt+inv_f*cfe;
bvar=Vtt-Vtt*inv_f*Vtt;
//bvar=Vtt-inv_f*Vtt;
arma::mat Y = arma::randn(m, 1);
bdraw.row(t-i-1)=trans(bmean+trans(arma::chol(bvar))*Y);
}
bdraw=trans(bdraw);
return wrap(bdraw);
}