# [Scipy-tickets] [SciPy] #300: stats.py - Population and sample variances backwards, causing downstream errors

SciPy scipy-tickets at scipy.net
Wed Nov 1 10:27:08 CST 2006

```#300: stats.py - Population and sample variances backwards, causing downstream
errors
-------------------------+--------------------------------------------------
Reporter:  zunzun       |       Owner:  somebody
Type:  defect       |      Status:  new
Priority:  high         |   Milestone:
Component:  scipy.stats  |     Version:
Severity:  major        |    Keywords:
-------------------------+--------------------------------------------------
In stats.py, sample and population variances are backwards.

Sample variation should be divided by (n-1) instead of n,
and population variance by n instead of (n-1).

This is causing many, many downstream effects on other
calculations such as standard deviations, kurtosis and skew.

I marked these below with <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

James Phillips
zunzun at zunzun.com

def samplevar(a, axis=0):
"""
Returns the sample standard deviation of the values in the passed
array (i.e., using N).  Axis can equal None (ravel array first),
an integer (the axis over which to operate)
"""
a, axis = _chk_asarray(a, axis)
mn = expand_dims(mean(a, axis), axis)
deviations = a - mn
n = a.shape[axis]
svar = ss(deviations,axis) / float(n)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
return svar

def var(a, axis=0, bias=False):
"""
Returns the estimated population variance of the values in the passed
array (i.e., N-1).  Axis can equal None (ravel array first), or an
integer (the axis over which to operate).
"""
a, axis = _chk_asarray(a, axis)
mn = expand_dims(mean(a,axis),axis)
deviations = a - mn
n = a.shape[axis]
vals = ss(deviations,axis)/(n-1.0)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if bias:
return vals * (n-1.0)/n
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
else:
return vals

--
Ticket URL: <http://projects.scipy.org/scipy/scipy/ticket/300>
SciPy <http://www.scipy.org/>
SciPy is open-source software for mathematics, science, and engineering.
```