I'm doing some Bayesian modelling in pymc3 and would like to plot the posterior distribution using plot_posterior (which comes from the arviz package). The resulting plot is awkwardly misaligned on the horizontal axis, and I would like to shift it over to be plotted precisely between -3 and +3. Unfortunately I can't work out what I should pass to the function to specify this.
The documentation for arviz.plot_posterior specifies the argument "coords" has the definition "Coordinates of var_names to be plotted. Passed to Dataset.sel" Presumably this is what I need to specify the range for the horizontal axis, but it doesn't tell me what sort of value it expects.
I have checked the documentation for Dataset.sel and it states that the first argument it expects is "A dict with keys matching dimensions and values given by scalars, slices or arrays of tick labels." My interpretation of this is that the keys are strings matching the name(s) of variable(s) and the values are some iterable structure of tickmarks.
My variable is called 'm' and was produced from the following code:
with pymc3.Model() as m1:
m = pymc3.Normal('m', mu = 0, sigma = 1)
obs = pymc3.Normal('obs', mu = m, sigma = 1, observed = numpy.random.randn(3))
trace = pymc3.sample(1000, tune = 500, cores = 1)
My guess at what plot_posterior expects is this:
plot_posterior(trace, coords = {'m': [-3.0, -2,0, -1,0, 0.0, 1.0, 2.0, 3.0]})
It gave me the error "ValueError: dimensions or multi-index levels ['m'] do not exist"
Presumably I'm on the right track but I just can't dig up any more precise definition of what arguments this function needs. Thanks for any help you can provide.
edit: I've worked out how to extend the axes themselves (the trick is ax = mpl.pyplot.axes(xlim = (-3.0, 3.0))) but I still don't know how to extend the plotting of the variable itself.
This is actually something you can go straight to matplotlib for: pm.plot_posterior
will return an axis, which has getters and setters for most display attributes:
ax, = pymc3.plot_posterior(trace)
ax.set_xlim(-3, 3)
The coords
argument is for multi-dimensional random variables. If your model looked like this:
with pymc3.Model() as m1:
m = pymc3.Normal('m', mu = 0, sigma = 1, shape = 4)
obs = pymc3.Normal('obs', mu = m, sigma = 1, observed = numpy.random.randn(3, 4))
trace = pymc3.sample(1000, tune = 500, cores = 1)
then your plot will have all 4 dimensions of m
:
pymc3.plot_posterior(trace)
You can use coords
to cut that down:
pymc3.plot_posterior(trace, coords={'m_dim_0': [0, 2]})