Canoe
Comprehensive Atmosphere N' Ocean Engine
gaussian_process.cpp
Go to the documentation of this file.
1 // C/C++ headers
2 #include "gaussian_process.hpp"
3 
4 #include <climath/linalg.h>
5 
6 #include <Eigen/Dense>
7 #include <cmath>
8 #include <iostream>
9 #include <utils/ndarrays.hpp>
10 
11 void gp_covariance(KernelFunction_t kernel, double **cov, double const *x,
12  double const *s, int n, double l) {
13  for (int i = 0; i < n; ++i)
14  for (int j = 0; j < n; ++j) {
15  cov[i][j] = kernel(x[i], x[j], l, s[i] * s[j]);
16  }
17 }
18 
19 void gp_covariance2(KernelFunction_t kernel, double **cov, double const *x1,
20  double const *s1, int n1, double const *x2,
21  double const *s2, int n2, double l) {
22  for (int i = 0; i < n1; ++i)
23  for (int j = 0; j < n2; ++j) {
24  cov[i][j] = kernel(x1[i], x2[j], l, s1[i] * s2[j]);
25  }
26 }
27 
28 double gp_predict(KernelFunction_t kernel, double *arr2, double const *x2,
29  double const *s2, int n2, double const *arr1,
30  double const *x1, double const *s1, int n1, double len) {
31  double **cov1, **cov2;
32  NewCArray(cov1, n1, n1);
33  NewCArray(cov2, n2, n1);
34 
35  gp_covariance(kernel, cov1, x1, s1, n1, len);
36  gp_covariance2(kernel, cov2, x2, s2, n2, x1, s1, n1, len);
37 
38  // copy arr1 to b because the solution x=A^{-1}*b will be stored
39  // in b after calling lubksb
40  int *indx = new int[n1];
41  double *b = new double[n1];
42  memcpy(b, arr1, n1 * sizeof(double));
43 
44  ludcmp(cov1, n1, indx);
45  lubksb(cov1, n1, indx, b);
46  mvdot(arr2, cov2, b, n2, n1);
47 
48  double d = -0.5 * vvdot(arr1, b, n1);
49 
50  delete[] b;
51  delete[] indx;
52  FreeCArray(cov1);
53  FreeCArray(cov2);
54 
55  // returns prior probability
56  return d;
57 }
58 
59 double gp_lnprior(KernelFunction_t kernel, double const *arr1, double const *x1,
60  double const *s1, int n1, double len) {
61  double **cov1;
62  NewCArray(cov1, n1, n1);
63 
64  gp_covariance(kernel, cov1, x1, s1, n1, len);
65 
66  int *indx = new int[n1];
67  double d, *b = new double[n1];
68  memcpy(b, arr1, n1 * sizeof(double));
69 
70  ludcmp(cov1, n1, indx);
71  lubksb(cov1, n1, indx, b);
72 
73  d = -0.5 * vvdot(arr1, b, n1);
74 
75  delete[] b;
76  delete[] indx;
77  FreeCArray(cov1);
78 
79  // returns prior probability
80  return d;
81 }
double gp_lnprior(KernelFunction_t kernel, double const *arr1, double const *x1, double const *s1, int n1, double len)
double gp_predict(KernelFunction_t kernel, double *arr2, double const *x2, double const *s2, int n2, double const *arr1, double const *x1, double const *s1, int n1, double len)
void gp_covariance(KernelFunction_t kernel, double **cov, double const *x, double const *s, int n, double l)
void gp_covariance2(KernelFunction_t kernel, double **cov, double const *x1, double const *s1, int n1, double const *x2, double const *s2, int n2, double l)
double(* KernelFunction_t)(double, double, double, double)
void lubksb(double **a, int n, int *indx, double *b)
Definition: lubksb.c:14
void mvdot(double *r, double **m, double const *v, int n1, int n2)
Definition: mvdot.c:7
int ludcmp(double **a, int n, int *indx)
Definition: ludcmp.c:14
double vvdot(double const *a, double const *b, int n)
Definition: vvdot.c:5
void FreeCArray(T **a)
Definition: ndarrays.hpp:13
void NewCArray(T **&a, int n1, int n2)
Definition: ndarrays.hpp:5