-
Notifications
You must be signed in to change notification settings - Fork 22
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
File renamed without changes.
166 changes: 166 additions & 0 deletions
166
tests/third_party/cupy/statics_tests/test_correlation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use function from utils