8000 API: Introduce correction argument for np.var and np.std · numpy/numpy@7b6e406 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b6e406

Browse files
committed
API: Introduce correction argument for np.var and np.std
1 parent b3d71f7 commit 7b6e406

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

doc/source/reference/array_api.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,6 @@ functions to include additional keyword arguments from those required.
186186
- The definitions of ``rtol`` and ``rcond`` are the same, but their
187187
default values differ, making this **breaking**. See
188188
:ref:`array_api-linear-algebra-differences`.
189-
* - ``std`` and ``var``
190-
- ``correction``
191-
- ``ddof``
192-
-
193189
* - ``reshape``
194190
- ``shape``
195191
- ``newshape``

numpy/_core/fromnumeric.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3513,13 +3513,13 @@ def mean(a, axis=None, dtype=None, out=None, keepdims=np._NoValue, *,
35133513

35143514

35153515
def _std_dispatcher(a, axis=None, dtype=None, out=None, ddof=None,
3516-
keepdims=None, *, where=None, mean=None):
3516+
keepdims=None, *, where=None, mean=None, correction=None):
35173517
return (a, where, out, mean)
35183518

35193519

35203520
@array_function_dispatch(_std_dispatcher)
35213521
def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
3522-
where=np._NoValue, mean=np._NoValue):
3522+
where=np._NoValue, mean=np._NoValue, correction=0):
35233523
"""
35243524
Compute the standard deviation along the specified axis.
35253525
@@ -3567,14 +3567,20 @@ def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
35673567
35683568
.. versionadded:: 1.20.0
35693569
3570-
mean : array like, optional
3570+
mean : array_like, optional
35713571
Provide the mean to prevent its recalculation. The mean should have
35723572
a shape as if it was calculated with ``keepdims=True``.
35733573
The axis for the calculation of the mean should be the same as used in
35743574
the call to this std function.
35753575
35763576
.. versionadded:: 1.26.0
35773577
3578+
correction : int, optional
3579+
Array API compatible name for the ``ddof`` parameter. Only one of them
3580+
can be provided at the same time.
3581+
3582+
.. versionadded:: 2.0.0
3583+
35783584
Returns
35793585
-------
35803586
standard_deviation : ndarray, see dtype parameter above.
@@ -3666,6 +3672,14 @@ def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
36663672
if mean is not np._NoValue:
36673673
kwargs['mean'] = mean
36683674

3675+
if correction != 0:
3676+
if ddof != 0:
3677+
raise ValueError(
3678+
"ddof and correction can't be provided simultaneously."
3679+
)
3680+
else:
3681+
ddof = correction
3682+
36693683
if type(a) is not mu.ndarray:
36703684
try:
36713685
std = a.std
@@ -3679,13 +3693,13 @@ def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
36793693

36803694

36813695
def _var_dispatcher(a, axis=None, dtype=None, out=None, ddof=None,
3682-
keepdims=None, *, where=None, mean=None):
3696+
keepdims=None, *, where=None, mean=None, correction=None):
36833697
return (a, where, out, mean)
36843698

36853699

36863700
@array_function_dispatch(_var_dispatcher)
36873701
def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
3688-
where=np._NoValue, mean=np._NoValue):
3702+
where=np._NoValue, mean=np._NoValue, correction=0):
36893703
"""
36903704
Compute the variance along the specified axis.
36913705
@@ -3742,6 +3756,12 @@ def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
37423756
37433757
.. versionadded:: 1.26.0
37443758
3759+
correction : int, optional
3760+
Array API compatible name for the ``ddof`` parameter. Only one of them
3761+
can be provided at the same time.
3762+
3763+
.. versionadded:: 2.0.0
3764+
37453765
Returns
37463766
-------
37473767
variance : ndarray, see dtype parameter above
@@ -3832,6 +3852,14 @@ def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
38323852
if mean is not np._NoValue:
38333853
kwargs['mean'] = mean
38343854

3855+
if correction != 0:
3856+
if ddof != 0:
3857+
raise ValueError(
3858+
"ddof and correction can't be provided simultaneously."
3859+
)
3860+
else:
3861+
ddof = correction
3862+
38353863
if type(a) is not mu.ndarray:
38363864
try:
38373865
var = a.var

numpy/_core/tests/test_numeric.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3084,6 +3084,22 @@ def test_ddof2(self):
30843084
assert_almost_equal(np.std(self.A, ddof=2)**2,
30853085
self.real_var * len(self.A) / (len(self.A) - 2))
30863086

3087+
def test_correction(self):
3088+
assert_almost_equal(
3089+
np.var(self.A, correction=1), np.var(self.A, ddof=1)
3090+
)
3091+
assert_almost_equal(
3092+
np.std(self.A, correction=1), np.std(self.A, ddof=1)
3093+
)
3094+
3095+
err_msg = "ddof and correction can't be provided simultaneously."
3096+
3097+
with assert_raises_regex(ValueError, err_msg):
3098+
np.var(self.A, ddof=1, correction=1)
3099+
3100+
with assert_raises_regex(ValueError, err_msg):
3101+
np.std(self.A, ddof=1, correction=1)
3102+
30873103
def test_out_scalar(self):
30883104
d = np.arange(10)
30893105
out = np.array(0.)

0 commit comments

Comments
 (0)
0