undesirable behaviour of plot_hdi, and a proposal #2021
Replies: 6 comments 3 replies
-
I think Michael Betancourt also argued against HDIs because they require an estimator over the posterior. |
Beta Was this translation helpful? Give feedback.
-
Maybe try Instead, drawing a You can always overlay HDI bounds or individual percentiles as a dashed line if needed. |
Beta Was this translation helpful? Give feedback.
-
We could also use quantiles (I thought that arviz did that by default): import arviz as az
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
RANDOM_SEED = 1234
rng = np.random.default_rng(RANDOM_SEED)
def f(x, s, c):
return s * np.tanh(x/(s*c))
s = xr.DataArray(data=rng.beta(a=1, b=20, size=(4, 1000)), dims=['chain', 'draw'])
c = xr.DataArray(data=rng.normal(loc=8, scale=3, size=(4, 1000)), dims=['chain', 'draw'])
x = xr.DataArray(data=np.linspace(0, 1, 500), dims='x')
Y = f(x, s, c)
hdi_50 = Y.quantile([0.25, 0.75], dim=["chain", "draw"])
hdi_95 = Y.quantile([0.025, 0.975], dim=["chain", "draw"])
fig, ax =plt.subplots()
ax.fill_between(x, hdi_95.isel(quantile=0), hdi_95.isel(quantile=1), color="C1", alpha=0.5)
ax.fill_between(x, hdi_50.isel(quantile=0), hdi_50.isel(quantile=1), color="C1", alpha=0.5)
ax.plot(x, Y.median(dim=("chain", "draw"))) |
Beta Was this translation helpful? Give feedback.
-
This is the sort of argument that people use to motivate equal-tailed intervals, which are basically just the quantiles that @lucianopaz described. If you poke around, you can find other odd, possibly-undesirable behavior with HDIs (e.g., multi-modality) even though the HDI is always "correct". |
Beta Was this translation helpful? Give feedback.
-
You can already use other estimators for the probability ranges, even if the name seems to indicate the opposite. I do agree we should make it easier or maybe even default to equal tail intervals, but there will always be arguments for both in my opinion and the choice should probably depend on the problem/model. We currently have #1998 open to use the median and eti in the summary for example. As example, change the plotting code to:
so equal tail intervals are used via hdi so you can even take advantage of the smooting if you want. And you can always use |
Beta Was this translation helpful? Give feedback.
-
Thanks for everyone's input on this. I think it could be worth considering @OriolAbril 's suggestion of changing default behaviour. Maybe |
Beta Was this translation helpful? Give feedback.
-
Ok, so I have noticed behaviour of
az.plot_hdi
which I do not believe is a bug as such, but does represent undesirable behaviour.A frequent use of
arviz
is to plot hdi intervals of a function for which you have a set of MCMC samples. Consider this example...Which results in this.
There is a certain point in the x-axis where the hdi interval shifts (around x=0.5). I believe this is because the underlying distribution is asymmetric, and so you can get these discontinuities. So while this is not a bug (it does represent the actual hdi), it doesn't represent desirable behaviour.
So how about a new plot function which derives from
plot_hdi
, but which plots percentiles?Beta Was this translation helpful? Give feedback.
All reactions