Hi all,

I am trying to model a periodic function as a Gaussian process. The modelled function has not other constraints than the need to be periodic and smooth. My initial idea was to use squared exponential kernel, and only redefine the distance metric, so that it treats the time variable as a circle. However, for certain combinations of parameters this fails to yield a positive semi-definite matrix. Any idea what might be wrong? Or a pointer to some literature on GPs using different distance metrics than Euclidean distance?

Thanks a lot!

Here is R code that shows the problem (I encountered this why preparing simulated data for a Stan model in R)

```
generate_periodic_gp <- function(time, scale, length) {
# Construct the squared exponential covariance matrix
cov_matrix = array(0,c(length(time), length(time)));
maxTime = max(time)
minTime = min(time)
for(i in 1:length(time)) {
cov_matrix[i,i] = scale^2 + 0.00000001; #Adding jitter to ensure positive semi-definiteness
if (i < length(time)) {
for(j in (i+1):length(time)) {
#Take either the normal distance or the distance wrapped around the edge, whichever is shorter
distance = min(abs(time[i] - time[j]), min(time[i], time[j]) - minTime + maxTime - max(time[i], time[j]))
covariance = (scale^2) * exp( (-0.5 / (length ^ 2)) * (distance ^ 2) );
cov_matrix[i,j] = covariance
cov_matrix[j,i] = covariance
}
}
}
# Draw from the multinormal distribution using the cholesky decomposition of the cov. matrix
chol_cov = t(chol(cov_matrix));
values = chol_cov %*% rnorm(length(time));
return(values)
}
times = seq(1,10, by =0.1)
values = generate_periodic_gp(times,1,0.7)
plot(times, values, type ="l") #Works as expected, gives a smooth curve and values[1] == values[length(values)]
values = generate_periodic_gp(times,1,0.8) #Fails, not positive definite
```