DM_CRED_INT
This function computes marginal credible intervals for each category probability under a Dirichlet posterior. Each category marginal follows a Beta distribution with parameters derived from the full Dirichlet vector.
For posterior parameters \boldsymbol{\alpha}' and category i, the marginal is:
\theta_i \sim \mathrm{Beta}\left(\alpha_i', \alpha_0' - \alpha_i'\right), \qquad \alpha_0' = \sum_{j=1}^K \alpha_j'
For credibility level c, the lower and upper bounds are the quantiles at (1-c)/2 and 1-(1-c)/2.
Excel Usage
=DM_CRED_INT(alpha_posterior, cred_level)
alpha_posterior(list[list], required): Posterior Dirichlet hyperparameters as a 2D range of positive values.cred_level(float, optional, default: 0.95): Central credibility level in (0, 1), such as 0.95.
Returns (list[list]): 2D array with lower bounds in the first row and upper bounds in the second row.
Example 1: Default 95 percent intervals for three-category posterior
Inputs:
| alpha_posterior | ||
|---|---|---|
| 6 | 3 | 4 |
Excel formula:
=DM_CRED_INT({6,3,4})
Expected output:
| Result | ||
|---|---|---|
| 0.210945 | 0.0548606 | 0.0992461 |
| 0.72333 | 0.484138 | 0.571858 |
Example 2: 90 percent intervals for skewed posterior
Inputs:
| alpha_posterior | cred_level | ||
|---|---|---|---|
| 15 | 2 | 1 | 0.9 |
Excel formula:
=DM_CRED_INT({15,2,1}, 0.9)
Expected output:
| Result | ||
|---|---|---|
| 0.673807 | 0.0213176 | 0.00301271 |
| 0.950102 | 0.250124 | 0.161566 |
Example 3: Matrix-shaped posterior parameters for interval computation
Inputs:
| alpha_posterior | cred_level | |
|---|---|---|
| 2 | 5 | 0.8 |
| 3 | 4 |
Excel formula:
=DM_CRED_INT({2,5;3,4}, 0.8)
Expected output:
| Result | |||
|---|---|---|---|
| 0.041691 | 0.200502 | 0.0879964 | 0.141611 |
| 0.267836 | 0.523429 | 0.359776 | 0.444263 |
Example 4: Intervals under weaker posterior concentration
Inputs:
| alpha_posterior | cred_level | |||
|---|---|---|---|---|
| 0.9 | 1.1 | 0.8 | 1.2 | 0.85 |
Excel formula:
=DM_CRED_INT({0.9,1.1,0.8,1.2}, 0.85)
Expected output:
| Result | |||
|---|---|---|---|
| 0.0179992 | 0.0346402 | 0.0117339 | 0.0449046 |
| 0.54483 | 0.60985 | 0.509197 | 0.639744 |
Python Code
from scipy.special import betaincinv as scipy_betaincinv
def dm_cred_int(alpha_posterior, cred_level=0.95):
"""
Compute category-wise credible intervals from posterior Dirichlet parameters.
See: https://en.wikipedia.org/wiki/Dirichlet_distribution#Marginal_distributions
This example function is provided as-is without any representation of accuracy.
Args:
alpha_posterior (list[list]): Posterior Dirichlet hyperparameters as a 2D range of positive values.
cred_level (float, optional): Central credibility level in (0, 1), such as 0.95. Default is 0.95.
Returns:
list[list]: 2D array with lower bounds in the first row and upper bounds in the second row.
"""
try:
def to2d(v):
return [[v]] if not isinstance(v, list) else v
alpha_posterior = to2d(alpha_posterior)
cred_level = float(cred_level)
if not isinstance(alpha_posterior, list) or not all(isinstance(row, list) for row in alpha_posterior):
return "Error: alpha_posterior must be a 2D list"
if cred_level <= 0 or cred_level >= 1:
return "Error: cred_level must be strictly between 0 and 1"
alpha_flat = []
for row in alpha_posterior:
for val in row:
try:
alpha_flat.append(float(val))
except (TypeError, ValueError):
continue
if len(alpha_flat) < 2:
return "Error: alpha_posterior must contain at least two positive values"
if any(a <= 0 for a in alpha_flat):
return "Error: alpha_posterior values must be positive"
alpha_total = float(sum(alpha_flat))
tail = (1.0 - cred_level) / 2.0
lower = []
upper = []
for ai in alpha_flat:
bi = alpha_total - ai
if bi <= 0:
return "Error: alpha_posterior must contain at least two categories"
lower.append(float(scipy_betaincinv(ai, bi, tail)))
upper.append(float(scipy_betaincinv(ai, bi, 1.0 - tail)))
return [lower, upper]
except Exception as e:
return f"Error: {str(e)}"