time-to-botec

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

main.js (2328B)


      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 var atan = require( '@stdlib/math/base/special/atan' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Returns an accumulator function which incrementally computes a moving mean arctangent absolute percentage error.
     33 *
     34 * @param {PositiveInteger} W - window size
     35 * @throws {TypeError} must provide a positive integer
     36 * @returns {Function} accumulator function
     37 *
     38 * @example
     39 * var accumulator = incrmmaape( 3 );
     40 *
     41 * var m = accumulator();
     42 * // returns null
     43 *
     44 * m = accumulator( 2.0, 3.0 );
     45 * // returns ~0.32
     46 *
     47 * m = accumulator( 5.0, 2.0 );
     48 * // returns ~0.65
     49 *
     50 * m = accumulator( 3.0, 2.0 );
     51 * // returns ~0.59
     52 *
     53 * m = accumulator( 2.0, 5.0 );
     54 * // returns ~0.66
     55 *
     56 * m = accumulator();
     57 * // returns ~0.66
     58 */
     59 function incrmmaape( W ) {
     60 	var mean;
     61 	if ( !isPositiveInteger( W ) ) {
     62 		throw new TypeError( 'invalid argument. Must provide a positive integer. Value: `' + W + '`.' );
     63 	}
     64 	mean = incrmmean( W );
     65 	return accumulator;
     66 
     67 	/**
     68 	* If provided input values, the accumulator function returns an updated mean arctangent absolute percentage error. If not provided input values, the accumulator function returns the current mean arctangent absolute percentage error.
     69 	*
     70 	* @private
     71 	* @param {number} [f] - input value
     72 	* @param {number} [a] - input value
     73 	* @returns {(number|null)} mean arctangent absolute percentage error or null
     74 	*/
     75 	function accumulator( f, a ) {
     76 		if ( arguments.length === 0 ) {
     77 			return mean();
     78 		}
     79 		return mean( atan( abs( (a-f)/a ) ) );
     80 	}
     81 }
     82 
     83 
     84 // EXPORTS //
     85 
     86 module.exports = incrmmaape;