Skip to content

Instantly share code, notes, and snippets.

@grinnbearit
grinnbearit / credible_interval.py
Last active September 20, 2021 19:34
Credible interval of 1 dimensional distributions in Python
def credible_interval(xlim, ys, mass=0.95):
"""Given a range, like (0, 20), and the y values for the probability density function,
returns the set of intervals spanning the minimum range to sum up to the mass."""
indexed = zip(ys, range(len(ys)))
indexed.sort(reverse=True)
acc = 0
points = []
for (y, x) in indexed: