Adding covariance matrix to Gaussian Processes in Pymc3

You’ll have to make a thin wrapper subclassing the base Covariance class here.

import theano.tensor as tt
import pymc3 as pm

class ConstantMatrix(pm.gp.cov.Covariance):
    def __init__(self, cov):
        self.cov = tt.as_tensor_variable(cov)
        
    def full(self, X, Xs):
        # covariance matrix known, not explicitly function of X 
        return self.cov
    
    def diag(self, X):
        return tt.diag(self.cov)

Then wrap your Sigma with it:

Sigma = ConstantMatrix(np.matrix([[1,0.1, 0,0], [0.1, 2,0.2,0.4], [0, 0.2,3,0.4],[0, 0.4,0.4,4]])) #Covariance  Matrix

You should be good to go then for inference and prediction.

2 Likes