POSTERIOR_TAILPROB

This function calculates posterior exceedance or lower-tail probabilities from discrete posterior support values and associated probabilities.

For support values x_i with normalized posterior probabilities p_i, the exceedance probability above threshold t is:

P(X > t) = \sum_{i: x_i > t} p_i

Alternative tails such as X \ge t, X < t, and X \le t are also available for decision-oriented Bayesian summaries.

Excel Usage

=POSTERIOR_TAILPROB(values, posterior, threshold, tail)
  • values (list[list], required): 2D array of posterior support values.
  • posterior (list[list], required): 2D array of nonnegative posterior probabilities or weights.
  • threshold (float, required): Decision threshold for tail probability evaluation.
  • tail (str, optional, default: “greater”): Tail direction relative to threshold.

Returns (float): Posterior probability mass in the selected tail region.

Example 1: Exceedance probability above threshold

Inputs:

values posterior threshold tail
1 2 3 4 0.1 0.2 0.3 0.4 2.5 greater

Excel formula:

=POSTERIOR_TAILPROB({1,2,3,4}, {0.1,0.2,0.3,0.4}, 2.5, "greater")

Expected output:

0.7

Example 2: Lower-tail probability below threshold

Inputs:

values posterior threshold tail
1 2 3 4 0.1 0.2 0.3 0.4 2.5 less

Excel formula:

=POSTERIOR_TAILPROB({1,2,3,4}, {0.1,0.2,0.3,0.4}, 2.5, "less")

Expected output:

0.3

Example 3: Inclusive upper-tail probability at threshold value

Inputs:

values posterior threshold tail
0 1 2 1 2 3 1 greater_equal

Excel formula:

=POSTERIOR_TAILPROB({0,1,2}, {1,2,3}, 1, "greater_equal")

Expected output:

0.833333

Example 4: Scalar support and posterior with threshold comparison

Inputs:

values posterior threshold tail
5 1 4 greater

Excel formula:

=POSTERIOR_TAILPROB(5, 1, 4, "greater")

Expected output:

1

Python Code

import numpy as np

def posterior_tailprob(values, posterior, threshold, tail='greater'):
    """
    Compute posterior tail probabilities relative to a decision threshold.

    See: https://en.wikipedia.org/wiki/Cumulative_distribution_function

    This example function is provided as-is without any representation of accuracy.

    Args:
        values (list[list]): 2D array of posterior support values.
        posterior (list[list]): 2D array of nonnegative posterior probabilities or weights.
        threshold (float): Decision threshold for tail probability evaluation.
        tail (str, optional): Tail direction relative to threshold. Valid options: Greater Than, Greater Than Or Equal, Less Than, Less Than Or Equal. Default is 'greater'.

    Returns:
        float: Posterior probability mass in the selected tail region.
    """
    try:
        def to2d(v):
            return [[v]] if not isinstance(v, list) else v

        values = to2d(values)
        posterior = to2d(posterior)

        if not isinstance(values, list) or not all(isinstance(row, list) for row in values):
            return "Error: values must be a 2D list"
        if not isinstance(posterior, list) or not all(isinstance(row, list) for row in posterior):
            return "Error: posterior must be a 2D list"

        value_flat = []
        for row in values:
            for value in row:
                try:
                    value_flat.append(float(value))
                except (TypeError, ValueError):
                    continue

        posterior_flat = []
        for row in posterior:
            for prob in row:
                try:
                    posterior_flat.append(float(prob))
                except (TypeError, ValueError):
                    continue

        if len(value_flat) == 0:
            return "Error: values must contain at least one numeric value"
        if len(posterior_flat) == 0:
            return "Error: posterior must contain at least one numeric value"
        if len(value_flat) != len(posterior_flat):
            return "Error: values and posterior must contain the same number of numeric entries"

        posterior_arr = np.array(posterior_flat, dtype=float)
        if np.any(posterior_arr < 0):
            return "Error: posterior values must be nonnegative"

        total_prob = float(np.sum(posterior_arr))
        if total_prob <= 0:
            return "Error: posterior sum must be positive"

        posterior_arr = posterior_arr / total_prob
        value_arr = np.array(value_flat, dtype=float)
        threshold = float(threshold)

        if tail == "greater":
            mask = value_arr > threshold
        elif tail == "greater_equal":
            mask = value_arr >= threshold
        elif tail == "less":
            mask = value_arr < threshold
        elif tail == "less_equal":
            mask = value_arr <= threshold
        else:
            return "Error: tail must be one of greater, greater_equal, less, less_equal"

        return float(np.sum(posterior_arr[mask]))
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

2D array of posterior support values.
2D array of nonnegative posterior probabilities or weights.
Decision threshold for tail probability evaluation.
Tail direction relative to threshold.