Reputation:
I need a javascript function to match the math used in Excel's STDEV function.
When I use the array [1,2,4,6] I get the standard deviation of 1.9202 with every js script I've found. However, when I run the same array through Excel's STDEV, I get 2.2173.
I'm not a mathematician, but I am trying to convert some excel reports into javascript. What I'm ultimately looking for is a JavaScript function to do what excel does.
Additionally, I've looked at other people with similar issues, and they spent a while explaining the differences, but gave no code sample, or direct math equation that I can convert to JS. Any help is appreciated, a javascript function would be most helpful.
Thanks.
Upvotes: 1
Views: 2360
Reputation: 4586
This might be helpful for those who wants to use a library -
math.std(2, 4, 6); // returns 2
math.std([2, 4, 6, 8]); // returns 2.581988897471611
math.std([2, 4, 6, 8], 'uncorrected'); // returns 2.23606797749979
math.std([2, 4, 6, 8], 'biased'); // returns 2
Upvotes: 0
Reputation: 234715
You're comparing apples with oranges.
The equivalent formula in Excel is STDEVP (i.e. the population standard deviation). This will match the Javascript result.
The population standard deviation has sqrt(n)
as the denominator whereas the unbiased standard deviation (STDEV) has sqrt(n - 1)
. n
is the number of terms making up the standard deviation.
To convert from one to the other, multiply / divide by sqrt(n / (n - 1))
:
1.9202 * sqrt(4 / 3) = 2.2173
Upvotes: 4