Hello pymc community,
Using PyMC4, I am trying to use a custom likelihood wrapped in a python function call (it computes a misfit between simulated data from a physics PDE and observations). It also has several variables. I have not been able to figure out the syntax to do that yet. Below is a minimalistic example of the syntax I have pieced together. The code runs BUT:
-
my output
idata
has no likelihood section, onlyposterior
andsample_stats
-
in the
as_op
, I need to add an additional variable for the name, otherwise the code returns an error
What should I be doing instead to use my custom likelihood, with pymc 4, and multiple variables? A code snippet would be deeply appreciated as I am still new to pymc.
Thanks in advance
import pymc as pm
from aesara.compile.ops import as_op
import numpy as np
import aesara.tensor as at
basic_model = pm.Model()
def custom_log_likelihood(x,y):
# do something, for simplicity here let just put a quadratic so that the code run
#return log_likelihood
return -0.5 * (x**2 + y**2) # placeholder log-likelihood
with basic_model:
# Priors for unknown model parameters
x0p = pm.Normal("x0p", mu=0, sigma=10)
y0p = pm.Normal("y0p", mu=-30, sigma=10)
# I somehow need an additional variable here for the name
@as_op(itypes=[at.dscalar, at.dscalar,at.dscalar], otypes=[at.dscalar])
def logp(name, x0p, y0p):
return custom_log_likelihood_quadratic(x0p, y0p)
likelihood = pm.DensityDist("likelihood",x0p,y0p,logp=logp)
start = pm.find_MAP()
# draw 1000 posterior samples
idata = pm.sample(step=pm.Metropolis(),cores=4, start=start) # 1, tune=1, start=start,