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

Add optimized simultaneous ECDF bands #2368

Merged
merged 17 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v0.x.x Unreleased

### New features
- Add optimized simultaneous ECDF confidence bands ([2368](https://github.com/arviz-devs/arviz/pull/2368))

### Maintenance and fixes

Expand Down
19 changes: 11 additions & 8 deletions arviz/plots/ecdfplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def plot_ecdf(
- False: No confidence bands are plotted (default).
- True: Plot bands computed with the default algorithm (subject to change)
- "pointwise": Compute the pointwise (i.e. marginal) confidence band.
- "optimized": Use optimization to estimate a simultaneous confidence band.
- "simulated": Use Monte Carlo simulation to estimate a simultaneous confidence
band.

Expand Down Expand Up @@ -216,8 +217,7 @@ def plot_ecdf(
>>> pit_vals = distribution.cdf(sample)
>>> uniform_dist = uniform(0, 1)
>>> az.plot_ecdf(
>>> pit_vals, cdf=uniform_dist.cdf,
>>> rvs=uniform_dist.rvs, confidence_bands=True
>>> pit_vals, cdf=uniform_dist.cdf, confidence_bands=True,
>>> )

Plot an ECDF-difference plot of PIT values.
Expand All @@ -226,8 +226,8 @@ def plot_ecdf(
:context: close-figs

>>> az.plot_ecdf(
>>> pit_vals, cdf = uniform_dist.cdf, rvs = uniform_dist.rvs,
>>> confidence_bands = True, difference = True
>>> pit_vals, cdf = uniform_dist.cdf, confidence_bands = True,
>>> difference = True
>>> )
"""
if confidence_bands is True:
Expand All @@ -238,9 +238,12 @@ def plot_ecdf(
)
confidence_bands = "pointwise"
else:
confidence_bands = "simulated"
elif confidence_bands == "simulated" and pointwise:
raise ValueError("Cannot specify both `confidence_bands='simulated'` and `pointwise=True`")
confidence_bands = "optimized"
# if pointwise especified, confidence_bands must be a bool or 'pointwise'
elif confidence_bands not in [False, "pointwise"] and pointwise:
raise ValueError(
f"Cannot specify both `confidence_bands='{confidence_bands}'` and `pointwise=True`"
)

if fpr is not None:
warnings.warn(
Expand Down Expand Up @@ -298,7 +301,7 @@ def plot_ecdf(
"`eval_points` explicitly.",
BehaviourChangeWarning,
)
if confidence_bands == "simulated":
if confidence_bands in ["optimized", "simulated"]:
warnings.warn(
"For simultaneous bands to be correctly calibrated, specify `eval_points` "
"independent of the `values`"
Expand Down
96 changes: 94 additions & 2 deletions arviz/stats/ecdf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
from scipy.stats import uniform, binom
from scipy.optimize import minimize_scalar


def compute_ecdf(sample: np.ndarray, eval_points: np.ndarray) -> np.ndarray:
Expand Down Expand Up @@ -73,7 +74,7 @@
eval_points: np.ndarray,
cdf_at_eval_points: np.ndarray,
prob: float = 0.95,
method="simulated",
method="optimized",
**kwargs,
) -> Tuple[np.ndarray, np.ndarray]:
"""Compute the `prob`-level confidence band for the ECDF.
Expand All @@ -92,6 +93,7 @@
method : string, default "simulated"
The method used to compute the confidence band. Valid options are:
- "pointwise": Compute the pointwise (i.e. marginal) confidence band.
- "optimized": Use optimization to estimate a simultaneous confidence band.
- "simulated": Use Monte Carlo simulation to estimate a simultaneous confidence band.
`rvs` must be provided.
rvs: callable, optional
Expand All @@ -115,12 +117,18 @@

if method == "pointwise":
prob_pointwise = prob
elif method == "optimized":
prob_pointwise = _optimize_simultaneous_ecdf_band_probability(
ndraws, eval_points, cdf_at_eval_points, prob=prob, **kwargs
)
elif method == "simulated":
prob_pointwise = _simulate_simultaneous_ecdf_band_probability(
ndraws, eval_points, cdf_at_eval_points, prob=prob, **kwargs
)
else:
raise ValueError(f"Unknown method {method}. Valid options are 'pointwise' or 'simulated'.")
raise ValueError(

Check warning on line 129 in arviz/stats/ecdf_utils.py

View check run for this annotation

Codecov / codecov/patch

arviz/stats/ecdf_utils.py#L129

Added line #L129 was not covered by tests
f"Unknown method {method}. Valid options are 'pointwise', 'optimized', or 'simulated'."
)

prob_lower, prob_upper = _get_pointwise_confidence_band(
prob_pointwise, ndraws, cdf_at_eval_points
Expand All @@ -129,6 +137,90 @@
return prob_lower, prob_upper


def _update_ecdf_band_interior_probabilities(
prob_left: np.ndarray,
interval_left: np.ndarray,
interval_right: np.ndarray,
cdf_left: float,
cdf_right: float,
ndraws: int,
) -> np.ndarray:
"""Update the probability that an ECDF has been within the envelope including at the current
point.

Arguments
---------
prob_left : np.ndarray
For each point in the interior at the previous point, the joint probability that it and all
points before are in the interior.
interval_left : np.ndarray
The set of points in the interior at the previous point.
interval_right : np.ndarray
The set of points in the interior at the current point.
cdf_left : float
The CDF at the previous point.
cdf_right : float
The CDF at the current point.
ndraws : int
Number of draws in the original dataset.

Returns
-------
prob_right : np.ndarray
For each point in the interior at the current point, the joint probability that it and all
previous points are in the interior.
"""
z_tilde = (cdf_right - cdf_left) / (1 - cdf_left)
interval_left = interval_left[:, np.newaxis]
prob_conditional = binom.pmf(interval_right, ndraws - interval_left, z_tilde, loc=interval_left)
prob_right = prob_left.dot(prob_conditional)
return prob_right


def _ecdf_band_optimization_objective(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should ideally be jitted with numba if available for a likely significant speed-up. Currently the problem is that the functions it calls use scipy.stats.binom.interval and scipy.stats.binom.pmf, which are not JIT-able. If we reimplement these functions here, then we could probably get a significant speed-up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interval calls ppf, which defers to Boost's implementation, which in turn uses TOMS Algorithm 748 to find a root in an interval: https://doi.org/10.1145/210089.210111, so reimplementing that to be JIT-able would be the bottleneck. Which seems like it could be a lot of work.

Copy link
Member Author

@sethaxen sethaxen Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah wait, since interval is called outside the for loop and only pmf is called within, this could be simple.

@conditional_jit doesn't seem to like it if I call another conditionally-jitted function within the first conditionally-jitted function, e.g.

E           numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
E           Untyped global name '_update_ecdf_band_interior_probabilities': Cannot determine Numba type of <class 'arviz.utils.maybe_numba_fn'>

Is there a way to make this work?

prob_pointwise: float,
cdf_at_eval_points: np.ndarray,
ndraws: int,
prob_target: float,
) -> float:
"""Objective function for optimizing the simultaneous confidence band probability."""
lower, upper = _get_pointwise_confidence_band(prob_pointwise, ndraws, cdf_at_eval_points)
lower_count = (lower * ndraws).astype(int)
upper_count = (upper * ndraws).astype(int) + 1

interval_left = np.zeros(1)
cdf_left = 0
prob_interior = np.ones(1)
for i in range(cdf_at_eval_points.shape[0]):
interval_right = np.arange(lower_count[i], upper_count[i])
cdf_right = cdf_at_eval_points[i]
prob_interior = _update_ecdf_band_interior_probabilities(
prob_interior, interval_left, interval_right, cdf_left, cdf_right, ndraws
)
interval_left = interval_right
cdf_left = cdf_right
prob_interior = prob_interior.sum()
return abs(prob_interior - prob_target)


def _optimize_simultaneous_ecdf_band_probability(
ndraws: int,
eval_points: np.ndarray, # pylint: disable=unused-argument
cdf_at_eval_points: np.ndarray,
prob: float = 0.95,
**kwargs, # pylint: disable=unused-argument
):
"""Estimate probability for simultaneous confidence band using optimization.

This function simulates the pointwise probability needed to construct pointwise confidence bands
that form a `prob`-level confidence envelope for the ECDF of a sample.
"""
cdf_at_eval_points = np.unique(cdf_at_eval_points)
objective = lambda p: _ecdf_band_optimization_objective(p, cdf_at_eval_points, ndraws, prob)
prob_pointwise = minimize_scalar(objective, bounds=(prob, 1), method="bounded").x
return prob_pointwise


def _simulate_simultaneous_ecdf_band_probability(
ndraws: int,
eval_points: np.ndarray,
Expand Down
4 changes: 3 additions & 1 deletion arviz/tests/base_tests/test_plots_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ def test_plot_ecdf_eval_points():
assert axes is not None


@pytest.mark.parametrize("confidence_bands", [True, "pointwise", "simulated"])
@pytest.mark.parametrize("confidence_bands", [True, "pointwise", "optimized", "simulated"])
def test_plot_ecdf_confidence_bands(confidence_bands):
"""Check that all confidence_bands values correctly accepted"""
data = np.random.randn(4, 1000)
Expand Down Expand Up @@ -1326,6 +1326,8 @@ def test_plot_ecdf_error():
# contradictory confidence band types
with pytest.raises(ValueError):
plot_ecdf(data, cdf=dist.cdf, confidence_bands="simulated", pointwise=True)
with pytest.raises(ValueError):
plot_ecdf(data, cdf=dist.cdf, confidence_bands="optimized", pointwise=True)
plot_ecdf(data, cdf=dist.cdf, confidence_bands=True, pointwise=True)
plot_ecdf(data, cdf=dist.cdf, confidence_bands="pointwise")

Expand Down
2 changes: 1 addition & 1 deletion arviz/tests/base_tests/test_stats_ecdf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_get_pointwise_confidence_band(dist, prob, ndraws, num_trials=1_000, see
ids=["continuous", "continuous default rvs", "discrete"],
)
@pytest.mark.parametrize("ndraws", [10_000])
@pytest.mark.parametrize("method", ["pointwise", "simulated"])
@pytest.mark.parametrize("method", ["pointwise", "optimized", "simulated"])
def test_ecdf_confidence_band(dist, rvs, prob, ndraws, method, num_trials=1_000, seed=57):
"""Test test_ecdf_confidence_band."""
eval_points = np.linspace(*dist.interval(0.99), 10)
Expand Down