Skip to content
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

Implementation of std and var. #327

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions cubed/array_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@

__all__ += ["argmax", "argmin", "where"]

from .statistical_functions import max, mean, min, prod, sum
from .statistical_functions import max, mean, min, prod, std, sum, var

__all__ += ["max", "mean", "min", "prod", "sum"]
__all__ += ["max", "mean", "min", "prod", "std", "sum", "var"]

from .utility_functions import all, any

Expand Down
10 changes: 10 additions & 0 deletions cubed/array_api/statistical_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
uint64,
)
from cubed.backend_array_api import namespace as nxp
from cubed.array_api.elementwise_functions import sqrt, square, subtract
from cubed.core import reduction


Expand Down Expand Up @@ -184,3 +185,12 @@ def sum(
split_every=split_every,
extra_func_kwargs=extra_func_kwargs,
)


def var(x, /, *, axis=None, keepdims=False):
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't match the signature in the specification since it's missing a correction parameter. Similarly for std.

mu = mean(x, axis=axis, keepdims=True)
return mean(square(subtract(x, mu)), axis=axis, keepdims=keepdims)


def std(x, /, *, axis=None, keepdims=False):
return sqrt(var(x, axis=axis, keepdims=keepdims))
68 changes: 68 additions & 0 deletions cubed/tests/test_array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,74 @@ def test_sum_axis_0(spec, executor):
assert_array_equal(b.compute(executor=executor), np.array([12, 15, 18]))


def test_var(spec, executor):
Copy link
Member

Choose a reason for hiding this comment

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

Can you parameterize all these tests for var for different axes, and for keepdims? There are other tests in the test suite that do this.

a = xp.asarray(
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], chunks=(2, 2), spec=spec
)
b = xp.var(a)
assert_array_equal(
b.compute(executor=executor),
np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]).var(),
)


def test_var_axis_0(spec, executor):
a = xp.asarray(
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], chunks=(2, 2), spec=spec
)
b = xp.var(a, axis=0)
assert_array_equal(
b.compute(executor=executor),
np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]).var(axis=0),
)


def test_var_axis_1(spec, executor):
a = xp.asarray(
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], chunks=(2, 2), spec=spec
)
b = xp.var(a, axis=1)
assert_array_equal(
b.compute(executor=executor),
np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]).var(axis=1),
)


def test_var_keepdims_true(spec, executor):
a = xp.asarray(
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], chunks=(2, 2), spec=spec
)
b = xp.var(a, keepdims=True)
assert_array_equal(
b.compute(executor=executor),
np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]).var(
keepdims=True
),
)


def test_std(spec, executor):
a = xp.asarray(
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], chunks=(2, 2), spec=spec
)
b = xp.std(a)
assert_array_equal(
b.compute(executor=executor),
np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]).std(),
)


def test_std_axis_0(spec, executor):
a = xp.asarray(
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], chunks=(2, 2), spec=spec
)
b = xp.std(a, axis=0)
assert_array_equal(
b.compute(executor=executor),
np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]).std(axis=0),
)


# Utility functions


Expand Down
Loading