# Modeling gaussian process with Turing.jl

**URL:** <https://discourse.julialang.org/t/modeling-gaussian-process-with-turing-jl/76001>\
**Category:** Probabilistic Programming\
**Tags:** gaussian-process\
**Created:** [February 8, 2022, 9:00am UTC](https://discourse.julialang.org/t/modeling-gaussian-process-with-turing-jl/76001 "2022-02-08T09:00:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RickandMortyforever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rickandmortyforever/32/33304_2.png) [@RickandMortyforever](https://discourse.julialang.org/u/RickandMortyforever)\
**Post date:** [February 8, 2022, 9:00am UTC](https://discourse.julialang.org/t/modeling-gaussian-process-with-turing-jl/76001/1 "2022-02-08T09:00:49Z")

</div>

Hi, I was trying to build a gaussian process model with Turing.jl

I’ve read Gaussianprocesses.jl and Stheno.jl. They must be good.  
But for the purpose of learning, I’d like to go through step by step in frame of Turing.jl.

I found this one in search.

> [@Gaussian Process Model with Turing](https://discourse.julialang.org/t/gaussian-process-model-with-turing/42453/9):
>
> Hi @kevbonham, is this what you are looking for? # Import Libraries using Turing using Turing: Variational using Distributions using Distances using PyPlot using StatsFuns import Random using Flux import LinearAlgebra # Squared-exponential covariance function sqexp\_cov\_fn(D, phi, eps=1e-3) = exp.(-D^2 / phi) + LinearAlgebra.I \* eps # Exponential covariance function exp\_cov\_fn(D, phi) = exp.(-D / phi) @model function GP(y, X, m=0, s=1, cov\_fn=exp\_cov\_fn) # Dimensions of predictors . N…

Also I’ve looked up this great reference.

> <https://github.com/luiarthur/TuringBnpBenchmarks/blob/master/src/gp/notebooks//gp_turing.ipynb>

So Followings are what I wrote and got an error. 😮‍💨

```julia
begin
	using Turing, Distributions
	using Random, LinearAlgebra
	using Plots, StatsPlots
	using AbstractGPs, KernelFunctions
	using CSV, HTTP, DataFrames
end

```

```julia
url = "https://raw.githubusercontent.com/delphinH/imagetoy/main/dataset/Fish.csv"

```

```julia
fish = CSV.read(HTTP.get(url).body, DataFrame)

```

```julia
begin
	xs = fish[:, :Length1]
	ys = fish[:, :Weight]
end

```

```julia
#Exponentiated quadratic kernel

function EQkernel(x1, x2, l, σ)
    t₁ = sum(x1.^2, dims=2)
    t₂ = sum(x2.^2, dims=2)
    t₃ = 2*x1*x2'
    t = (t₁ .+ t₂') - t₃
    return σ^2 * exp.(-0.5/l^2 * t)
end

```

```julia
EQkernel(xs, xs, 1, 1) #this work!

```

```julia
@model gptest(x, y) = begin
	#Prior
	σ ~ LogNormal(0.0, 0.1)
	l ~ LogNormal(0.0, 1.0)
	σy ~ LogNormal(0.0, 1.0)
	
	#covariance function
	kernel = EQkernel(x, x, σ, l)
    K = kernelmatrix(kernel, x[:, :], obsdims=1)

	#add noise
    y ~ MvNormal(K + LinearAlgebra.I * σy^2)
end

```

```julia
model = gptest(xs, ys)

```

```julia
chain = sample(model, NUTS(), 2000) # Got an MethodError 

```

MethodError: no method matching kernelmatrix(::Matrix{Float64}, ::Matrix{Float64}; obsdims=1)

How can I fix this error?  
Are there better ways?  
I don’t know what _ **kernelmatrix** _ function is.

Thanks for reading and your attention! 🙏

---

<div class="post-metadata">

**Author:** ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)\
**Post date:** [February 8, 2022, 10:20am UTC](https://discourse.julialang.org/t/modeling-gaussian-process-with-turing-jl/76001/2 "2022-02-08T10:20:13Z")

</div>

> [@RickandMortyforever](#):
>
> I don’t know what _ **kernelmatrix** _ function is.

Since you said you want to go step by step you’d have to define that function yourself.  
This function should return an NxN matrix that contains `kernel(X[i], X[j])` at position `(i, j)`, where `X` is your NxM matrix of independent variables (M = number of independent variables).

Note also that instead of:

> [@RickandMortyforever](#):
>
> `	kernel = EQkernel(x, x, σ, l)`

you want `kernel` to be a _function_ that is called inside `kernelmatrix` for every `(i, j)`, i.e. you’d probably want to instead define:

```julia
kernel(x1, x2) = EQkernel(x1, x2, σ, l)

```

---

<div class="post-metadata">

**Author:** ![RickandMortyforever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rickandmortyforever/32/33304_2.png) [@RickandMortyforever](https://discourse.julialang.org/u/RickandMortyforever)\
**Post date:** [February 8, 2022, 10:38am UTC](https://discourse.julialang.org/t/modeling-gaussian-process-with-turing-jl/76001/3 "2022-02-08T10:38:37Z")

</div>

Thx for replying!  
EQkernel that I made gives me N by N matrix. so I already have one.  
👍

---

<div class="post-metadata">

**Author:** ![RickandMortyforever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rickandmortyforever/32/33304_2.png) [@RickandMortyforever](https://discourse.julialang.org/u/RickandMortyforever)\
**Post date:** [February 8, 2022, 10:47am UTC](https://discourse.julialang.org/t/modeling-gaussian-process-with-turing-jl/76001/4 "2022-02-08T10:47:29Z")

</div>

Based on your advice, I delete ` K = kernelmatrix(kernel, x[:, :], obsdims=1)`  
this line.

and now I have another error  
`MethodError: no method matching +(::Type{KernelFunctions.Kernel}, ::LinearAlgebra.UniformScaling{Float64})`

Endless error 😂

---

<div class="post-metadata">

**Author:** ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)\
**Post date:** [February 8, 2022, 11:00am UTC](https://discourse.julialang.org/t/modeling-gaussian-process-with-turing-jl/76001/5 "2022-02-08T11:00:14Z")

</div>

Try to understand the error (it is actually quite informative).  
It tells you that in this line:

> [@RickandMortyforever](#):
>
> ` y ~ MvNormal(K + LinearAlgebra.I * σy^2)`

you’re trying to add something that is of type `Type{KernelFunctions.Kernel}` to something of type `LinearAlgebra.UniformScaling{Float64}`.  
Is this what you expect? No. You want the first summand to be of type `Matrix{Float64}`.  
In other words: Your `K` is not a matrix (you said you deleted the line, but you didn’t say what you replaced it with).

My general advice is: Try to understand the errors and go step by step from there (e.g. make sure you understand every line and the output of every line before continuing iterating on the full script/model)

---

<div class="post-metadata">

**Author:** ![RickandMortyforever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rickandmortyforever/32/33304_2.png) [@RickandMortyforever](https://discourse.julialang.org/u/RickandMortyforever)\
**Post date:** [February 8, 2022, 11:06am UTC](https://discourse.julialang.org/t/modeling-gaussian-process-with-turing-jl/76001/6 "2022-02-08T11:06:20Z")

</div>

I’ve tried to find out that.  
It should not be the Type `Type{KernelFunctions.Kernel} `.  
And it wasn’t.  
it was a capital letter problem…I should have typed `kernel` not `Kernel`

Really sorry for bothering 🥺

---

<div class="post-metadata">

**Author:** ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)\
**Post date:** [February 8, 2022, 11:08am UTC](https://discourse.julialang.org/t/modeling-gaussian-process-with-turing-jl/76001/7 "2022-02-08T11:08:42Z")

</div>

> [@RickandMortyforever](#):
>
> Really sorry for bothering 🥺

No worries at all! 🙂  
glad you found it!
