Skip to content

Commit 1963b2e

Browse files
steff456kgryte
andauthoredJan 31, 2022
PR: Transform statistical functions md to rst (#370)
* Transform statistical functions md to rst * Update note directives * Lowercase first sentence to be consistent with rest of spec This commit addresses an oversight in the spec prior to Markdown to rST conversion. Co-authored-by: Athan <[email protected]>
1 parent 6a106cc commit 1963b2e

File tree

3 files changed

+267
-302
lines changed

3 files changed

+267
-302
lines changed
 
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
from ._types import Optional, Tuple, Union, array, dtype
2+
3+
def max(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = False) -> array:
4+
"""
5+
Calculates the maximum value of the input array ``x``.
6+
7+
.. note::
8+
When the number of elements over which to compute the maximum value is zero, the maximum value is implementation-defined. Specification-compliant libraries may choose to raise an error, return a sentinel value (e.g., if ``x`` is a floating-point input array, return ``NaN``), or return the minimum possible value for the input array ``x`` data type (e.g., if ``x`` is a floating-point array, return ``-infinity``).
9+
10+
**Special Cases**
11+
12+
For floating-point operands,
13+
14+
- If ``x_i`` is ``NaN``, the maximum value is ``NaN`` (i.e., ``NaN`` values propagate).
15+
16+
Parameters
17+
----------
18+
x: array
19+
input array. Should have a numeric data type.
20+
axis: Optional[Union[int, Tuple[int, ...]]]
21+
axis or axes along which maximum values must be computed. By default, the maximum value must be computed over the entire array. If a tuple of integers, maximum values must be computed over multiple axes. Default: ``None``.
22+
keepdims: bool
23+
if ``True``, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see :ref:`broadcasting`). Otherwise, if ``False``, the reduced axes (dimensions) must not be included in the result. Default: ``False``.
24+
25+
Returns
26+
-------
27+
out: array
28+
if the maximum value was computed over the entire array, a zero-dimensional array containing the maximum value; otherwise, a non-zero-dimensional array containing the maximum values. The returned array must have the same data type as ``x``.
29+
"""
30+
31+
def mean(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = False) -> array:
32+
"""
33+
Calculates the arithmetic mean of the input array ``x``.
34+
35+
**Special Cases**
36+
37+
Let ``N`` equal the number of elements over which to compute the arithmetic mean.
38+
39+
- If ``N`` is ``0``, the arithmetic mean is ``NaN``.
40+
- If ``x_i`` is ``NaN``, the arithmetic mean is ``NaN`` (i.e., ``NaN`` values propagate).
41+
42+
Parameters
43+
----------
44+
x: array
45+
input array. Should have a floating-point data type.
46+
axis: Optional[Union[int, Tuple[int, ...]]]
47+
axis or axes along which arithmetic means must be computed. By default, the mean must be computed over the entire array. If a tuple of integers, arithmetic means must be computed over multiple axes. Default: ``None``.
48+
keepdims: bool
49+
if ``True``, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see :ref:`broadcasting`). Otherwise, if ``False``, the reduced axes (dimensions) must not be included in the result. Default: ``False``.
50+
51+
Returns
52+
-------
53+
out: array
54+
if the arithmetic mean was computed over the entire array, a zero-dimensional array containing the arithmetic mean; otherwise, a non-zero-dimensional array containing the arithmetic means. The returned array must have the same data type as ``x``.
55+
56+
.. note::
57+
While this specification recommends that this function only accept input arrays having a floating-point data type, specification-compliant array libraries may choose to accept input arrays having an integer data type. While mixed data type promotion is implementation-defined, if the input array ``x`` has an integer data type, the returned array must have the default floating-point data type.
58+
"""
59+
60+
def min(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = False) -> array:
61+
"""
62+
Calculates the minimum value of the input array ``x``.
63+
64+
.. note::
65+
When the number of elements over which to compute the minimum value is zero, the minimum value is implementation-defined. Specification-compliant libraries may choose to raise an error, return a sentinel value (e.g., if ``x`` is a floating-point input array, return ``NaN``), or return the maximum possible value for the input array ``x`` data type (e.g., if ``x`` is a floating-point array, return ``+infinity``).
66+
67+
**Special Cases**
68+
69+
For floating-point operands,
70+
71+
- If ``x_i`` is ``NaN``, the minimum value is ``NaN`` (i.e., ``NaN`` values propagate).
72+
73+
Parameters
74+
----------
75+
x: array
76+
input array. Should have a numeric data type.
77+
axis: Optional[Union[int, Tuple[int, ...]]]
78+
axis or axes along which minimum values must be computed. By default, the minimum value must be computed over the entire array. If a tuple of integers, minimum values must be computed over multiple axes. Default: ``None``.
79+
keepdims: bool
80+
if ``True``, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see :ref:`broadcasting`). Otherwise, if ``False``, the reduced axes (dimensions) must not be included in the result. Default: ``False``.
81+
82+
Returns
83+
-------
84+
out: array
85+
if the minimum value was computed over the entire array, a zero-dimensional array containing the minimum value; otherwise, a non-zero-dimensional array containing the minimum values. The returned array must have the same data type as ``x``.
86+
"""
87+
88+
def prod(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype: Optional[dtype] = None, keepdims: bool = False) -> array:
89+
"""
90+
Calculates the product of input array ``x`` elements.
91+
92+
**Special Cases**
93+
94+
Let ``N`` equal the number of elements over which to compute the product.
95+
96+
- If ``N`` is ``0``, the product is `1` (i.e., the empty product).
97+
98+
For floating-point operands,
99+
100+
- If ``x_i`` is ``NaN``, the product is ``NaN`` (i.e., ``NaN`` values propagate).
101+
102+
Parameters
103+
----------
104+
x: array
105+
input array. Should have a numeric data type.
106+
axis: Optional[Union[int, Tuple[int, ...]]]
107+
axis or axes along which products must be computed. By default, the product must be computed over the entire array. If a tuple of integers, products must be computed over multiple axes. Default: ``None``.
108+
dtype: Optional[dtype]
109+
data type of the returned array. If ``None``,
110+
111+
- if the default data type corresponding to the data type "kind" (integer or floating-point) of ``x`` has a smaller range of values than the data type of ``x`` (e.g., ``x`` has data type ``int64`` and the default data type is ``int32``, or ``x`` has data type ``uint64`` and the default data type is ``int64``), the returned array must have the same data type as ``x``.
112+
- if ``x`` has a floating-point data type, the returned array must have the default floating-point data type.
113+
- if ``x`` has a signed integer data type (e.g., ``int16``), the returned array must have the default integer data type.
114+
- if ``x`` has an unsigned integer data type (e.g., ``uint16``), the returned array must have an unsigned integer data type having the same number of bits as the default integer data type (e.g., if the default integer data type is ``int32``, the returned array must have a ``uint32`` data type).
115+
116+
If the data type (either specified or resolved) differs from the data type of ``x``, the input array should be cast to the specified data type before computing the product. Default: ``None``.
117+
118+
.. note::
119+
This keyword argument is intended to help prevent data type overflows.
120+
121+
keepdims: bool
122+
if ``True``, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see :ref:`broadcasting`). Otherwise, if ``False``, the reduced axes (dimensions) must not be included in the result. Default: ``False``.
123+
124+
Returns
125+
-------
126+
out: array
127+
if the product was computed over the entire array, a zero-dimensional array containing the product; otherwise, a non-zero-dimensional array containing the products. The returned array must have a data type as described by the ``dtype`` parameter above.
128+
"""
129+
130+
def std(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, correction: Union[int, float] = 0.0, keepdims: bool = False) -> array:
131+
"""
132+
Calculates the standard deviation of the input array ``x``.
133+
134+
**Special Cases**
135+
136+
Let ``N`` equal the number of elements over which to compute the standard deviation.
137+
138+
- If ``N - correction`` is less than or equal to ``0``, the standard deviation is ``NaN``.
139+
- If ``x_i`` is ``NaN``, the standard deviation is ``NaN`` (i.e., ``NaN`` values propagate).
140+
141+
Parameters
142+
----------
143+
x: array
144+
input array. Should have a floating-point data type.
145+
axis: Optional[Union[int, Tuple[int, ...]]]
146+
axis or axes along which standard deviations must be computed. By default, the standard deviation must be computed over the entire array. If a tuple of integers, standard deviations must be computed over multiple axes. Default: ``None``.
147+
correction: Union[int, float]
148+
degrees of freedom adjustment. Setting this parameter to a value other than ``0`` has the effect of adjusting the divisor during the calculation of the standard deviation according to ``N-c`` where ``N`` corresponds to the total number of elements over which the standard deviation is computed and ``c`` corresponds to the provided degrees of freedom adjustment. When computing the standard deviation of a population, setting this parameter to ``0`` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the corrected sample standard deviation, setting this parameter to ``1`` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction). Default: ``0``.
149+
keepdims: bool
150+
if ``True``, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see :ref:`broadcasting`). Otherwise, if ``False``, the reduced axes (dimensions) must not be included in the result. Default: ``False``.
151+
152+
Returns
153+
-------
154+
out: array
155+
if the standard deviation was computed over the entire array, a zero-dimensional array containing the standard deviation; otherwise, a non-zero-dimensional array containing the standard deviations. The returned array must have the same data type as ``x``.
156+
157+
.. note::
158+
While this specification recommends that this function only accept input arrays having a floating-point data type, specification-compliant array libraries may choose to accept input arrays having an integer data type. While mixed data type promotion is implementation-defined, if the input array ``x`` has an integer data type, the returned array must have the default floating-point data type.
159+
"""
160+
161+
def sum(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype: Optional[dtype] = None, keepdims: bool = False) -> array:
162+
"""
163+
Calculates the sum of the input array ``x``.
164+
165+
**Special Cases**
166+
167+
Let ``N`` equal the number of elements over which to compute the sum.
168+
169+
- If ``N`` is ``0``, the sum is ``0`` (i.e., the empty sum).
170+
171+
For floating-point operands,
172+
173+
- If ``x_i`` is ``NaN``, the sum is ``NaN`` (i.e., ``NaN`` values propagate).
174+
175+
Parameters
176+
----------
177+
x: array
178+
input array. Should have a numeric data type.
179+
axis: Optional[Union[int, Tuple[int, ...]]]
180+
axis or axes along which sums must be computed. By default, the sum must be computed over the entire array. If a tuple of integers, sums must be computed over multiple axes. Default: ``None``.
181+
dtype: Optional[dtype]
182+
data type of the returned array. If ``None``,
183+
184+
- if the default data type corresponding to the data type "kind" (integer or floating-point) of ``x`` has a smaller range of values than the data type of ``x`` (e.g., ``x`` has data type ``int64`` and the default data type is ``int32``, or ``x`` has data type ``uint64`` and the default data type is ``int64``), the returned array must have the same data type as ``x``.
185+
- if ``x`` has a floating-point data type, the returned array must have the default floating-point data type.
186+
- if ``x`` has a signed integer data type (e.g., ``int16``), the returned array must have the default integer data type.
187+
- if ``x`` has an unsigned integer data type (e.g., ``uint16``), the returned array must have an unsigned integer data type having the same number of bits as the default integer data type (e.g., if the default integer data type is ``int32``, the returned array must have a ``uint32`` data type).
188+
189+
If the data type (either specified or resolved) differs from the data type of ``x``, the input array should be cast to the specified data type before computing the sum. Default: ``None``.
190+
191+
.. note::
192+
keyword argument is intended to help prevent data type overflows.
193+
194+
keepdims: bool
195+
if ``True``, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see :ref:`broadcasting`). Otherwise, if ``False``, the reduced axes (dimensions) must not be included in the result. Default: ``False``.
196+
197+
Returns
198+
-------
199+
out: array
200+
if the sum was computed over the entire array, a zero-dimensional array containing the sum; otherwise, an array containing the sums. The returned array must have a data type as described by the ``dtype`` parameter above.
201+
"""
202+
203+
def var(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, correction: Union[int, float] = 0.0, keepdims: bool = False) -> array:
204+
"""
205+
Calculates the variance of the input array ``x``.
206+
207+
**Special Cases**
208+
209+
Let ``N`` equal the number of elements over which to compute the variance.
210+
211+
- If ``N - correction`` is less than or equal to ``0``, the variance is ``NaN``.
212+
- If ``x_i`` is ``NaN``, the variance is ``NaN`` (i.e., ``NaN`` values propagate).
213+
214+
Parameters
215+
----------
216+
x: array
217+
input array. Should have a floating-point data type.
218+
axis: Optional[Union[int, Tuple[int, ...]]]
219+
axis or axes along which variances must be computed. By default, the variance must be computed over the entire array. If a tuple of integers, variances must be computed over multiple axes. Default: ``None``.
220+
correction: Union[int, float]
221+
degrees of freedom adjustment. Setting this parameter to a value other than ``0`` has the effect of adjusting the divisor during the calculation of the variance according to ``N-c`` where ``N`` corresponds to the total number of elements over which the variance is computed and ``c`` corresponds to the provided degrees of freedom adjustment. When computing the variance of a population, setting this parameter to ``0`` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the unbiased sample variance, setting this parameter to ``1`` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction). Default: ``0``.
222+
keepdims: bool
223+
if ``True``, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see :ref:`broadcasting`). Otherwise, if ``False``, the reduced axes (dimensions) must not be included in the result. Default: ``False``.
224+
225+
Returns
226+
-------
227+
out: array
228+
if the variance was computed over the entire array, a zero-dimensional array containing the variance; otherwise, a non-zero-dimensional array containing the variances. The returned array must have the same data type as ``x``.
229+
230+
231+
.. note::
232+
While this specification recommends that this function only accept input arrays having a floating-point data type, specification-compliant array libraries may choose to accept input arrays having an integer data type. While mixed data type promotion is implementation-defined, if the input array ``x`` has an integer data type, the returned array must have the default floating-point data type.
233+
"""
234+
235+
__all__ = ['max', 'mean', 'min', 'prod', 'std', 'sum', 'var']

‎spec/API_specification/statistical_functions.md

Lines changed: 0 additions & 302 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Statistical Functions
2+
=====================
3+
4+
Array API specification for statistical functions.
5+
6+
A conforming implementation of the array API standard must provide and support the following functions adhering to the following conventions.
7+
8+
- Positional parameters must be `positional-only <https://www.python.org/dev/peps/pep-0570/>`_ parameters. Positional-only parameters have no externally-usable name. When a function accepting positional-only parameters is called, positional arguments are mapped to these parameters based solely on their order.
9+
- Optional parameters must be `keyword-only <https://www.python.org/dev/peps/pep-3102/>`_ arguments.
10+
- Broadcasting semantics must follow the semantics defined in :ref:`broadcasting`.
11+
- Unless stated otherwise, functions must support the data types defined in :ref:`data-types`.
12+
- Unless stated otherwise, functions must adhere to the type promotion rules defined in :ref:`type-promotion`.
13+
- Unless stated otherwise, floating-point operations must adhere to IEEE 754-2019.
14+
15+
Objects in API
16+
--------------
17+
18+
.. currentmodule:: signatures.statistical_functions
19+
20+
..
21+
NOTE: please keep the functions in alphabetical order
22+
23+
.. autosummary::
24+
:toctree: generated
25+
26+
max
27+
mean
28+
min
29+
prod
30+
std
31+
sum
32+
var

0 commit comments

Comments
 (0)
Please sign in to comment.