Skip to content

Commit e1cf69e

Browse files
committed
feat: add stats/strided/svariance
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: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - 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 ebf0e0e commit e1cf69e

37 files changed

+3801
-0
lines changed
Lines changed: 378 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,378 @@
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+
# svariance
22+
23+
> Calculate the [variance][variance] of a single-precision floating-point strided array.
24+
25+
<section class="intro">
26+
27+
The population [variance][variance] of a finite size population of size `N` is given by
28+
29+
<!-- <equation class="equation" label="eq:population_variance" align="center" raw="\sigma^2 = \frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2" alt="Equation for the population variance."> -->
30+
31+
```math
32+
\sigma^2 = \frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\sigma^2 = \frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2" data-equation="eq:population_variance">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@839977fd2e3a8d0b5cdd9aa567ce53a5979ceed5/lib/node_modules/@stdlib/stats/strided/svariance/docs/img/equation_population_variance.svg" alt="Equation for the population variance.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
where the population mean is given by
43+
44+
<!-- <equation class="equation" label="eq:population_mean" align="center" raw="\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i" alt="Equation for the population mean."> -->
45+
46+
```math
47+
\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i
48+
```
49+
50+
<!-- <div class="equation" align="center" data-raw-text="\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i" data-equation="eq:population_mean">
51+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@839977fd2e3a8d0b5cdd9aa567ce53a5979ceed5/lib/node_modules/@stdlib/stats/strided/svariance/docs/img/equation_population_mean.svg" alt="Equation for the population mean.">
52+
<br>
53+
</div> -->
54+
55+
<!-- </equation> -->
56+
57+
Often in the analysis of data, the true population [variance][variance] is not known _a priori_ and must be estimated from a sample drawn from the population distribution. If one attempts to use the formula for the population [variance][variance], the result is biased and yields a **biased sample variance**. To compute an **unbiased sample variance** for a sample of size `n`,
58+
59+
<!-- <equation class="equation" label="eq:unbiased_sample_variance" align="center" raw="s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2" alt="Equation for computing an unbiased sample variance."> -->
60+
61+
```math
62+
s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2
63+
```
64+
65+
<!-- <div class="equation" align="center" data-raw-text="s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2" data-equation="eq:unbiased_sample_variance">
66+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@839977fd2e3a8d0b5cdd9aa567ce53a5979ceed5/lib/node_modules/@stdlib/stats/strided/svariance/docs/img/equation_unbiased_sample_variance.svg" alt="Equation for computing an unbiased sample variance.">
67+
<br>
68+
</div> -->
69+
70+
<!-- </equation> -->
71+
72+
where the sample mean is given by
73+
74+
<!-- <equation class="equation" label="eq:sample_mean" align="center" raw="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the sample mean."> -->
75+
76+
```math
77+
\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i
78+
```
79+
80+
<!-- <div class="equation" align="center" data-raw-text="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:sample_mean">
81+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@839977fd2e3a8d0b5cdd9aa567ce53a5979ceed5/lib/node_modules/@stdlib/stats/strided/svariance/docs/img/equation_sample_mean.svg" alt="Equation for the sample mean.">
82+
<br>
83+
</div> -->
84+
85+
<!-- </equation> -->
86+
87+
The use of the term `n-1` is commonly referred to as Bessel's correction. Note, however, that applying Bessel's correction can increase the mean squared error between the sample variance and population variance. Depending on the characteristics of the population distribution, other correction factors (e.g., `n-1.5`, `n+1`, etc) can yield better estimators.
88+
89+
</section>
90+
91+
<!-- /.intro -->
92+
93+
<section class="usage">
94+
95+
## Usage
96+
97+
```javascript
98+
var svariance = require( '@stdlib/stats/strided/svariance' );
99+
```
100+
101+
#### svariance( N, correction, x, strideX )
102+
103+
Computes the [variance][variance] of a single-precision floating-point strided array.
104+
105+
```javascript
106+
var Float32Array = require( '@stdlib/array/float32' );
107+
108+
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
109+
110+
var v = svariance( x.length, 1, x, 1 );
111+
// returns ~4.3333
112+
```
113+
114+
The function has the following parameters:
115+
116+
- **N**: number of indexed elements.
117+
- **correction**: 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][variance] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [variance][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][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).
118+
- **x**: input [`Float32Array`][@stdlib/array/float32].
119+
- **strideX**: stride length for `x`.
120+
121+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [variance][variance] of every other element in `x`,
122+
123+
```javascript
124+
var Float32Array = require( '@stdlib/array/float32' );
125+
126+
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
127+
128+
var v = svariance( 4, 1, x, 2 );
129+
// returns 6.25
130+
```
131+
132+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
133+
134+
<!-- eslint-disable stdlib/capitalized-comments -->
135+
136+
```javascript
137+
var Float32Array = require( '@stdlib/array/float32' );
138+
139+
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
140+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
141+
142+
var v = svariance( 4, 1, x1, 2 );
143+
// returns 6.25
144+
```
145+
146+
#### svariance.ndarray( N, correction, x, strideX, offsetX )
147+
148+
Computes the [variance][variance] of a single-precision floating-point strided array using alternative indexing semantics.
149+
150+
```javascript
151+
var Float32Array = require( '@stdlib/array/float32' );
152+
153+
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
154+
155+
var v = svariance.ndarray( x.length, 1, x, 1, 0 );
156+
// returns ~4.33333
157+
```
158+
159+
The function has the following additional parameters:
160+
161+
- **offsetX**: starting index for `x`.
162+
163+
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 [variance][variance] for every other element in `x` starting from the second element
164+
165+
```javascript
166+
var Float32Array = require( '@stdlib/array/float32' );
167+
168+
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
169+
170+
var v = svariance.ndarray( 4, 1, x, 2, 1 );
171+
// returns 6.25
172+
```
173+
174+
</section>
175+
176+
<!-- /.usage -->
177+
178+
<section class="notes">
179+
180+
## Notes
181+
182+
- If `N <= 0`, both functions return `NaN`.
183+
- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`.
184+
185+
</section>
186+
187+
<!-- /.notes -->
188+
189+
<section class="examples">
190+
191+
## Examples
192+
193+
<!-- eslint no-undef: "error" -->
194+
195+
```javascript
196+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
197+
var svariance = require( '@stdlib/stats/strided/svariance' );
198+
199+
var x = discreteUniform( 10, -50, 50, {
200+
'dtype': 'float32'
201+
});
202+
console.log( x );
203+
204+
var v = svariance( x.length, 1, x, 1 );
205+
console.log( v );
206+
```
207+
208+
</section>
209+
210+
<!-- /.examples -->
211+
212+
<!-- C interface documentation. -->
213+
214+
* * *
215+
216+
<section class="c">
217+
218+
## C APIs
219+
220+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
221+
222+
<section class="intro">
223+
224+
</section>
225+
226+
<!-- /.intro -->
227+
228+
<!-- C usage documentation. -->
229+
230+
<section class="usage">
231+
232+
### Usage
233+
234+
```c
235+
#include "stdlib/stats/strided/svariance.h"
236+
```
237+
238+
#### stdlib_strided_svariance( N, correction, \*X, strideX )
239+
240+
Computes the [variance][variance] of a single-precision floating-point strided array.
241+
242+
```c
243+
const float x[] = { 1.0f, -2.0f, 2.0f };
244+
245+
float v = stdlib_strided_svariance( 3, 1.0f, x, 1 );
246+
// returns ~4.3333f
247+
```
248+
249+
The function accepts the following arguments:
250+
251+
- **N**: `[in] CBLAS_INT` number of indexed elements.
252+
- **correction**: `[in] float` 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][variance] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [variance][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][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).
253+
- **X**: `[in] float*` input array.
254+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
255+
256+
```c
257+
float stdlib_strided_svariance( const CBLAS_INT N, const float correction, const float *X, const CBLAS_INT strideX );
258+
```
259+
260+
#### stdlib_strided_svariance_ndarray( N, correction, \*X, strideX, offsetX )
261+
262+
Computes the [variance][variance] of a single-precision floating-point strided array using alternative indexing semantics.
263+
264+
```c
265+
const float x[] = { 1.0f, -2.0f, 2.0f };
266+
267+
float v = stdlib_strided_svariance_ndarray( 4, 1.0f, x, 1, 0 );
268+
// returns ~4.3333f
269+
```
270+
271+
The function accepts the following arguments:
272+
273+
- **N**: `[in] CBLAS_INT` number of indexed elements.
274+
- **correction**: `[in] float` 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][variance] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [variance][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][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).
275+
- **X**: `[in] float*` input array.
276+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
277+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
278+
279+
```c
280+
float stdlib_strided_svariance_ndarray( const CBLAS_INT N, const float correction, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
281+
```
282+
283+
</section>
284+
285+
<!-- /.usage -->
286+
287+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
288+
289+
<section class="notes">
290+
291+
</section>
292+
293+
<!-- /.notes -->
294+
295+
<!-- C API usage examples. -->
296+
297+
<section class="examples">
298+
299+
### Examples
300+
301+
```c
302+
#include "stdlib/stats/strided/svariance.h"
303+
#include <stdio.h>
304+
305+
int main( void ) {
306+
// Create a strided array:
307+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
308+
309+
// Specify the number of elements:
310+
const int N = 4;
311+
312+
// Specify the stride length:
313+
const int strideX = 2;
314+
315+
// Compute the variance:
316+
float v = stdlib_strided_svariance( N, 1.0f, x, strideX );
317+
318+
// Print the result:
319+
printf( "sample variance: %f\n", v );
320+
}
321+
```
322+
323+
</section>
324+
325+
<!-- /.examples -->
326+
327+
</section>
328+
329+
<!-- /.c -->
330+
331+
<section class="references">
332+
333+
</section>
334+
335+
<!-- /.references -->
336+
337+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
338+
339+
<section class="related">
340+
341+
* * *
342+
343+
## See Also
344+
345+
- <span class="package-name">[`@stdlib/stats/strided/dvariance`][@stdlib/stats/strided/dvariance]</span><span class="delimiter">: </span><span class="description">calculate the variance of a double-precision floating-point strided array.</span>
346+
- <span class="package-name">[`@stdlib/stats/base/snanvariance`][@stdlib/stats/base/snanvariance]</span><span class="delimiter">: </span><span class="description">calculate the variance of a single-precision floating-point strided array ignoring NaN values.</span>
347+
- <span class="package-name">[`@stdlib/stats/strided/sstdev`][@stdlib/stats/strided/sstdev]</span><span class="delimiter">: </span><span class="description">calculate the standard deviation of a single-precision floating-point strided array.</span>
348+
- <span class="package-name">[`@stdlib/stats/base/variance`][@stdlib/stats/base/variance]</span><span class="delimiter">: </span><span class="description">calculate the variance of a strided array.</span>
349+
350+
</section>
351+
352+
<!-- /.related -->
353+
354+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
355+
356+
<section class="links">
357+
358+
[variance]: https://en.wikipedia.org/wiki/Variance
359+
360+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
361+
362+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
363+
364+
<!-- <related-links> -->
365+
366+
[@stdlib/stats/strided/dvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dvariance
367+
368+
[@stdlib/stats/base/snanvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/snanvariance
369+
370+
[@stdlib/stats/strided/sstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/sstdev
371+
372+
[@stdlib/stats/base/variance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/variance
373+
374+
<!-- </related-links> -->
375+
376+
</section>
377+
378+
<!-- /.links -->

0 commit comments

Comments
 (0)