Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

'use strict';

Check warning on line 19 in lib/node_modules/@stdlib/stats/incr/lib/index.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

`nanmin` should be exported from namespace `index.js`

Check warning on line 19 in lib/node_modules/@stdlib/stats/incr/lib/index.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

`nanmcv` should be exported from namespace `index.js`

Check warning on line 19 in lib/node_modules/@stdlib/stats/incr/lib/index.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

`nanhmean` should be exported from namespace `index.js`

Check warning on line 19 in lib/node_modules/@stdlib/stats/incr/lib/index.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

`nangmean` should be exported from namespace `index.js`

/*
* When adding modules to the namespace, ensure that they are added in alphabetical order according to module name.
Expand Down Expand Up @@ -945,6 +945,15 @@
*/
setReadOnly( ns, 'incrwmean', require( '@stdlib/stats/incr/wmean' ) );

/**
* @name incrwvariance
* @memberof ns
* @readonly
* @type {Function}
* @see {@link module:@stdlib/stats/incr/wvariance}
*/
setReadOnly( ns, 'incrwvariance', require( '@stdlib/stats/incr/wvariance' ) );


// EXPORTS //

Expand Down
163 changes: 163 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/wvariance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<!--

@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.

-->

# incrwvariance

> Compute a [weighted variance][weighted-variance] incrementally.

<section class="intro">

The [weighted variance][weighted-variance] is defined as

<!-- <equation class="equation" label="eq:weighted_variance" align="center" raw="\sigma^2_w = \frac{\sum_{i=1}^N w_i (x_i - \mu_w)^2}{\sum_{i=1}^N w_i}" alt="Equation for the weighted variance."> -->

```math
\sigma^2_w = \frac{\sum_{i=1}^N w_i (x_i - \mu_w)^2}{\sum_{i=1}^N w_i}
```

<!-- <div class="equation" align="center" data-raw-text="\sigma^2_w = \frac{\sum_{i=1}^N w_i (x_i - \mu_w)^2}{\sum_{i=1}^N w_i}" data-equation="eq:weighted_variance">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@adbea9806383f70c982e3191475c874efba1296b/lib/node_modules/@stdlib/stats/incr/wvariance/docs/img/equation_weighted_variance.svg" alt="Equation for the weighted variance.">
<br>
</div> -->

<!-- </equation> -->

where `μ_w` is the weighted arithmetic mean.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var incrwvariance = require( '@stdlib/stats/incr/wvariance' );
```

#### incrwvariance()

Returns an accumulator `function` which incrementally computes a [weighted variance][weighted-variance].

```javascript
var accumulator = incrwvariance();
```

#### accumulator( \[x, w] )

If provided an input value `x` and a weight `w`, the accumulator function returns an updated weighted variance. If not provided any input values, the accumulator function returns the current variance.

```javascript
var accumulator = incrwvariance();

var s2 = accumulator( 2.0, 1.0 );
// returns 0.0

s2 = accumulator( 2.0, 0.5 );
// returns 0.0

s2 = accumulator( 3.0, 1.5 );
// returns 0.25

s2 = accumulator();
// returns 0.25
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **all** future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var incrwvariance = require( '@stdlib/stats/incr/wvariance' );

var accumulator;
var v;
var w;
var i;

// Initialize an accumulator:
accumulator = incrwvariance();

// For each simulated datum, update the weighted variance...
for ( i = 0; i < 100; i++ ) {
v = randu() * 100.0;
w = randu() * 100.0;
accumulator( v, w );
}
console.log( accumulator() );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/stats/incr/ewvariance`][@stdlib/stats/incr/ewvariance]</span><span class="delimiter">: </span><span class="description">compute an exponentially weighted variance incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/variance`][@stdlib/stats/incr/variance]</span><span class="delimiter">: </span><span class="description">compute a variance incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/wmean`][@stdlib/stats/incr/wmean]</span><span class="delimiter">: </span><span class="description">compute a weighted arithmetic mean incrementally.</span>

</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">

[weighted-variance]: https://en.wikipedia.org/wiki/Weighted_arithmetic_mean#Weighted_sample_variance

<!-- <related-links> -->

[@stdlib/stats/incr/ewvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/ewvariance

[@stdlib/stats/incr/variance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/variance

[@stdlib/stats/incr/wmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/wmean

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @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.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var incrwvariance = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var f;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
f = incrwvariance();
if ( typeof f !== 'function' ) {
b.fail( 'should return a function' );
}
}
b.toc();
if ( typeof f !== 'function' ) {
b.fail( 'should return a function' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s::accumulator', pkg ), function benchmark( b ) {
var acc;
var v;
var i;

acc = incrwvariance();

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = acc( randu(), 1.0 );
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
39 changes: 39 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/wvariance/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

{{alias}}()
Returns an accumulator function which incrementally computes a weighted
variance.

If provided arguments, the accumulator function returns an updated weighted
variance. If not provided arguments, the accumulator function returns the
current weighted variance.

If provided `NaN` or a value which, when used in computations, results in
`NaN`, the accumulated value is `NaN` for all future invocations.

The accumulator function accepts two arguments:

- x: value.
- w: weight.

Returns
-------
acc: Function
Accumulator function.

Examples
--------
> var accumulator = {{alias}}();
> var s2 = accumulator()
null
> s2 = accumulator( 2.0, 1.0 )
0.0
> s2 = accumulator( 2.0, 0.5 )
0.0
> s2 = accumulator( 3.0, 1.5 )
0.25
> s2 = accumulator()
0.25

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* @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.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

/**
* If provided both arguments, returns an updated weighted variance; otherwise, returns the current weighted variance.
*
* ## Notes
*
* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations.
*
* @param x - value
* @param w - weight
* @returns weighted variance
*/
type accumulator = ( x?: number, w?: number ) => number | null;

/**
* Returns an accumulator function which incrementally computes a weighted variance.
*
* @returns accumulator function
*
* @example
* var accumulator = incrwvariance();
*
* var s2 = accumulator();
* // returns null
*
* s2 = accumulator( 2.0, 1.0 );
* // returns 0.0
*
* s2 = accumulator( 2.0, 0.5 );
* // returns 0.0
*
* s2 = accumulator( 3.0, 1.5 );
* // returns 0.25
*
* s2 = accumulator();
* // returns 0.25
*/
declare function incrwvariance(): accumulator;


// EXPORTS //

export = incrwvariance;
Loading