main.js (2016B)
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 incrmean = require( './../../../incr/mean' ); 24 var signum = require( '@stdlib/math/base/special/signum' ); 25 var kroneckerDelta = require( '@stdlib/math/base/special/kronecker-delta' ); 26 27 28 // MAIN // 29 30 /** 31 * Returns an accumulator function which incrementally computes the mean directional accuracy. 32 * 33 * @returns {Function} accumulator function 34 * 35 * @example 36 * var accumulator = incrmda(); 37 * 38 * var m = accumulator(); 39 * // returns null 40 * 41 * m = accumulator( 2.0, 3.0 ); 42 * // returns 1.0 43 * 44 * m = accumulator( -5.0, 4.0 ); 45 * // returns 0.5 46 * 47 * m = accumulator(); 48 * // returns 0.5 49 */ 50 function incrmda() { 51 var mean; 52 var FLG; 53 var f0; 54 var a0; 55 56 mean = incrmean(); 57 return accumulator; 58 59 /** 60 * If provided a value, the accumulator function returns an updated mean directional accuracy. If not provided a value, the accumulator function returns the current mean directional accuracy. 61 * 62 * @private 63 * @param {number} [f] - forecast value 64 * @param {number} [a] - actual value 65 * @returns {(number|null)} mean directional accuracy or null 66 */ 67 function accumulator( f, a ) { 68 var sf; 69 var sa; 70 if ( arguments.length === 0 ) { 71 return mean(); 72 } 73 if ( FLG === void 0 ) { 74 FLG = true; 75 f0 = f; 76 a0 = a; 77 } 78 sf = signum( f-f0 ); 79 sa = signum( a-a0 ); 80 f0 = f; 81 a0 = a; 82 return mean( kroneckerDelta( sf, sa ) ); 83 } 84 } 85 86 87 // EXPORTS // 88 89 module.exports = incrmda;