I got the error of “PosDefException” when trying Bayesian Gaussian Process in Turing.jl. The Bayesian framework looks as follows
@model function GP_μ(μ, D; cov_fn::Function=exp_cov_fn)
n_station = length(μ)
# weakly informative prior over mean field for the μ_i
μ_mean ~ Normal(100, 25)
# weakly informative priors over the kernel variance parameter
# kernel variance parameter is > 0
μ_kernelvariance ~ InverseGamma(10, 50) # 10 ± 5 roughly
# weakly informative priors over kernel length scale ℓ
min_dist = minimum(D) + 1E-3
max_dist = maximum(D)
μ_kernellength ~ Uniform(min_dist, max_dist)
########################
# GAUSSIAN PROCESS MODEL
########################
# the kernels are computed exactly
μ_kernel = cov_fn(D, μ_kernelvariance_log, μ_kernellength)
# process layer implements the Gaussian Process
δ = 1E-6 # add an offset for numerical stability
μ ~ MvNormal(μ_mean * ones(n_station), Matrix(Hermitian(μ_kernel + δ * I)))
end
function calc_μ_true(lon, lat)
dist = Euclidean()(CENTER, [lon, lat])
return 100 + 25 * sin(2 * π * dist / 5 + 3π / 2) + rand(Normal(0, 10))/5
end
n_station = 30 # number of synthetic stations
n_obs = 30 # number of obs per station
# get the true values
lons = rand(Uniform(LON_RANGE[1], LON_RANGE[2]), n_station)
lats = rand(Uniform(LAT_RANGE[1], LAT_RANGE[2]), n_station)
μ_true = calc_μ_true.(lons, lats)
distance = calc_dist(hcat(lats, lons))
model_μ = GP_μ(μ_true, distance)
chains = begin
mcmc_sampler = DynamicNUTS()
n_per_chain = 1000
nchains = 2
sample(model_μ, mcmc_sampler, MCMCThreads(), n_per_chain, nchains; drop_warmup=true)
end
I tried adding a noise term, using Hermitian(), Symmetric(), but still got the error of either “matrix is not positive definite” or “matrix is not Hermitian” for the covariance I suppose. Any idea how I might solve this?