from typing import OrderedDict


def posterior_credible_interval(variations, level=95):
    """Calculate posterior P(parameter | data) credible interval for each
    variation.

    Returns a 3-tuple (lower, median, upper)
    """

    values = OrderedDict()
    for label, variation in variations.items():
        left_percentile = 0.5*(100-level)/100
        left = variation.posterior.ppf(left_percentile)
        median = variation.posterior.ppf(0.5)
        right = variation.posterior.ppf((level+left_percentile)/100)
        values[label] = min(left, right), median, max(left, right)

    return values