forked from helske/bssm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_psi.cpp
More file actions
222 lines (188 loc) · 7.87 KB
/
Copy pathR_psi.cpp
File metadata and controls
222 lines (188 loc) · 7.87 KB
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
#include "model_ssm_mlg.h"
#include "model_ssm_ulg.h"
#include "model_bsm_lg.h"
#include "model_ar1_lg.h"
#include "model_ssm_mng.h"
#include "model_ssm_ung.h"
#include "model_bsm_ng.h"
#include "model_svm.h"
#include "model_ar1_ng.h"
#include "model_ssm_nlg.h"
#include "distr_consts.h"
#include "filter_smoother.h"
#include "summary.h"
// [[Rcpp::export]]
arma::cube gaussian_psi_smoother(const Rcpp::List model_,
const unsigned int nsim, const unsigned int seed,
const int model_type) {
switch (model_type) {
case 0: {
ssm_mlg model(model_, seed);
arma::cube alpha(model.m, model.n + 1, nsim, arma::fill::zeros);
model.psi_filter(nsim, alpha);
return alpha;
} break;
case 1: {
ssm_ulg model(model_, seed);
arma::cube alpha(model.m, model.n + 1, nsim, arma::fill::zeros);
model.psi_filter(nsim, alpha);
return alpha;
} break;
case 2: {
bsm_lg model(model_, seed);
arma::cube alpha(model.m, model.n + 1, nsim, arma::fill::zeros);
model.psi_filter(nsim, alpha);
return alpha;
} break;
case 3: {
ar1_lg model(model_, seed);
arma::cube alpha(model.m, model.n + 1, nsim, arma::fill::zeros);
model.psi_filter(nsim, alpha);
return alpha;
} break;
default:
return arma::cube(0,0,0);
break;
}
}
// [[Rcpp::export]]
Rcpp::List psi_smoother(const Rcpp::List model_,
const unsigned int nsim, const unsigned int seed,
const int model_type) {
switch (model_type) {
case 0: {
ssm_mng model(model_, seed);
arma::cube alpha(model.m, model.n + 1, nsim, arma::fill::zeros);
arma::mat weights(nsim, model.n + 1, arma::fill::zeros);
arma::umat indices(nsim, model.n, arma::fill::zeros);
double loglik = model.psi_filter(nsim, alpha, weights, indices);
if (!std::isfinite(loglik))
Rcpp::warning("Particle filtering stopped prematurely due to nonfinite log-likelihood.");
arma::mat alphahat(model.m, model.n + 1);
arma::cube Vt(model.m, model.m, model.n + 1);
filter_smoother(alpha, indices);
summary(alpha, alphahat, Vt); // weights are uniform due to extra time point
arma::inplace_trans(alphahat);
return Rcpp::List::create(
Rcpp::Named("alphahat") = alphahat, Rcpp::Named("Vt") = Vt,
Rcpp::Named("weights") = weights,
Rcpp::Named("logLik") = loglik, Rcpp::Named("alpha") = alpha);
} break;
case 1: {
ssm_ung model(model_, seed);
arma::cube alpha(model.m, model.n + 1, nsim, arma::fill::zeros);
arma::mat weights(nsim, model.n + 1, arma::fill::zeros);
arma::umat indices(nsim, model.n, arma::fill::zeros);
double loglik = model.psi_filter(nsim, alpha, weights, indices);
if (!std::isfinite(loglik))
Rcpp::warning("Particle filtering stopped prematurely due to nonfinite log-likelihood.");
arma::mat alphahat(model.m, model.n + 1);
arma::cube Vt(model.m, model.m, model.n + 1);
filter_smoother(alpha, indices);
summary(alpha, alphahat, Vt); // weights are uniform due to extra time point
arma::inplace_trans(alphahat);
return Rcpp::List::create(
Rcpp::Named("alphahat") = alphahat, Rcpp::Named("Vt") = Vt,
Rcpp::Named("weights") = weights,
Rcpp::Named("logLik") = loglik, Rcpp::Named("alpha") = alpha);
} break;
case 2: {
bsm_ng model(model_, seed);
arma::cube alpha(model.m, model.n + 1, nsim, arma::fill::zeros);
arma::mat weights(nsim, model.n + 1, arma::fill::zeros);
arma::umat indices(nsim, model.n, arma::fill::zeros);
double loglik = model.psi_filter(nsim, alpha, weights, indices);
if (!std::isfinite(loglik))
Rcpp::warning("Particle filtering stopped prematurely due to nonfinite log-likelihood.");
arma::mat alphahat(model.m, model.n + 1);
arma::cube Vt(model.m, model.m, model.n + 1);
filter_smoother(alpha, indices);
summary(alpha, alphahat, Vt); // weights are uniform due to extra time point
arma::inplace_trans(alphahat);
return Rcpp::List::create(
Rcpp::Named("alphahat") = alphahat, Rcpp::Named("Vt") = Vt,
Rcpp::Named("weights") = weights,
Rcpp::Named("logLik") = loglik, Rcpp::Named("alpha") = alpha);
} break;
case 3: {
svm model(model_, seed);
arma::cube alpha(model.m, model.n + 1, nsim, arma::fill::zeros);
arma::mat weights(nsim, model.n + 1, arma::fill::zeros);
arma::umat indices(nsim, model.n, arma::fill::zeros);
double loglik = model.psi_filter(nsim, alpha, weights, indices);
if (!std::isfinite(loglik))
Rcpp::warning("Particle filtering stopped prematurely due to nonfinite log-likelihood.");
arma::mat alphahat(model.m, model.n + 1);
arma::cube Vt(model.m, model.m, model.n + 1);
filter_smoother(alpha, indices);
summary(alpha, alphahat, Vt); // weights are uniform due to extra time point
arma::inplace_trans(alphahat);
return Rcpp::List::create(
Rcpp::Named("alphahat") = alphahat, Rcpp::Named("Vt") = Vt,
Rcpp::Named("weights") = weights,
Rcpp::Named("logLik") = loglik, Rcpp::Named("alpha") = alpha);
} break;
case 4: {
ar1_ng model(model_, seed);
arma::cube alpha(model.m, model.n + 1, nsim, arma::fill::zeros);
arma::mat weights(nsim, model.n + 1, arma::fill::zeros);
arma::umat indices(nsim, model.n, arma::fill::zeros);
double loglik = model.psi_filter(nsim, alpha, weights, indices);
if (!std::isfinite(loglik))
Rcpp::warning("Particle filtering stopped prematurely due to nonfinite log-likelihood.");
arma::mat alphahat(model.m, model.n + 1);
arma::cube Vt(model.m, model.m, model.n + 1);
filter_smoother(alpha, indices);
summary(alpha, alphahat, Vt); // weights are uniform due to extra time point
arma::inplace_trans(alphahat);
return Rcpp::List::create(
Rcpp::Named("alphahat") = alphahat, Rcpp::Named("Vt") = Vt,
Rcpp::Named("weights") = weights,
Rcpp::Named("logLik") = loglik, Rcpp::Named("alpha") = alpha);
} break;
}
return Rcpp::List::create(Rcpp::Named("error") = 0);
}
// [[Rcpp::export]]
Rcpp::List psi_smoother_nlg(const arma::mat& y, SEXP Z, SEXP H,
SEXP T, SEXP R, SEXP Zg, SEXP Tg, SEXP a1, SEXP P1,
const arma::vec& theta, SEXP log_prior_pdf, const arma::vec& known_params,
const arma::mat& known_tv_params, const unsigned int n_states,
const unsigned int n_etas, const arma::uvec& time_varying,
const unsigned int nsim,
const unsigned int seed, const unsigned int max_iter,
const double conv_tol, const unsigned int iekf_iter) {
Rcpp::XPtr<nvec_fnPtr> xpfun_Z(Z);
Rcpp::XPtr<nmat_fnPtr> xpfun_H(H);
Rcpp::XPtr<nvec_fnPtr> xpfun_T(T);
Rcpp::XPtr<nmat_fnPtr> xpfun_R(R);
Rcpp::XPtr<nmat_fnPtr> xpfun_Zg(Zg);
Rcpp::XPtr<nmat_fnPtr> xpfun_Tg(Tg);
Rcpp::XPtr<a1_fnPtr> xpfun_a1(a1);
Rcpp::XPtr<P1_fnPtr> xpfun_P1(P1);
Rcpp::XPtr<prior_fnPtr> xpfun_prior(log_prior_pdf);
ssm_nlg model(y, *xpfun_Z, *xpfun_H, *xpfun_T, *xpfun_R, *xpfun_Zg, *xpfun_Tg,
*xpfun_a1, *xpfun_P1, theta, *xpfun_prior, known_params, known_tv_params, n_states, n_etas,
time_varying, seed, iekf_iter, max_iter, conv_tol);
unsigned int m = model.m;
unsigned n = model.n;
model.approximate();
if(!arma::is_finite(model.mode_estimate)) {
Rcpp::warning("Approximation did not converge. ");
}
arma::cube alpha(m, n + 1, nsim, arma::fill::zeros);
arma::mat weights(nsim, n + 1, arma::fill::zeros);
arma::umat indices(nsim, n, arma::fill::zeros);
double loglik = model.psi_filter(nsim, alpha, weights, indices);
if (!std::isfinite(loglik))
Rcpp::warning("Particle filtering stopped prematurely due to nonfinite log-likelihood.");
arma::mat alphahat(model.m, model.n + 1);
arma::cube Vt(model.m, model.m, model.n + 1);
filter_smoother(alpha, indices);
summary(alpha, alphahat, Vt); // weights are uniform due to extra time point
arma::inplace_trans(alphahat);
return Rcpp::List::create(
Rcpp::Named("alphahat") = alphahat, Rcpp::Named("Vt") = Vt,
Rcpp::Named("weights") = weights,
Rcpp::Named("logLik") = loglik, Rcpp::Named("alpha") = alpha);
}