-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add stats/strided/dnanvarmpn
#10568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Om-A-osc
wants to merge
2
commits into
stdlib-js:develop
Choose a base branch
from
Om-A-osc:feat/stats-strided-dnanvarmpn
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
387 changes: 387 additions & 0 deletions
387
lib/node_modules/@stdlib/stats/strided/dnanvarmpn/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,387 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # dnanvarmpn | ||
|
|
||
| > Calculate the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values, provided a known mean, and using Neely's correction algorithm. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| The population [variance][variance] of a finite size population of size `N` is given by | ||
|
|
||
| <!-- <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."> --> | ||
|
|
||
| ```math | ||
| \sigma^2 = \frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2 | ||
| ``` | ||
|
|
||
| <!-- <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"> | ||
| <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@083669e7e6ae042b7a77ba147a320a1f3c5379ae/lib/node_modules/@stdlib/stats/strided/dnanvarmpn/docs/img/equation_population_variance.svg" alt="Equation for the population variance."> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| where the population mean is given by | ||
|
|
||
| <!-- <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."> --> | ||
|
|
||
| ```math | ||
| \mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i | ||
| ``` | ||
|
|
||
| <!-- <div class="equation" align="center" data-raw-text="\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i" data-equation="eq:population_mean"> | ||
| <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@083669e7e6ae042b7a77ba147a320a1f3c5379ae/lib/node_modules/@stdlib/stats/strided/dnanvarmpn/docs/img/equation_population_mean.svg" alt="Equation for the population mean."> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| 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`, | ||
|
|
||
| <!-- <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."> --> | ||
|
|
||
| ```math | ||
| s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 | ||
| ``` | ||
|
|
||
| <!-- <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"> | ||
| <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@083669e7e6ae042b7a77ba147a320a1f3c5379ae/lib/node_modules/@stdlib/stats/strided/dnanvarmpn/docs/img/equation_unbiased_sample_variance.svg" alt="Equation for computing an unbiased sample variance."> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| where the sample mean is given by | ||
|
|
||
| <!-- <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."> --> | ||
|
|
||
| ```math | ||
| \bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i | ||
| ``` | ||
|
|
||
| <!-- <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"> | ||
| <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@083669e7e6ae042b7a77ba147a320a1f3c5379ae/lib/node_modules/@stdlib/stats/strided/dnanvarmpn/docs/img/equation_sample_mean.svg" alt="Equation for the sample mean."> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| 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. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var dnanvarmpn = require( '@stdlib/stats/strided/dnanvarmpn' ); | ||
| ``` | ||
|
|
||
| #### dnanvarmpn( N, correction, mean, x, strideX ) | ||
|
|
||
| Computes the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values, provided a known `mean`, and using Neely's correction algorithm. | ||
|
|
||
| ```javascript | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
|
|
||
| var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); | ||
|
|
||
| var v = dnanvarmpn( x.length, 1, 1.0/3.0, x, 1 ); | ||
| // returns ~4.3333 | ||
| ``` | ||
|
|
||
| The function has the following parameters: | ||
|
|
||
| - **N**: number of indexed elements. | ||
| - **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 and `n` corresponds to the number of non-`NaN` indexed elements. 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). | ||
| - **mean**: mean. | ||
| - **x**: input [`Float64Array`][@stdlib/array/float64]. | ||
| - **strideX**: stride length for `x`. | ||
|
|
||
| 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`, | ||
|
|
||
| ```javascript | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
|
|
||
| var x = new Float64Array([ | ||
| 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN | ||
| ]); | ||
|
|
||
| var v = dnanvarmpn( 5, 1, 1.25, x, 2 ); | ||
| // returns 6.25 | ||
| ``` | ||
|
|
||
| Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. | ||
|
|
||
| <!-- eslint-disable stdlib/capitalized-comments --> | ||
|
|
||
| ```javascript | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
|
|
||
| var x0 = new Float64Array([ | ||
| 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN | ||
| ]); | ||
| var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | ||
|
|
||
| var v = dnanvarmpn( 5, 1, 1.25, x1, 2 ); | ||
| // returns 6.25 | ||
| ``` | ||
|
|
||
| #### dnanvarmpn.ndarray( N, correction, mean, x, strideX, offsetX ) | ||
|
|
||
| Computes the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values, provided a known `mean`, and using Neely's correction algorithm and alternative indexing semantics. | ||
|
|
||
| ```javascript | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
|
|
||
| var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); | ||
|
|
||
| var v = dnanvarmpn.ndarray( x.length, 1, 1.0/3.0, x, 1, 0 ); | ||
| // returns ~4.3333 | ||
| ``` | ||
|
|
||
| The function has the following additional parameters: | ||
|
|
||
| - **offsetX**: starting index for `x`. | ||
|
|
||
| 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 | ||
|
|
||
| ```javascript | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
|
|
||
| var x = new Float64Array([ | ||
| 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN | ||
| ]); | ||
|
|
||
| var v = dnanvarmpn.ndarray( 5, 1, 1.25, x, 2, 1 ); | ||
| // returns 6.25 | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - If `N <= 0`, both functions return `NaN`. | ||
| - If `n - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements), both functions return `NaN`. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var uniform = require( '@stdlib/random/base/uniform' ); | ||
| var bernoulli = require( '@stdlib/random/base/bernoulli' ); | ||
| var filledarrayBy = require( '@stdlib/array/filled-by' ); | ||
| var dnanvarmpn = require( '@stdlib/stats/strided/dnanvarmpn' ); | ||
|
|
||
| function rand() { | ||
| if ( bernoulli( 0.8 ) < 1 ) { | ||
| return NaN; | ||
| } | ||
| return uniform( -50.0, 50.0 ); | ||
| } | ||
|
|
||
| var x = filledarrayBy( 10, 'float64', rand ); | ||
| console.log( x ); | ||
|
|
||
| var v = dnanvarmpn( x.length, 1, 0.0, x, 1 ); | ||
| console.log( v ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- C interface documentation. --> | ||
|
|
||
| * * * | ||
|
|
||
| <section class="c"> | ||
|
|
||
| ## C APIs | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- C usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ### Usage | ||
|
|
||
| ```c | ||
| #include "stdlib/stats/strided/dnanvarmpn.h" | ||
| ``` | ||
|
|
||
| #### stdlib_strided_dnanvarmpn( N, correction, mean, \*X, strideX ) | ||
|
|
||
| Computes the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values, provided a known `mean`, and using Neely's correction algorithm. | ||
|
|
||
| ```c | ||
| const double x[] = { 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, 0.0/0.0, 0.0/0.0 }; | ||
|
|
||
| double v = stdlib_strided_dnanvarmpn( 5, 1.0, 1.25, x, 2 ); | ||
| // returns 6.25 | ||
| ``` | ||
|
|
||
| The function accepts the following arguments: | ||
|
|
||
| - **N**: `[in] CBLAS_INT` number of indexed elements. | ||
| - **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 and `n` corresponds to the number of non-`NaN` indexed elements. 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). | ||
| - **mean**: `[in] double` mean. | ||
| - **X**: `[in] double*` input array. | ||
| - **strideX**: `[in] CBLAS_INT` stride length for `X`. | ||
|
|
||
| ```c | ||
| double stdlib_strided_dnanvarmpn( const CBLAS_INT N, const double correction, const double mean, const double *X, const CBLAS_INT strideX ); | ||
| ``` | ||
|
|
||
| #### stdlib_strided_dnanvarmpn_ndarray( N, correction, mean, \*X, strideX, offsetX ) | ||
|
|
||
| Computes the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values, provided a known `mean`, and using Neely's correction algorithm and alternative indexing semantics. | ||
|
|
||
| ```c | ||
| const double x[] = { 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, 0.0/0.0, 0.0/0.0 }; | ||
|
|
||
| double v = stdlib_strided_dnanvarmpn_ndarray( 5, 1.0, 1.25, x, 2, 0 ); | ||
| // returns 6.25 | ||
| ``` | ||
|
|
||
| The function accepts the following arguments: | ||
|
|
||
| - **N**: `[in] CBLAS_INT` number of indexed elements. | ||
| - **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 and `n` corresponds to the number of non-`NaN` indexed elements. 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). | ||
| - **mean**: `[in] double` mean. | ||
| - **X**: `[in] double*` input array. | ||
| - **strideX**: `[in] CBLAS_INT` stride length for `X`. | ||
| - **offsetX**: `[in] CBLAS_INT` starting index for `X`. | ||
|
|
||
| ```c | ||
| double stdlib_strided_dnanvarmpn_ndarray( const CBLAS_INT N, const double correction, const double mean, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- C API usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ### Examples | ||
|
|
||
| ```c | ||
| #include "stdlib/stats/strided/dnanvarmpn.h" | ||
| #include <stdio.h> | ||
|
|
||
| int main( void ) { | ||
| // Create a strided array: | ||
| const double x[] = { 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, 0.0/0.0, 0.0/0.0 }; | ||
|
|
||
| // Specify the number of elements: | ||
| const int N = 5; | ||
|
|
||
| // Specify the stride length: | ||
| const int strideX = 2; | ||
|
|
||
| // Compute the variance: | ||
| double v = stdlib_strided_dnanvarmpn( N, 1, 1.25, x, strideX ); | ||
|
|
||
| // Print the result: | ||
| printf( "sample variance: %lf\n", v ); | ||
| } | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.c --> | ||
|
|
||
| <section class="references"> | ||
|
|
||
| ## References | ||
|
|
||
| - Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958][@neely:1966a]. | ||
| - Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036][@schubert:2018a]. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [variance]: https://en.wikipedia.org/wiki/Variance | ||
|
|
||
| [@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64 | ||
|
|
||
| [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | ||
|
|
||
| [@neely:1966a]: https://doi.org/10.1145/365719.365958 | ||
|
|
||
| [@schubert:2018a]: https://doi.org/10.1145/3221269.3223036 | ||
|
|
||
| <!-- <related-links> --> | ||
|
|
||
|
|
||
| <!-- </related-links> --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.