POSTERIOR_BMV
This function summarizes posterior uncertainty for the mean, variance, and standard deviation of a continuous sample using a Bayesian model with Jeffrey’s prior, following SciPy’s bayes_mvs implementation.
For each target quantity, it returns a posterior center estimate and a central credible interval with probability level \alpha.
If \theta denotes one of the target parameters and p(\theta \mid x) is the posterior density given data x, the reported center is the posterior mean:
\mathbb{E}[\theta \mid x]
and the interval is a central posterior interval satisfying:
P(\theta_{\text{lower}} \le \theta \le \theta_{\text{upper}} \mid x) = \alpha
Excel Usage
=POSTERIOR_BMV(data, alpha)
data(list[list], required): 2D array of numeric sample observations.alpha(float, optional, default: 0.9): Credible interval probability level (unitless, between 0 and 1).
Returns (list[list]): 2D table containing statistic name, posterior center, and credible interval bounds.
Example 1: Posterior summaries for balanced sample with default alpha
Inputs:
| data | ||
|---|---|---|
| 6 | 9 | 12 |
| 7 | 8 | 13 |
Excel formula:
=POSTERIOR_BMV({6,9,12;7,8,13})
Expected output:
| Statistic | Center | Lower | Upper |
|---|---|---|---|
| Mean | 9.16667 | 6.87407 | 11.4593 |
| Variance | 12.9444 | 3.50782 | 33.9015 |
| StdDev | 3.31475 | 1.87292 | 5.8225 |
Example 2: Posterior summaries with 95 percent credible interval
Inputs:
| data | alpha | ||||
|---|---|---|---|---|---|
| 5 | 8 | 9 | 10 | 12 | 0.95 |
Excel formula:
=POSTERIOR_BMV({5,8,9,10,12}, 0.95)
Expected output:
| Statistic | Center | Lower | Upper |
|---|---|---|---|
| Mean | 8.8 | 5.58603 | 12.014 |
| Variance | 13.4 | 2.40504 | 55.3241 |
| StdDev | 3.24412 | 1.55082 | 7.43801 |
Example 3: Scalar input is normalized to one-cell 2D data with extra point
Inputs:
| data | alpha | |
|---|---|---|
| 4 | 6 | 0.9 |
Excel formula:
=POSTERIOR_BMV({4,6}, 0.9)
Expected output:
| Statistic | Center | Lower | Upper |
|---|---|---|---|
| Mean | Infinity | -1.31375 | 11.3138 |
| Variance | Infinity | 0.520636 | 508.629 |
| StdDev | Infinity | 0.721551 | 22.5528 |
Example 4: Integer and decimal observations are both processed
Inputs:
| data | |
|---|---|
| 1 | 2.5 |
| 3 | 4.75 |
| 6 | 8 |
Excel formula:
=POSTERIOR_BMV({1,2.5;3,4.75;6,8})
Expected output:
| Statistic | Center | Lower | Upper |
|---|---|---|---|
| Mean | 4.20833 | 2.10932 | 6.30734 |
| Variance | 10.8507 | 2.94044 | 28.4179 |
| StdDev | 3.03486 | 1.71477 | 5.33085 |
Python Code
import numpy as np
from scipy.stats import bayes_mvs as scipy_bayes_mvs
def posterior_bmv(data, alpha=0.9):
"""
Compute Bayesian posterior summaries for mean, variance, and standard deviation.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.bayes_mvs.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): 2D array of numeric sample observations.
alpha (float, optional): Credible interval probability level (unitless, between 0 and 1). Default is 0.9.
Returns:
list[list]: 2D table containing statistic name, posterior center, and credible interval bounds.
"""
try:
def to2d(x):
return [[x]] if not isinstance(x, list) else x
data = to2d(data)
if not isinstance(data, list) or not all(isinstance(row, list) for row in data):
return "Error: data must be a 2D list"
try:
alpha = float(alpha)
except (TypeError, ValueError):
return "Error: alpha must be numeric"
if alpha <= 0 or alpha >= 1:
return "Error: alpha must be between 0 and 1"
flat = []
for row in data:
for value in row:
try:
flat.append(float(value))
except (TypeError, ValueError):
continue
if len(flat) < 2:
return "Error: data must contain at least two numeric values"
mean_res, var_res, std_res = scipy_bayes_mvs(flat, alpha=alpha)
return [
["Statistic", "Center", "Lower", "Upper"],
["Mean", float(mean_res.statistic), float(mean_res.minmax[0]), float(mean_res.minmax[1])],
["Variance", float(var_res.statistic), float(var_res.minmax[0]), float(var_res.minmax[1])],
["StdDev", float(std_res.statistic), float(std_res.minmax[0]), float(std_res.minmax[1])]
]
except Exception as e:
return f"Error: {str(e)}"