-
-
Notifications
You must be signed in to change notification settings - Fork 393
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
Add support for discrete variables in plot_bpv #1379
Changes from 14 commits
e594039
54adcc8
186b4cd
1fecc13
46a24eb
0bba9a5
4a6bb6c
40ad344
11c8d81
13c9dcc
3c8837b
e35e73b
bffb8a3
976250c
6f28af8
11615e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
import matplotlib.pyplot as plt | ||
import numpy as np | ||
from scipy import stats | ||
from scipy.interpolate import CubicSpline | ||
|
||
from ....stats.density_utils import kde | ||
from ...kdeplot import plot_kde | ||
|
@@ -27,6 +28,7 @@ def plot_bpv( | |
bpv, | ||
plot_mean, | ||
reference, | ||
mse, | ||
n_ref, | ||
hdi_prob, | ||
color, | ||
|
@@ -60,6 +62,9 @@ def plot_bpv( | |
if kind == "p_value" and reference == "analytical": | ||
plot_ref_kwargs.setdefault("color", "k") | ||
plot_ref_kwargs.setdefault("linestyle", "--") | ||
elif kind == "u_value" and reference == "analytical": | ||
plot_ref_kwargs.setdefault("color", "k") | ||
plot_ref_kwargs.setdefault("alpha", 0.2) | ||
else: | ||
plot_ref_kwargs.setdefault("alpha", 0.1) | ||
plot_ref_kwargs.setdefault("color", color) | ||
|
@@ -81,7 +86,17 @@ def plot_bpv( | |
pp_var_name, _, pp_vals = pp_plotters[i] | ||
|
||
obs_vals = obs_vals.flatten() | ||
pp_vals = pp_vals.reshape(total_pp_samples, -1) | ||
if pp_vals.ndim > 2: | ||
pp_vals = pp_vals.reshape(total_pp_samples, -1) | ||
|
||
if obs_vals.dtype.kind == "i" or pp_vals.dtype.kind == "i": | ||
x = np.linspace(0, 1, len(obs_vals)) | ||
csi = CubicSpline(x, obs_vals) | ||
obs_vals = csi(np.linspace(0.001, 0.999, len(obs_vals))) | ||
|
||
x = np.linspace(0, 1, pp_vals.shape[1]) | ||
csi = CubicSpline(x, pp_vals, axis=1) | ||
pp_vals = csi(np.linspace(0.001, 0.999, pp_vals.shape[1])) | ||
|
||
if kind == "p_value": | ||
tstat_pit = np.mean(pp_vals <= obs_vals, axis=-1) | ||
|
@@ -95,32 +110,36 @@ def plot_bpv( | |
upb = 1 - lwb | ||
x = np.linspace(lwb, upb, 500) | ||
dens_ref = dist.pdf(x) | ||
ax_i.plot(x, dens_ref, **plot_ref_kwargs) | ||
ax_i.plot(x, dens_ref, zorder=1, **plot_ref_kwargs) | ||
elif reference == "samples": | ||
x_ss, u_dens = sample_reference_distribution( | ||
dist, | ||
( | ||
n_ref, | ||
tstat_pit_dens.size, | ||
n_ref, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is also needed in bokeh backend |
||
), | ||
) | ||
ax_i.plot(x_ss, u_dens, linewidth=linewidth, **plot_ref_kwargs) | ||
|
||
elif kind == "u_value": | ||
tstat_pit = np.mean(pp_vals <= obs_vals, axis=0) | ||
x_s, tstat_pit_dens = kde(tstat_pit) | ||
ax_i.plot(x_s, tstat_pit_dens, color=color) | ||
if reference is not None: | ||
if reference == "analytical": | ||
n_obs = obs_vals.size | ||
hdi = stats.beta(n_obs / 2, n_obs / 2).ppf((1 - hdi_prob) / 2) | ||
hdi_odds = (hdi / (1 - hdi), (1 - hdi) / hdi) | ||
hdi_ = stats.beta(n_obs / 2, n_obs / 2).ppf((1 - hdi_prob) / 2) | ||
hdi_odds = (hdi_ / (1 - hdi_), (1 - hdi_) / hdi_) | ||
ax_i.axhspan(*hdi_odds, **plot_ref_kwargs) | ||
ax_i.axhline(1, color="w") | ||
ax_i.axhline(1, color="w", zorder=1) | ||
elif reference == "samples": | ||
dist = stats.uniform(0, 1) | ||
x_ss, u_dens = sample_reference_distribution(dist, (tstat_pit_dens.size, n_ref)) | ||
ax_i.plot(x_ss, u_dens, linewidth=linewidth, **plot_ref_kwargs) | ||
ax_i.plot(x_s, tstat_pit_dens, color=color) | ||
if mse: | ||
ax_i.plot(0, 0, label=f"mse={np.mean((1 - tstat_pit_dens)**2) * 100:.2f}") | ||
ax_i.legend() | ||
|
||
ax_i.set_ylim(0, None) | ||
ax_i.set_xlim(0, 1) | ||
else: | ||
|
@@ -147,7 +166,7 @@ def plot_bpv( | |
ax_i.set_yticks([]) | ||
if bpv: | ||
p_value = np.mean(pp_vals <= obs_vals) | ||
ax_i.plot(0, 0, label=f"bpv={p_value:.2f}", alpha=0) | ||
ax_i.plot(obs_vals, 0, label=f"bpv={p_value:.2f}", alpha=0) | ||
ax_i.legend() | ||
|
||
if plot_mean: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1034,3 +1034,15 @@ def test_plot_dist_comparison_warn(models): | |
def test_plot_bpv(models, kwargs): | ||
axes = plot_bpv(models.model_1, backend="bokeh", show=False, **kwargs) | ||
assert axes.shape | ||
|
||
|
||
def test_plot_bpv_discrete(): | ||
fake_obs = {"a": np.random.poisson(2.5, 100)} | ||
fake_pp = {"a": np.random.poisson(2.5, (10, 100))} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a 2 dim array this will be understood as a |
||
fake_model = from_dict(posterior_predictive=fake_pp, observed_data=fake_obs) | ||
axes = plot_bpv( | ||
fake_model, | ||
backend="bokeh", | ||
show=False, | ||
) | ||
assert axes.shape |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will break the ArviZ shape convention and interpret the draw dimension as the observations.