# Highest Density Regions (HDR) misunderstanding

**URL:** <https://discourse.pymc.io/t/highest-density-regions-hdr-misunderstanding/1364>\
**Category:** Questions\
**Created:** [June 12, 2018, 4:55pm UTC](https://discourse.pymc.io/t/highest-density-regions-hdr-misunderstanding/1364 "2018-06-12T16:55:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![savamirkovic](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@savamirkovic](https://discourse.pymc.io/u/savamirkovic)\
**Post date:** [June 12, 2018, 4:55pm UTC](https://discourse.pymc.io/t/highest-density-regions-hdr-misunderstanding/1364/1 "2018-06-12T16:55:42Z")

</div>

Hi,

I came across pymc3.stats.hpd() as a good solution for obtaining HDR. I have one question:

- How should I interpret the output of pymc3.stats.hpd()? For instance, in my use case, a get array([0.34652132, 1.])

The output for HDR mentioned in the question actually comes from finding HDR on beta distribution with a=0.5, b=0.5. Thus, if the output from the question is correct, than this output does not have any sense to me at the moment since the distribution in my case is bimodal. I should get two intervals, not only one (If the output in my question is considered to be starting and ending points of the HDR interval).

Here is my code if someone is interested:

import pymc3  
from scipy.stats import beta

beta\_dist = beta.rvs(size=100000, a = 0.5, b = 0.5)  
print(pymc3.stats.hpd(beta\_dist, alpha=0.4))

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [June 12, 2018, 7:38pm UTC](https://discourse.pymc.io/t/highest-density-regions-hdr-misunderstanding/1364/2 "2018-06-12T19:38:19Z")

</div>

I think the current implementation only works for single mode marginal, as you observed for bimodal where the modes are far away it will gives incorrect answer.  
I remember seeing a version that works for multiple mode from @aloctavodia, maybe he can provide a bit more information.

---

<div class="post-metadata">

**Author:** ![savamirkovic](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@savamirkovic](https://discourse.pymc.io/u/savamirkovic)\
**Post date:** [June 12, 2018, 8:15pm UTC](https://discourse.pymc.io/t/highest-density-regions-hdr-misunderstanding/1364/3 "2018-06-12T20:15:18Z")

</div>

Thanks @junpenglao for the quick response! 🙂

I hope will get an answer from the @aloctavodia, perhaps he knows a bit more regarding the bimodal/multimodal case, as you said.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [June 12, 2018, 8:24pm UTC](https://discourse.pymc.io/t/highest-density-regions-hdr-misunderstanding/1364/4 "2018-06-12T20:24:35Z")

</div>

This is the one:

> <https://github.com/aloctavodia/BAP/blob/master/code/Chp1/plot_post.py>

It’s from @aloctavodia’s book.

---

<div class="post-metadata">

**Author:** ![savamirkovic](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@savamirkovic](https://discourse.pymc.io/u/savamirkovic)\
**Post date:** [June 13, 2018, 7:03am UTC](https://discourse.pymc.io/t/highest-density-regions-hdr-misunderstanding/1364/5 "2018-06-13T07:03:05Z")

</div>

Thanks a lot @junpenglao for finding this one!

---

<div class="post-metadata">

**Author:** ![rlouf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/rlouf/32/461_2.png) [@rlouf](https://discourse.pymc.io/u/rlouf)\
**Post date:** [June 15, 2018, 6:20am UTC](https://discourse.pymc.io/t/highest-density-regions-hdr-misunderstanding/1364/6 "2018-06-15T06:20:58Z")

</div>

Is it on the roadmap? Or is this kind of thing going into Arviz now?

---

<div class="post-metadata">

**Author:** ![Vicki\_Brown](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vicki_brown/32/2231_2.png) [@Vicki\_Brown](https://discourse.pymc.io/u/Vicki_Brown)\
**Post date:** [September 30, 2019, 8:17pm UTC](https://discourse.pymc.io/t/highest-density-regions-hdr-misunderstanding/1364/7 "2019-09-30T20:17:28Z")

</div>

The code above is truncated. The original is gone.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [October 1, 2019, 10:47am UTC](https://discourse.pymc.io/t/highest-density-regions-hdr-misunderstanding/1364/8 "2019-10-01T10:47:52Z")

</div>

@aloctavodia might have an improved version now, pinging him.

---

<div class="post-metadata">

**Author:** ![aloctavodia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aloctavodia/32/8642_2.png) [@aloctavodia](https://discourse.pymc.io/u/aloctavodia)\
**Post date:** [October 5, 2019, 1:03pm UTC](https://discourse.pymc.io/t/highest-density-regions-hdr-misunderstanding/1364/9 "2019-10-05T13:03:21Z")

</div>

I will add it to ArviZ ASAP. this the new link [https://github.com/aloctavodia/BAP/blob/02d559a664ee7a55ca5d3e8f5af58e49c40dee75/first\_edition/code/Chp1/plot\_post.py](https://github.com/aloctavodia/BAP/blob/02d559a664ee7a55ca5d3e8f5af58e49c40dee75/first_edition/code/Chp1/plot_post.py)

---

<div class="post-metadata">

**Author:** ![aloctavodia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aloctavodia/32/8642_2.png) [@aloctavodia](https://discourse.pymc.io/u/aloctavodia)\
**Post date:** [October 5, 2019, 1:44pm UTC](https://discourse.pymc.io/t/highest-density-regions-hdr-misunderstanding/1364/10 "2019-10-05T13:44:59Z")

</div>

BTW, I think I have a more robust method, but I need to test it to see if actually works as expected.
