All files / stats/incr/wstdev/lib main.js

97.08% Statements 200/206
87.5% Branches 21/24
100% Functions 3/3
97.08% Lines 200/206

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 2071x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 10x 8x 8x 2x 2x 2x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 29x 2x 1x 1x 1x 1x 29x 11x 11x 29x 2x 2x 14x 14x 14x 14x 14x 14x 29x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 5x 1x     1x 1x 5x     5x     4x 4x 4x 4x 4x 5x 26x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ).isPrimitive;
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var format = require( '@stdlib/string/format' );
 
 
// MAIN //
 
/**
* Returns an accumulator function which incrementally computes a weighted sample standard deviation.
*
* ## Method
*
* -   The weighted sample standard deviation is defined as
*
*     ```tex
*     \sigma = \sqrt{\frac{\sum_{i=0}^{n-1} w_i (x_i - \mu)^2}{\sum_{i=0}^{n-1} w_i}}
*     ```
*
*     where \\( w_i \\) are the weights and \\( \mu \\) is the weighted arithmetic mean.
*
* -   To derive an incremental formula for computing a weighted sample standard deviation, let:
*
*     ```tex
*     W_n = \sum_{i=1}^{n} w_i
*     ```
*
* -   Accordingly:
*
*     ```tex
*     \begin{align*}
*     \sigma^2 &= \frac{1}{W_n} \sum_{i=1}^{n} w_i (x_i - \mu)^2 \\
*              &= \frac{1}{W_n} \sum_{i=1}^{n} w_i x_i^2 - \mu^2
*     \end{align*}
*     ```
*
* -   Let:
*
*     ```tex
*     M2_n = W_n \sigma^2_n = \sum_{i=1}^{n} w_i x_i^2 - W_n \mu_n^2
*     ```
*
* -   To compute the incremental update for \\(M2_n\\):
*
*     ```tex
*     \begin{align*}
*     M2_n - M2_{n-1} &= \sum_{i=1}^{n} w_i x_i^2 - W_n \mu_n^2 - \sum_{i=1}^{n-1} w_i x_i^2 + W_{n-1} \mu_{n-1}^2 \\
*                   &= w_n x_n^2 - W_n \mu_n^2 + W_{n-1} \mu_{n-1}^2 \\
*                   &= w_n x_n^2 - W_n \mu_n^2 + (W_n - w_n) \mu_{n-1}^2 \\
*                   &= w_n (x_n^2 - \mu_{n-1}^2) + W_n (\mu_{n-1}^2 - \mu_n^2)
*     \end{align*}
*     ```
*
* -   Simplify further:
*
*     ```tex
*     \begin{align*}
*     M2_n - M2_{n-1} &= w_n (x_n - \mu_{n-1}) (x_n - \mu_n) \\
*     M2_n &= M2_{n-1} + w_n (x_n - \mu_{n-1}) (x_n - \mu_n)
*     \end{align*}
*     ```
*
* -   Finally, compute the weighted sample standard deviation:
*
*     ```tex
*     \sigma_n = \sqrt{\frac{M2_n}{W_n}}
*     ```
*
* -   Incrementally updating the weighted sample mean:
*
*     ```tex
*     \begin{align*}
*     \mu_n &= \mu_{n-1} + \frac{w_n}{W_n} (x_n - \mu_{n-1})
*     \end{align*}
*     ```
*
* @param {number} [mean] - mean value
* @throws {TypeError} must provide a number primitive
* @returns {Function} accumulator function
*
* @example
* var accumulator = incrwstdev();
*
* var s = accumulator();
* // returns null
*
* s = accumulator( 2.0, 1.0 );
* // returns 0.0
*
* s = accumulator( 3.0, 2.0 );
* // returns ~0.471
*
* s = accumulator( 2.0, 0.1 );
* // returns ~0.478
*
* s = accumulator();
* // returns ~0.478
*
* @example
* var accumulator = incrwstdev( 3.0 );
*/
function incrwstdev( mean ) {
	var delta;
	var wsum;
	var FLG;
	var M2;
	var mu;
 
	wsum = 0.0;
	M2 = 0.0;
	if ( arguments.length ) {
		if ( !isNumber( mean ) ) {
			throw new TypeError( format( 'invalid argument. Must provide a number. Value: `%s`.', mean ) );
		}
		mu = mean;
		return accumulator2;
	}
	mu = 0.0;
	return accumulator1;
 
	/**
	* If provided a value, the accumulator function returns an updated weighted sample standard deviation. If not provided a value, the accumulator function returns the current weighted sample standard deviation.
	*
	* @private
	* @param {number} [x] - value
	* @param {NonNegativeNumber} [w] - weight
	* @throws {TypeError} second argument must be a nonnegative number
	* @returns {(number|null)} weighted sample standard deviation or null
	*/
	function accumulator1( x, w ) {
		if ( arguments.length === 0 ) {
			if ( FLG === void 0 ) {
				return null;
			}
			return sqrt( M2 / wsum );
		}
		if ( !isNonNegativeNumber( w ) ) {
			throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative number. Value: `%s`.', w ) );
		}
		if ( w === 0.0 ) {
			return ( FLG === void 0 ) ? null : sqrt( M2 / wsum );
		}
		FLG = true;
		delta = x - mu;
		wsum += w;
		mu += ( w / wsum ) * delta;
		M2 += w * delta * ( x - mu );
		return sqrt( M2 / wsum );
	}
 
	/**
	* If provided a value, the accumulator function returns an updated weighted sample standard deviation. If not provided a value, the accumulator function returns the current weighted sample standard deviation.
	*
	* @private
	* @param {number} [x] - value
	* @param {NonNegativeNumber} [w] - weight
	* @throws {TypeError} second argument must be a nonnegative number
	* @returns {(number|null)} weighted sample standard deviation or null
	*/
	function accumulator2( x, w ) {
		if ( arguments.length === 0 ) {
			if ( FLG === void 0 ) {
				return null;
			}
			return sqrt( M2 / wsum );
		}
		if ( !isNonNegativeNumber( w ) ) {
			throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative number. Value: `%s`.', w ) );
		}
		if ( w === 0.0 ) {
			return ( FLG === void 0 ) ? null : sqrt( M2 / wsum );
		}
		FLG = true;
		delta = x - mu;
		wsum += w;
		M2 += w * delta * delta;
		return sqrt( M2 / wsum );
	}
}
 
 
// EXPORTS //
 
module.exports = incrwstdev;