Euclidean Distance in built-in GP covariance functions

Hello,

I would like to use the built-in gp_exp_cov function, to define a gaussian process prior for points in 2D space, as I think it’s faster than what I wrote. I think that the way that I am passing the coordinates to the function however is calculating the element-wise distance(x^2-y^2) rather than the Euclidian distance for the coordinates vector.

However, if I pass just the full vector (single argument, coords), the matrix becomes non-symmetric.

Could someone suggest how to use this built in function for spatial models where I have a 2d vector of coordinates and want to compute the euclidian distance?

A minimal example:

data{
  int<lower=0> n; 
  array[s] vector[2] coords;

}
parameters{
   real sigma2;
   real rho;

   vector[n] z_eta;
}
transformed parameters{
   matrix[n,n] SIGMA;
    vector[n] eta; 
    LS = cholesky_decompose(SIGMA)
    
   eta = LS*z_eta
}
model{
   sigma2 ~ inv_gamma();
   rho ~ gamma();
   z_eta ~ std_normal();
}

Thank you!!

First, a note that Stan does not have a currently gp_exp_cov() covariance function. If you are looking for the squared exponential (exponentiated quadratic) it is gp_exp_quad_cov or if you are looking for just a plain exponential it is gp_exponential_cov(). The below applies to each.

The gp_exponential_cov() function should work just fine with a array of 2-vectors like you have declared. Looking at your code there might be a few potential issues:

  • coords should have the same dimension as the data i.e. array[n] vector[2] coords.
  • Your transformed parameters block is missing the actual covariance matrix assembly call i.e. SIGMA = gp_exponential_cov(coords, sigma2, rho);
  • Your priors on sigma2 and rho might be swapped
  • Missing line ends ; for LS and eta declarations
1 Like