Custom "Black box" likelihood example not working

Hello,
This may be a silly question, but I’m quite new to this software and can’t get around this issue.
I’m trying to set up an mcmc code for which I need a custom likelihood, that is defined as a complicated external function, that involves ffts, integrals, etc. As I understood, the best choice is to define a class based on a Theano Op. I’m now following the “Black box” example for a Gaussian distribution, just to be sure that everything is set up. But it’s not working. This is my code, it is really simple:

alpha, sigma = 5, 1
size = 10000
# Simulate outcome variable
Y = alpha + np.random.randn(size)*sigma

def my_loglike(theta):
    alpha,sigma = theta
    return -(0.5/sigma**2)*np.sum((alpha - Y)**2)

class LogLike(tt.Op):
    itypes = [tt.dvector] # expects a vector of parameter values when called
    otypes = [tt.dscalar] # outputs a single scalar value (the log likelihood)

    def __init__(self, loglike):
        self.likelihood = loglike

    def perform(self, node, inputs, outputs):
        # the method that is used when calling the Op
        theta, = inputs  # this will contain my variables
        # call the log-likelihood function
        logl = self.likelihood(theta)
        outputs[0][0] = np.array(logl) # output the log-likelihood

loglik = LogLike(my_loglike)

with pm.Model() as model:
    alpha = pm.Uniform('alpha', 0, 25)
    sigma = pm.Uniform('sigma', 0.1, 2)

    # Create likelihood
    theta = tt.as_tensor_variable([alpha, sigma])
    # use a DensityDist (use a lamdba function to "call" the Op)
    pm.DensityDist('likelihood', lambda v: loglik(v), observed={'v': theta})
    trace = pm.sample(1000)

The error that I’m getting is the following. Looking for it on the internet, I haven’t found anything useful
MissingInputError: Input 0 of the graph (indices start from 0), used to compute sigmoid(sigma_interval__), was not provided and not given a value. Use the Theano flag exception_verbosity='high', for more information on this error.

This discussion may help: Blackbox likelihood example doesn't work

1 Like

Thank you very much, it helped me a lot. I’ve solved this problem downgrading arviz.