Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
149 changes: 149 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,149 @@
<!--

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

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

<!-- </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.
Comment thread
Om-A-osc marked this conversation as resolved.
Outdated

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;
69 changes: 69 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/wvariance/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* @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.
*/

import incrwvariance = require( './index' );


// TESTS //

// The function returns an accumulator function...
{
incrwvariance(); // $ExpectType accumulator
}

// The compiler throws an error if the function is provided arguments...
{
incrwvariance( '5' ); // $ExpectError
incrwvariance( 5 ); // $ExpectError
incrwvariance( true ); // $ExpectError
incrwvariance( false ); // $ExpectError
incrwvariance( null ); // $ExpectError
incrwvariance( undefined ); // $ExpectError
incrwvariance( [] ); // $ExpectError
incrwvariance( {} ); // $ExpectError
incrwvariance( ( x: number ): number => x ); // $ExpectError
}

// The function returns an accumulator function which returns an accumulated result...
{
const acc = incrwvariance();

acc(); // $ExpectType number | null
acc( 3.14, 1.0 ); // $ExpectType number | null
}

// The compiler throws an error if the returned accumulator function is provided invalid arguments...
{
const acc = incrwvariance();

acc( '5', 1.0 ); // $ExpectError
acc( true, 1.0 ); // $ExpectError
acc( false, 1.0 ); // $ExpectError
acc( null, 1.0 ); // $ExpectError
acc( [], 1.0 ); // $ExpectError
acc( {}, 1.0 ); // $ExpectError
acc( ( x: number ): number => x, 1.0 ); // $ExpectError

acc( 3.14, '5' ); // $ExpectError
acc( 3.14, true ); // $ExpectError
acc( 3.14, false ); // $ExpectError
acc( 3.14, null ); // $ExpectError
acc( 3.14, [] ); // $ExpectError
acc( 3.14, {} ); // $ExpectError
acc( 3.14, ( x: number ): number => x ); // $ExpectError
}
Loading