# Stheno marginals

**URL:** <https://discourse.julialang.org/t/stheno-marginals/58943>\
**Category:** Probabilistic Programming\
**Tags:** stheno\
**Created:** [April 9, 2021, 8:28pm UTC](https://discourse.julialang.org/t/stheno-marginals/58943 "2021-04-09T20:28:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [April 9, 2021, 8:28pm UTC](https://discourse.julialang.org/t/stheno-marginals/58943/1 "2021-04-09T20:28:08Z")

</div>

I’m trying to follow the examples but having some issues. What’s going wrong here?

[8188c328] Stheno v0.7.1

```julia
#
# This file just shows how some of the basic manipulations you can do in Stheno work in
# practice. See the main documentation and the rest of this examples folder for more info.
#

# Set up the environment to run this example. Make sure you're within the folder that this
# file lives in.

# The time-to-first-plot issue means that this might take a little while.
using LinearAlgebra, Random, Stheno
using Stheno: posterior, logpdf, marginals
rng = MersenneTwister(123456)

# Construct a Matern-5/2 kernel with lengthscale 0.5 and variance 1.2.
k = 1.2 * transform(Matern52Kernel(), 0.5)

# Construct a zero-mean GP with a simple kernel. Don't worry about the GPC object.
f = Stheno.wrap(GP(k), GPC())

# Specify some locations at which to consider the GP.
N = 50
x = rand(rng, N) * 10

# Specify the variance of the noise under which we'll make observations of the GP.
# We could also have made this a `Real` to specify isotropic noise.
Σ = Diagonal(rand(rng, N) .+ 0.1)

# Construct marginal distribution over `f` at `x` added to some independent zero-mean
# Gaussian noise with covariance matrix `Σ`.
fx = f(x, Σ)

# Generate a sample from the prior.
y = rand(rng, fx)

# Compute the log marginal probability of the sample under the prior.
# logpdf(fx, y)

# Do inference: compute the posterior distribution over `f` given we observe it + noise to
# be `y` at locations `x`.
f_post = posterior(fx, y);

# Specify some points at which to plot the posterior.
Npr = 1000
xpr = range(-3.0, 13.0; length=Npr)

# Construct the posterior predictive distribution at `xpr`. Add some jitter.
fx_post = f_post(xpr, 1e-9)

# Draw samples from the posterior.
y_post = rand(rng, fx_post)

# Compute the marginal posterior predictive probability of the samples.
logpdf(fx_post, y_post)

# Compute the posterior marginal distributions. (We could equally have done this with `fx`).
posterior_marginals = marginals(fx_post)

```

```julia

julia> posterior_marginals = marginals(fx_post)
ERROR: MethodError: no method matching marginals(::AbstractGPs.FiniteGP{AbstractGPs.PosteriorGP{Stheno.WrappedGP{GP{AbstractGPs.ZeroMean{Float64},ScaledKernel{TransformedKernel{Matern52Kernel,ScaleTransform{Float64}},Float64}}},NamedTuple{(:α, :C, :x, :δ),Tuple{Array{Float64,1},Cholesky{Float64,Array{Float64,2}},Array{Float64,1},Array{Float64,1}}}},StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Diagonal{Float64,FillArrays.Fill{Float64,1,Tuple{Base.OneTo{Int64}}}}})
Closest candidates are:
  marginals(::SparseFiniteGP)

```

---

<div class="post-metadata">

**Author:** ![devmotion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devmotion/32/9201_2.png) [@devmotion](https://discourse.julialang.org/u/devmotion)\
**Post date:** [April 9, 2021, 11:45pm UTC](https://discourse.julialang.org/t/stheno-marginals/58943/2 "2021-04-09T23:45:30Z")

</div>

I assume that `Stheno.marginals != AbstractGPs.marginals` and that it works if you replace `using Stheno: posterior, logpdf, marginals` with `using AbstractGPs`. I opened a potential fix in Stheno: [Import `marginals` by devmotion · Pull Request #173 · JuliaGaussianProcesses/Stheno.jl · GitHub](https://github.com/JuliaGaussianProcesses/Stheno.jl/pull/173)

---

<div class="post-metadata">

**Author:** ![willtebbutt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willtebbutt/32/6790_2.png) [@willtebbutt](https://discourse.julialang.org/u/willtebbutt)\
**Post date:** [April 10, 2021, 9:06am UTC](https://discourse.julialang.org/t/stheno-marginals/58943/3 "2021-04-10T09:06:31Z")

</div>

Thanks @devmotion for the fix, it’s now merged.

@jzr please let us know if upgrading to 0.7.2 doesn’t fix the problem (should be available within the next hour or two)

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [April 10, 2021, 7:09pm UTC](https://discourse.julialang.org/t/stheno-marginals/58943/4 "2021-04-10T19:09:24Z")

</div>

Yep it works.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [April 11, 2021, 6:35am UTC](https://discourse.julialang.org/t/stheno-marginals/58943/5 "2021-04-11T06:35:34Z")

</div>

@willtebbutt 0.7 is no longer compatible with Soss?

```julia
pkg> add Stheno@0.7.2 Soss
   Updating registry at `~/.julia/registries/General`
  Resolving package versions...
ERROR: Unsatisfiable requirements detected for package Optim [429524aa]:
 Optim [429524aa] log:
 ├─possible versions are: [0.15.3, 0.16.0, 0.17.0-0.17.2, 0.18.0-0.18.1, 0.19.0-0.19.7, 0.20.0-0.20.6, 0.21.0, 0.22.0, 1.0.0, 1.1.0, 1.2.0-1.2.4, 1.3.0] or uninstalled
 ├─restricted by compatibility requirements with SimpleGraphs [55797a34] to versions: [0.15.3, 0.16.0, 0.17.0-0.17.2, 0.18.0-0.18.1, 0.19.0-0.19.7, 0.20.0-0.20.6, 0.21.0, 0.22.0]
 │ └─SimpleGraphs [55797a34] log:
 │ ├─possible versions are: [0.2.3-0.2.10, 0.3.0, 0.4.0-0.4.4, 0.5.0-0.5.3, 0.6.0-0.6.10, 0.7.0-0.7.6] or uninstalled
 │ ├─restricted by compatibility requirements with Soss [8ce77f84] to versions: [0.2.3-0.2.10, 0.3.0, 0.4.0-0.4.4, 0.5.0-0.5.3, 0.6.0-0.6.10, 0.7.0-0.7.6]
 │ │ └─Soss [8ce77f84] log:
 │ │ ├─possible versions are: [0.1.0, 0.2.0, 0.3.0, 0.4.0, 0.5.0, 0.6.0, 0.7.0, 0.8.0, 0.9.0, 0.10.0, 0.11.0, 0.12.0, 0.13.0-0.13.1, 0.14.0-0.14.4, 0.15.0-0.15.3, 0.16.0-0.16.2, 0.17.0] or uninstalled
 │ │ ├─restricted to versions * by an explicit requirement, leaving only versions [0.1.0, 0.2.0, 0.3.0, 0.4.0, 0.5.0, 0.6.0, 0.7.0, 0.8.0, 0.9.0, 0.10.0, 0.11.0, 0.12.0, 0.13.0-0.13.1, 0.14.0-0.14.4, 0.15.0-0.15.3, 0.16.0-0.16.2, 0.17.0]
 │ │ ├─restricted by compatibility requirements with Stheno [8188c328] to versions: [0.1.0, 0.2.0, 0.3.0, 0.4.0, 0.5.0, 0.6.0, 0.9.0, 0.10.0, 0.11.0, 0.12.0, 0.13.0-0.13.1, 0.14.0-0.14.4, 0.15.0-0.15.3, 0.16.0-0.16.2, 0.17.0] or uninstalled, leaving only versions: [0.1.0, 0.2.0, 0.3.0, 0.4.0, 0.5.0, 0.6.0, 0.9.0, 0.10.0, 0.11.0, 0.12.0, 0.13.0-0.13.1, 0.14.0-0.14.4, 0.15.0-0.15.3, 0.16.0-0.16.2, 0.17.0]
 │ │ │ └─Stheno [8188c328] log:
 │ │ │ ├─possible versions are: [0.1.0-0.1.1, 0.2.0-0.2.1, 0.3.0-0.3.2, 0.4.0-0.4.2, 0.5.0, 0.6.0-0.6.20, 0.7.0-0.7.2] or uninstalled
 │ │ │ └─restricted to versions 0.7.2 by an explicit requirement, leaving only versions 0.7.2
...

```

(cc @cscherrer )

---

<div class="post-metadata">

**Author:** ![willtebbutt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willtebbutt/32/6790_2.png) [@willtebbutt](https://discourse.julialang.org/u/willtebbutt)\
**Post date:** [April 11, 2021, 9:02am UTC](https://discourse.julialang.org/t/stheno-marginals/58943/6 "2021-04-11T09:02:50Z")

</div>

Hmm yeah, it looks like Stheno and Soss have quite different deps now.

I’m looking into fixing this, will report back when I know more.

---

<div class="post-metadata">

**Author:** ![willtebbutt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willtebbutt/32/6790_2.png) [@willtebbutt](https://discourse.julialang.org/u/willtebbutt)\
**Post date:** [April 11, 2021, 9:14am UTC](https://discourse.julialang.org/t/stheno-marginals/58943/7 "2021-04-11T09:14:36Z")

</div>

Actually looks like it’s just related to SimpleGraphs.jl – it’s still depending on an old pre-1.0 version of Optim, which in turn is holding some other dependencies back.

I’ve opened a PR [here](https://github.com/scheinerman/SimpleGraphs.jl/pull/7) which should hopefully fix the issue.

In the meantime, could you try `dev --local SimpleGraphs` and modify the `[compat]` entry in the `Project.toml` for `Optim.jl` to include version 1, and confirm this also fixes the problem for you?

edit: also, thank you for raising these issues – it’s very much appreciated that you let us know when stuff is broken!

edit2: fix typo

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [April 11, 2021, 12:48pm UTC](https://discourse.julialang.org/t/stheno-marginals/58943/8 "2021-04-11T12:48:30Z")

</div>

Thanks @jzr for catching this, and @willtebbutt for the quick fix! I think it’s worth noting that there are some big changes in Soss v0.17. Maybe most notably, we still depend on Distributions.jl but have started to move more toward [MeasureTheory.jl](https://github.com/cscherrer/MeasureTheory.jl). Distributions should still work just fine, but performance and flexibility can be a little better with MT. OTOH it’s still under heavy development, so we fall back to Distributions as needed.
