Skip to content

Commit

Permalink
Add optional ddof parameter to lar.std() and lar.var().
Browse files Browse the repository at this point in the history
  • Loading branch information
kwgoodman committed Mar 10, 2011
1 parent c26c6eb commit 0e7fbf8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
9 changes: 8 additions & 1 deletion RELEASE.rst
Expand Up @@ -32,6 +32,10 @@ Bottleneck package (http://pypi.python.org/pypi/Bottleneck).
- la.unique(): Find the unique elements of a larry
- larry.tofile(): Save 1d or 2d larry to text file

**Enhancements**

- Add optional `ddof` input parameter to larry.std() and larry.var()

**Breakage from la 0.4**

- Bottleneck is now a dependency of la
Expand All @@ -42,11 +46,14 @@ Bottleneck package (http://pypi.python.org/pypi/Bottleneck).

**Bugs fixes**

- Please report bugs at https://github.com/kwgoodman/la/issues
- #1 Due to a typo, la.info() crashed if h5py could not be imported
- #2 larry.sortaxis(None) chopped off singleton dimensions
- #5 la.farray.lastrank() choked on empty array input
- #7 larry.quantile() choked on axis=None
- #8 demean, demedian, zscore choked on 1d input when axis=-1
- #8 demean, demedian, zscore choked on 1d input when axis=-1
- #9 cross_validation docstring refers to old name of function (cv)
- #10 Unit tests: "Warning: invalid value encountered in..."


Older versions
Expand Down
61 changes: 51 additions & 10 deletions la/deflarry.py
Expand Up @@ -1160,15 +1160,36 @@ def median(self, axis=None):
"""
return self.__reduce(bn.nanmedian, axis=axis)

def std(self, axis=None):
def std(self, axis=None, ddof=0):
"""
Standard deviation of values along axis, ignoring NaNs.
`float64` intermediate and return values are used for integer inputs.
Instead of a faster one-pass algorithm, a more stable two-pass
algorithm is used.
An example of a one-pass algorithm:
>>> np.sqrt((arr*arr).mean() - arr.mean()**2)
An example of a two-pass algorithm:
>>> np.sqrt(((arr - arr.mean())**2).mean())
Note in the two-pass algorithm the mean must be found (first pass)
before the squared deviation (second pass) can be found.
Parameters
----------
axis : {None, integer}, optional
Axis to find the standard deviation along (integer) or the global
standard deviation (None, default).
axis : {int, None}, optional
Axis along which the standard deviation is computed. The default
(axis=None) is to compute the standard deviation of the flattened
array.
ddof : int, optional
Means Delta Degrees of Freedom. The divisor used in calculations
is ``N - ddof``, where ``N`` represents the number of elements.
By default `ddof` is zero.
Returns
-------
Expand All @@ -1195,17 +1216,37 @@ def std(self, axis=None):
array([ 0., 1.])
"""
return self.__reduce(bn.nanstd, axis=axis)
return self.__reduce(bn.nanstd, axis=axis, ddof=ddof)

def var(self, axis=None):
def var(self, axis=None, ddof=0):
"""
Variance of values along axis, ignoring NaNs.
`float64` intermediate and return values are used for integer inputs.
Instead of a faster one-pass algorithm, a more stable two-pass
algorithm is used.
An example of a one-pass algorithm:
>>> np.sqrt((arr*arr).mean() - arr.mean()**2)
An example of a two-pass algorithm:
>>> np.sqrt(((arr - arr.mean())**2).mean())
Note in the two-pass algorithm the mean must be found (first pass)
before the squared deviation (second pass) can be found.
Parameters
----------
axis : {None, integer}, optional
Axis to find the variance along (integer) or the global variance
(None, default).
axis : {int, None}, optional
Axis along which the variance is computed. The default (axis=None)
is to compute the variance of the flattened array.
ddof : int, optional
Means Delta Degrees of Freedom. The divisor used in calculations
is ``N - ddof``, where ``N`` represents the number of elements.
By default `ddof` is zero.
Returns
-------
Expand All @@ -1232,7 +1273,7 @@ def var(self, axis=None):
array([ 0., 1.])
"""
return self.__reduce(bn.nanvar, axis=axis)
return self.__reduce(bn.nanvar, axis=axis, ddof=ddof)

def max(self, axis=None):
"""
Expand Down

0 comments on commit 0e7fbf8

Please sign in to comment.