Reputation: 13
Why am I getting different results?
from scipy.stats import sem
import numpy as np
l = [0,2,4,5,6,7]
print(sem(l))
print(np.std(l)/np.sqrt(len(l)))
1.0645812948447542
0.9718253158075502
Upvotes: 1
Views: 6556
Reputation: 3811
The scipy.stats.sem
function uses a default value of ddof=1
for the number-of-degrees-of-freedom parameter while numpy.std
uses ddof=0
by default. This is also highlighted in the docs:
The default value for ddof is different to the default (0) used by other ddof containing routines, such as np.std and np.nanstd.
Consequently you get:
>>> print(sem(l))
1.06458129484
>>> print(sem(l, ddof=0))
0.971825315808
>>> print(sem(l, ddof=1))
1.06458129484
>>> print(np.std(l)/np.sqrt(len(l)))
0.971825315808
>>> print(np.std(l, ddof=0)/np.sqrt(len(l)))
0.971825315808
>>> print(np.std(l, ddof=1)/np.sqrt(len(l)))
1.06458129484
Upvotes: 4