pymc icon indicating copy to clipboard operation
pymc copied to clipboard

pymc.gp.util.stabilize is broken

Open fonnesbeck opened this issue 1 year ago • 1 comments

Describe the issue:

Wrapping a covariance function with stabilize causes a failure. Not sure why it is expecting a tensor variable.

Reproduceable code example:

import pymc as pm
import numpy as np

with pm.Model() as m:

    K = pm.gp.util.stabilize(pm.gp.cov.ExpQuad(input_dim=1, ls=1))

    gp = pm.gp.Latent(cov_func=K)

    f = gp.prior('f', X = np.random.randn(10)[:, None])

    y = pm.Normal('y', mu=f, sigma=1, observed=np.random.randn(10))

Error message:

NotImplementedError: Cannot convert <pymc.gp.cov.ExpQuad object at 0x7fdd378246d0> to a tensor variable.


### PyMC version information:

PyMC 5.10

### Context for the issue:

_No response_

fonnesbeck avatar Apr 06 '24 21:04 fonnesbeck

Hi, the stabilize function adds a diagonal to a matrix K by using pytensor.tensor.identity_like(K), so the input of stabilize should be a matrix, not a function. Therefore, it should be implemented as follows:

import pymc as pm
import numpy as np

with pm.Model() as m:'
    X = np.random.randn(10)[:, None]
    cov_fun = pm.gp.cov.ExpQuad(input_dim=1, ls=1)
    K = pm.gp.util.stabilize(cov_fun(X))

    gp = pm.gp.Latent(cov_func=K)

    f = gp.prior('f', X)

    y = pm.Normal('y', mu=f, sigma=1, observed=np.random.randn(10))

Plus, I don't think you need to stabilize the covariance matrix before using gp.Latent, since gp.prior already applies stabilize to the covariance matrix, you don't need to apply it again.

hchen19 avatar Apr 07 '24 22:04 hchen19