-
Notifications
You must be signed in to change notification settings - Fork 0
/
GP.cpp
600 lines (581 loc) · 20.4 KB
/
GP.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#include "GP.h"
#include "util.h"
#include "def.h"
#include "MVMO.h"
#include <nlopt.hpp>
#include <Eigen/Dense>
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <iomanip>
#include <map>
#include <chrono>
using namespace std;
using namespace Eigen;
using namespace std::chrono;
// train_in :: dim * num_data
// train_out :: num_data * num_spec
GP::GP(const MatrixXd& train_in, const MatrixXd& train_out, CovFunc cf, MatrixDecomp md)
: _train_in(train_in),
_train_out(train_out),
_cf(cf),
_md(md),
_cov(_specify_cov(train_in.rows(), cf)),
_matrix_solver(_specify_matrix_solver(md)),
_num_train(train_in.cols()),
_noise_lb(1e-3),
_dim(train_in.rows()),
_num_hyp(_cov->num_hyp() + 2),
_hyps_lb(VectorXd(_num_hyp)),
_hyps_ub(VectorXd(_num_hyp)),
_trained(false)
{
assert(_num_train == static_cast<size_t>(_train_out.rows()));
_set_hyp_range();
}
GP::~GP(){
delete _cov;
delete _matrix_solver;
}
void GP::add_data(const MatrixXd& x, const MatrixXd& y)
{
assert(static_cast<size_t>(x.rows()) == _dim);
assert(x.cols() == y.rows());
const size_t num_added = x.cols();
_train_in.conservativeResize(Eigen::NoChange, _num_train + num_added);
_train_out.conservativeResize(_num_train + num_added, Eigen::NoChange);
_train_in.middleCols(_num_train, num_added) = x;
_train_out.middleRows(_num_train, num_added) = y;
_num_train += num_added;
_trained = false;
}
size_t GP::dim() const noexcept { return _dim; }
size_t GP::num_hyp() const noexcept { return _num_hyp; }
size_t GP::num_train() const noexcept { return _num_train; }
bool GP::trained() const noexcept { return _trained; }
const MatrixXd& GP::train_in() const noexcept { return _train_in; }
const VectorXd& GP::train_out() const noexcept { return _train_out; }
void GP::set_fixed(bool f) {_fixhyps = f;}
void GP::set_noise_lower_bound(double nlb) noexcept
{
if(nlb < 0)
cerr << "Can't set noise lower bound: lower bound must be positive" << endl;
else
{
_noise_lb = nlb;
if(_noise_free)
cerr << "Noise-free GP, ignore the noise lower bound" << endl;
else if(_noise_lb == 0)
{
_noise_lb += numeric_limits<double>::epsilon();
cerr << "Noise level can't be zero, reset it to " << _noise_lb << ", if you want zero noise, you can use set_noise_free() function" << endl;
}
}
}
void GP::set_noise_free(bool flag)
{
_noise_free = flag;
if(_noise_free)
_noise_lb = 0;
}
VectorXd GP::get_default_hyps() const noexcept
{
VectorXd hyp(_num_hyp);
hyp.head(_cov->num_hyp()) = _cov->default_hyp(_train_in, _train_out);
hyp(hyp.size()-2) = noise_free() ? -1 * INF : max(log(_noise_lb), log(stddev<VectorXd>(_train_out) * 1e-3));
hyp(hyp.size()-1) = _train_out.mean();
return hyp;
}
void GP::_init()
{
// _calcUtilGradM();
_set_hyp_range();
_trained = false;
}
double GP::_calcNegLogProb(const std::vector<double>& x, std::vector<double>& g) const
{
// for nlopt optimization
assert(x.size() == _noise_free ? _num_hyp - 1 : _num_hyp);
const bool need_g = !g.empty();
const VectorXd eig_x = vec2hyp(x);
VectorXd eig_g;
double nlz = _calcNegLogProb(eig_x, eig_g, need_g);
if(need_g)
g = hyp2vec(eig_g);
return nlz;
}
double GP::_calcNegLogProb(const Eigen::VectorXd& hyp) const
{
VectorXd dummy_g;
return _calcNegLogProb(hyp, dummy_g, false);
}
double GP::_calcNegLogProb(const Eigen::VectorXd& hyp, Eigen::VectorXd& grad) const
{
return _calcNegLogProb(hyp, grad, true);
}
double GP::_calcNegLogProb(const VectorXd& hyp, VectorXd& g, bool calc_grad) const
{
assert(_num_hyp == _cov->num_hyp() + 2);
if(_noise_free)
assert(0 == _hyp_sn2(hyp));
const double sn2 = _hyp_sn2(hyp);
const double mean = _hyp_mean(hyp);
MatrixXd K = _cov->k(hyp.head(_cov->num_hyp()), _train_in, _train_in);
_matrix_solver->decomp(K+sn2*MatrixXd::Identity(_num_train, _num_train));
// ColPivHouseholderQR<MatrixXd> K_solver = (K+sn2*MatrixXd::Identity(_num_train, _num_train)).colPivHouseholderQr();
double negLogProb = INF;
if(calc_grad)
g = VectorXd::Constant(_num_hyp, 1, INF);
// if(K_solver.info() == Eigen::Success and K_solver.isInvertible())
if(_matrix_solver->check_SPD())
{
const VectorXd train_y = _train_out.array() - mean;
VectorXd alpha = _matrix_solver->solve(train_y);
const double data_fit_term = 0.5 * train_y.dot(alpha);
const double model_complexity = 0.5 * _matrix_solver->log_det();
const double norm_const = 0.5 * _num_train * log(2 * M_PI);
negLogProb = data_fit_term + model_complexity + norm_const;
#ifdef MYDEBUG
cout << "NegLogProb: " << negLogProb << ", Data fit: " << data_fit_term << ", model_complexity: " << model_complexity << ", calc_grad: " << calc_grad << endl;
#endif
if(not isfinite(negLogProb))
negLogProb = INF; // no NaN allowed
else
{
if(calc_grad)
{
g = VectorXd::Constant(_num_hyp, INF);
MatrixXd Q = _matrix_solver->inverse() - alpha * alpha.transpose();
for(size_t i = 0; i < _cov->num_hyp(); ++i)
{
// if A = A' and B = B' then trace(A * B) == sum(sum(A.*B))
// g(i) = 0.5 * exp(-2 * hyp(i)) * QK.cwiseProduct(utilGradMatrix.middleCols(_num_train * i, _num_train)).sum(); // log length scale
MatrixXd dK = _cov->dk_dhyp(hyp.head(_cov->num_hyp()), i, _train_in, _train_in, K);
g(i) = 0.5 * (Q.cwiseProduct(dK)).sum();
}
g(_num_hyp-2) = sn2 * Q.trace(); // log sn
g(_num_hyp-1) = -1*alpha.sum(); // mean
if(! g.allFinite())
{
#ifdef MYDEBUG
cerr << "Gradient is not finite: " << g.transpose() << endl;
#endif
negLogProb = INF;
g = VectorXd::Constant(_num_hyp, INF);
}
}
}
}
return negLogProb;
}
double GP::train()
{
double prob = train(get_default_hyps());
assert(_trained);
return prob;
}
double GP::train(const VectorXd& init_hyps)
{
_init();
const auto f = [](const vector<double>& x, vector<double>& g, void* data) -> double {
GP* gp = reinterpret_cast<GP*>(data);
if(gp->vec2hyp(x).array().hasNaN())
throw nlopt::forced_stop();
return gp->_calcNegLogProb(x, g);
};
_hyps = init_hyps;
if(_noise_free)
_hyps(_hyps.size()-2) = -1 * INF;
double nlz = _calcNegLogProb(_hyps);
if(not isfinite(nlz))
_hyps = select_init_hyp(_num_hyp * 50, _hyps);
if(_fixhyps)
{
_setK();
_trained = true;
nlz = _calcNegLogProb(_hyps);
return nlz;
}
vector<double> hyp_lb = hyp2vec(_hyps_lb);
vector<double> hyp_ub = hyp2vec(_hyps_ub);
vector<double> hyp0 = hyp2vec(_hyps);
for(size_t i = 0; i < hyp0.size(); ++i)
{
hyp0[i] = std::max(hyp0[i], hyp_lb[i]);
hyp0[i] = std::min(hyp0[i], hyp_ub[i]);
}
// make sure the starting point is valid
vector<double> fake_g(hyp0.size(), 0);
nlz = f(hyp0, fake_g, this);
VectorXd dummy_g = vec2hyp(fake_g);
if(_noise_free)
dummy_g[_num_hyp-2] = 0;
#ifdef MYDEBUG
cout << "Starting nlz: " << nlz << endl;
_check_hyp_range(vec2hyp(hyp0));
double rel_err = _likelihood_gradient_checking(vec2hyp(hyp0), dummy_g);
cout << "Relative error of gradient: " << rel_err << endl;
#endif
nlopt::algorithm algo = nlopt::LD_SLSQP;
size_t max_eval = 160;
nlopt::opt optimizer(algo, hyp0.size());
optimizer.set_maxeval(max_eval);
optimizer.set_min_objective(f, this);
optimizer.set_lower_bounds(hyp_lb);
optimizer.set_upper_bounds(hyp_ub);
// optimizer.set_ftol_abs(1e-6);
// if(not _noise_free)
// {
// optimizer.add_inequality_constraint( [](const vector<double>& hyp, vector<double>&, void*) -> double {
// const size_t dim = hyp.size() - 3;
// const double sf = hyp[dim];
// const double sn = hyp[dim + 1];
// return sn - sf; // variance should be greater than noise
// }, nullptr);
// }
try
{
#ifdef MYDEBUG
auto t1 = chrono::high_resolution_clock::now();
optimizer.optimize(hyp0, nlz);
auto t2 = chrono::high_resolution_clock::now();
cout << "Training time: " << duration_cast<chrono::seconds>(t2-t1).count() << " seconds" << endl;
#else
optimizer.optimize(hyp0, nlz);
#endif
}
catch(std::runtime_error& e)
{
#ifdef MYDEBUG
cerr << "Nlopt exception for GP training caught: " << e.what() << ", algorithm: " << optimizer.get_algorithm_name() << endl;
#endif
}
_hyps = vec2hyp(hyp0);
_setK();
_trained = true;
nlz = _calcNegLogProb(_hyps);
return nlz;
}
void GP::_predict(const MatrixXd& x, bool need_g, VectorXd& y, VectorXd& s2, MatrixXd& gy, MatrixXd& gs2) const noexcept
{
assert(_trained);
const size_t num_test = x.cols();
const VectorXd sf2 = _cov->diag_k(_hyps, x);
const double sn2 = _hyp_sn2(_hyps);
const double mean = _hyp_mean(_hyps);
const MatrixXd k_test = _cov->k(_hyps.head(_cov->num_hyp()), x, _train_in);
const MatrixXd kks = _matrix_solver->solve(k_test.transpose());
y = VectorXd::Constant(num_test, 1, mean) + k_test * _invKys;
s2 = (sf2 - k_test.cwiseProduct(kks.transpose()).rowwise().sum()).cwiseMax(0) + VectorXd::Constant(num_test, 1, sn2);
if(need_g)
{
gy = MatrixXd::Zero(_dim, num_test);
gs2 = MatrixXd::Zero(_dim, num_test);
MatrixXd dk_diag = _cov->diag_dk_dx1(_hyps, x);
for(size_t i = 0; i < num_test; ++i)
{
// XXX: assume k(x, x) = sigma_f^2
const MatrixXd grad_ktest = _cov->dk_dx1(_hyps, x.col(i), _train_in);
gy.col(i) = grad_ktest * _invKys;
gs2.col(i) = dk_diag.col(i) -2 * grad_ktest * kks.col(i);
}
}
}
void GP::_predict_y(const Eigen::MatrixXd& x, bool need_g, Eigen::VectorXd& y, Eigen::MatrixXd& gy) const noexcept
{
assert(_trained);
const size_t num_test = x.cols();
const double mean = _hyp_mean(_hyps);
const MatrixXd k_test = _cov->k(_hyps.head(_cov->num_hyp()), x, _train_in); // num_test * num_train;
y = VectorXd::Constant(num_test, 1, mean) + k_test * _invKys;
if(need_g)
{
gy = MatrixXd::Zero(_dim, num_test);
for(size_t i = 0; i < num_test; ++i)
{
const MatrixXd grad_ktest = _cov->dk_dx1(_hyps, x.col(i), _train_in);
gy.col(i) = grad_ktest * _invKys;
}
}
}
void GP::_predict_s2(const MatrixXd& x, bool need_g, VectorXd& s2, MatrixXd& gs2) const noexcept
{
assert(_trained);
const size_t num_test = x.cols();
const VectorXd sf2 = _cov->diag_k(_hyps, x);
const double sn2 = _hyp_sn2(_hyps);
const MatrixXd k_test = _cov->k(_hyps.head(_cov->num_hyp()), x, _train_in); // num_test * num_train;
const MatrixXd kks = _matrix_solver->solve(k_test.transpose());
s2 = (sf2 - k_test.cwiseProduct(kks.transpose()).rowwise().sum()).cwiseMax(0) + VectorXd::Constant(num_test, 1, sn2);
if(need_g)
{
gs2 = MatrixXd::Zero(_dim, num_test);
const MatrixXd dk_diag = _cov->diag_dk_dx1(_hyps, x);
for(size_t i = 0; i < num_test; ++i)
{
const MatrixXd grad_ktest = _cov->dk_dx1(_hyps, x.col(i), _train_in);
gs2.col(i) = dk_diag.col(i) -2 * grad_ktest * kks.col(i);
}
}
}
double GP::predict_y(const VectorXd& x) const
{
VectorXd y; MatrixXd dummy_g;
_predict_y(x, false, y, dummy_g);
return y(0);
}
double GP::predict_s2(const VectorXd& x) const
{
VectorXd s2; MatrixXd dummy_g;
_predict_s2(x, false, s2, dummy_g);
return s2(0);
}
double GP::predict_y_with_grad(const VectorXd& xs, VectorXd& g) const
{
VectorXd y; MatrixXd gg;
_predict_y(xs, true, y, gg);
g = gg.col(0);
return y(0);
}
double GP::predict_s2_with_grad(const VectorXd& xs, VectorXd& g) const
{
VectorXd s2; MatrixXd gg;
_predict_s2(xs, true, s2, gg);
g = gg.col(0);
return s2(0);
}
pair<double, VectorXd> GP::predict_y_with_grad(const VectorXd& x) const
{
VectorXd y; MatrixXd gg;
_predict_y(x, true, y, gg);
return {y(0), gg.col(0)};
}
pair<double, VectorXd> GP::predict_s2_with_grad(const VectorXd& x) const
{
VectorXd s2; MatrixXd gg;
_predict_s2(x, true, s2, gg);
return {s2(0), gg.col(0)};
}
void GP::predict(const VectorXd& x, double& y, double& s2) const
{
VectorXd yy, ss2;
MatrixXd dummy_gy, dummy_gs2;
_predict(x, false, yy, ss2, dummy_gy, dummy_gs2);
y = yy(0);
s2 = ss2(0);
}
void GP::predict_with_grad(const VectorXd& xs, double& y, double& s2, VectorXd& gy, VectorXd& gs2) const
{
VectorXd yy, ss2;
MatrixXd dummy_gy, dummy_gs2;
_predict(xs, true, yy, ss2, dummy_gy, dummy_gs2);
y = yy(0);
s2 = ss2(0);
gy = dummy_gy.col(0);
gs2 = dummy_gs2.col(0);
}
std::tuple<double, double> GP::predict(const Eigen::VectorXd& xs) const
{
double y, s2;
predict(xs, y, s2);
return std::tuple<double, double>(y, s2);
}
std::tuple<double, double, VectorXd, VectorXd> GP::predict_with_grad(const VectorXd& xs) const
{
double y, s2;
VectorXd gy; VectorXd gs2;
predict_with_grad(xs, y, s2, gy, gs2);
return std::tuple<double, double, VectorXd, VectorXd>(y, s2, gy, gs2);
}
VectorXd GP::batch_predict_y(const MatrixXd& xs) const
{
VectorXd ys;
MatrixXd dummy_g;
_predict_y(xs, false, ys, dummy_g);
return ys;
}
VectorXd GP::batch_predict_s2(const MatrixXd& xs) const
{
VectorXd s2;
MatrixXd dummy_g;
_predict_s2(xs, false, s2, dummy_g);
return s2;
}
void GP::batch_predict(const MatrixXd& xs, VectorXd& y, VectorXd& s2) const
{
MatrixXd dummy_gy, dummy_gs2;
_predict(xs, false, y, s2, dummy_gy, dummy_gs2);
}
void GP::_setK()
{
_invKys = VectorXd::Zero(_num_train);
const MatrixXd EyeM = MatrixXd::Identity(_num_train, _num_train);
const MatrixXd Kcov = _cov->k(_hyps.head(_cov->num_hyp()), _train_in, _train_in);
double sn2 = _hyp_sn2(_hyps);
MatrixXd K = sn2 * EyeM + Kcov;
bool is_SPD = _check_SPD(K);
while(not is_SPD)
{
_hyps(_num_hyp-2) = std::isinf(_hyps(_num_hyp -2)) ? log(numeric_limits<double>::epsilon()) : _hyps(_num_hyp-2) + log(sqrt(10));
sn2 = _hyp_sn2(_hyps);
K = sn2 * EyeM + Kcov;
#ifdef MYDEBUG
cerr << "Add noise to " << sqrt(sn2) << endl;
#endif
is_SPD = _check_SPD(K);
}
_matrix_solver->decomp(K);
// _K_solver = K.colPivHouseholderQr();
_invKys = _matrix_solver->solve(static_cast<VectorXd>(_train_out.array() - _hyp_mean(_hyps)));
}
bool GP::_check_SPD(const MatrixXd& K) const
{
// XXX: don't check symmetry
MYASSERT(K.rows() == K.cols());
const size_t size = K.rows();
const MatrixXd Eye = MatrixXd::Identity(size, size);
const VectorXd eigenv = K.selfadjointView<Lower>().eigenvalues();
const double cond = abs(eigenv.maxCoeff() / eigenv.minCoeff());
_matrix_solver->decomp(K);
const double inv_err = (Eye - K * _matrix_solver->inverse()).cwiseAbs().mean();
const bool is_SPD = (inv_err < 1e-4) and (eigenv.array() >= 0).all() and _matrix_solver->check_SPD();
#ifdef MYDEBUG
if(not is_SPD)
cerr << "Inv_err: " << inv_err << ", cond: " << cond << ", DecompSuccess: " << _matrix_solver->check_SPD() << endl;
#endif
return is_SPD;
}
const VectorXd& GP::get_hyp() const noexcept { return _hyps; }
VectorXd GP::select_init_hyp(size_t max_eval, const Eigen::VectorXd& def_hyp)
{
MVMO::MVMO_Obj calc_nlz = [&](const VectorXd& x)->double{
VectorXd transform_x = vec2hyp(convert(x));
if(_noise_free)
transform_x(_num_hyp-2) = -1 * INF;
assert((size_t)transform_x.size() == _num_hyp);
const double sf2 = _cov->diag_k(transform_x, _train_in).mean();
double nlz = _hyp_sn2(transform_x) > sf2 ? INF : _calcNegLogProb(transform_x);
return nlz;
};
VectorXd lb = convert(hyp2vec(_hyps_lb));
VectorXd ub = convert(hyp2vec(_hyps_ub));
VectorXd initial_guess = convert(hyp2vec(def_hyp));
MVMO optimizer(calc_nlz, lb, ub);
optimizer.set_max_eval(max_eval);
optimizer.set_fs_init(0.5);
optimizer.set_fs_final(20);
optimizer.set_archive_size(25);
optimizer.optimize(initial_guess);
return std::isinf(optimizer.best_y()) ? def_hyp : vec2hyp(convert(optimizer.best_x()));
}
double GP::_likelihood_gradient_checking(const VectorXd& hyp, const VectorXd& grad)
{
const double epsi = 1e-3;
VectorXd check_grad(_num_hyp);
for(size_t i = 0; i < _num_hyp; ++i)
{
VectorXd check_hyp = hyp;
check_hyp[i] = hyp[i] + epsi;
double check_nlz1 = _calcNegLogProb(check_hyp);
check_hyp[i] = hyp[i] - epsi;
double check_nlz2 = _calcNegLogProb(check_hyp);
check_grad[i] = (check_nlz1 - check_nlz2) / (2 * epsi);
}
double rel_err = (grad - check_grad).norm() / grad.norm();
#ifdef MYDEBUG
MatrixXd check_matrix(3, _num_hyp);
check_matrix << hyp.transpose(), grad.transpose(), check_grad.transpose();
cout << "CheckGrad:\n" << check_matrix << endl;
cout << "Rel Difference: " << rel_err << endl;
#endif
return rel_err;
}
void GP::_check_hyp_range(const VectorXd& hyp) const noexcept
{
MatrixXd check_matrix(_num_hyp, 3);
check_matrix << _hyps_lb, _hyps_ub, hyp;
cout << "Hyp-range: \n" << check_matrix.transpose() << endl;
}
void GP::_set_hyp_range()
{
_hyps_lb = VectorXd::Constant(_num_hyp, -1 * INF);
_hyps_ub = VectorXd::Constant(_num_hyp, 0.5 * log(0.5 * numeric_limits<double>::max()));
pair<VectorXd, VectorXd> cov_range = _cov->cov_hyp_range(_train_in, _train_out);
_hyps_lb.head(_cov->num_hyp()) = cov_range.first;
_hyps_ub.head(_cov->num_hyp()) = cov_range.second;
// noise
_hyps_lb(_num_hyp-2) = log(_noise_lb);
_hyps_ub(_num_hyp-2) = max(log(10 * _noise_lb), _hyps_ub(_cov->num_hyp()-1));
//mean
_hyps_lb(_num_hyp - 1) = _train_out.minCoeff();
_hyps_ub(_num_hyp - 1) = _train_out.maxCoeff();
_hyps_lb = _hyps_lb.array() - numeric_limits<double>::epsilon();
_hyps_ub = _hyps_ub.array() + numeric_limits<double>::epsilon();
}
Eigen::VectorXd GP::vec2hyp(const std::vector<double>& vx) const
{
assert(vx.size() == _noise_free ? _num_hyp - 1 : _num_hyp);
VectorXd hyp(_num_hyp);
if(not _noise_free)
hyp = convert(vx);
else
{
for(size_t i = 0; i < _cov->num_hyp(); ++i)
hyp(i) = vx[i];
hyp(_num_hyp - 2) = -1 * INF;
hyp(_num_hyp - 1) = vx.back();
}
return hyp;
}
std::vector<double> GP::hyp2vec(const Eigen::VectorXd& hyp) const
{
assert((size_t)hyp.size() == _num_hyp);
vector<double> vx(_noise_free ? _num_hyp - 1 : _num_hyp);
for(size_t i = 0; i < _cov->num_hyp(); ++i)
vx[i] = hyp(i);
if(_noise_free)
vx[_num_hyp-2] = hyp(_num_hyp-1);
else
{
vx[_num_hyp-2] = hyp(_num_hyp-2);
vx[_num_hyp-1] = hyp(_num_hyp-1);
}
return vx;
}
double GP::_hyp_sn2(const VectorXd& hyp) const
{
assert((size_t)hyp.size() == _num_hyp);
return exp(2*hyp(_num_hyp-2));
}
double GP::_hyp_mean(const Eigen::VectorXd& hyp) const
{
assert((size_t)hyp.size() == _num_hyp);
return hyp(_num_hyp-1);
}
Cov* GP::_specify_cov(size_t d, CovFunc cf) const
{
switch(cf)
{
case CovFunc::COV_SE_ARD:
return new CovSEard(d);
case CovFunc::COV_SE_ISO:
return new CovSEiso(d);
default:
cerr << "Unknown cov: " << cf << endl;
exit(EXIT_FAILURE);
}
}
MatrixSolver* GP::_specify_matrix_solver(GP::MatrixDecomp md) const
{
switch(md)
{
case MatrixDecomp::Cholesky:
return new MatrixSolverLLT();
case MatrixDecomp::QR:
return new MatrixSolverQR();
default:
cerr << "Unknown matrix decomposition: " << md << endl;
exit(EXIT_FAILURE);
}
}