Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std,var return NaN when there is only one element in array. #889

Closed
liupgd opened this issue Sep 4, 2023 · 1 comment
Closed

std,var return NaN when there is only one element in array. #889

liupgd opened this issue Sep 4, 2023 · 1 comment

Comments

@liupgd
Copy link

liupgd commented Sep 4, 2023

using StatsBase
a = [1.0]
std(a) # --> returns NaN
var(a) # --> returns NaN

Is this normal? I thought they should return 0.

@ararslan
Copy link
Member

ararslan commented Oct 6, 2023

This is because var and std apply Bessel's correction by default. When your sample size is $n = 1$, the sum of squared deviations from the mean is multiplied by $\frac{1}{n} = 1$ without the correction, which will give you 0. But with the correction, $\frac{1}{n - 1} = \frac{1}{0} = \text{NaN}$ since Julia's Base float types and arithmetic follow the IEEE 754 standard.

@ararslan ararslan closed this as completed Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants