1 2 /** 3 * extension of the factorial to floating points 4 * 5 * @param z 6 * @returns {Number} 7 */ 8 function gamma(z){ 9 var g = 7; 10 var p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028, 11 771.32342877765313, -176.61502916214059, 12.507343278686905, 12 -0.13857109526572012, 9.9843695780195716*Math.pow(10,-6), 1.5056327351493116*Math.pow(10,-7)]; 13 if(z < 0.5){ 14 return Math.PI / (Math.sin(Math.PI*z)*gamma(1-z)); 15 }else{ 16 z--; 17 x = p[0]; 18 for(var i = 1; i <= g; i++){ 19 x += p[i]/(z+i); 20 } 21 t = z+g+0.5; 22 return Math.sqrt(2*Math.PI)*Math.pow(t,(z+0.5))*Math.pow(Math.E, -t)*x; 23 } 24 } 25 26 /** 27 * evaluate the poisonDistribution at position k with lambda l 28 * 29 * @param l lambda 30 * @param k position 31 * @returns {Number} 32 */ 33 function poisonDistribution(l, k){ 34 return Math.pow(l, k)*Math.pow(Math.E, -l)/ gamma(k+1); 35 }