PyMC: How to get gradient of the loglikelihood function of a model

Hello Everyone,

I am trying to get gradient of the loglikelihood function of a model. I am doing this to be able to compute Outer Product Gradient of my likelihood function. I used pm.gradient() on model.compile_logp() function but I got AttributeError: ‘PointFunc’ object has no attribute ‘owner’ error.
I replicated my error below based on an example available on pymc quick start page. Please help me understand what may I be doing wrong?

I am using ‘4.0.1’ version of pymc.

Code:

import pymc as pm
import numpy as np

RANDOM_SEED = 8927
rng = np.random.default_rng(RANDOM_SEED)
with pm.Model() as model:
mu = pm.Normal(“mu”, mu=0, sigma=1)
obs = pm.Normal(“obs”, mu=mu, sigma=1, observed=rng.standard_normal(100))

model.compile_logp()({“mu”: 0})
#array(-143.03962875)

#The code works uptil above line. Now I wanted to get gradient of the logp function.
#Hence, I wrote below code

logp = model.compile_logp()
log_grad = pm.gradient(logp)
#Below is the error I got

Error:

Thank you for your time.

The most straightforward way is to use model.compile_dlogp(), for example call model.compile_dlogp()({“mu”: 0})

4 Likes

I suggest you update. There were important bugfixes since then.

1 Like