BAYES_MVS_CI
This function wraps SciPy’s Bayesian mean-variance-standard-deviation estimator to produce equal-tailed credible intervals for three population parameters: mean, variance, and standard deviation.
Given observed data and confidence level \alpha, it returns posterior centers and interval bounds based on Jeffreys prior assumptions used by the underlying method.
For each parameter \theta, the reported interval is an equal-tailed posterior interval:
P(\theta_{L} \leq \theta \leq \theta_{U} \mid \text{data}) = \alpha
Excel Usage
=BAYES_MVS_CI(data, alpha)
data(list[list], required): 2D array of sample observations (unitless).alpha(float, optional, default: 0.9): Credible interval probability level (0 to 1).
Returns (list[list]): 2D table with parameter name, posterior center, and lower/upper credible bounds.
Example 1: Credible intervals for balanced sample values
Inputs:
| data | alpha | ||
|---|---|---|---|
| 6 | 9 | 12 | 0.9 |
| 7 | 8 | 13 |
Excel formula:
=BAYES_MVS_CI({6,9,12;7,8,13}, 0.9)
Expected output:
| Metric | Center | Lower | Upper |
|---|---|---|---|
| Mean | 9.16667 | 6.87407 | 11.4593 |
| Variance | 12.9444 | 3.50782 | 33.9015 |
| Std Dev | 3.31475 | 1.87292 | 5.8225 |
Example 2: Credible intervals with decimal-valued sample
Inputs:
| data | alpha | ||
|---|---|---|---|
| 1.5 | 2 | 2.5 | 0.95 |
| 3 | 3.5 | 4 |
Excel formula:
=BAYES_MVS_CI({1.5,2,2.5;3,3.5,4}, 0.95)
Expected output:
| Metric | Center | Lower | Upper |
|---|---|---|---|
| Mean | 2.75 | 1.76834 | 3.73166 |
| Variance | 1.45833 | 0.340931 | 5.2634 |
| Std Dev | 1.1126 | 0.583893 | 2.29421 |
Example 3: Single-row sample array is accepted
Inputs:
| data | alpha | |||
|---|---|---|---|---|
| 10 | 11 | 9 | 12 | 0.8 |
Excel formula:
=BAYES_MVS_CI({10,11,9,12}, 0.8)
Expected output:
| Metric | Center | Lower | Upper |
|---|---|---|---|
| Mean | 10.5 | 9.44284 | 11.5572 |
| Variance | 5 | 0.799822 | 8.55616 |
| Std Dev | 1.78412 | 0.894328 | 2.92509 |
Example 4: Scalar sample input is normalized to 2D
Inputs:
| data | alpha | ||
|---|---|---|---|
| 4 | 5 | 6 | 0.9 |
Excel formula:
=BAYES_MVS_CI({4,5,6}, 0.9)
Expected output:
| Metric | Center | Lower | Upper |
|---|---|---|---|
| Mean | 5 | 3.31415 | 6.68585 |
| Variance | Infinity | 0.333808 | 19.4957 |
| Std Dev | 1.77245 | 0.577761 | 4.4154 |
Python Code
from scipy.stats import bayes_mvs as scipy_bayes_mvs
def bayes_mvs_ci(data, alpha=0.9):
"""
Compute Bayesian credible intervals for mean, variance, and standard deviation from sample data.
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 sample observations (unitless).
alpha (float, optional): Credible interval probability level (0 to 1). Default is 0.9.
Returns:
list[list]: 2D table with parameter name, posterior center, and lower/upper credible 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: Invalid input - data must be a 2D list"
if not (0 < 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 [
["Metric", "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])],
["Std Dev", float(std_res.statistic), float(std_res.minmax[0]), float(std_res.minmax[1])]
]
except Exception as e:
return f"Error: {str(e)}"