# How is the mode point estimate calculated by the \`plot\_posterior\` method?

**URL:** <https://discourse.pymc.io/t/how-is-the-mode-point-estimate-calculated-by-the-plot-posterior-method/5326>\
**Category:** Questions\
**Tags:** arviz\
**Created:** [June 24, 2020, 6:27am UTC](https://discourse.pymc.io/t/how-is-the-mode-point-estimate-calculated-by-the-plot-posterior-method/5326 "2020-06-24T06:27:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrzeliboba](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@mrzeliboba](https://discourse.pymc.io/u/mrzeliboba)\
**Post date:** [June 24, 2020, 6:27am UTC](https://discourse.pymc.io/t/how-is-the-mode-point-estimate-calculated-by-the-plot-posterior-method/5326/1 "2020-06-24T06:27:44Z")

</div>

When plotting the posterior from the trace, you can choose the `point_estimate='mode'`. I also need the value of a mode as a separate statistic, which is unfortunately not incorporated into default statistics of `stats.summary()` method. So, I pass in the `stats_funcs` parameter a wrapper for `scipy.stats.mode` (because by default `scipy.stats.mode` returns additionally number of bins, which has to be disposed). Now, if I compare what value `plot_posterior(point_estimate='mode')` is showing and the value `scipy.stats.mode` is returning, these are two super different in my case. E.g. I get `7.339` for `plot_posterior()` and `scipy.stats.mode()` method returns `29.097`, which is more plausible value.  
Now, question is: how is mode calculated by the `plot_posterior` method, where this discrepancy from `scipy` implementation can come from and what I should rely on?

---

<div class="post-metadata">

**Author:** ![mrzeliboba](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@mrzeliboba](https://discourse.pymc.io/u/mrzeliboba)\
**Post date:** [June 24, 2020, 6:33am UTC](https://discourse.pymc.io/t/how-is-the-mode-point-estimate-calculated-by-the-plot-posterior-method/5326/2 "2020-06-24T06:33:36Z")

</div>

To add, I now have checked, `scipy.stats.mode` is making its calculations based on only 30 bins binning, which is super law value for a range of values my posterior is spanning over. Still it would be interesting to know how mode is calculated inside the PyMC3, how reliable it is and how to calculate it with the tools of PyMC3, rather than third-party libs, which results in such discrepancies.

---

<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 24, 2020, 6:35am UTC](https://discourse.pymc.io/t/how-is-the-mode-point-estimate-calculated-by-the-plot-posterior-method/5326/3 "2020-06-24T06:35:17Z")

</div>

PyMC3 use Arviz which for continuous value use a kde function to smooth out the histogram and get the local maximum:

> <https://github.com/arviz-devs/arviz/blob/1d9297c0f06ceb65211e574338610f14f9d004c9/arviz/plots/plot_utils.py#L646-L648>

It might not work well if you have multi mode but otherwise we think it is quite good in most case

---

<div class="post-metadata">

**Author:** ![mrzeliboba](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@mrzeliboba](https://discourse.pymc.io/u/mrzeliboba)\
**Post date:** [June 24, 2020, 7:20am UTC](https://discourse.pymc.io/t/how-is-the-mode-point-estimate-calculated-by-the-plot-posterior-method/5326/4 "2020-06-24T07:20:15Z")

</div>

Thank you, Junpeng, for such a fast response. Now it have become even more weird, bacause calling `az.plots.plot_utils.calculate_point_estimate('mode', trace['tau'], bw=4.5)` results into `array([29.09679086])` – super close to the value returned by the `scipy.stats.mode`. Yet, `plot_posterior` shows `7.339`. The only parameter which could have effect (`bw` parameter) does not really have effect. So I am now wondering what could be the reason `plot_posterior` is showing such a value of mode.

---

<div class="post-metadata">

**Author:** ![mrzeliboba](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@mrzeliboba](https://discourse.pymc.io/u/mrzeliboba)\
**Post date:** [June 24, 2020, 8:54am UTC](https://discourse.pymc.io/t/how-is-the-mode-point-estimate-calculated-by-the-plot-posterior-method/5326/5 "2020-06-24T08:54:31Z")

</div>

More info: when calculated through passing the `stats_funcs` parameter of the `az.stats.summary()`, it results in the same value of `7.339`, so something happens to the trace on the way, which is not done when calling `az.plots.plot_utils.calculate_point_estimate('mode', trace['tau'], bw=4.5)` directly.

---

<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 24, 2020, 8:59am UTC](https://discourse.pymc.io/t/how-is-the-mode-point-estimate-calculated-by-the-plot-posterior-method/5326/6 "2020-06-24T08:59:47Z")

</div>

hmmm sounds like a bug - could you raise an issue on Arviz?

---

<div class="post-metadata">

**Author:** ![mrzeliboba](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@mrzeliboba](https://discourse.pymc.io/u/mrzeliboba)\
**Post date:** [June 26, 2020, 6:38am UTC](https://discourse.pymc.io/t/how-is-the-mode-point-estimate-calculated-by-the-plot-posterior-method/5326/7 "2020-06-26T06:38:03Z")

</div>

Thank you, I will!

---

<div class="post-metadata">

**Author:** ![marissafichera](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/marissafichera/32/6653_2.png) [@marissafichera](https://discourse.pymc.io/u/marissafichera)\
**Post date:** [August 23, 2023, 5:36pm UTC](https://discourse.pymc.io/t/how-is-the-mode-point-estimate-calculated-by-the-plot-posterior-method/5326/8 "2023-08-23T17:36:12Z")

</div>

Has this been fixed? I’m running into a similar issue
