time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (2422B)


      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 isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ).isPrimitive;
     24 var sqrt = require( '@stdlib/math/base/special/sqrt' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Returns an accumulator function which incrementally computes an exponentially weighted standard deviation.
     31 *
     32 * @param {NonNegativeNumber} alpha - smoothing factor
     33 * @throws {TypeError} must provide a nonnegative number
     34 * @throws {RangeError} must be on the interval `[0,1]`
     35 * @returns {Function} accumulator function
     36 *
     37 * @example
     38 * var accumulator = increwstdev( 0.5 );
     39 *
     40 * var s = accumulator();
     41 * // returns null
     42 *
     43 * s = accumulator( 2.0 );
     44 * // returns 0.0
     45 *
     46 * s = accumulator( -5.0 );
     47 * // returns 3.5
     48 *
     49 * s = accumulator();
     50 * // returns 3.5
     51 */
     52 function increwstdev( alpha ) {
     53 	var incr;
     54 	var s2;
     55 	var s;
     56 	var r;
     57 	var m;
     58 	var c;
     59 	if ( !isNonNegativeNumber( alpha ) ) {
     60 		throw new TypeError( 'invalid argument. Must provide a nonnegative number. Value: `' + alpha + '`.' );
     61 	}
     62 	if ( alpha < 0.0 || alpha > 1.0 ) {
     63 		throw new RangeError( 'invalid argument. Must provide a nonnegative number on the interval [0,1]. Value: `' + alpha + '`.' );
     64 	}
     65 	c = 1.0 - alpha;
     66 	return accumulator;
     67 
     68 	/**
     69 	* If provided a value, the accumulator function returns an updated standard deviation. If not provided a value, the accumulator function returns the current standard deviation.
     70 	*
     71 	* @private
     72 	* @param {number} [x] - new value
     73 	* @returns {(number|null)} standard deviation or null
     74 	*/
     75 	function accumulator( x ) {
     76 		if ( arguments.length === 0 ) {
     77 			return ( s === void 0 ) ? null : s;
     78 		}
     79 		if ( s === void 0 ) {
     80 			m = x;
     81 			s2 = 0.0;
     82 		} else {
     83 			r = x - m;
     84 			incr = alpha * r;
     85 			m += incr;
     86 			s2 = c * ( s2+(r*incr) );
     87 		}
     88 		s = sqrt( s2 );
     89 		return s;
     90 	}
     91 }
     92 
     93 
     94 // EXPORTS //
     95 
     96 module.exports = increwstdev;