# Implementing sparse Gaussian process model with Turing and Stheno

**URL:** <https://discourse.julialang.org/t/implementing-sparse-gaussian-process-model-with-turing-and-stheno/46886>\
**Category:** Probabilistic Programming\
**Tags:** question, turing\
**Created:** [September 18, 2020, 11:19pm UTC](https://discourse.julialang.org/t/implementing-sparse-gaussian-process-model-with-turing-and-stheno/46886 "2020-09-18T23:19:24Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [September 29, 2020, 3:53am UTC](https://discourse.julialang.org/t/implementing-sparse-gaussian-process-model-with-turing-and-stheno/46886/7 "2020-09-29T03:53:09Z")

</div>

Okay, I came back to this and after some hacking have got the following to run and produce basically sensible results.

```julia
struct SparseFiniteGP{T1<:Stheno.FiniteGP, T2<:Stheno.FiniteGP} <: Distributions.AbstractMvNormal
    fobs::T1
    finducing::T2
end

function Stheno.logpdf(f::T, y::AbstractArray{T1, 1}) where {T <: SparseFiniteGP, T1 <: Real}
    return elbo(f.fobs, y, f.finducing)
end

Base.length(f::SparseFiniteGP) = length(f.fobs)
Distributions._rand!(rng::Random._GLOBAL_RNG, f::SparseFiniteGP1, x::Array{T}) where {T<:Real} = rand(f.fobs)

@model function example_model_sparse(x, z, xu)
    f = GP(Matern32(), GPC())
    fsparse = SparseFiniteGP(f(x, 1e-6), f(xu, 1e-6))
    y ~ fsparse
    λ = exp.(y)
    z .~ Poisson.(λ)
end

chn_sparse = sample(example_model_sparse(x, z, xu), NUTS(), 200)

```

On the left is the dense model’s posterior for λ, on the right is the sparse model’s posterior (with the locations of the inducing points shown by the vertical lines).

![image](https://global.discourse-cdn.com/julialang/original/3X/4/1/4183df4f3d13f2a7a7337bfcb043abfccdca2096.png) ![image](https://global.discourse-cdn.com/julialang/original/3X/2/b/2b5213d483238712fb1ebafbaef239f4e642503a.png)

The posteriors do have some differences, but…with 900 observations, drawing 200 NUTS samples from the dense model takes something like 4 hours. Using the sparse model with 19 inducing points, the same sampling run takes about 60 seconds, i.e. a roughly 240-fold speedup.

@willtebbutt, do you think a type like this is worth having in Stheno? If so, I can start work on a PR…

---

_[View the full topic](https://discourse.julialang.org/t/implementing-sparse-gaussian-process-model-with-turing-and-stheno/46886)._
