time-to-botec

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

weibull-test.js (2641B)


      1 var vows = require('vows');
      2 var assert = require('assert');
      3 var suite = vows.describe('jStat.distribution');
      4 
      5 require('../env.js');
      6 
      7 var tol = 0.0000001;
      8 var gamma = jStat.gammafn;
      9 
     10 
     11 suite.addBatch({
     12     'weibull pdf': {
     13         'topic': function() {
     14             return jStat;
     15         },
     16         'check pdf calculation': function(jStat) {
     17             var pdf = jStat.weibull.pdf;
     18 
     19             assert.epsilon(tol, pdf(1, 1, 1), Math.exp(-1));
     20             assert.epsilon(tol, pdf(3, 1, 1), Math.exp(-3));
     21 
     22             //Scale parameter
     23             assert.epsilon(tol, pdf(1, 2, 1), 0.5 * Math.exp(-0.5));
     24             assert.epsilon(tol, pdf(1, 10, 1), 0.1 * Math.exp(-0.1));
     25 
     26             //Shape parameter
     27             assert.epsilon(tol, pdf(1, 1, 4), 4 * Math.exp(-1));
     28             assert.epsilon(tol, pdf(2, 1, 2), 4 * Math.exp(-4));
     29 
     30             //Negative test cases for invalid parameters
     31             assert.epsilon(tol, pdf(-1, 1, 1), 0);
     32             assert.epsilon(tol, pdf(1, -1, 1), 0);
     33             assert.epsilon(tol, pdf(1, 1, -1), 0);
     34         },
     35         'check cdf calculation': function(jStat) {
     36             var cdf = jStat.weibull.cdf;
     37 
     38             assert.epsilon(tol, cdf(1, 1, 1), 1 - Math.exp(-1));
     39 
     40             assert.epsilon(tol, cdf(2, 1, 1), 1 - Math.exp(-2));
     41             assert.epsilon(tol, cdf(1, 2, 1), 1 - Math.exp(-0.5));
     42             assert.epsilon(tol, cdf(2, 1, 2), 1 - Math.exp(-4));
     43         },
     44         'mean': function(jStat) {
     45             var mean = jStat.weibull.mean;
     46 
     47             assert.epsilon(tol, mean(1, 1), gamma(2));
     48             assert.epsilon(tol, mean(2, 1), 2 * gamma(2));
     49 
     50             assert.epsilon(tol, mean(1, 2), gamma(1.5));
     51         },
     52         'median': function(jStat) {
     53             var median = jStat.weibull.median;
     54 
     55             assert.epsilon(tol, median(1, 1), Math.log(2));
     56 
     57             assert.epsilon(tol, median(1, 2), Math.pow(Math.log(2), 0.5));
     58             assert.epsilon(tol, median(2, 1), 2 * Math.log(2));
     59         },
     60         'mode': function(jStat) {
     61             var mode = jStat.weibull.mode;
     62 
     63             assert.epsilon(tol, mode(1, 1), 0);
     64             assert.epsilon(tol, mode(1, 2), Math.pow(0.5, 0.5))
     65             assert.epsilon(tol, mode(3, 2), 3 * Math.pow(0.5, 0.5))
     66         },
     67         'variance': function(jStat) {
     68             var variance = jStat.weibull.variance;
     69 
     70             assert.epsilon(tol, variance(1, 1), gamma(3) - Math.pow(gamma(2), 2));
     71             assert.epsilon(tol, variance(1, 2), gamma(2) - Math.pow(gamma(1.5), 2));
     72             assert.epsilon(tol, variance(3, 2), 9 * (gamma(2) - Math.pow(gamma(1.5), 2)));
     73         }
     74 
     75     }
     76 });
     77 
     78 suite.export(module);