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

[WIP] Add a parameter to enable/disable smoothing on discrete variables in BLV (Issue #2325) #2344

Merged
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
7 changes: 4 additions & 3 deletions arviz/plots/backends/bokeh/bpvplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def plot_bpv(
plot_ref_kwargs,
backend_kwargs,
show,
smoothing,
):
"""Bokeh bpv plot."""
if backend_kwargs is None:
Expand Down Expand Up @@ -90,6 +91,9 @@ def plot_bpv(
obs_vals = obs_vals.flatten()
pp_vals = pp_vals.reshape(total_pp_samples, -1)

if (obs_vals.dtype.kind == "i" or pp_vals.dtype.kind == "i") and smoothing is True:
obs_vals, pp_vals = smooth_data(obs_vals, pp_vals)

if kind == "p_value":
tstat_pit = np.mean(pp_vals <= obs_vals, axis=-1)
x_s, tstat_pit_dens = kde(tstat_pit)
Expand All @@ -115,9 +119,6 @@ def plot_bpv(
)

elif kind == "u_value":
if obs_vals.dtype.kind == "i" or pp_vals.dtype.kind == "i":
obs_vals, pp_vals = smooth_data(obs_vals, pp_vals)

tstat_pit = np.mean(pp_vals <= obs_vals, axis=0)
x_s, tstat_pit_dens = kde(tstat_pit)
ax_i.line(x_s, tstat_pit_dens, color=color)
Expand Down
3 changes: 2 additions & 1 deletion arviz/plots/backends/matplotlib/bpvplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def plot_bpv(
plot_ref_kwargs,
backend_kwargs,
show,
smoothing,
):
"""Matplotlib bpv plot."""
if backend_kwargs is None:
Expand Down Expand Up @@ -87,7 +88,7 @@ def plot_bpv(
obs_vals = obs_vals.flatten()
pp_vals = pp_vals.reshape(total_pp_samples, -1)

if obs_vals.dtype.kind == "i" or pp_vals.dtype.kind == "i":
if (obs_vals.dtype.kind == "i" or pp_vals.dtype.kind == "i") and smoothing is True:
obs_vals, pp_vals = smooth_data(obs_vals, pp_vals)

if kind == "p_value":
Expand Down
11 changes: 10 additions & 1 deletion arviz/plots/bpvplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def plot_bpv(
bpv=True,
plot_mean=True,
reference="analytical",
smoothing=None,
mse=False,
n_ref=100,
hdi_prob=0.94,
Expand Down Expand Up @@ -72,6 +73,9 @@ def plot_bpv(
reference : {"analytical", "samples", None}, default "analytical"
How to compute the distributions used as reference for ``kind=u_values``
or ``kind=p_values``. Use `None` to not plot any reference.
smoothing : bool, optional
If True and the data has integer dtype, smooth the data before computing the p-values,
u-values or tstat. By default, True when `kind` is "u_value" and False otherwise.
mse : bool, default False
Show scaled mean square error between uniform distribution and marginal p_value
distribution.
Expand Down Expand Up @@ -166,7 +170,8 @@ def plot_bpv(
Notes
-----
Discrete data is smoothed before computing either p-values or u-values using the
function :func:`~arviz.smooth_data`
function :func:`~arviz.smooth_data` if the data is integer type
and the smoothing parameter is True.

Examples
--------
Expand Down Expand Up @@ -206,6 +211,9 @@ def plot_bpv(
elif not 1 >= hdi_prob > 0:
raise ValueError("The value of hdi_prob should be in the interval (0, 1]")

if smoothing is None:
smoothing = kind.lower() == "u_value"

if data_pairs is None:
data_pairs = {}

Expand Down Expand Up @@ -291,6 +299,7 @@ def plot_bpv(
plot_ref_kwargs=plot_ref_kwargs,
backend_kwargs=backend_kwargs,
show=show,
smoothing=smoothing,
)

# TODO: Add backend kwargs
Expand Down