Skip to content

fix(variance): return NaN for sample variance of single-element collection - #180

Merged
JimmyMAndersson merged 1 commit into
mainfrom
fix/wrong-variance
May 23, 2026
Merged

fix(variance): return NaN for sample variance of single-element collection#180
JimmyMAndersson merged 1 commit into
mainfrom
fix/wrong-variance

Conversation

@JimmyMAndersson

Copy link
Copy Markdown
Owner

Population variance of a single observation is 0 (no spread), but sample variance is mathematically undefined because the denominator n-1 equals 0. The prior guard short-circuited both cases with 0, silently producing incorrect results for the sample case.

Tests are split into separate population and sample cases to make the distinction explicit, and two constant-value two-element cases are added to cover the zero-spread boundary.

Fixes: #179

@JimmyMAndersson JimmyMAndersson self-assigned this May 23, 2026
@JimmyMAndersson JimmyMAndersson added the bug Something isn't working label May 23, 2026
…ction

Population variance of a single observation is 0 (no spread), but
sample variance is mathematically undefined because the denominator
n-1 equals 0. The prior guard short-circuited both cases with 0,
silently producing incorrect results for the sample case.

Tests are split into separate population and sample cases to make
the distinction explicit, and two constant-value two-element cases
are added to cover the zero-spread boundary.

Fixes: #179
@JimmyMAndersson
JimmyMAndersson merged commit b7baadd into main May 23, 2026
5 checks passed
@JimmyMAndersson
JimmyMAndersson deleted the fix/wrong-variance branch May 23, 2026 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

variance(variable:from:) returns 0 instead of NaN for single-element collections

1 participant