# Multidimensional MarginalSparse throws with a simple example - "Random variables detected in the logp graph"

**URL:** <https://discourse.pymc.io/t/multidimensional-marginalsparse-throws-with-a-simple-example-random-variables-detected-in-the-logp-graph/10198>\
**Category:** v5\
**Tags:** bug, gaussian\_process, modeling\
**Created:** [August 20, 2022, 12:44pm UTC](https://discourse.pymc.io/t/multidimensional-marginalsparse-throws-with-a-simple-example-random-variables-detected-in-the-logp-graph/10198 "2022-08-20T12:44:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![motivationalmodels](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/motivationalmodels/32/5555_2.png) [@motivationalmodels](https://discourse.pymc.io/u/motivationalmodels)\
**Post date:** [August 20, 2022, 12:44pm UTC](https://discourse.pymc.io/t/multidimensional-marginalsparse-throws-with-a-simple-example-random-variables-detected-in-the-logp-graph/10198/1 "2022-08-20T12:44:09Z")

</div>

Hello everyone

I’m trying the forum out! I’ve been enjoying using PyMC but have run into an issue which I haven’t been able to resolve.

The following code minimally replicates my error:

```auto
from itertools import product

# columns are times, rows are wavelengths, entries are intensities
intensity_data = np.arange(10).reshape(2, 5)
y = intensity_data.ravel()

image_wavelengths = [700, 800] # / nm
timestep_range = [0, 1, 2, 3, 4]

wavelengths, steps = np.meshgrid(image_wavelengths, timestep_range)
x = np.asarray(list(product(timestep_range, image_wavelengths)))

with pm.Model() as model:
    # Specify the covariance function.
    size = pm.HalfCauchy('size', 1)
    length = pm.Gamma('length', 2, 0.5)
    cov_func = size ** 2 * pm.gp.cov.ExpQuad(2, ls=length)

    # Specify the GP. The default mean function is `Zero`.
    gp = pm.gp.MarginalApprox(cov_func=cov_func, approx='FITC')

    Xu = pm.gp.util.kmeans_inducing_points(len(x) // 2, x)

    sigma = pm.Exponential('sigma', 1)
    likelihood = gp.marginal_likelihood('likelihood', X=x, Xu=Xu, y=y, noise=sigma)

```

And the error (upon running something like pm.find\_MAP() or pm.sample()) is:

```auto
ValueError: Random variables detected in the logp graph: [size, length].
This can happen when DensityDist logp or Interval transform functions reference nonlocal variables.

```

Inference appears to be performed correctly when the covariance function is specified with constant values however this is not really what I need. Any ideas or suggestions would be really appreciated.

Cheers

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [August 20, 2022, 2:24pm UTC](https://discourse.pymc.io/t/multidimensional-marginalsparse-throws-with-a-simple-example-random-variables-detected-in-the-logp-graph/10198/2 "2022-08-20T14:24:03Z")

</div>

This is a known bug that is still being solved: [MarginalApprox doesn't allow non-constant covariance parameters or inducing point locations in v4 · Issue #5922 · pymc-devs/pymc · GitHub](https://github.com/pymc-devs/pymc/issues/5922)

CC @bwengals

---

<div class="post-metadata">

**Author:** ![motivationalmodels](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/motivationalmodels/32/5555_2.png) [@motivationalmodels](https://discourse.pymc.io/u/motivationalmodels)\
**Post date:** [August 23, 2022, 10:18pm UTC](https://discourse.pymc.io/t/multidimensional-marginalsparse-throws-with-a-simple-example-random-variables-detected-in-the-logp-graph/10198/3 "2022-08-23T22:18:37Z")

</div>

Thanks ricardo for confirming. I appreciate your time

---

<div class="post-metadata">

**Author:** ![Oliver](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/oliver/32/3849_2.png) [@Oliver](https://discourse.pymc.io/u/Oliver)\
**Post date:** [October 4, 2022, 12:13pm UTC](https://discourse.pymc.io/t/multidimensional-marginalsparse-throws-with-a-simple-example-random-variables-detected-in-the-logp-graph/10198/4 "2022-10-04T12:13:53Z")

</div>

I am getting this error from the example, [Sparse Approximations — PyMC example gallery](https://www.pymc.io/projects/examples/en/latest/gaussian_processes/GP-SparseApprox.html) is that expected too?

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [October 4, 2022, 1:58pm UTC](https://discourse.pymc.io/t/multidimensional-marginalsparse-throws-with-a-simple-example-random-variables-detected-in-the-logp-graph/10198/5 "2022-10-04T13:58:30Z")

</div>

It should be fixed in the latest release. Which version of PyMC are you using?

---

<div class="post-metadata">

**Author:** ![Oliver](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/oliver/32/3849_2.png) [@Oliver](https://discourse.pymc.io/u/Oliver)\
**Post date:** [October 5, 2022, 5:17pm UTC](https://discourse.pymc.io/t/multidimensional-marginalsparse-throws-with-a-simple-example-random-variables-detected-in-the-logp-graph/10198/6 "2022-10-05T17:17:23Z")

</div>

4.14 is that the reason?
