forked from helske/bssm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_ar1_ng.cpp
More file actions
101 lines (86 loc) · 2.87 KB
/
Copy pathmodel_ar1_ng.cpp
File metadata and controls
101 lines (86 loc) · 2.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
#include "model_ar1_ng.h"
// from Rcpp::List
ar1_ng::ar1_ng(const Rcpp::List model, const unsigned int seed) :
ssm_ung(model, seed),
prior_distributions(Rcpp::as<arma::uvec>(model["prior_distributions"])),
prior_parameters(Rcpp::as<arma::mat>(model["prior_parameters"])),
mu_est(Rcpp::as<bool>(model["mu_est"])),
phi_est(Rcpp::as<bool>(model["phi_est"])) {
}
void ar1_ng::update_model(const arma::vec& new_theta) {
T(0, 0, 0) = new_theta(0);
R(0, 0, 0) = new_theta(1);
if (mu_est) {
a1(0) = new_theta(2);
C.fill(new_theta(2) * (1.0 - new_theta(0)));
}
P1(0, 0) = std::pow(new_theta(1), 2) / (1.0 - std::pow(new_theta(0), 2));
compute_RR();
if(phi_est) {
phi = new_theta(2 + mu_est);
}
if(xreg.n_cols > 0) {
beta = new_theta.subvec(new_theta.n_elem - xreg.n_cols, new_theta.n_elem - 1);
compute_xbeta();
}
theta = new_theta;
// approximation does not match theta anymore (keep as -1 if so)
if (approx_state > 0) approx_state = 0;
}
void ar1_ng::update_model(const arma::vec& new_theta, const Rcpp::Function update_fn) {
T(0, 0, 0) = new_theta(0);
R(0, 0, 0) = new_theta(1);
if (mu_est) {
a1(0) = new_theta(2);
C.fill(new_theta(2) * (1.0 - new_theta(0)));
}
P1(0, 0) = std::pow(new_theta(1), 2) / (1.0 - std::pow(new_theta(0), 2));
compute_RR();
if(phi_est) {
phi = new_theta(2 + mu_est);
}
if(xreg.n_cols > 0) {
beta = new_theta.subvec(new_theta.n_elem - xreg.n_cols, new_theta.n_elem - 1);
compute_xbeta();
}
theta = new_theta;
// approximation does not match theta anymore (keep as -1 if so)
if (approx_state > 0) approx_state = 0;
}
double ar1_ng::log_prior_pdf(const arma::vec& x, const Rcpp::Function prior_fn) const {
double log_prior = 0.0;
for(unsigned int i = 0; i < x.n_elem; i++) {
switch(prior_distributions(i)) {
case 0 :
if (x(i) < prior_parameters(0, i) || x(i) > prior_parameters(1, i)) {
return -std::numeric_limits<double>::infinity();
}
break;
case 1 :
if (x(i) < 0) {
return -std::numeric_limits<double>::infinity();
} else {
log_prior -= 0.5 * std::pow(x(i) / prior_parameters(0, i), 2);
}
break;
case 2 :
log_prior -= 0.5 * std::pow((x(i) - prior_parameters(0, i)) / prior_parameters(1, i), 2);
break;
case 3 : // truncated normal
if (x(i) < prior_parameters(2, i) || x(i) > prior_parameters(3, i)) {
return -std::numeric_limits<double>::infinity();
} else {
log_prior -= 0.5 * std::pow((x(i) - prior_parameters(0, i)) / prior_parameters(1, i), 2);
}
break;
case 4 : // gamma
if (x(i) < 0) {
return -std::numeric_limits<double>::infinity();
} else {
log_prior += (prior_parameters(0, i) - 1) * log(x(i)) - prior_parameters(1, i) * x(i);
}
break;
}
}
return log_prior;
}