time-to-botec

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

hypergeometric-test.js (1391B)


      1 var vows = require('vows');
      2 var assert = require('assert');
      3 var suite = vows.describe('jStat.distribution');
      4 
      5 require('../env.js');
      6 
      7 suite.addBatch({
      8   'hypergeometric pdf': {
      9     'topic': function() {
     10       return jStat;
     11     },
     12     'check pdf calculation': function(jStat) {
     13       var tol = 0.0000001;
     14       // How many 1s were obtained by sampling?
     15       var successes = [10, 16];
     16       // How big was the source population?
     17       var population = [100, 3589];
     18       // How many 1s were in it?
     19       var available = [20, 16];
     20       // How big a sample was taken?
     21       var draws = [15, 2290];
     22       // What was the probability of exactly this many 1s?
     23       // Obtained from the calculator at
     24       // <http://www.geneprof.org/GeneProf/tools/hypergeometric.jsp>
     25       var answers = [0.000017532028090435493, 0.0007404996809672229];
     26 
     27       for (var i = 0; i < answers.length; i++) {
     28         // See if we get the right answer for each calculation.
     29         var calculated = jStat.hypgeom.pdf(successes[i],
     30                                            population[i],
     31                                            available[i],
     32                                            draws[i]);
     33         // None of the answers should be NaN
     34         assert(!isNaN(calculated));
     35         // And they should all match
     36         assert.epsilon(tol, calculated, answers[i]);
     37       }
     38     }
     39   }
     40 });
     41 
     42 suite.export(module);