statistics: covariance() Stays Silent on Constant Input (tested)
statistics.covariance(), correlation(), and linear_regression() are closely related -- all three take two equal-length data series and describe how they move together. We measured what each one does when one of the series is constant (zero variance), and the three functions don't agree at all.
1. covariance() returns 0.0 silently; correlation() raises StatisticsError
import statistics as st
x = [1, 1, 1, 1]
y = [1, 2, 3, 4]
print(st.covariance(x, y))
try:
st.correlation(x, y)
except st.StatisticsError as e:
print("correlation raised:", e)
0.0
correlation raised: at least one of the inputs is constant
Measured: with x constant, covariance(x, y) returns 0.0 -- mathematically correct, since a constant has zero variance and therefore zero covariance with anything -- but gives no indication that the result is a degenerate edge case rather than "genuinely no relationship." correlation(x, y) on the exact same data raises StatisticsError instead, because computing the correlation coefficient requires dividing by each series' standard deviation, and dividing by zero is undefined.
2. linear_regression() also raises, with a different message
import statistics as st
x = [1, 1, 1, 1]
y = [1, 2, 3, 4]
try:
st.linear_regression(x, y)
except st.StatisticsError as e:
print("linear_regression raised:", e)
linear_regression raised: x is constant
Measured: linear_regression(x, y) also raises StatisticsError on the same constant x -- fitting a line needs a slope, and a constant x makes the slope undefined (division by zero variance again) -- but with yet a third distinct message ("x is constant" vs. correlation's "at least one of the inputs is constant"), so code catching one function's exact text won't reliably catch the other's.
3. Swapping which series is constant doesn't change the pattern
import statistics as st
both_constant = (st.covariance([5, 5, 5], [5, 5, 5]))
print(both_constant)
try:
st.correlation([5, 5, 5], [5, 5, 5])
except st.StatisticsError as e:
print("correlation raised:", e)
0.0
correlation raised: at least one of the inputs is constant
Measured: even with both series constant, covariance() still returns 0.0 with no error at all, while correlation() still raises. A pipeline that calls covariance() on a batch of sensor readings that occasionally includes a flat (unchanging) channel will get a silent 0.0 for that channel instead of a signal that the input was degenerate -- but the same pipeline calling correlation() on that channel would crash and reveal the problem immediately. Catching StatisticsError around correlation() or linear_regression() won't protect a nearby covariance() call from the same underlying issue, since it never raises.
Frequently asked questions
Does statistics.covariance() raise an error for constant input?
No -- we measured covariance([1,1,1,1], [1,2,3,4]) returning 0.0 silently, with no exception and no warning, even though the input is a degenerate edge case.
Does statistics.correlation() raise an error for constant input?
Yes -- we measured correlation() raising StatisticsError with the message 'at least one of the inputs is constant' on the same data where covariance() returns 0.0 silently.
Do correlation() and linear_regression() raise the same error message for constant input?
No -- we measured correlation() raising 'at least one of the inputs is constant' while linear_regression() raises 'x is constant' for the equivalent case. Code matching on exact error text for one won't catch the other.
Why does covariance() not raise an error like correlation() does?
covariance() doesn't need to divide by either series' standard deviation, so a zero-variance input just produces a mathematically valid 0.0 result. correlation() and linear_regression() both require dividing by a standard deviation or variance term, which is undefined when that value is zero, so they raise StatisticsError instead.
Written with AI assistance. Measured on 2026-10-04 with Python 3.13.5's built-in statistics module: called covariance(), correlation(), and linear_regression() with the same constant-valued input and compared behavior. PlainHeaders is our extension; Python's statistics module is built in.