Hi everyone,

I am wondering whether it is possible to use a custom distance metric with a gaussian process in Stan. I have a classification problem, and the idea is that items which are close together should be assigned to the same class. However, the way of calculating the distance between two items is non-trivial and as far as I can see, non of the implemented kernels do what I want. So far, I have tried to pass a distance matrix to Stan, and then use those distances instead of the (x_{i} - y_{j})^2 term in the exponentiated quadratic function.

This is my code so far:

```
data {
int<lower=1> N1; // N train
int<lower=1> N2; // N test
int<lower=1> N; // train + test
int<lower=0, upper=1> x1[N1]; // observations
matrix [N, N] K; // distances
real<lower=0> gamma1; //priors
real<lower=0> gamma2; //priors
}
parameters {
real<lower=0> rho;
real<lower=0> alpha;
real a;
vector[N] eta;
}
transformed parameters {
vector[N] f;
matrix[N, N] L_K;
matrix[N, N] K2;
for(i in 1:N)
for(j in 1:N)
K2[i,j] = square(alpha) * exp(-0.5 * rho * (K[i, j]));
for (n in 1:N)
K2[n, n] = K2[n, n] + 1e-10;
L_K = cholesky_decompose(K2);
{
f = L_K * eta;
}
}
model {
rho ~ inv_gamma(gamma1, gamma2);
alpha ~ normal(0, 1);
a ~ normal(0, 1);
eta ~ normal(0, 1);
x1 ~ bernoulli_logit(a + f[1:N1]);
}
generated quantities {
int z2[N];
for (n in 1:N)
z2[n] = bernoulli_logit_rng(a + f[n]);
}
```

The model at least compiles and samples. But the out of sample predictions are not very good.

Does this set up make sense? Is there a better way to do this?

Thanks!