time-to-botec

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

main.js (2867B)


      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 sample Pearson product-moment correlation distance.
     31 *
     32 * ## Method
     33 *
     34 * -   The sample Pearson product-moment correlation distance is defined as
     35 *
     36 *     ```tex
     37 *     d = 1 - r = 1 - \frac{\operatorname{cov}_n(x,y)}{\sigma_x \sigma_y}
     38 *     ```
     39 *
     40 * -   The implementation thus computes the sample Pearson product-moment correlation coefficient \\(r\\) and subtracts the coefficient from 1.
     41 *
     42 * @param {number} [meanx] - mean value
     43 * @param {number} [meany] - mean value
     44 * @throws {TypeError} first argument must be a number primitive
     45 * @throws {TypeError} second argument must be a number primitive
     46 * @returns {Function} accumulator function
     47 *
     48 * @example
     49 * var accumulator = incrpcorrdist();
     50 *
     51 * var d = accumulator();
     52 * // returns null
     53 *
     54 * d = accumulator( 2.0, 1.0 );
     55 * // returns 1.0
     56 *
     57 * d = accumulator( -5.0, 3.14 );
     58 * // returns ~2.0
     59 *
     60 * d = accumulator();
     61 * // returns ~2.0
     62 *
     63 * @example
     64 * var accumulator = incrpcorrdist( 2.0, -3.0 );
     65 */
     66 function incrpcorrdist( meanx, meany ) {
     67 	var pcorr;
     68 	if ( arguments.length ) {
     69 		if ( !isNumber( meanx ) ) {
     70 			throw new TypeError( 'invalid argument. First argument must be a number primitive. Value: `' + meanx + '`.' );
     71 		}
     72 		if ( !isNumber( meany ) ) {
     73 			throw new TypeError( 'invalid argument. Second argument must be a number primitive. Value: `' + meany + '`.' );
     74 		}
     75 		pcorr = incrpcorr( meanx, meany );
     76 	} else {
     77 		pcorr = incrpcorr();
     78 	}
     79 	return accumulator;
     80 
     81 	/**
     82 	* If provided input values, the accumulator function returns an updated sample correlation distance. If not provided input values, the accumulator function returns the current sample correlation distance.
     83 	*
     84 	* @private
     85 	* @param {number} [x] - new value
     86 	* @param {number} [y] - new value
     87 	* @returns {(number|null)} sample correlation distance or null
     88 	*/
     89 	function accumulator( x, y ) {
     90 		var r;
     91 		if ( arguments.length === 0 ) {
     92 			r = pcorr();
     93 			if ( r === null ) {
     94 				return r;
     95 			}
     96 			return 1.0 - r;
     97 		}
     98 		return 1.0 - pcorr( x, y );
     99 	}
    100 }
    101 
    102 
    103 // EXPORTS //
    104 
    105 module.exports = incrpcorrdist;