Skip to content

Statistics funcs cov #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions dpnp/backend_statistics.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,44 @@ from dpnp.dpnp_utils cimport checker_throw_type_error, normalize_axis


__all__ += [
"dpnp_cov",
"dpnp_mean"
]


cpdef dparray dpnp_cov(dparray array1):
# behaviour of original numpy
if array1.ndim > 2:
raise ValueError("array has more than 2 dimensions")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use function from utils


if array1.ndim < 2:
raise NotImplementedError

# numpy provide result as float64 for any input type
cdef dparray mean = dparray(array1.shape[0], dtype=numpy.float64)
cdef dparray X = dparray(array1.shape, dtype=numpy.float64)

# mean(array1, axis=1) #################################
# dpmp.mean throws: 'dpnp.dparray.dparray' object is not callable
for i in range(array1.shape[0]):
sum = 0.0
for j in range(array1.shape[1]):
sum += array1[i, j]
mean[i] = sum / array1.shape[1]
########################################################
#X = array1 - mean[:, None]
#X = array1 - mean[:, numpy.newaxis]
#X = array1 - mean.reshape((array1.shape[0], 1))
for i in range(array1.shape[0]):
for j in range(array1.shape[1]):
X[i, j] = array1[i, j] - mean[i]
########################################################
Y = X.transpose()
res = dpnp_matmul(X, Y) / (array1.shape[1] - 1)

return res


cpdef dparray dpnp_mean(dparray a, axis):
cdef dparray_shape_type shape_a = a.shape
cdef long size_a = a.size
Expand Down
27 changes: 27 additions & 0 deletions dpnp/dpnp_iface_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,37 @@
from dpnp.dpnp_utils import checker_throw_value_error, use_origin_backend

__all__ = [
'cov',
'mean'
]


def cov(in_array1, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None):
"""
Estimate a covariance matrix, given data and weights.
"""

is_dparray1 = isinstance(in_array1, dparray)

if (not use_origin_backend(in_array1) and is_dparray1):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if y is not None:
checker_throw_value_error("cov", "y", type(y), None)
if rowvar is not True:
checker_throw_value_error("cov", "rowvar", rowvar, True)
if bias is not False:
checker_throw_value_error("cov", "bias", bias, False)
if ddof is not None:
checker_throw_value_error("cov", "ddof", type(ddof), None)
if fweights is not None:
checker_throw_value_error("cov", "fweights", type(fweights), None)
if aweights is not None:
checker_throw_value_error("cov", "aweights", type(aweights), None)

return dpnp_cov(in_array1)

return numpy.cov(in_array1)


def mean(a, axis=None):
"""
Compute the arithmetic mean along the specified axis.
Expand Down
166 changes: 166 additions & 0 deletions tests/third_party/cupy/statics_tests/test_correlation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import unittest

import numpy
import pytest

import dpnp as cupy
from tests.third_party.cupy import testing


@testing.gpu
class TestCorrcoef(unittest.TestCase):

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_corrcoef(self, xp, dtype):
a = testing.shaped_arange((2, 3), xp, dtype)
return xp.corrcoef(a)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_corrcoef_diag_exception(self, xp, dtype):
a = testing.shaped_arange((1, 3), xp, dtype)
return xp.corrcoef(a)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_corrcoef_y(self, xp, dtype):
a = testing.shaped_arange((2, 3), xp, dtype)
y = testing.shaped_arange((2, 3), xp, dtype)
return xp.corrcoef(a, y=y)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_corrcoef_rowvar(self, xp, dtype):
a = testing.shaped_arange((2, 3), xp, dtype)
y = testing.shaped_arange((2, 3), xp, dtype)
return xp.corrcoef(a, y=y, rowvar=False)


@testing.gpu
class TestCov(unittest.TestCase):

def generate_input(self, a_shape, y_shape, xp, dtype):
a = testing.shaped_arange(a_shape, xp, dtype)
y = None
if y_shape is not None:
y = testing.shaped_arange(y_shape, xp, dtype)
return a, y

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def check(self, a_shape, y_shape=None, rowvar=True, bias=False,
ddof=None, xp=None, dtype=None):
a, y = self.generate_input(a_shape, y_shape, xp, dtype)
return xp.cov(a, y, rowvar, bias, ddof)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def check_warns(self, a_shape, y_shape=None, rowvar=True, bias=False,
ddof=None, xp=None, dtype=None):
with testing.assert_warns(RuntimeWarning):
a, y = self.generate_input(a_shape, y_shape, xp, dtype)
return xp.cov(a, y, rowvar, bias, ddof)

@testing.for_all_dtypes()
def check_raises(self, a_shape, y_shape=None, rowvar=True, bias=False,
ddof=None, dtype=None):
for xp in (numpy, cupy):
a, y = self.generate_input(a_shape, y_shape, xp, dtype)
with pytest.raises(ValueError):
xp.cov(a, y, rowvar, bias, ddof)

def test_cov(self):
self.check((2, 3))
self.check((2,), (2,))
self.check((1, 3), (1, 3), rowvar=False)
self.check((2, 3), (2, 3), rowvar=False)
self.check((2, 3), bias=True)
self.check((2, 3), ddof=2)

def test_cov_warns(self):
self.check_warns((2, 3), ddof=3)
self.check_warns((2, 3), ddof=4)

def test_cov_raises(self):
self.check_raises((2, 3), ddof=1.2)
self.check_raises((3, 4, 2))
self.check_raises((2, 3), (3, 4, 2))

def test_cov_empty(self):
self.check((0, 1))


@testing.gpu
@testing.parameterize(*testing.product({
'mode': ['valid', 'same', 'full'],
'shape1': [(5,), (6,), (20,), (21,)],
'shape2': [(5,), (6,), (20,), (21,)],
}))
class TestCorrelateShapeCombination(unittest.TestCase):

@testing.for_all_dtypes(no_float16=True)
@testing.numpy_cupy_allclose(rtol=1e-4)
def test_correlate(self, xp, dtype):
a = testing.shaped_arange(self.shape1, xp, dtype)
b = testing.shaped_arange(self.shape2, xp, dtype)
return xp.correlate(a, b, mode=self.mode)


@testing.gpu
@testing.parameterize(*testing.product({
'mode': ['valid', 'full', 'same']
}))
class TestCorrelate(unittest.TestCase):

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose(rtol=1e-5)
def test_correlate_non_contiguous(self, xp, dtype):
a = testing.shaped_arange((300,), xp, dtype)
b = testing.shaped_arange((100,), xp, dtype)
return xp.correlate(a[::200], b[10::70], mode=self.mode)

@testing.for_all_dtypes(no_float16=True)
@testing.numpy_cupy_allclose(rtol=1e-4)
def test_correlate_large_non_contiguous(self, xp, dtype):
a = testing.shaped_arange((10000,), xp, dtype)
b = testing.shaped_arange((1000,), xp, dtype)
return xp.correlate(a[200::], b[10::700], mode=self.mode)

@testing.for_all_dtypes_combination(names=['dtype1', 'dtype2'])
@testing.numpy_cupy_allclose(rtol=1e-2)
def test_correlate_diff_types(self, xp, dtype1, dtype2):
a = testing.shaped_random((200,), xp, dtype1)
b = testing.shaped_random((100,), xp, dtype2)
return xp.correlate(a, b, mode=self.mode)


@testing.gpu
@testing.parameterize(*testing.product({
'mode': ['valid', 'same', 'full']
}))
class TestCorrelateInvalid(unittest.TestCase):

@testing.with_requires('numpy>=1.18')
@testing.for_all_dtypes()
def test_correlate_empty(self, dtype):
for xp in (numpy, cupy):
a = xp.zeros((0,), dtype)
with pytest.raises(ValueError):
xp.correlate(a, a, mode=self.mode)

@testing.for_all_dtypes()
def test_correlate_ndim(self, dtype):
for xp in (numpy, cupy):
a = testing.shaped_arange((5, 10, 2), xp, dtype)
b = testing.shaped_arange((3, 4, 4), xp, dtype)
with pytest.raises(ValueError):
xp.correlate(a, b, mode=self.mode)

@testing.for_all_dtypes()
def test_correlate_zero_dim(self, dtype):
for xp in (numpy, cupy):
a = testing.shaped_arange((), xp, dtype)
b = testing.shaped_arange((1,), xp, dtype)
with pytest.raises(ValueError):
xp.correlate(a, b, mode=self.mode)
Loading