File size: 917 Bytes
eb725f8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | """HDR-style contour levels for 2D probability maps on a grid."""
from __future__ import annotations
import numpy as np
def compute_sigma_levels(
posterior_norm: np.ndarray,
credibility_mass: tuple[float, ...] | list[float],
) -> list[float]:
"""
Highest-density containment: find density thresholds such that descending sort
of mass covers ``credibility_mass[j]`` of total probability.
Returned levels are ascending (suitable order for matplotlib ``contour``).
"""
p = np.asarray(posterior_norm, dtype=np.float64).ravel()
s = p.sum()
if s <= 0:
return [0.0 for _ in credibility_mass]
ps = np.sort((p / s).flatten())[::-1]
cdf = np.cumsum(ps)
out: list[float] = []
for cred in credibility_mass:
j = int(np.searchsorted(cdf, cred, side="left"))
j = min(max(j, 0), len(ps) - 1)
out.append(float(ps[j]))
return sorted(out)
|