ML Foundations
Handle a single observation in sample standard deviation
Distinguish insufficient sample data from a zero population standard deviation in Python.
Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
One sample is not enough for sample spread
Suppose an evaluation reports the standard deviation of repeated measurements, but a particular group has only one observation. Python statistics.stdev calculates sample standard deviation and raises StatisticsError when fewer than two data points are provided. That failure is useful information: the requested sample statistic cannot be calculated from that group. [1]
Do not replace the missing statistic with zero by default. Zero is a numeric claim about spread. A missing value or explicit insufficient-data status can instead tell the caller that the sample calculation was not available.
References: [1]
Guard the sample calculation
Run python examples/python_single_sample_stdev.py. The helper converts its input to a list so it can check the count once, then returns None for fewer than two observations. Otherwise it calls statistics.stdev. The script also catches the actual StatisticsError raised by a direct one-observation call. [1]
For the fixed pair of four and six, sample standard deviation rounds to 1.414214. The population standard deviation of the same pair is 1.0. For one value, pstdev returns 0.0, while the guarded sample calculation returns None. These outcomes answer different questions; choosing the population function solely to suppress an exception changes the statistic. [1, 2]
Assertions check the guarded result and both calculations for this small fixture. Downstream code should explicitly skip or label None rather than treating it as a measured zero.
Executed with Python 3.12.14; exact stdout and checksum are saved in private evidence.
"""Report sample standard deviation only when at least two observations exist."""
from statistics import StatisticsError, stdev, pstdev
def sample_spread(values):
values = list(values)
return stdev(values) if len(values) >= 2 else None
one = [4.0]
two = [4.0, 6.0]
try:
stdev(one)
except StatisticsError:
print('one sample stdev: StatisticsError')
print('one sample guarded:', sample_spread(one))
print('one population pstdev:', pstdev(one))
print('two sample stdev:', round(sample_spread(two), 6))
print('two population pstdev:', pstdev(two))
assert sample_spread(one) is None
assert round(sample_spread(two), 6) == 1.414214
assert pstdev(two) == 1.0
Recorded output
one sample stdev: StatisticsError one sample guarded: None one population pstdev: 0.0 two sample stdev: 1.414214 two population pstdev: 1.0
Choose the statistic for the question
Use the population function when the supplied values are the complete population for the calculation, and sample standard deviation when the values are a sample for that calculation. Neither function can determine whether the data collection design represents a larger population. [1, 2]
This example uses a small fixed list of finite values. It does not assess sampling bias, uncertainty intervals, missing measurements or numerical behavior on extreme inputs. A caller should handle those separately for its own application. The example does not claim a model improvement.