CovarianceFunction.hpp 1.32 KB
Newer Older
niklas.baumgarten's avatar
niklas.baumgarten committed
1
2
3
#ifndef M_COVARIANCEFUNCTION_H
#define M_COVARIANCEFUNCTION_H

niklas.baumgarten's avatar
niklas.baumgarten committed
4
#include "utility/Config.hpp"
niklas.baumgarten's avatar
niklas.baumgarten committed
5
#include "utility/Assertion.h"
niklas.baumgarten's avatar
niklas.baumgarten committed
6

niklas.baumgarten's avatar
niklas.baumgarten committed
7

niklas.baumgarten's avatar
niklas.baumgarten committed
8
9
class CovarianceFunction {
public:
10
11
    double sigma = 1.0;
    int norm_p = 2;
12
    std::vector<double> lambda;
13
    double smoothing = 1.0;
niklas.baumgarten's avatar
niklas.baumgarten committed
14
15

    CovarianceFunction() {
16
17
        config.get("sigma", sigma);
        config.get("norm_p", norm_p);
18
        config.get("lambda", lambda);
niklas.baumgarten's avatar
niklas.baumgarten committed
19
        config.get("smoothing", smoothing);
niklas.baumgarten's avatar
niklas.baumgarten committed
20
21
22
23
24
25
26
27
28
29
    }

    virtual double covariance_fct(double *tau) = 0;

    virtual double norm(const double *x) = 0;
};

class CovarianceFunction1D : CovarianceFunction {
public:
    double covariance_fct(double *tau) override {
30
        tau[0] /= lambda[0];
31
        return pow(sigma, 2) * exp(-pow(norm(tau), smoothing));
niklas.baumgarten's avatar
niklas.baumgarten committed
32
33
34
35
36
37
38
39
40
41
    }

    double norm(const double *x) override {
        return abs(x[0]);
    }
};

class CovarianceFunction2D : CovarianceFunction {
public:
    double covariance_fct(double *tau) override {
42
43
        tau[0] /= lambda[0];
        tau[1] /= lambda[1];
44
        return pow(sigma, 2) * exp(-pow(norm(tau), smoothing));
niklas.baumgarten's avatar
niklas.baumgarten committed
45
46
47
48
49
50
51
52
53
54
55
56
    }

    double norm(const double *x) override {
        if (norm_p == 1)
            return abs(x[0]) + abs(x[1]);
        if (norm_p == 2)
            return sqrt(pow(x[0], 2) + pow(x[1], 2));
        else Exit("Choose p=1 or p=2")
    }
};

#endif //M_COVARIANCEFUNCTION_H