question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add example to az.plot_hdi that shows how to plot hdi from InferenceData posterior or posterior predictive

See original GitHub issue

Tell us about it

The az.plot_hdi docs only show how to plot the hdi from synthetic data. What would be nice is if it also shows users how to plot the hdi from an az.InferenceData as thats the standard data representation most people will be working with.

image

Thoughts on implementation

Use one of the precomputed datasets that has a posterior predictive group and create a plot.

Here’s an example of loading a dataset with posterior predictive group https://arviz-devs.github.io/arviz/api/generated/arviz.plot_ppc.html

Here’s an (unrelated) example where PyMC3 generates a trace and posterior predictive group and its used with az.plot_hdi

with pm.Model() as model_linear:
    β = pm.Normal('β', sd=10, shape=2)

    μ = pm.Deterministic('μ', pm.math.dot(babies[["Intercept", "Month"]], β))
    ϵ = pm.HalfNormal('ϵ', sd=10)

    length = pm.Normal('length', mu=μ, sd=ϵ, observed=babies.Length)

    linear_trace = pm.sample(2000, tune=4000)
    linear_ppc = pm.sample_posterior_predictive(inf_data_linear)
    inf_data_linear = az.from_pymc3(trace=linear_trace, posterior_predictive= linear_ppc)

fig, ax = plt.subplots()

ax.set_ylabel("Length")
ax.set_xlabel("Month");

μ_m = inf_data_linear.posterior["μ"].values.reshape(-1, babies.Length.shape[0]).mean(axis=0)

ax.plot(babies.Month, μ_m, c='C4')
az.plot_hdi(babies.Month, inf_data_linear.posterior_predictive["length"], hdi_prob=.94, ax=ax)

ax.plot(babies.Month, babies.Length, 'C0.', alpha=0.1)

plt.savefig('img/Baby_Length_Linear_Fit.png', dpi=300)

image image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kenkiritocommented, Feb 4, 2021

Using precomputed data set

0reactions
OriolAbrilcommented, Mar 11, 2021

yes, assigning it to you

Read more comments on GitHub >

github_iconTop Results From Across the Web

Source code for arviz.plots.hdiplot
This example shows how to use :func:`~arviz.hdi` to precalculate the values and ... Here we use the posterior predictive to plot the HDI...
Read more >
Trying to plot posterior Predictive plot using az ...
Hello I am using CmdStanModel using Python. The model has run well and I have all the summary I need. Here is the...
Read more >
arviz.plot_ppc — ArviZ 0.14.0 documentation
InferenceData object containing the observed and posterior/prior predictive data. kind: str. Type of plot to display (“kde”, “cumulative”, or “scatter”).
Read more >
Summarize inference data (HDI) - PyMC Discourse
Hi! When I summarize the statistics of my inference data using az.summary(), one of the columns of the dataframe is hdi_3%.
Read more >
Pymc3 And Arviz: Visualizing Highest Posterior Density For ...
Calculate highest density interval HDI of array for given probability. Examples. Plot HDI interval of simulated regression data using y argument: >>>. Plot...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found