Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BF plot: Use arviz's kde instead of the one from scipy #2237

Merged
merged 3 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### New features

- Bayes Factor plot: Use arviz's kde instead of the one from scipy ([2237](https://github.com/arviz-devs/arviz/pull/2237))

### Maintenance and fixes

### Deprecation
Expand Down
28 changes: 16 additions & 12 deletions arviz/plots/backends/matplotlib/bfplot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import matplotlib.pyplot as plt
import numpy as np

from . import backend_kwarg_defaults, backend_show, create_axes_grid, matplotlib_kwarg_dealiaser
from ...distplot import plot_dist
Expand All @@ -10,11 +9,8 @@ def plot_bf(
ax,
bf_10,
bf_01,
xlim,
prior,
posterior,
prior_pdf,
posterior_pdf,
ref_val,
prior_at_ref_val,
posterior_at_ref_val,
Expand Down Expand Up @@ -52,14 +48,22 @@ def plot_bf(
if ax is None:
_, ax = create_axes_grid(1, backend_kwargs=backend_kwargs)

x = np.linspace(*xlim, 5000)

if posterior.dtype.kind == "f":
ax.plot(x, prior_pdf(x), color=colors[0], label="Prior", **plot_kwargs)
ax.plot(x, posterior_pdf(x), color=colors[1], label="Posterior", **plot_kwargs)
elif posterior.dtype.kind == "i":
plot_dist(prior, color=colors[0], label="Prior", ax=ax, hist_kwargs=hist_kwargs)
plot_dist(posterior, color=colors[1], label="Posterior", ax=ax, hist_kwargs=hist_kwargs)
plot_dist(
prior,
color=colors[0],
label="Prior",
ax=ax,
plot_kwargs=plot_kwargs,
hist_kwargs=hist_kwargs,
)
plot_dist(
posterior,
color=colors[1],
label="Posterior",
ax=ax,
plot_kwargs=plot_kwargs,
hist_kwargs=hist_kwargs,
)

ax.plot(ref_val, posterior_at_ref_val, "ko", lw=1.5)
ax.plot(ref_val, prior_at_ref_val, "ko", lw=1.5)
Expand Down
28 changes: 9 additions & 19 deletions arviz/plots/bfplot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Plotting and reporting Bayes Factor given idata, var name, prior distribution and reference value
# pylint: disable=unbalanced-tuple-unpacking
import logging

from scipy.stats import gaussian_kde
from numpy import interp

from ..data.utils import extract
from .plot_utils import get_plotting_function
from ..stats.density_utils import _kde_linear

_log = logging.getLogger(__name__)

Expand All @@ -14,7 +16,6 @@ def plot_bf(
var_name,
prior=None,
ref_val=0,
xlim=None,
colors=("C0", "C1"),
figsize=None,
textsize=None,
Expand Down Expand Up @@ -47,8 +48,6 @@ def plot_bf(
In case we want to use different prior, for example for sensitivity analysis.
ref_val : int, default 0
Point-null for Bayes factor estimation.
xlim : tuple, optional
Set the x limits, which might be used for visualization purposes.
colors : tuple, default ('C0', 'C1')
Tuple of valid Matplotlib colors. First element for the prior, second for the posterior.
figsize : (float, float), optional
Expand Down Expand Up @@ -94,7 +93,7 @@ def plot_bf(
... prior={"a":np.random.normal(0, 1, 5000)})
>>> az.plot_bf(idata, var_name="a", ref_val=0)
"""
posterior = extract(idata, var_names=var_name)
posterior = extract(idata, var_names=var_name).values

if ref_val > posterior.max() or ref_val < posterior.min():
_log.warning(
Expand All @@ -106,21 +105,15 @@ def plot_bf(
_log.warning("Posterior distribution has {posterior.ndim} dimensions")

if prior is None:
prior = extract(idata, var_names=var_name, group="prior")

if xlim is None:
xlim = (prior.min(), prior.max())
prior = extract(idata, var_names=var_name, group="prior").values

if posterior.dtype.kind == "f":
posterior_pdf = gaussian_kde(posterior)
prior_pdf = gaussian_kde(prior)

posterior_at_ref_val = posterior_pdf(ref_val)
prior_at_ref_val = prior_pdf(ref_val)
posterior_grid, posterior_pdf = _kde_linear(posterior)
prior_grid, prior_pdf = _kde_linear(prior)
posterior_at_ref_val = interp(ref_val, posterior_grid, posterior_pdf)
prior_at_ref_val = interp(ref_val, prior_grid, prior_pdf)

elif posterior.dtype.kind == "i":
prior_pdf = None
posterior_pdf = None
posterior_at_ref_val = (posterior == ref_val).mean()
prior_at_ref_val = (prior == ref_val).mean()

Expand All @@ -131,11 +124,8 @@ def plot_bf(
ax=ax,
bf_10=bf_10.item(),
bf_01=bf_01.item(),
xlim=xlim,
prior=prior,
posterior=posterior,
prior_pdf=prior_pdf,
posterior_pdf=posterior_pdf,
ref_val=ref_val,
prior_at_ref_val=prior_at_ref_val,
posterior_at_ref_val=posterior_at_ref_val,
Expand Down