How to identify which chain is being used in sample_smc

Hi, I want to identify which chain is being used each time the simulator is run in Approximate Bayesian Computation (using sample_smc). I need this functionality because I want to use an external software to do the calculations. Does anyone know how to do that?

Simplifying a lot, here is what the script would look like:

def simulator(xs1, xs2, xs3, xs4, xs5):
    xs = [xs1, xs2, xs3, xs4, xs5] #searched model parameters
    chain_number = ? # identify chain number
    os.system("mkdir -p chain_"+chain_number) #create a folder with chain number appended
    generate_input(chain_number, xs) #generate input for the external software
    os.system("ansys chain_"+chain_number+"/input.inp") #run the input using external software
    result = read_result(chain_number) # read the result
    os.system("rm -rf chain_"+chain_number) #remove the folder with input file
    return result

bay_scale_model = pm.Model()

with bay_scale_model:
    # Priors for simulator model parameters
    BoundedNormal = pm.Bound(pm.Normal, lower = 0.0, upper = 2.4)
    xs1 = BoundedNormal("xs1", mu=1, sd=0.227689)
    xs2 = BoundedNormal("xs2", mu=1, sd=0.257015)
    xs3 = BoundedNormal("xs3", mu=1, sd=0.309559)
    xs4 = BoundedNormal("xs4", mu=1, sd=0.340311)
    xs5 = BoundedNormal("xs5", mu=1, sd=0.340311)
    sim = pm.Simulator("sim", simulator, params=(xs1, xs2, xs3, xs4, xs5), sum_stat="sort", epsilon=0.003, observed=observed_data)
    trace, sim_data = pm.sample_smc(2000, kernel="ABC", n_steps=20, parallel=True, save_sim_data=True, cores = 4, chains = 4, threshold=0.5)
    idata = az.from_pymc3(trace, posterior_predictive=sim_data)

So I need to know how to find chain_number.

From the top of my head there is no easy way of doing that without changing the source code. You could always run the chains sequentially

1 Like

I can’t run the chains seqentially because it would take too much time, thank you though :slight_smile:

Oh you meant run them seperately from different python scripts and join results afterwards, that will work, thank you!