time-to-botec

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

main.js (2230B)


      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 abs = require( '@stdlib/math/base/special/abs' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns an accumulator function which incrementally computes a moving mean absolute percentage 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 = incrmmape( 3 );
     39 *
     40 * var m = accumulator();
     41 * // returns null
     42 *
     43 * m = accumulator( 2.0, 3.0 );
     44 * // returns ~33.33
     45 *
     46 * m = accumulator( 5.0, 2.0 );
     47 * // returns ~91.67
     48 *
     49 * m = accumulator( 3.0, 2.0 );
     50 * // returns ~77.78
     51 *
     52 * m = accumulator( 2.0, 5.0 );
     53 * // returns ~86.67
     54 *
     55 * m = accumulator();
     56 * // returns ~86.67
     57 */
     58 function incrmmape( 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 mean absolute percentage error. If not provided input values, the accumulator function returns the current mean absolute percentage error.
     68 	*
     69 	* @private
     70 	* @param {number} [f] - input value
     71 	* @param {number} [a] - input value
     72 	* @returns {(number|null)} mean absolute percentage error or null
     73 	*/
     74 	function accumulator( f, a ) {
     75 		if ( arguments.length === 0 ) {
     76 			return mean();
     77 		}
     78 		return mean( 100.0 * abs( (a-f)/a ) );
     79 	}
     80 }
     81 
     82 
     83 // EXPORTS //
     84 
     85 module.exports = incrmmape;