time-to-botec

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

main.js (2268B)


      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 incrmmean = require( './../../../incr/mmean' );
     25 var sqrt = require( '@stdlib/math/base/special/sqrt' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns an accumulator function which incrementally computes a moving root mean squared error.
     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 = incrmrmse( 3 );
     39 *
     40 * var r = accumulator();
     41 * // returns null
     42 *
     43 * r = accumulator( 2.0, 3.0 );
     44 * // returns 1.0
     45 *
     46 * r = accumulator( -5.0, 2.0 );
     47 * // returns 5.0
     48 *
     49 * r = accumulator( 3.0, 2.0 );
     50 * // returns ~4.12
     51 *
     52 * r = accumulator( 5.0, -2.0 );
     53 * // returns ~5.74
     54 *
     55 * r = accumulator();
     56 * // returns ~5.74
     57 */
     58 function incrmrmse( W ) {
     59 	var mean;
     60 	if ( !isPositiveInteger( W ) ) {
     61 		throw new TypeError( 'invalid argument. Must provide a positive integer. Value: `' + W + '`.' );
     62 	}
     63 	mean = incrmmean( W );
     64 	return accumulator;
     65 
     66 	/**
     67 	* If provided input values, the accumulator function returns an updated root mean squared error. If not provided input values, the accumulator function returns the current root mean squared error.
     68 	*
     69 	* @private
     70 	* @param {number} [x] - input value
     71 	* @param {number} [y] - input value
     72 	* @returns {(number|null)} root mean squared error or null
     73 	*/
     74 	function accumulator( x, y ) {
     75 		var r;
     76 		if ( arguments.length === 0 ) {
     77 			r = mean();
     78 			if ( r === null ) {
     79 				return r;
     80 			}
     81 			return sqrt( r );
     82 		}
     83 		r = y - x;
     84 		return sqrt( mean( r*r ) );
     85 	}
     86 }
     87 
     88 
     89 // EXPORTS //
     90 
     91 module.exports = incrmrmse;