time-to-botec

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

main.js (2520B)


      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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
     24 var incrpcorr = require( './../../../incr/pcorr' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Returns an accumulator function which incrementally computes a squared sample Pearson product-moment correlation coefficient.
     31 *
     32 * @param {number} [meanx] - mean value
     33 * @param {number} [meany] - mean value
     34 * @throws {TypeError} first argument must be a number primitive
     35 * @throws {TypeError} second argument must be a number primitive
     36 * @returns {Function} accumulator function
     37 *
     38 * @example
     39 * var accumulator = incrpcorr2();
     40 *
     41 * var r2 = accumulator();
     42 * // returns null
     43 *
     44 * r2 = accumulator( 2.0, 1.0 );
     45 * // returns 0.0
     46 *
     47 * r2 = accumulator( -5.0, 3.14 );
     48 * // returns ~1.0
     49 *
     50 * r2 = accumulator();
     51 * // returns ~1.0
     52 *
     53 * @example
     54 * var accumulator = incrpcorr2( 2.0, -3.0 );
     55 */
     56 function incrpcorr2( meanx, meany ) {
     57 	var acc;
     58 	if ( arguments.length ) {
     59 		if ( !isNumber( meanx ) ) {
     60 			throw new TypeError( 'invalid argument. First argument must be a number primitive. Value: `' + meanx + '`.' );
     61 		}
     62 		if ( !isNumber( meany ) ) {
     63 			throw new TypeError( 'invalid argument. Second argument must be a number primitive. Value: `' + meany + '`.' );
     64 		}
     65 		acc = incrpcorr( meanx, meany );
     66 	} else {
     67 		acc = incrpcorr();
     68 	}
     69 	return accumulator;
     70 
     71 	/**
     72 	* If provided input values, the accumulator function returns an updated accumulated value. If not provided input values, the accumulator function returns the current accumulate value.
     73 	*
     74 	* @private
     75 	* @param {number} [x] - new value
     76 	* @param {number} [y] - new value
     77 	* @returns {(number|null)} squared sample correlation coefficient or null
     78 	*/
     79 	function accumulator( x, y ) {
     80 		var r;
     81 		if ( arguments.length === 0 ) {
     82 			r = acc();
     83 			if ( r === null ) {
     84 				return r;
     85 			}
     86 			return r * r;
     87 		}
     88 		r = acc( x, y );
     89 		return r * r;
     90 	}
     91 }
     92 
     93 
     94 // EXPORTS //
     95 
     96 module.exports = incrpcorr2;