Skip to content

Commit

Permalink
Keep replacing hpd with hdi (#1190)
Browse files Browse the repository at this point in the history
* replace hpd with hdi

* add plot_hdi and update changelog

* skip pydocstyle error
  • Loading branch information
aloctavodia committed May 18, 2020
1 parent 2de3918 commit a6ea9e5
Show file tree
Hide file tree
Showing 24 changed files with 124 additions and 114 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
* plot_ppc animation: improve docs and error handling (#1162)

### Deprecation
* `credible_interval` argument replaced by `hdi_prob`throughout with exception of `plot_loo_pit` (#1176)
* `hpd` function deprecated in favor of `hdi`. `credible_interval` argument replaced by `hdi_prob`throughout with exception of `plot_loo_pit` (#1176)
* `plot_hpd` function deprecated in favor of `plot_hdi`. (#1190)

### Documentation
* Add classifier to `setup.py` including Matplotlib framework (#1133)
Expand Down
3 changes: 2 additions & 1 deletion arviz/plots/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .energyplot import plot_energy
from .essplot import plot_ess
from .forestplot import plot_forest
from .hpdplot import plot_hpd
from .hdiplot import plot_hdi, plot_hpd
from .jointplot import plot_joint
from .kdeplot import plot_kde
from .khatplot import plot_khat
Expand All @@ -32,6 +32,7 @@
"plot_energy",
"plot_ess",
"plot_forest",
"plot_hdi",
"plot_hpd",
"plot_joint",
"plot_kde",
Expand Down
2 changes: 1 addition & 1 deletion arviz/plots/backends/bokeh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def backend_kwarg_defaults(*args, **kwargs):
from .energyplot import plot_energy
from .essplot import plot_ess
from .forestplot import plot_forest
from .hpdplot import plot_hpd
from .hdiplot import plot_hdi
from .jointplot import plot_joint
from .kdeplot import plot_kde
from .khatplot import plot_khat
Expand Down
2 changes: 1 addition & 1 deletion arviz/plots/backends/bokeh/forestplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def forestplot(self, hdi_prob, quartiles, linewidth, markersize, ax, rope):
x=values[mid], y=y, size=markersize * 0.75, fill_color=color,
)
_title = Title()
_title.text = "{:.1%} hdi Interval".format(hdi_prob)
_title.text = "{:.1%} hdi".format(hdi_prob)
ax.title = _title

return ax
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Bokeh hpdplot."""
"""Bokeh hdiplot."""
from itertools import cycle

import bokeh.plotting as bkp
Expand All @@ -9,8 +9,8 @@
from .. import show_layout


def plot_hpd(ax, x_data, y_data, plot_kwargs, fill_kwargs, backend_kwargs, show):
"""Bokeh hpd plot."""
def plot_hdi(ax, x_data, y_data, plot_kwargs, fill_kwargs, backend_kwargs, show):
"""Bokeh hdi plot."""
if backend_kwargs is None:
backend_kwargs = {}

Expand Down
12 changes: 6 additions & 6 deletions arviz/plots/backends/bokeh/loopitplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from . import backend_kwarg_defaults
from .. import show_layout
from ...hpdplot import plot_hpd
from ...hdiplot import plot_hdi
from ...kdeplot import _fast_kde


Expand All @@ -19,10 +19,10 @@ def plot_loo_pit(
p025,
fill_kwargs,
ecdf_fill,
use_hpd,
use_hdi,
x_vals,
unif_densities,
hpd_kwargs,
hdi_kwargs,
n_unif,
unif,
plot_unif_kwargs,
Expand Down Expand Up @@ -114,15 +114,15 @@ def plot_loo_pit(
line_width=plot_unif_kwargs.get("linewidth", 1.0),
)
else:
if use_hpd:
plot_hpd(
if use_hdi:
plot_hdi(
x_vals,
unif_densities,
backend="bokeh",
ax=ax,
backend_kwargs={},
show=False,
**hpd_kwargs
**hdi_kwargs
)
else:
for idx in range(n_unif):
Expand Down
14 changes: 7 additions & 7 deletions arviz/plots/backends/bokeh/posteriorplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,24 @@ def display_point_estimate(max_data):

ax.text(x=[point_value], y=[max_data * 0.8], text=[point_text], text_align="center")

def display_hpd(max_data):
def display_hdi(max_data):
# np.ndarray with 2 entries, min and max
# pylint: disable=line-too-long
hdi_probs = hdi(values, hdi_prob=hdi_prob, multimodal=multimodal) # type: np.ndarray

for hpdi in np.atleast_2d(hdi_probs):
for hdi_i in np.atleast_2d(hdi_probs):
ax.line(
hpdi,
hdi_i,
(max_data * 0.02, max_data * 0.02),
line_width=linewidth * 2,
line_color="black",
)

ax.text(
x=list(hpdi) + [(hpdi[0] + hpdi[1]) / 2],
x=list(hdi_i) + [(hdi_i[0] + hdi_i[1]) / 2],
y=[max_data * 0.07, max_data * 0.07, max_data * 0.3],
text=list(map(str, map(lambda x: round_num(x, round_to), hpdi)))
+ [format_as_percent(hdi_prob) + " HPD"],
text=list(map(str, map(lambda x: round_num(x, round_to), hdi_i)))
+ [format_as_percent(hdi_prob) + " HDI"],
text_align="center",
)

Expand Down Expand Up @@ -254,7 +254,7 @@ def format_axes():
format_axes()
max_data = hist.max()
if hdi_prob != "hide":
display_hpd(max_data)
display_hdi(max_data)
display_point_estimate(max_data)
display_ref_val(max_data)
display_rope(max_data)
2 changes: 1 addition & 1 deletion arviz/plots/backends/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def backend_show(show):
from .energyplot import plot_energy
from .essplot import plot_ess
from .forestplot import plot_forest
from .hpdplot import plot_hpd
from .hdiplot import plot_hdi
from .jointplot import plot_joint
from .kdeplot import plot_kde
from .khatplot import plot_khat
Expand Down
2 changes: 1 addition & 1 deletion arviz/plots/backends/matplotlib/densityplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _d_helper(
markersize : float
Size of markers
hdi_prob : float
hdi intervals. Defaults to 0.94
Probability for the highest density interval. Defaults to 0.94
point_estimate : Optional[str]
Plot point estimate per variable. Values should be 'mean', 'median', 'mode' or None.
Defaults to 'auto' i.e. it falls back to default set in rcParams.
Expand Down
2 changes: 1 addition & 1 deletion arviz/plots/backends/matplotlib/forestplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def forestplot(
color=color,
)
ax.tick_params(labelsize=xt_labelsize)
ax.set_title("{:.1%} hdi Interval".format(hdi_prob), fontsize=titlesize, wrap=True)
ax.set_title("{:.1%} hdi".format(hdi_prob), fontsize=titlesize, wrap=True)
if rope is None or isinstance(rope, dict):
return
elif len(rope) == 2:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Matplotlib hpdplot."""
"""Matplotlib hdiplot."""
import warnings
import matplotlib.pyplot as plt

from . import backend_show


def plot_hpd(ax, x_data, y_data, plot_kwargs, fill_kwargs, backend_kwargs, show):
"""Matplotlib hpd plot."""
def plot_hdi(ax, x_data, y_data, plot_kwargs, fill_kwargs, backend_kwargs, show):
"""Matplotlib hdi plot."""
if backend_kwargs is not None:
warnings.warn(
(
"Argument backend_kwargs has not effect in matplotlib.plot_hpd"
"Argument backend_kwargs has not effect in matplotlib.plot_hdi"
"Supplied value won't be used"
)
)
Expand Down
12 changes: 6 additions & 6 deletions arviz/plots/backends/matplotlib/loopitplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from . import backend_kwarg_defaults, backend_show
from ....numeric_utils import _fast_kde
from ...hpdplot import plot_hpd
from ...hdiplot import plot_hdi


def plot_loo_pit(
Expand All @@ -18,10 +18,10 @@ def plot_loo_pit(
p025,
fill_kwargs,
ecdf_fill,
use_hpd,
use_hdi,
x_vals,
unif_densities,
hpd_kwargs,
hdi_kwargs,
n_unif,
unif,
plot_unif_kwargs,
Expand Down Expand Up @@ -54,8 +54,8 @@ def plot_loo_pit(
else:
ax.plot(unif_ecdf, p975 - unif_ecdf, unif_ecdf, p025 - unif_ecdf, **plot_unif_kwargs)
else:
if use_hpd:
plot_hpd(x_vals, unif_densities, **hpd_kwargs)
if use_hdi:
plot_hdi(x_vals, unif_densities, **hdi_kwargs)
else:
for idx in range(n_unif):
unif_density, _, _ = _fast_kde(unif[idx, :], xmin=0, xmax=1)
Expand All @@ -64,7 +64,7 @@ def plot_loo_pit(

ax.tick_params(labelsize=xt_labelsize)
if legend:
if not (use_hpd or (ecdf and ecdf_fill)):
if not (use_hdi or (ecdf and ecdf_fill)):
label = "{:.3g}% credible interval".format(credible_interval) if ecdf else "Uniform"
ax.plot([], label=label, **plot_unif_kwargs)
ax.legend()
Expand Down
20 changes: 10 additions & 10 deletions arviz/plots/backends/matplotlib/posteriorplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,37 +195,37 @@ def display_point_estimate():
horizontalalignment="center",
)

def display_hpd():
def display_hdi():
# np.ndarray with 2 entries, min and max
# pylint: disable=line-too-long
hdi_probs = hdi(values, hdi_prob=hdi_prob, multimodal=multimodal) # type: np.ndarray

for hpdi in np.atleast_2d(hdi_probs):
for hdi_i in np.atleast_2d(hdi_probs):
ax.plot(
hpdi,
hdi_i,
(plot_height * 0.02, plot_height * 0.02),
lw=linewidth * 2,
color="k",
solid_capstyle="butt",
)
ax.text(
hpdi[0],
hdi_i[0],
plot_height * 0.07,
round_num(hpdi[0], round_to),
round_num(hdi_i[0], round_to),
size=ax_labelsize,
horizontalalignment="center",
)
ax.text(
hpdi[1],
hdi_i[1],
plot_height * 0.07,
round_num(hpdi[1], round_to),
round_num(hdi_i[1], round_to),
size=ax_labelsize,
horizontalalignment="center",
)
ax.text(
(hpdi[0] + hpdi[1]) / 2,
(hdi_i[0] + hdi_i[1]) / 2,
plot_height * 0.3,
format_as_percent(hdi_prob) + " HPD",
format_as_percent(hdi_prob) + " HDI",
size=ax_labelsize,
horizontalalignment="center",
)
Expand Down Expand Up @@ -270,7 +270,7 @@ def format_axes():

format_axes()
if hdi_prob != "hide":
display_hpd()
display_hdi()
display_point_estimate()
display_ref_val()
display_rope()
13 changes: 7 additions & 6 deletions arviz/plots/densityplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def plot_density(
):
"""Generate KDE plots for continuous variables and histograms for discrete ones.
Plots are truncated at their 100*(1-alpha)% hpd intervals. Plots are grouped per variable
and colors assigned to models.
Plots are truncated at their 100*(1-alpha)% highest density intervals. Plots are grouped per
variable and colors assigned to models.
Parameters
----------
Expand All @@ -63,7 +63,8 @@ def plot_density(
transform : callable
Function to transform data (defaults to None i.e. the identity function)
hdi_prob : float
hpd interval. Should be in the interval (0, 1]. Defaults to 0.94.
Probability for the highest density interval. Should be in the interval (0, 1].
Defaults to 0.94.
point_estimate : Optional[str]
Plot point estimate per variable. Values should be 'mean', 'median', 'mode' or None.
Defaults to 'auto' i.e. it falls back to default set in rcParams.
Expand All @@ -75,8 +76,8 @@ def plot_density(
outline : bool
Use a line to draw KDEs and histograms. Default to True
hdi_markers : str
A valid `matplotlib.markers` like 'v', used to indicate the limits of the hpd interval.
Defaults to empty string (no marker).
A valid `matplotlib.markers` like 'v', used to indicate the limits of the highest density
interval. Defaults to empty string (no marker).
shade : Optional[float]
Alpha blending value for the shaded area under the curve, between 0 (no shade) and 1
(opaque). Defaults to 0.
Expand Down Expand Up @@ -132,7 +133,7 @@ def plot_density(
>>> az.plot_density([centered, non_centered], var_names=["mu"], group="prior")
Specify hpd interval
Specify highest density interval
.. plot::
:context: close-figs
Expand Down
Loading

0 comments on commit a6ea9e5

Please sign in to comment.