forked from helske/bssm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_importance_sample.cpp
More file actions
77 lines (70 loc) · 2.58 KB
/
Copy pathR_importance_sample.cpp
File metadata and controls
77 lines (70 loc) · 2.58 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
#include "model_ssm_ulg.h"
#include "model_ssm_ung.h"
#include "model_ssm_mng.h"
#include "model_bsm_ng.h"
#include "model_svm.h"
#include "model_ar1_ng.h"
// [[Rcpp::export]]
Rcpp::List importance_sample_ng(const Rcpp::List model_,
unsigned int nsim, bool use_antithetic, const unsigned int seed,
const int model_type) {
switch (model_type) {
case 0: {
ssm_mng model(model_, seed);
model.approximate();
arma::cube alpha = model.approx_model.simulate_states(nsim);
model.update_scales();
arma::vec weights = model.importance_weights(alpha) - arma::accu(model.scales);
double maxw = weights.max();
weights = arma::exp(weights - maxw);
return Rcpp::List::create(Rcpp::Named("alpha") = alpha,
Rcpp::Named("weights") = weights);
} break;
case 1: {
ssm_ung model(model_, seed);
model.approximate();
arma::cube alpha = model.approx_model.simulate_states(nsim);
model.update_scales();
arma::vec weights = model.importance_weights(alpha) - arma::accu(model.scales);
double maxw = weights.max();
weights = arma::exp(weights - maxw);
return Rcpp::List::create(Rcpp::Named("alpha") = alpha,
Rcpp::Named("weights") = weights);
} break;
case 2: {
bsm_ng model(model_, seed);
model.approximate();
arma::cube alpha = model.approx_model.simulate_states(nsim);
model.update_scales();
arma::vec weights = model.importance_weights(alpha) - arma::accu(model.scales);
double maxw = weights.max();
weights = arma::exp(weights - maxw);
return Rcpp::List::create(Rcpp::Named("alpha") = alpha,
Rcpp::Named("weights") = weights);
} break;
case 3: {
svm model(model_, seed);
model.approximate();
arma::cube alpha = model.approx_model.simulate_states(nsim);
model.update_scales();
arma::vec weights = model.importance_weights(alpha) - arma::accu(model.scales);
double maxw = weights.max();
weights = arma::exp(weights - maxw);
return Rcpp::List::create(Rcpp::Named("alpha") = alpha,
Rcpp::Named("weights") = weights);
} break;
case 4: {
ar1_ng model(model_, seed);
model.approximate();
arma::cube alpha = model.approx_model.simulate_states(nsim);
model.update_scales();
arma::vec weights = model.importance_weights(alpha) - arma::accu(model.scales);
double maxw = weights.max();
weights = arma::exp(weights - maxw);
return Rcpp::List::create(Rcpp::Named("alpha") = alpha,
Rcpp::Named("weights") = weights);
} break;
default:
return Rcpp::List::create(Rcpp::Named("alpha") = 0, Rcpp::Named("weights") = 0);
}
}