time-to-botec

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

mean_table.js (2042B)


      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 incrstdev = require( './../../incr/stdev' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Computes the vector of means for each treatment.
     30 *
     31 * @private
     32 * @param {NumericArray} x - measured values
     33 * @param {Array} factor - array of treatments
     34 * @param {Array} treats - unique treatments
     35 * @returns {Object} summary statistics for the categories.
     36 */
     37 function meanTable( x, factor, treats ) {
     38 	var tableOfMeans;
     39 	var factorCount;
     40 	var accumulator;
     41 	var newMean;
     42 	var j;
     43 	var i;
     44 	var k;
     45 
     46 	factorCount = treats.length;
     47 	tableOfMeans = {};
     48 	for ( j = 0; j < factorCount; j++ ) {
     49 		accumulator = incrstdev();
     50 		tableOfMeans[ treats[j] ] = {
     51 			'mean': 0,
     52 			'sampleSize': 0,
     53 			'SD': accumulator
     54 		};
     55 		for ( i = 0; i < x.length; i++ ) {
     56 			if ( factor[i] === treats[j] ) {
     57 				tableOfMeans[ treats[j] ][ 'SD' ] = accumulator( x[i] );
     58 			}
     59 		}
     60 	}
     61 	// Go through, add to meanTable by indexing factor[i]
     62 	// X[i] is the individual continuous
     63 	for ( i = 0; i < x.length; i++ ) {
     64 		tableOfMeans[factor[i]]['mean'] += x[i];
     65 		tableOfMeans[factor[i]]['sampleSize'] += 1;
     66 	}
     67 
     68 	// Now make the mean by dividing by the sample size
     69 	// Get the standard deviation through the helper function
     70 	for ( k = 0; k < factorCount; k++ ) {
     71 		newMean = tableOfMeans[treats[k]]['mean'] / tableOfMeans[treats[k]]['sampleSize'];
     72 		tableOfMeans[treats[k]]['mean'] = newMean;
     73 	}
     74 	return tableOfMeans;
     75 }
     76 
     77 
     78 // EXPORTS //
     79 
     80 module.exports = meanTable;