How to select some chains and plot them

Sorry, I am new here.
I have sampled many chains and I want to plot one of the chains.

Blockquote
trace = pm.sample(2000, tune=2000,nchains=4,njobs=8)
pm.traceplot(trace)

What should I do?
Thanks.

Honesty I do not know! I tried just now and couldn’t figure it out. But in the meantime you could combine them (as this is usually what I want to know anyways-the aggregate of all the samples) if this is a suitable work around for your problem.

pm.traceplot(trace, combined=True)

or my favorite:

pm.traceplot(trace, varnames=['yourvar','yourvar2'], combined=True, priors=[youvar.distribtion, yourvar2.distribution ]
1 Like

I agree with Brian, usually you want the combination of all chains, so combined=True is your man :wink:
Note that PyMC’s plotting is based on ArviZ. You’ll find more doc on their website and have more control by using it directly

Thanks for your suggestion.
I find my model not so stable, maybe because of the prior. In about five chains, maybe only one or two converge. So I have to choose the chain manually to see the result.
Right now, I just sample a chain at a time and fortunately find out the convergent chains show similar results. But I am open to an easier way.

1 Like

Thanks.
I find my model not so stable, maybe because of the prior. In about five chains, maybe only one or two converge. So I have to choose the chain manually to see the result.

I found the trace a MultiTrace object and tried to get a slice or delete some chains, but failed:pensive:.

1 Like

If only 1 or 2 out of 5 chains converge, you probably have a problem with your model.
If you don’t know why, I think you should also inspect the diverging chains, to see where the divergences are. That’s usually more informative than looking at the convergent chains – in these situations your convergent chains may be biased, even if they converged.

1 Like

I agree with @AlexAndorra. You might want to check your model again to make sure it is doing what you think it is doing. Also, you may need to reparameterize (change how the sampler “sees” the model, but leaving it mathematically equivalent).

2 Likes

Chiming in to agree with @Firgicutte. I’d like select from many chains and end up with a MultiTrace object of only the selected chains.

You should use InferenceData instead of MultiTrace for this. It has many more features too in addition to better subsetting. This page covers how to select a subset of the chains: Working with InferenceData — ArviZ dev documentation. You get an inferencedata again that you can then pass to plotting functions.

I also have a blogpost on making the most of the integration between pymc v3 and arviz: PyMC3 with labeled coords and dims | Oriol unraveled that I think will be useful too. The integration between arviz and pymc v4 (currently on beta release) is a bit different (aka even better!) but the subsetting and plotting once you have the inferencedata is version agnostic.