time-to-botec

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

main.js (3457B)


      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 ln = require( '@stdlib/math/base/special/ln' );
     26 var exp = require( '@stdlib/math/base/special/exp' );
     27 var Float64Array = require( '@stdlib/array/float64' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Returns an accumulator function which incrementally computes a moving geometric mean.
     34 *
     35 * @param {PositiveInteger} W - window size
     36 * @throws {TypeError} must provide a positive integer
     37 * @returns {Function} accumulator function
     38 *
     39 * @example
     40 * var accumulator = incrmgmean( 3 );
     41 *
     42 * var v = accumulator();
     43 * // returns null
     44 *
     45 * v = accumulator( 2.0 );
     46 * // returns 2.0
     47 *
     48 * v = accumulator( 5.0 );
     49 * // returns ~3.16
     50 *
     51 * v = accumulator( 3.0 );
     52 * // returns ~3.11
     53 *
     54 * v = accumulator( 5.0 );
     55 * // returns ~4.22
     56 *
     57 * v = accumulator();
     58 * // returns ~4.22
     59 */
     60 function incrmgmean( W ) {
     61 	var buf;
     62 	var sum;
     63 	var gm;
     64 	var N;
     65 	var i;
     66 	if ( !isPositiveInteger( W ) ) {
     67 		throw new TypeError( 'invalid argument. Must provide a positive integer. Value: `' + W + '`.' );
     68 	}
     69 	buf = new Float64Array( W );
     70 	sum = 0.0;
     71 	i = -1;
     72 	N = 0;
     73 
     74 	return accumulator;
     75 
     76 	/**
     77 	* If provided a value, the accumulator function returns an updated geometric mean. If not provided a value, the accumulator function returns the current geometric mean.
     78 	*
     79 	* @private
     80 	* @param {number} [x] - input value
     81 	* @returns {(number|null)} geometric mean or null
     82 	*/
     83 	function accumulator( x ) {
     84 		var lnx;
     85 		var k;
     86 		if ( arguments.length === 0 ) {
     87 			if ( N === 0 ) {
     88 				return null;
     89 			}
     90 			return gm;
     91 		}
     92 		// Update the index for managing the circular buffer:
     93 		i = (i+1) % W;
     94 
     95 		// Compute the natural log:
     96 		lnx = ln( x );
     97 
     98 		// Case: incoming value is NaN, the accumulated value is automatically NaN...
     99 		if ( isnan( x ) ) {
    100 			N = W; // explicitly set to avoid `N < W` branch
    101 			sum = NaN;
    102 		}
    103 		// Case: initial window...
    104 		else if ( N < W ) {
    105 			N += 1;
    106 			sum += lnx;
    107 		}
    108 		// Case: outgoing value is NaN, and, thus, we need to compute the accumulated value...
    109 		else if ( isnan( buf[ i ] ) ) {
    110 			N = 1;
    111 			sum = lnx;
    112 			for ( k = 0; k < W; k++ ) {
    113 				if ( k !== i ) {
    114 					if ( isnan( buf[ k ] ) ) {
    115 						N = W; // explicitly set to avoid `N < W` branch
    116 						sum = NaN;
    117 						break; // sum is automatically NaN, so no need to continue
    118 					}
    119 					N += 1;
    120 					sum += buf[ k ];
    121 				}
    122 			}
    123 		}
    124 		// Case: neither the current accumulated value nor the incoming value are NaN, so we need to update the accumulated value...
    125 		else if ( isnan( sum ) === false ) {
    126 			sum += lnx - buf[ i ];
    127 		}
    128 		// Case: the current accumulated value is NaN, so nothing to do until the buffer no longer contains NaN values...
    129 		buf[ i ] = lnx;
    130 
    131 		gm = exp( sum/N );
    132 		return gm;
    133 	}
    134 }
    135 
    136 
    137 // EXPORTS //
    138 
    139 module.exports = incrmgmean;