Tracking an externally calculated value in the trace

Hello PyMC community.

May I please get your advice on how the script below can be modified to include keyvalue in the trace? keyvalue is not a prior nor a likelihood - it is just a prediction value I would like to track.

def my_blackbox(m, c):
    likelihood, keyvalue = run_my_external_program(m, c)
    return likelihood

class my_tt(tt.Op):
    itypes = [tt.dvector]
    otypes = [tt.dscalar]

    def __init__(self, likelihood):
        self.likelihood = likelihood
    def perform(self, node, inputs, outputs):
        theta, = inputs
        m, c = theta
        outputs[0][0] = np.array(self.likelihood(m, c))

def like_fun(v):
    return like_tt(v)

global like_tt
like_tt = my_tt(my_blackbox)

with pm.Model():
    m = pm.Normal('m', mu = 0, sigma = 1)
    c = pm.Normal('c', mu = 0, sigma = 1)
    params = tt.as_tensor_variable([m, c])
    pm.DensityDist('likelihood', like_tt, observed = {'v':params})
    trace = pm.sample(500)

Many thanks in advance.

Hi! From what I understand, keyvalue is not in your PyMC model, is it? So it’s not supposed to depend on sampling?

Thank you very much for the reply. Yes, it is a bit similar to pm.deterministic(), in that keyvalue is completely dependent on the values of m and c through my external program. Syntax-wise I am a bit uncertain on how to incorporate keyvalue into the pymc workflow (e.g. so that I can include keyvalue in pm.traceplot) through the theano Op. Any advice on this matter would be greatly appreciated.

Mmmh ok, I think I understand.

  • If keyvalue is entirely dependent on theano variables from your model, I think you can just use pm.Deterministic.
  • Otherwise, maybe you can try something like trace.add_values({"Keyvalue": keyvalue}) after you model ran. And then keyvalue should be available in your trace, under the name “Keyvalue” (assuming keyvalue is something like a numpy array).

Hope that helps!

Thank you, I will give that a go.