CovarianceFunction.hpp 1.76 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.hpp"
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
10
11
12
13
typedef std::vector<double> ToeplitzRow;

typedef std::vector<double> ToeplitzColumn;

typedef std::pair<ToeplitzRow, ToeplitzColumn> ToeplitzMatrix;

niklas.baumgarten's avatar
niklas.baumgarten committed
14
15
class CovarianceFunction {
public:
16
17
    double sigma = 1.0;
    int norm_p = 2;
niklas.baumgarten's avatar
niklas.baumgarten committed
18
    std::vector<double> lambda{0.3, 0.3};
19
    double smoothing = 1.0;
niklas.baumgarten's avatar
niklas.baumgarten committed
20
21

    CovarianceFunction() {
22
23
        config.get("sigma", sigma);
        config.get("norm_p", norm_p);
24
        config.get("lambda", lambda);
niklas.baumgarten's avatar
niklas.baumgarten committed
25
        config.get("smoothing", smoothing);
niklas.baumgarten's avatar
niklas.baumgarten committed
26
27
    }

niklas.baumgarten's avatar
niklas.baumgarten committed
28
29
    virtual double norm(const double *x) = 0;

niklas.baumgarten's avatar
niklas.baumgarten committed
30
31
    virtual double covariance_fct(double *tau) = 0;

niklas.baumgarten's avatar
niklas.baumgarten committed
32
33
34
35
36
37
38
    virtual ToeplitzMatrix covarianceMatrix() {
        /*
         * Idea: Covariance function combined with mesh
         * gives covariance matrix in Toeplitz structure
         */
        return ToeplitzMatrix{};
    };
niklas.baumgarten's avatar
niklas.baumgarten committed
39
40
41
42
43
};

class CovarianceFunction1D : CovarianceFunction {
public:
    double covariance_fct(double *tau) override {
44
        tau[0] /= lambda[0];
45
        return std::pow(sigma, 2) * std::exp(-std::pow(norm(tau), smoothing));
niklas.baumgarten's avatar
niklas.baumgarten committed
46
47
48
    }

    double norm(const double *x) override {
49
        return std::abs(x[0]);
niklas.baumgarten's avatar
niklas.baumgarten committed
50
51
52
53
54
55
    }
};

class CovarianceFunction2D : CovarianceFunction {
public:
    double covariance_fct(double *tau) override {
56
57
        tau[0] /= lambda[0];
        tau[1] /= lambda[1];
58
        return std::pow(sigma, 2) * std::exp(-std::pow(norm(tau), smoothing));
niklas.baumgarten's avatar
niklas.baumgarten committed
59
60
61
62
    }

    double norm(const double *x) override {
        if (norm_p == 1)
63
            return std::abs(x[0]) + std::abs(x[1]);
niklas.baumgarten's avatar
niklas.baumgarten committed
64
        if (norm_p == 2)
65
            return sqrt(std::pow(x[0], 2) + std::pow(x[1], 2));
niklas.baumgarten's avatar
niklas.baumgarten committed
66
67
68
69
70
        else Exit("Choose p=1 or p=2")
    }
};

#endif //M_COVARIANCEFUNCTION_H