Access values of the parameters at each draw

I am using PyMC for the first time and this is my model:

with pm.Model() as model:

**p1 = pm.Uniform('parameter1', lower=1, upper=100)**
**p2 = pm.Uniform('parameter2', lower=0.01), upper=1)**

**value=model_function(p1, p2)**
**likelihood = pm.Normal('likelihood', mu=value, sigma=0.01, observed=observed_data)**

**trace = pm.sample(5000,tune=800,chains=4,cores=4,target_accept=0.95)**

My questions comes from the fact that in the function model_function I need the current values of p1 and p2 (meaning, the values that are being sampled at each draw). This is because I am writing each of them in another program in C that has to run and return the values to be compared with the observed_data. So, is that possible? Can I access each value for p1 and p2 that is being used to provide it to my other program?

Do you need to be running the other program in real-time during sampling? If not, it would be easier to just save your posterior trace as data and load it into your other program after you’re done sampling.

It should be possible to do stuff at each MCMC iteration by writing a custom step function; see here. @ricardoV94 would know more. But I think you might be doing too much work for what you really need at that point.

Sounds like you want to do something like the blackbox likelihood example: Using a “black box” likelihood function (numpy) — PyMC example gallery

Thanks! That’s what I need.

I tried at first using the posterior, but what I really need is to run my C program at the same time. But thanks anyway!