time-to-botec

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

statistic.js (1450B)


      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 PINF = require( '@stdlib/constants/float64/pinf' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Computes a chi-square test statistic.
     30 *
     31 * @private
     32 * @param {NonNegativeInteger} N - number of indexed elements
     33 * @param {Float64Array} x - observation frequencies
     34 * @param {integer} strideX - `x` stride length
     35 * @param {Float64Array} y - expected frequencies
     36 * @param {integer} strideY - `y` stride length
     37 * @returns {number} test statistic
     38 */
     39 function testStatistic( N, x, strideX, y, strideY ) {
     40 	var stat;
     41 	var v1;
     42 	var v2;
     43 	var d;
     44 	var i;
     45 
     46 	stat = 0.0;
     47 	for ( i = 0; i < N; i++ ) {
     48 		v1 = x[ i*strideX ];
     49 		v2 = y[ i*strideY ];
     50 		if ( v2 === 0.0 ) {
     51 			if ( v1 === 0.0 ) {
     52 				continue;
     53 			}
     54 			return PINF;
     55 		}
     56 		d = v1 - v2;
     57 		stat += ( d * d ) / v2;
     58 	}
     59 	return stat;
     60 }
     61 
     62 
     63 // EXPORTS //
     64 
     65 module.exports = testStatistic;