gamma-test.js (2217B)
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 'gamma pdf': { 9 'topic': function() { 10 return jStat; 11 }, 12 'check instance and static pdf method': function (jStat) { 13 var shape = 5; 14 var scale = 1; 15 var gamma = jStat.gamma(shape, scale); 16 var xValues = [-1, 0, 1]; 17 var x; 18 for (var i = 0; i < xValues.length; i++) { 19 x = xValues[i]; 20 pStatic = jStat.gamma.pdf(x, shape, scale); 21 pInstance = gamma.pdf(x); 22 if (isNaN(pStatic)) { 23 assert(isNaN(pInstance)); 24 } else { 25 assert(pStatic === pInstance, 26 'Gamma pdf evaluated at ' + 27 x + ' should be equal for instance and static methods.'); 28 } 29 } 30 }, 31 32 // Checked against R's dgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE) 33 // options(digits=10) 34 // dgamma(2, 1, scale=1) 35 // dgamma(5, 10, scale=2) 36 // dgamma(18, 22, scale=0.8) 37 // dgamma(-5, 7, scale=10) 38 'check pdf': function(jStat) { 39 tol = 0.000001; 40 assert.epsilon(tol, jStat.gamma.pdf(2, 1, 1), 0.1353352832); 41 assert.epsilon(tol, jStat.gamma.pdf(5, 10, 2), 0.000431450369); 42 assert.epsilon(tol, jStat.gamma.pdf(18, 22, 0.8), 0.1029848021); 43 assert.epsilon(tol, jStat.gamma.pdf(-5, 7, 10), 0); 44 }, 45 46 //Checked against R's pgamma(q, shape, rate = 1/scale) 47 //That is, jStat.gamma.cdf(5, 10, 2) == pgamma(5, 10, 1/2) 48 'check cdf': function(jStat) { 49 tol = 0.000001; 50 assert.epsilon(tol, jStat.gamma.cdf(2, 1, 1), 0.8646647); 51 assert.epsilon(tol, jStat.gamma.cdf(5, 10, 2), 0.0002773); 52 assert.epsilon(tol, jStat.gamma.cdf(18, 22, 0.8), 0.5701725); 53 assert.epsilon(tol, jStat.gamma.cdf(-1, 5, 5), 0); 54 }, 55 56 //Checked against R's qgamma(p, shape, rate = 1/scale) 57 //That is, jStat.gamma.inv(0.86, 10, 2) == qgamma(0.86, 10, 1/2) 58 'check inv': function(jStat) { 59 tol = 0.00001; 60 assert.epsilon(tol, jStat.gamma.inv(0.86, 10, 2), 26.83397); 61 assert.epsilon(tol, jStat.gamma.inv(0.57, 22, 0.8), 17.99833); 62 } 63 }, 64 }); 65 66 suite.export(module);