Skip to content

Commit

Permalink
Add support for ddof param to skcuda.misc.std().
Browse files Browse the repository at this point in the history
  • Loading branch information
lebedov committed Oct 20, 2015
1 parent 13ac172 commit cdd0bcf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion docs/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Release 0.5.1 - (under development)
Erichson).
* Randomized linear algebra routines (enh. by N. Ben Erichson).
* Add triu function (enh. by N. Ben Erichson).
* Support Bessel correction in computation of variance (#143).
* Support Bessel correction in computation of variance and standard
deviation (#143).

Release 0.5.0 - (July 14, 2015)
-------------------------------
Expand Down
11 changes: 8 additions & 3 deletions skcuda/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ def _inplace_pow(x_gpu, p, stream):
return out


def std(x_gpu, axis=None, stream=None, keepdims=False):
def std(x_gpu, ddof=0, axis=None, stream=None, keepdims=False):
"""
Compute the standard deviation along the specified axis.
Expand All @@ -1348,6 +1348,11 @@ def std(x_gpu, axis=None, stream=None, keepdims=False):
----------
x_gpu : pycuda.gpuarray.GPUArray
Array containing numbers whose std is desired.
ddof : int (optional)
"Delta Degrees of Freedom": the divisor used in computing the
variance is ``N - ddof``, where ``N`` is the number of elements.
Setting ``ddof = 1`` is equivalent to applying Bessel's
correction.
axis : int (optional)
Axis along which the std are computed. The default is to
compute the std of the flattened array.
Expand All @@ -1368,9 +1373,9 @@ def _inplace_pow(x_gpu, p, stream):
p, x_gpu.gpudata, x_gpu.gpudata, x_gpu.mem_size)

if axis is None:
return var(x_gpu, stream=stream, keepdims=keepdims) ** 0.5
return var(x_gpu, ddof=ddof, stream=stream, keepdims=keepdims) ** 0.5
else:
out = var(x_gpu, axis=axis, stream=stream, keepdims=keepdims)
out = var(x_gpu, ddof=ddof, axis=axis, stream=stream, keepdims=keepdims)
_inplace_pow(out, 0.5, stream)
return out

Expand Down
6 changes: 6 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ def impl_test_std(self, dtype):
assert np.allclose(misc.std(x_gpu).get(), x.std())
assert np.allclose(misc.std(x_gpu, axis=0).get(), x.std(axis=0))
assert np.allclose(misc.std(x_gpu, axis=1).get(), x.std(axis=1))

assert np.allclose(misc.std(x_gpu, ddof=1).get(), x.std(ddof=1))
assert np.allclose(misc.std(x_gpu, ddof=1, axis=0).get(),
x.std(ddof=1, axis=0))
assert np.allclose(misc.std(x_gpu, ddof=1, axis=1).get(),
x.std(ddof=1, axis=1))
# Currently not working due to a bug in PyCUDA, see Issue #92
#x = x.astype(dtype=dtype, order='F')
#x_gpu = gpuarray.to_gpu(x)
Expand Down

0 comments on commit cdd0bcf

Please sign in to comment.