time-to-botec

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

main.js (3053B)


      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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
     25 var incrmpcorr = require( './../../../incr/mpcorr' );
     26 var abs = require( '@stdlib/math/base/special/abs' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Returns an accumulator function which incrementally computes a moving sample absolute Pearson product-moment correlation coefficient.
     33 *
     34 * @param {PositiveInteger} W - window size
     35 * @param {number} [meanx] - mean value
     36 * @param {number} [meany] - mean value
     37 * @throws {TypeError} first argument must be a positive integer
     38 * @throws {TypeError} second argument must be a number primitive
     39 * @throws {TypeError} third argument must be a number primitive
     40 * @returns {Function} accumulator function
     41 *
     42 * @example
     43 * var accumulator = incrmapcorr( 3 );
     44 *
     45 * var ar = accumulator();
     46 * // returns null
     47 *
     48 * ar = accumulator( 2.0, 1.0 );
     49 * // returns 0.0
     50 *
     51 * ar = accumulator( -5.0, 3.14 );
     52 * // returns ~1.0
     53 *
     54 * ar = accumulator( 3.0, -1.0 );
     55 * // returns ~0.925
     56 *
     57 * ar = accumulator( 5.0, -9.5 );
     58 * // returns ~0.863
     59 *
     60 * ar = accumulator();
     61 * // returns ~0.863
     62 *
     63 * @example
     64 * var accumulator = incrmapcorr( 3, -2.0, 10.0 );
     65 */
     66 function incrmapcorr( W, meanx, meany ) {
     67 	var acc;
     68 	if ( !isPositiveInteger( W ) ) {
     69 		throw new TypeError( 'invalid argument. First argument must be a positive integer. Value: `' + W + '`.' );
     70 	}
     71 	if ( arguments.length > 1 ) {
     72 		if ( !isNumber( meanx ) ) {
     73 			throw new TypeError( 'invalid argument. Second argument must be a number primitive. Value: `' + meanx + '`.' );
     74 		}
     75 		if ( !isNumber( meany ) ) {
     76 			throw new TypeError( 'invalid argument. Third argument must be a number primitive. Value: `' + meany + '`.' );
     77 		}
     78 		acc = incrmpcorr( W, meanx, meany );
     79 	} else {
     80 		acc = incrmpcorr( W );
     81 	}
     82 	return accumulator;
     83 
     84 	/**
     85 	* If provided a value, the accumulator function returns an updated accumulated value. If not provided a value, the accumulator function returns the current accumulated value.
     86 	*
     87 	* @private
     88 	* @param {number} [x] - input value
     89 	* @param {number} [y] - input value
     90 	* @returns {(number|null)} sample absolute correlation coefficient or null
     91 	*/
     92 	function accumulator( x, y ) {
     93 		var r;
     94 		if ( arguments.length === 0 ) {
     95 			r = acc();
     96 			if ( r === null ) {
     97 				return r;
     98 			}
     99 			return abs( r );
    100 		}
    101 		return abs( acc( x, y ) );
    102 	}
    103 }
    104 
    105 
    106 // EXPORTS //
    107 
    108 module.exports = incrmapcorr;