main.js (3261B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; 24 var isnan = require( '@stdlib/math/base/assert/is-nan' ); 25 var Float64Array = require( '@stdlib/array/float64' ); 26 27 28 // MAIN // 29 30 /** 31 * Returns an accumulator function which incrementally computes a moving arithmetic mean. 32 * 33 * @param {PositiveInteger} W - window size 34 * @throws {TypeError} must provide a positive integer 35 * @returns {Function} accumulator function 36 * 37 * @example 38 * var accumulator = incrmmean( 3 ); 39 * 40 * var mu = accumulator(); 41 * // returns null 42 * 43 * mu = accumulator( 2.0 ); 44 * // returns 2.0 45 * 46 * mu = accumulator( -5.0 ); 47 * // returns -1.5 48 * 49 * mu = accumulator( 3.0 ); 50 * // returns 0.0 51 * 52 * mu = accumulator( 5.0 ); 53 * // returns 1.0 54 * 55 * mu = accumulator(); 56 * // returns 1.0 57 */ 58 function incrmmean( W ) { 59 var delta; 60 var buf; 61 var mu; 62 var N; 63 var i; 64 if ( !isPositiveInteger( W ) ) { 65 throw new TypeError( 'invalid argument. Must provide a positive integer. Value: `' + W + '`.' ); 66 } 67 buf = new Float64Array( W ); 68 mu = 0.0; 69 i = -1; 70 N = 0; 71 72 return accumulator; 73 74 /** 75 * If provided a value, the accumulator function returns an updated mean. If not provided a value, the accumulator function returns the current mean. 76 * 77 * @private 78 * @param {number} [x] - input value 79 * @returns {(number|null)} mean or null 80 */ 81 function accumulator( x ) { 82 var k; 83 if ( arguments.length === 0 ) { 84 if ( N === 0 ) { 85 return null; 86 } 87 return mu; 88 } 89 // Update the index for managing the circular buffer: 90 i = (i+1) % W; 91 92 // Case: incoming value is NaN, the sliding mean is automatically NaN... 93 if ( isnan( x ) ) { 94 N = W; // explicitly set to avoid `N < W` branch 95 mu = NaN; 96 } 97 // Case: initial window... 98 else if ( N < W ) { 99 N += 1; 100 delta = x - mu; 101 mu += delta / N; 102 } 103 // Case: outgoing value is NaN, and, thus, we need to compute the sample mean... 104 else if ( isnan( buf[ i ] ) ) { 105 N = 1; 106 mu = x; 107 for ( k = 0; k < W; k++ ) { 108 if ( k !== i ) { 109 if ( isnan( buf[ k ] ) ) { 110 N = W; // explicitly set to avoid `N < W` branch 111 mu = NaN; 112 break; // mean is automatically NaN, so no need to continue 113 } 114 N += 1; 115 delta = buf[ k ] - mu; 116 mu += delta / N; 117 } 118 } 119 } 120 // Case: neither the current mean nor the incoming value are NaN, so we need to update the sample mean... 121 else if ( isnan( mu ) === false ) { 122 delta = x - buf[ i ]; 123 mu += delta / W; 124 } 125 // Case: the current mean is NaN, so nothing to do until the buffer no longer contains NaN values... 126 127 buf[ i ] = x; 128 return mu; 129 } 130 } 131 132 133 // EXPORTS // 134 135 module.exports = incrmmean;