Issue
Scipy and numpy standard deviation methods give slightly different results. I don't understand why. Can anyone explain that to me?
Here is an example.
import numpy as np
import scipy.stats
ar = np.arange(20)
print(np.std(ar))
print(scipy.stats.tstd(ar))
returns
5.766281297335398
5.916079783099616
Solution
With np.std()
you are computing the standard deviation:
x = np.abs(ar - ar.mean())**2
std = np.sqrt(np.sum(x) / len(ar)) # 5.766281297335398
However, with scipy.stats.tstd
you are computing the trimmed standard deviation:
x = np.abs(ar - ar.mean())**2
std = np.sqrt(np.sum(x) / (len(ar) - 1)) # 5.916079783099616
Note that you are computing the square root of the mean of x
when using np.std()
(the mean of x
is the sum of x
divided by the length of x
). When computing the trimmed version you are dividing by n-1
, n
being the length of the array.
Answered By - T C Molenaar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.