-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkalman.cpp
174 lines (121 loc) · 5.12 KB
/
kalman.cpp
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
#include <iostream>
#include <nlopt.h>
#include "kalman.h"
#include "armadillo"
using namespace arma;
using namespace std;
using arma::colvec;
using arma::log;
using arma::normcdf;
using std::exp;
using std::log;
using std::pow;
using std::sqrt;
#define PII 3.1415926
// Start Likelihood -------------
double lik(unsigned n, const double* theta, double *grad, void *my_func_data) {
colvec stock;
stock.load("./stock.csv", csv_ascii);
colvec market;
market.load("./market.csv", csv_ascii);
arma::vec y = stock;
mat X(y.n_elem, 1, fill::ones);
X.insert_cols(1, market);
double alpha0 = theta[0];
double alpha1 = theta[1];
double alpha2 = theta[2];
int num_observations = y.n_elem;
int num_variables = X.n_cols;
arma::mat F;
F.eye(num_variables, num_variables);
double R = pow(alpha0, 2);
// Q is a kxk matrix of parameters we must estimate: I have made it diagonal.
// use the below when we improve
// arma::mat Q = arma::mat(num_variables, num_variables, fill::zeros);
// with the translated R diag(Q) <- c(theta[2]^2, theta[3]^2)
// TODO: need to adjust this manually at this point
arma::mat Q;
Q << alpha1 << 0 << arma::endr
<< 0 << alpha2 << arma::endr;
//pick up kalman filter here with betatt
arma::mat betatt = arma::mat(num_observations, num_variables, arma::fill::zeros);
arma::mat betatt_1 = arma::mat(num_observations, num_variables, arma::fill::zeros);
arma::vec eta(num_observations, arma::fill::zeros);
arma::vec f(num_observations, arma::fill::zeros);
arma::mat Ptt = arma::mat(num_observations, pow(num_variables, 2), arma::fill::zeros);
arma::mat Ptt_1 = arma::mat(num_observations, pow(num_variables, 2), arma::fill::zeros);
// # need to set initial values for beta and p
// # make Ptt based on some estimate
arma::vec first_row_betatt = (arma::inv(X.t() * X) * X.t()) * y;
arma::vec first_row_Ptt = arma::vec(pow(num_variables, 2));
first_row_Ptt.fill(.3);
betatt.row(0) = first_row_betatt.t();
Ptt.row(0) = first_row_Ptt.t();
for (int i = 1; i < num_observations; i++){
///Prediction
betatt_1.row(i) = (F * betatt.row(i - 1).t()).t();
//ok so the next line converts (on the fly) the ith row of Ptt to a matrix. Would be better to use an array...
arma::mat tempPtt(2, 2);
tempPtt << Ptt.row(i - 1)[0] << Ptt.row(i - 1)[2] << arma::endr
<< Ptt.row(i - 1)[1] << Ptt.row(i - 1)[3] << arma::endr;
Ptt_1.row(i) = arma::vectorise(F * tempPtt * F.t() + Q).t();
eta.at(i) = y.at(i) - dot(X.row(i), betatt_1.row(i));
//temp matrix
arma::mat tempPtt_1(2, 2);
tempPtt_1 << Ptt_1.row(i)[0] << Ptt_1.row(i)[2] << arma::endr
<< Ptt_1.row(i)[1] << Ptt_1.row(i)[3] << arma::endr;
f.at(i) = as_scalar(X.row(i) * tempPtt_1 * X.row(i).t()) + R;
/// Updating
//was this but i think it must be wrong, we want 2x2 matrix
// arma::vec as_vector_tempPtt_1(4);
// as_vector_tempPtt_1.at(0) = Ptt_1.row(i)[0];
// as_vector_tempPtt_1.at(1) = Ptt_1.row(i)[1];
// as_vector_tempPtt_1.at(2) = Ptt_1.row(i)[2];
// as_vector_tempPtt_1.at(3) = Ptt_1.row(i)[3];
arma::mat as_mat_tempPtt_1(2, 2);
as_mat_tempPtt_1 << Ptt_1.row(i)[0] << Ptt_1.row(i)[2] << arma::endr
<< Ptt_1.row(i)[1] << Ptt_1.row(i)[3] << arma::endr;
betatt.row(i) = betatt_1.row(i) + (((as_mat_tempPtt_1 * X.row(i).t()) * eta.at(i)) * (1 / f.at(i))).t();
Ptt.row(i) = arma::vectorise(as_mat_tempPtt_1 - as_mat_tempPtt_1 * X.row(i).t() * X.row(i) * as_mat_tempPtt_1 * (1 / f.at(i))).t();
}
//the first element of f is 0, so change to 1---log(1) is 0 and 1/1 is 1, so OK
f.at(0) = 1;
double logl = -0.5 * arma::accu(log(abs(f))) - 0.5 * arma::accu(eta % eta * (1 / f).t());
return -1 * logl;
}
int main()
{
//import market and stock--------
// colvec stock;
// stock.load("./stock.csv", csv_ascii);
// colvec market;
// market.load("./market.csv", csv_ascii);
// colvec y_ = stock;
// colvec X_ = market;
double theta[3] = {0.1, 0.1, 0.1};
// cout << "neg log lik = " << lik(&theta0, &theta1, &theta2, stock, market) << endl;
//can use function minimization from the GSL here: https://www.gnu.org/software/gsl/doc/html/multimin.html#
//to implement it this answer may be useful: https://stackoverflow.com/questions/62264648/using-gsl-minimize-in-c
//BETTER: Use NLOpt
//minimization
// double lb[3] = {-HUGE_VAL, -HUGE_VAL, -HUGE_VAL};
nlopt_opt opt;
opt = nlopt_create(NLOPT_LN_NELDERMEAD, 3); /* algorithm and dimensionality */
// opt = nlopt_create(NLOPT_LD_MMA, 3);
// nlopt_set_lower_bounds(opt, lb);
nlopt_set_min_objective(opt, lik, NULL);
nlopt_set_xtol_rel(opt, 1e-4);
double minf; /* `*`the` `minimum` `objective` `value,` `upon` `return`*` */
if (nlopt_optimize(opt, theta, &minf) < 0) {
printf("nlopt failed!\n");
} else {
printf("found minimum at f(%g,%g,%g) = %0.10g\n", theta[0], theta[1],
theta[2], minf);
}
forward_pass(theta);
}
/*
Compile with:
clang++ -Wall kalman.cpp kalman_forward_pass.cpp -o kalman -I/home/matt/include
-lopenblas -llapack -lm -lnlopt
*/