main.js (2997B)
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 squared sample Pearson product-moment correlation coefficient. 32 * 33 * @param {PositiveInteger} W - window size 34 * @param {number} [meanx] - mean value 35 * @param {number} [meany] - mean value 36 * @throws {TypeError} first argument must be a positive integer 37 * @throws {TypeError} second argument must be a number primitive 38 * @throws {TypeError} third argument must be a number primitive 39 * @returns {Function} accumulator function 40 * 41 * @example 42 * var accumulator = incrmpcorr2( 3 ); 43 * 44 * var r2 = accumulator(); 45 * // returns null 46 * 47 * r2 = accumulator( 2.0, 1.0 ); 48 * // returns 0.0 49 * 50 * r2 = accumulator( -5.0, 3.14 ); 51 * // returns ~1.0 52 * 53 * r2 = accumulator( 3.0, -1.0 ); 54 * // returns ~0.86 55 * 56 * r2 = accumulator( 5.0, -9.5 ); 57 * // returns ~0.74 58 * 59 * r2 = accumulator(); 60 * // returns ~0.74 61 * 62 * @example 63 * var accumulator = incrmpcorr2( 3, -2.0, 10.0 ); 64 */ 65 function incrmpcorr2( W, meanx, meany ) { 66 var acc; 67 if ( !isPositiveInteger( W ) ) { 68 throw new TypeError( 'invalid argument. First argument must be a positive integer. Value: `' + W + '`.' ); 69 } 70 if ( arguments.length > 1 ) { 71 if ( !isNumber( meanx ) ) { 72 throw new TypeError( 'invalid argument. Second argument must be a number primitive. Value: `' + meanx + '`.' ); 73 } 74 if ( !isNumber( meany ) ) { 75 throw new TypeError( 'invalid argument. Third argument must be a number primitive. Value: `' + meany + '`.' ); 76 } 77 acc = incrmpcorr( W, meanx, meany ); 78 } else { 79 acc = incrmpcorr( W ); 80 } 81 return accumulator; 82 83 /** 84 * 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. 85 * 86 * @private 87 * @param {number} [x] - input value 88 * @param {number} [y] - input value 89 * @returns {(number|null)} squared sample correlation coefficient or null 90 */ 91 function accumulator( x, y ) { 92 var r; 93 if ( arguments.length === 0 ) { 94 r = acc(); 95 if ( r === null ) { 96 return r; 97 } 98 return r * r; 99 } 100 r = acc( x, y ); 101 return r * r; 102 } 103 } 104 105 106 // EXPORTS // 107 108 module.exports = incrmpcorr2;