time-to-botec

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

main.js (3388B)


      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 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns an accumulator function which incrementally computes a moving sample Pearson product-moment correlation distance.
     32 *
     33 * ## Method
     34 *
     35 * -   The sample Pearson product-moment correlation distance is defined as
     36 *
     37 *     ```tex
     38 *     d_{n} = 1 - r_{n} = 1 - \frac{\operatorname{cov}_n(x,y)}{\sigma_{x,n} \sigma_{y,n}}
     39 *     ```
     40 *
     41 * -   The implementation thus computes the sample Pearson product-moment correlation coefficient \\(r_n\\) for each window \\(n\\) and subtracts the coefficient from 1.
     42 *
     43 *
     44 * @param {PositiveInteger} W - window size
     45 * @param {number} [meanx] - mean value
     46 * @param {number} [meany] - mean value
     47 * @throws {TypeError} first argument must be a positive integer
     48 * @throws {TypeError} second argument must be a number primitive
     49 * @throws {TypeError} third argument must be a number primitive
     50 * @returns {Function} accumulator function
     51 *
     52 * @example
     53 * var accumulator = incrmpcorrdist( 3 );
     54 *
     55 * var d = accumulator();
     56 * // returns null
     57 *
     58 * d = accumulator( 2.0, 1.0 );
     59 * // returns 1.0
     60 *
     61 * d = accumulator( -5.0, 3.14 );
     62 * // returns ~2.0
     63 *
     64 * d = accumulator( 3.0, -1.0 );
     65 * // returns ~1.925
     66 *
     67 * d = accumulator( 5.0, -9.5 );
     68 * // returns ~1.863
     69 *
     70 * d = accumulator();
     71 * // returns ~1.863
     72 *
     73 * @example
     74 * var accumulator = incrmpcorrdist( 3, -2.0, 10.0 );
     75 */
     76 function incrmpcorrdist( W, meanx, meany ) {
     77 	var pcorr;
     78 	if ( !isPositiveInteger( W ) ) {
     79 		throw new TypeError( 'invalid argument. First argument must be a positive integer. Value: `' + W + '`.' );
     80 	}
     81 	if ( arguments.length > 1 ) {
     82 		if ( !isNumber( meanx ) ) {
     83 			throw new TypeError( 'invalid argument. Second argument must be a number primitive. Value: `' + meanx + '`.' );
     84 		}
     85 		if ( !isNumber( meany ) ) {
     86 			throw new TypeError( 'invalid argument. Third argument must be a number primitive. Value: `' + meany + '`.' );
     87 		}
     88 		pcorr = incrmpcorr( W, meanx, meany );
     89 	} else {
     90 		pcorr = incrmpcorr( W );
     91 	}
     92 	return accumulator;
     93 
     94 	/**
     95 	* If provided a value, the accumulator function returns an updated sample correlation distance. If not provided a value, the accumulator function returns the current sample correlation distance.
     96 	*
     97 	* @private
     98 	* @param {number} [x] - input value
     99 	* @param {number} [y] - input value
    100 	* @returns {(number|null)} sample correlation distance or null
    101 	*/
    102 	function accumulator( x, y ) {
    103 		var r;
    104 		if ( arguments.length === 0 ) {
    105 			r = pcorr();
    106 			if ( r === null ) {
    107 				return r;
    108 			}
    109 			return 1.0 - r;
    110 		}
    111 		return 1.0 - pcorr( x, y );
    112 	}
    113 }
    114 
    115 
    116 // EXPORTS //
    117 
    118 module.exports = incrmpcorrdist;