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