Skip to content

Commit 5cee2da

Browse files
committed
feat: add stats/strided/dvarm
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 7496a50 commit 5cee2da

37 files changed

+3785
-0
lines changed
Lines changed: 372 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,372 @@
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+
# dvarm
22+
23+
> Calculate the [variance][variance] of a double-precision floating-point strided array provided a known mean.
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@79744a33483f9d23b00195aaf2d4b66614b97fac/lib/node_modules/@stdlib/stats/strided/dvarm/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@79744a33483f9d23b00195aaf2d4b66614b97fac/lib/node_modules/@stdlib/stats/strided/dvarm/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@79744a33483f9d23b00195aaf2d4b66614b97fac/lib/node_modules/@stdlib/stats/strided/dvarm/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@79744a33483f9d23b00195aaf2d4b66614b97fac/lib/node_modules/@stdlib/stats/strided/dvarm/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 dvarm = require( '@stdlib/stats/strided/dvarm' );
99+
```
100+
101+
#### dvarm( N, mean, correction, x, strideX )
102+
103+
Computes the [variance][variance] of a double-precision floating-point strided array provided a known `mean`.
104+
105+
```javascript
106+
var Float64Array = require( '@stdlib/array/float64' );
107+
108+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
109+
110+
var v = dvarm( x.length, 1.0/3.0, 1, x, 1 );
111+
// returns ~4.3333
112+
```
113+
114+
The function has the following parameters:
115+
116+
- **N**: number of indexed elements.
117+
- **mean**: mean.
118+
- **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).
119+
- **x**: input [`Float64Array`][@stdlib/array/float64].
120+
- **stride**: stride length for `x`.
121+
122+
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`,
123+
124+
```javascript
125+
var Float64Array = require( '@stdlib/array/float64' );
126+
127+
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
128+
129+
var v = dvarm( 4, 1.25, 1, x, 2 );
130+
// returns 6.25
131+
```
132+
133+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
134+
135+
<!-- eslint-disable stdlib/capitalized-comments -->
136+
137+
```javascript
138+
var Float64Array = require( '@stdlib/array/float64' );
139+
140+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
141+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
142+
143+
var v = dvarm( 4, 1.25, 1, x1, 2 );
144+
// returns 6.25
145+
```
146+
147+
#### dvarm.ndarray( N, mean, correction, x, strideX, offsetX )
148+
149+
Computes the [variance][variance] of a double-precision floating-point strided array provided a known `mean` and using alternative indexing semantics.
150+
151+
```javascript
152+
var Float64Array = require( '@stdlib/array/float64' );
153+
154+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
155+
156+
var v = dvarm.ndarray( x.length, 1.0/3.0, 1, x, 1, 0 );
157+
// returns ~4.33333
158+
```
159+
160+
The function has the following additional parameters:
161+
162+
- **offsetX**: starting index for `x`.
163+
164+
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
165+
166+
```javascript
167+
var Float64Array = require( '@stdlib/array/float64' );
168+
169+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
170+
171+
var v = dvarm.ndarray( 4, 1.25, 1, x, 2, 1 );
172+
// returns 6.25
173+
```
174+
175+
</section>
176+
177+
<!-- /.usage -->
178+
179+
<section class="notes">
180+
181+
## Notes
182+
183+
- If `N <= 0`, both functions return `NaN`.
184+
- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`.
185+
186+
</section>
187+
188+
<!-- /.notes -->
189+
190+
<section class="examples">
191+
192+
## Examples
193+
194+
<!-- eslint no-undef: "error" -->
195+
196+
```javascript
197+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
198+
var dvarm = require( '@stdlib/stats/strided/dvarm' );
199+
200+
var x = discreteUniform( 10, -50, 50, {
201+
'dtype': 'float64'
202+
});
203+
console.log( x );
204+
205+
var v = dvarm( x.length, 0.0, 1, x, 1 );
206+
console.log( v );
207+
```
208+
209+
</section>
210+
211+
<!-- /.examples -->
212+
213+
<!-- C interface documentation. -->
214+
215+
* * *
216+
217+
<section class="c">
218+
219+
## C APIs
220+
221+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
222+
223+
<section class="intro">
224+
225+
</section>
226+
227+
<!-- /.intro -->
228+
229+
<!-- C usage documentation. -->
230+
231+
<section class="usage">
232+
233+
### Usage
234+
235+
```c
236+
#include "stdlib/stats/strided/dvarm.h"
237+
```
238+
239+
#### stdlib_strided_dvarm( N, mean, correction, \*X, strideX )
240+
241+
Computes the [variance][variance] of a double-precision floating-point strided array provided a known `mean`.
242+
243+
```c
244+
const double x[] = { 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 };
245+
246+
double v = stdlib_strided_dvarm( 4, 1.25, 1.0, x, 2 );
247+
// returns 6.25
248+
```
249+
250+
The function accepts the following arguments:
251+
252+
- **N**: `[in] CBLAS_INT` number of indexed elements.
253+
- **mean**: `[in] double` mean.
254+
- **correction**: `[in] double` 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).
255+
- **X**: `[in] double*` input array.
256+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
257+
258+
```c
259+
double stdlib_strided_dvarm( const CBLAS_INT N, const double mean, const double correction, const double *X, const CBLAS_INT strideX );
260+
```
261+
262+
#### stdlib_strided_dvarm_ndarray( N, mean, correction, \*X, strideX, offsetX )
263+
264+
Computes the [variance][variance] of a double-precision floating-point strided array provided a known `mean` and using alternative indexing semantics.
265+
266+
```c
267+
const double x[] = { 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 };
268+
269+
double v = stdlib_strided_dvarm_ndarray( 4, 1.25, 1.0, x, 2, 1 );
270+
// returns 6.25
271+
```
272+
273+
The function accepts the following arguments:
274+
275+
- **N**: `[in] CBLAS_INT` number of indexed elements.
276+
- **mean**: `[in] double` mean.
277+
- **correction**: `[in] double` 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).
278+
- **X**: `[in] double*` input array.
279+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
280+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
281+
282+
```c
283+
double stdlib_strided_dvarm_ndarray( const CBLAS_INT N, const double mean, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
284+
```
285+
286+
</section>
287+
288+
<!-- /.usage -->
289+
290+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
291+
292+
<section class="notes">
293+
294+
</section>
295+
296+
<!-- /.notes -->
297+
298+
<!-- C API usage examples. -->
299+
300+
<section class="examples">
301+
302+
### Examples
303+
304+
```c
305+
#include "stdlib/stats/strided/dvarm.h"
306+
#include <stdio.h>
307+
308+
int main( void ) {
309+
// Create a strided array:
310+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
311+
312+
// Specify the number of elements:
313+
const int N = 4;
314+
315+
// Specify the stride length:
316+
const int strideX = 2;
317+
318+
// Compute the variance:
319+
double v = stdlib_strided_dvarm( N, 4.5, 1, x, strideX );
320+
321+
// Print the result:
322+
printf( "sample variance: %lf\n", v );
323+
}
324+
```
325+
326+
</section>
327+
328+
<!-- /.examples -->
329+
330+
</section>
331+
332+
<!-- /.c -->
333+
334+
<section class="references">
335+
336+
</section>
337+
338+
<!-- /.references -->
339+
340+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
341+
342+
<section class="related">
343+
344+
* * *
345+
346+
## See Also
347+
348+
- <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>
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/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
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+
<!-- </related-links> -->
369+
370+
</section>
371+
372+
<!-- /.links -->

0 commit comments

Comments
 (0)