Skip to content

Commit 6c984fb

Browse files
committed
feat: add stats/strided/dmeanlipw
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 ca97a2b commit 6c984fb

34 files changed

+3493
-0
lines changed
Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
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+
# dmeanlipw
22+
23+
> Calculate the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using a one-pass trial mean algorithm with pairwise summation.
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@4415a930fcdcfd8c5ff6a8781a93d88b40ab0e18/lib/node_modules/@stdlib/stats/strided/dmeanlipw/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 dmeanlipw = require( '@stdlib/stats/strided/dmeanlipw' );
52+
```
53+
54+
#### dmeanlipw( N, x, strideX )
55+
56+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array `x` using a one-pass trial mean algorithm with pairwise summation.
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 = dmeanlipw( 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 = dmeanlipw( 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 = dmeanlipw( 4, x1, 2 );
95+
// returns 1.25
96+
```
97+
98+
#### dmeanlipw.ndarray( N, x, strideX, offsetX )
99+
100+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using a one-pass trial mean algorithm with pairwise summation 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 = dmeanlipw.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 = dmeanlipw.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+
- The underlying algorithm is a specialized case of Welford's algorithm. Similar to the method of assumed mean, the first strided array element is used as a trial mean. The trial mean is subtracted from subsequent data values, and the average deviations used to adjust the initial guess. Accordingly, the algorithm's accuracy is best when data is **unordered** (i.e., the data is **not** sorted in either ascending or descending order such that the first value is an "extreme" value).
136+
137+
</section>
138+
139+
<!-- /.notes -->
140+
141+
<section class="examples">
142+
143+
## Examples
144+
145+
<!-- eslint no-undef: "error" -->
146+
147+
```javascript
148+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
149+
var dmeanlipw = require( '@stdlib/stats/strided/dmeanlipw' );
150+
151+
var x = discreteUniform( 10, -50, 50, {
152+
'dtype': 'float64'
153+
});
154+
console.log( x );
155+
156+
var v = dmeanlipw( x.length, x, 1 );
157+
console.log( v );
158+
```
159+
160+
</section>
161+
162+
<!-- /.examples -->
163+
164+
<!-- C usage documentation. -->
165+
166+
<section class="usage">
167+
168+
### Usage
169+
170+
```c
171+
#include "stdlib/stats/strided/dmeanlipw.h"
172+
```
173+
174+
#### stdlib_strided_dmeanlipw( N, \*X, strideX )
175+
176+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using a one-pass trial mean algorithm with pairwise summation.
177+
178+
```c
179+
const double x[] = { 1.0, -2.0, 2.0 };
180+
181+
double v = stdlib_strided_dmeanlipw( 3, x, 1 );
182+
// returns ~0.3333
183+
```
184+
185+
The function accepts the following arguments:
186+
187+
- **N**: `[in] CBLAS_INT` number of indexed elements.
188+
- **X**: `[in] double*` input array.
189+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
190+
191+
```c
192+
double stdlib_strided_dmeanlipw( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
193+
```
194+
195+
#### stdlib_strided_dmeanlipw_ndarray( N, \*X, strideX, offsetX )
196+
197+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using a one-pass trial mean algorithm with pairwise summation and alternative indexing semantics.
198+
199+
```c
200+
const double x[] = { 1.0, -2.0, 2.0 };
201+
202+
double v = stdlib_strided_dmeanlipw_ndarray( 3, x, 1, 0 );
203+
// returns ~0.3333
204+
```
205+
206+
The function accepts the following arguments:
207+
208+
- **N**: `[in] CBLAS_INT` number of indexed elements.
209+
- **X**: `[in] double*` input array.
210+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
211+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
212+
213+
```c
214+
double stdlib_strided_dmeanlipw_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
215+
```
216+
217+
</section>
218+
219+
<!-- /.usage -->
220+
221+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
222+
223+
<section class="notes">
224+
225+
</section>
226+
227+
<!-- /.notes -->
228+
229+
<!-- C API usage examples. -->
230+
231+
<section class="examples">
232+
233+
### Examples
234+
235+
```c
236+
#include "stdlib/stats/strided/dmeanlipw.h"
237+
#include <stdio.h>
238+
239+
int main( void ) {
240+
// Create a strided array:
241+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
242+
243+
// Specify the number of elements:
244+
const int N = 4;
245+
246+
// Specify the stride length:
247+
const int strideX = 2;
248+
249+
// Compute the arithmetic mean:
250+
double v = stdlib_strided_dmeanlipw( N, x, strideX );
251+
252+
// Print the result:
253+
printf( "mean: %lf\n", v );
254+
}
255+
```
256+
257+
</section>
258+
259+
<!-- /.examples -->
260+
261+
</section>
262+
263+
<!-- /.c -->
264+
265+
* * *
266+
267+
<section class="references">
268+
269+
## References
270+
271+
- 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].
272+
- 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].
273+
- Ling, Robert F. 1974. "Comparison of Several Algorithms for Computing Sample Means and Variances." _Journal of the American Statistical Association_ 69 (348). American Statistical Association, Taylor & Francis, Ltd.: 859–66. doi:[10.2307/2286154][@ling:1974a].
274+
- Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050][@higham:1993a].
275+
276+
</section>
277+
278+
<!-- /.references -->
279+
280+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
281+
282+
<section class="related">
283+
284+
* * *
285+
286+
## See Also
287+
288+
- <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>
289+
- <span class="package-name">[`@stdlib/stats/strided/dmeanli`][@stdlib/stats/strided/dmeanli]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array using a one-pass trial mean algorithm.</span>
290+
- <span class="package-name">[`@stdlib/stats/base/dmeanpw`][@stdlib/stats/base/dmeanpw]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array using pairwise summation.</span>
291+
- <span class="package-name">[`@stdlib/stats/base/smeanlipw`][@stdlib/stats/base/smeanlipw]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array using a one-pass trial mean algorithm with pairwise summation.</span>
292+
293+
</section>
294+
295+
<!-- /.related -->
296+
297+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
298+
299+
<section class="links">
300+
301+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
302+
303+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
304+
305+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
306+
307+
[@welford:1962a]: https://doi.org/10.1080/00401706.1962.10490022
308+
309+
[@vanreeken:1968a]: https://doi.org/10.1145/362929.362961
310+
311+
[@ling:1974a]: https://doi.org/10.2307/2286154
312+
313+
[@higham:1993a]: https://doi.org/10.1137/0914050
314+
315+
<!-- <related-links> -->
316+
317+
[@stdlib/stats/base/dmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dmean
318+
319+
[@stdlib/stats/strided/dmeanli]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmeanli
320+
321+
[@stdlib/stats/base/dmeanpw]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dmeanpw
322+
323+
[@stdlib/stats/base/smeanlipw]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/smeanlipw
324+
325+
<!-- </related-links> -->
326+
327+
</section>
328+
329+
<!-- /.links -->

0 commit comments

Comments
 (0)