From cdd0bcf91a5703226d9116bcc52c3c50b1c0cf8a Mon Sep 17 00:00:00 2001 From: Lev Givon Date: Tue, 20 Oct 2015 14:02:48 -0400 Subject: [PATCH] Add support for ddof param to skcuda.misc.std(). --- docs/source/changes.rst | 3 ++- skcuda/misc.py | 11 ++++++++--- tests/test_misc.py | 6 ++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/docs/source/changes.rst b/docs/source/changes.rst index f8fd89b3..c3642df5 100644 --- a/docs/source/changes.rst +++ b/docs/source/changes.rst @@ -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) ------------------------------- diff --git a/skcuda/misc.py b/skcuda/misc.py index a7bbf7b9..c15f0883 100644 --- a/skcuda/misc.py +++ b/skcuda/misc.py @@ -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. @@ -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. @@ -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 diff --git a/tests/test_misc.py b/tests/test_misc.py index b1975464..ca36ded7 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -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)