forked from helske/bssm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_ekpf.cpp
More file actions
100 lines (79 loc) · 3.49 KB
/
Copy pathR_ekpf.cpp
File metadata and controls
100 lines (79 loc) · 3.49 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
#include "model_ssm_nlg.h"
#include "filter_smoother.h"
#include "summary.h"
// [[Rcpp::export]]
Rcpp::List ekpf(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) {
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);
unsigned int m = model.m;
unsigned n = model.n;
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.ekf_filter(nsim, alpha, weights, indices);
if (!std::isfinite(loglik))
Rcpp::warning("Particle filtering stopped prematurely due to nonfinite log-likelihood.");
arma::mat at(m, n + 1);
arma::mat att(m, n + 1);
arma::cube Pt(m, m, n + 1);
arma::cube Ptt(m, m, n + 1);
filter_summary(alpha, at, att, Pt, Ptt, weights);
arma::inplace_trans(att);
return Rcpp::List::create(
Rcpp::Named("att") = att,
Rcpp::Named("Ptt") = Ptt,
Rcpp::Named("weights") = weights,
Rcpp::Named("logLik") = loglik, Rcpp::Named("alpha") = alpha);
}
// [[Rcpp::export]]
Rcpp::List ekpf_smoother(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) {
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);
unsigned int m = model.m;
unsigned n = model.n;
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.ekf_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);
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);
}