Skip to content

Commit 17e9eb3

Browse files
committed
feat: add stats/strided/dmeanwd
Ref: #4797 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent cf81564 commit 17e9eb3

34 files changed

+3558
-0
lines changed
+339
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# dmeanwd
22+
23+
> Calculate the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using Welford's algorithm.
24+
25+
<section class="intro">
26+
27+
The [arithmetic mean][arithmetic-mean] is defined as
28+
29+
<!-- <equation class="equation" label="eq:arithmetic_mean" align="center" raw="\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the arithmetic mean."> -->
30+
31+
```math
32+
\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:arithmetic_mean">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@339637af8d39d156edb39d8f600e0c29d6c96ec0/lib/node_modules/@stdlib/stats/strided/dmeanwd/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var dmeanwd = require( '@stdlib/stats/strided/dmeanwd' );
52+
```
53+
54+
#### dmeanwd( N, x, strideX )
55+
56+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array `x` using Welford's algorithm.
57+
58+
```javascript
59+
var Float64Array = require( '@stdlib/array/float64' );
60+
61+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
62+
63+
var v = dmeanwd( x.length, x, 1 );
64+
// returns ~0.3333
65+
```
66+
67+
The function has the following parameters:
68+
69+
- **N**: number of indexed elements.
70+
- **x**: input [`Float64Array`][@stdlib/array/float64].
71+
- **strideX**: stride length for `x`.
72+
73+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`,
74+
75+
```javascript
76+
var Float64Array = require( '@stdlib/array/float64' );
77+
78+
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
79+
80+
var v = dmeanwd( 4, x, 2 );
81+
// returns 1.25
82+
```
83+
84+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
85+
86+
<!-- eslint-disable stdlib/capitalized-comments -->
87+
88+
```javascript
89+
var Float64Array = require( '@stdlib/array/float64' );
90+
91+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
92+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
93+
94+
var v = dmeanwd( 4, x1, 2 );
95+
// returns 1.25
96+
```
97+
98+
#### dmeanwd.ndarray( N, x, strideX, offsetX )
99+
100+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using Welford's algorithm and alternative indexing semantics.
101+
102+
```javascript
103+
var Float64Array = require( '@stdlib/array/float64' );
104+
105+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
106+
107+
var v = dmeanwd.ndarray( x.length, x, 1, 0 );
108+
// returns ~0.33333
109+
```
110+
111+
The function has the following additional parameters:
112+
113+
- **offsetX**: starting index for `x`.
114+
115+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [arithmetic mean][arithmetic-mean] for every other element in `x` starting from the second element
116+
117+
```javascript
118+
var Float64Array = require( '@stdlib/array/float64' );
119+
120+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
121+
122+
var v = dmeanwd.ndarray( 4, x, 2, 1 );
123+
// returns 1.25
124+
```
125+
126+
</section>
127+
128+
<!-- /.usage -->
129+
130+
<section class="notes">
131+
132+
## Notes
133+
134+
- If `N <= 0`, both functions return `NaN`.
135+
136+
</section>
137+
138+
<!-- /.notes -->
139+
140+
<section class="examples">
141+
142+
## Examples
143+
144+
<!-- eslint no-undef: "error" -->
145+
146+
```javascript
147+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
148+
var dmeanwd = require( '@stdlib/stats/strided/dmeanwd' );
149+
150+
var x = discreteUniform( 10, -50, 50, {
151+
'dtype': 'float64'
152+
});
153+
console.log( x );
154+
155+
var v = dmeanwd( x.length, x, 1 );
156+
console.log( v );
157+
```
158+
159+
</section>
160+
161+
<!-- /.examples -->
162+
163+
<!-- C interface documentation. -->
164+
165+
* * *
166+
167+
<section class="c">
168+
169+
## C APIs
170+
171+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
172+
173+
<section class="intro">
174+
175+
</section>
176+
177+
<!-- /.intro -->
178+
179+
<!-- C usage documentation. -->
180+
181+
<section class="usage">
182+
183+
### Usage
184+
185+
```c
186+
#include "stdlib/stats/strided/dmeanwd.h"
187+
```
188+
189+
#### stdlib_strided_dmeanwd( N, \*X, strideX )
190+
191+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using Welford's algorithm.
192+
193+
```c
194+
const double x[] = { 1.0, -2.0, 2.0 };
195+
196+
double v = stdlib_strided_dmeanwd( 3, x, 1 );
197+
// returns ~0.333
198+
```
199+
200+
The function accepts the following arguments:
201+
202+
- **N**: `[in] CBLAS_INT` number of indexed elements.
203+
- **X**: `[in] double*` input array.
204+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
205+
206+
```c
207+
double stdlib_strided_dmeanwd( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
208+
```
209+
210+
#### stdlib_strided_dmeanwd_ndarray( N, \*X, strideX, offsetX )
211+
212+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using Welford's algorithm and alternative indexing semantics.
213+
214+
```c
215+
const double x[] = { 1.0, -2.0, 2.0 };
216+
217+
double v = stdlib_strided_dmeanwd_ndarray( 3, x, 1, 0 );
218+
// returns ~0.333
219+
```
220+
221+
The function accepts the following arguments:
222+
223+
- **N**: `[in] CBLAS_INT` number of indexed elements.
224+
- **X**: `[in] double*` input array.
225+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
226+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
227+
228+
```c
229+
double stdlib_strided_dmeanwd_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
230+
```
231+
232+
</section>
233+
234+
<!-- /.usage -->
235+
236+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
237+
238+
<section class="notes">
239+
240+
</section>
241+
242+
<!-- /.notes -->
243+
244+
<!-- C API usage examples. -->
245+
246+
<section class="examples">
247+
248+
### Examples
249+
250+
```c
251+
#include "stdlib/stats/strided/dmeanwd.h"
252+
#include <stdint.h>
253+
#include <stdio.h>
254+
255+
int main( void ) {
256+
// Create a strided array:
257+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
258+
259+
// Specify the number of elements:
260+
const int N = 4;
261+
262+
// Specify the stride length:
263+
const int strideX = 2;
264+
265+
// Compute the minimum value:
266+
double v = stdlib_strided_dmeanwd( N, x, strideX );
267+
268+
// Print the result:
269+
printf( "mean: %lf\n", v );
270+
}
271+
```
272+
273+
</section>
274+
275+
<!-- /.examples -->
276+
277+
</section>
278+
279+
<!-- /.c -->
280+
281+
* * *
282+
283+
<section class="references">
284+
285+
## References
286+
287+
- Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022][@welford:1962a].
288+
- van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961][@vanreeken:1968a].
289+
290+
</section>
291+
292+
<!-- /.references -->
293+
294+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
295+
296+
<section class="related">
297+
298+
* * *
299+
300+
## See Also
301+
302+
- <span class="package-name">[`@stdlib/stats/base/dmean`][@stdlib/stats/base/dmean]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array.</span>
303+
- <span class="package-name">[`@stdlib/stats/base/dnanmeanwd`][@stdlib/stats/base/dnanmeanwd]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array, using Welford's algorithm and ignoring NaN values.</span>
304+
- <span class="package-name">[`@stdlib/stats/base/meanwd`][@stdlib/stats/base/meanwd]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a strided array using Welford's algorithm.</span>
305+
- <span class="package-name">[`@stdlib/stats/base/smeanwd`][@stdlib/stats/base/smeanwd]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array using Welford's algorithm.</span>
306+
307+
</section>
308+
309+
<!-- /.related -->
310+
311+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
312+
313+
<section class="links">
314+
315+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
316+
317+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
318+
319+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
320+
321+
[@welford:1962a]: https://doi.org/10.1080/00401706.1962.10490022
322+
323+
[@vanreeken:1968a]: https://doi.org/10.1145/362929.362961
324+
325+
<!-- <related-links> -->
326+
327+
[@stdlib/stats/base/dmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dmean
328+
329+
[@stdlib/stats/base/dnanmeanwd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dnanmeanwd
330+
331+
[@stdlib/stats/base/meanwd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/meanwd
332+
333+
[@stdlib/stats/base/smeanwd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/smeanwd
334+
335+
<!-- </related-links> -->
336+
337+
</section>
338+
339+
<!-- /.links -->

0 commit comments

Comments
 (0)