# Covariance function

Jump to: navigation, search

The covariance function, or kernel, describes the spatial covariance of a random field.

## stats++

In stats++, a covariance function is defined by implementing a derived class of the CovarianceFunction class:

```#include <memory>
#include <vector>
#include "jScience/linalg/Vector.hpp"

class CovarianceFunction
{

public:

CovarianceFunction(const std::vector<double> &th);
~CovarianceFunction();

virtual std::unique_ptr<CovarianceFunction> clone() const = 0;

virtual double              cov(const Vector<double> &x1, const Vector<double> &x2) = 0;
virtual std::vector<double> ddtheta(const Vector<double> &x1, const Vector<double> &x2);
virtual Vector<double>      ddx(const Vector<double> &x, const Vector<double> &x2);

protected:

std::vector<double> theta;

};```

This class is purely abstract, as the clone() and cov() functions must be defined in the derived class; note that implementations of ddtheta() and ddx() are not required, unless certain functionality is desired (e.g., optimization of hyperparameters).

#### Example

An example for a simplified squared exponential covariance function:

```#include <memory>
#include <vector>
#include "jScience/linalg/Vector.hpp"

class SquaredExponential : public CovarianceFunction
{

public:

SquaredExponential(const std::vector<double> &th) : CovarianceFunction(th) {};
~SquaredExponential() {};

std::unique_ptr<CovarianceFunction> SquaredExponential::clone() const
{
return std::unique_ptr<CovarianceFunction>(new SquaredExponential(*this));
}

double cov(const Vector<double> &x1, const Vector<double> &x2)
{
Vector<double> x1mx2 = x1 - x2;

return ( this->theta*this->theta*std::exp(-dot_product(x1mx2, x1mx2)/(2.0*this->theta*this->theta)) );
}

};```

## Notes and references

1. The simplified squared exponential covariance function is not what is implemented in stats++.