forked from helske/bssm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_svm.cpp
More file actions
89 lines (76 loc) · 2.56 KB
/
Copy pathmodel_svm.cpp
File metadata and controls
89 lines (76 loc) · 2.56 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
#include "model_svm.h"
// construct SV model from Rcpp::List
svm::svm(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"])),
svm_type(model["svm_type"]) {
}
// update model given the parameters theta
void svm::update_model(const arma::vec& new_theta) {
if(svm_type == 0) {
phi = new_theta(2);
} else {
a1(0) = new_theta(2);
C.fill(new_theta(2) * (1.0 - new_theta(0)));
}
T(0, 0, 0) = new_theta(0);
R(0, 0, 0) = new_theta(1);
compute_RR();
P1(0, 0) = new_theta(1) * new_theta(1) / (1 - new_theta(0) * new_theta(0));
theta = new_theta;
// approximation does not match theta anymore (keep as -1 if so)
if (approx_state > 0) approx_state = 0;
}
void svm::update_model(const arma::vec& new_theta, const Rcpp::Function update_fn) {
if(svm_type == 0) {
phi = new_theta(2);
} else {
a1(0) = new_theta(2);
C.fill(new_theta(2) * (1.0 - new_theta(0)));
}
T(0, 0, 0) = new_theta(0);
R(0, 0, 0) = new_theta(1);
compute_RR();
P1(0, 0) = new_theta(1) * new_theta(1) / (1 - new_theta(0) * new_theta(0));
theta = new_theta;
// approximation does not match theta anymore (keep as -1 if so)
if (approx_state > 0) approx_state = 0;
}
double svm::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;
}