POSTERIOR_MAP
This function finds the maximum a posteriori (MAP) estimate from discrete posterior support values and their associated probabilities or weights.
Let x_i be support values and p_i be nonnegative posterior weights normalized so that \sum_i p_i = 1. The MAP estimate is:
x_{\mathrm{MAP}} = x_{k}, \quad k = \arg\max_i p_i
The function returns both the MAP support value and its normalized posterior probability.
Excel Usage
=POSTERIOR_MAP(values, posterior)
values(list[list], required): 2D array of posterior support values.posterior(list[list], required): 2D array of nonnegative posterior probabilities or weights.
Returns (list[list]): 2D table containing MAP value and MAP probability.
Example 1: MAP estimate from a unimodal posterior table
Inputs:
| values | posterior | ||||||
|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 0.1 | 0.2 | 0.5 | 0.2 |
Excel formula:
=POSTERIOR_MAP({1,2,3,4}, {0.1,0.2,0.5,0.2})
Expected output:
| MAP Value | MAP Probability |
|---|---|
| 3 | 0.5 |
Example 2: Non-normalized posterior weights are normalized internally
Inputs:
| values | posterior | ||||
|---|---|---|---|---|---|
| 10 | 20 | 30 | 1 | 4 | 2 |
Excel formula:
=POSTERIOR_MAP({10,20,30}, {1,4,2})
Expected output:
| MAP Value | MAP Probability |
|---|---|
| 20 | 0.571429 |
Example 3: Matrix-shaped support and posterior values are flattened consistently
Inputs:
| values | posterior | ||
|---|---|---|---|
| 0 | 1 | 0.1 | 0.6 |
| 2 | 3 | 0.2 | 0.1 |
Excel formula:
=POSTERIOR_MAP({0,1;2,3}, {0.1,0.6;0.2,0.1})
Expected output:
| MAP Value | MAP Probability |
|---|---|
| 1 | 0.6 |
Example 4: Scalar support with scalar posterior returns the same support value
Inputs:
| values | posterior |
|---|---|
| 7 | 1 |
Excel formula:
=POSTERIOR_MAP(7, 1)
Expected output:
| MAP Value | MAP Probability |
|---|---|
| 7 | 1 |
Python Code
import numpy as np
def posterior_map(values, posterior):
"""
Extract the MAP estimate from a tabulated posterior distribution.
See: https://en.wikipedia.org/wiki/Maximum_a_posteriori_estimation
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.
Returns:
list[list]: 2D table containing MAP value and MAP probability.
"""
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)
map_idx = int(np.argmax(posterior_arr))
map_value = float(value_arr[map_idx])
map_prob = float(posterior_arr[map_idx])
return [
["MAP Value", "MAP Probability"],
[map_value, map_prob]
]
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.