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

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:

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 *, ls=length)

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

    Xu = // 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:

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.


1 Like

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

CC @bwengals

Thanks ricardo for confirming. I appreciate your time

I am getting this error from the example, Sparse Approximations — PyMC example gallery is that expected too?

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

4.14 is that the reason?