12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- var cephes = require('cephes');
- function BinomialDistribution(properbility, size) {
- if (!(this instanceof BinomialDistribution)) {
- return new BinomialDistribution(properbility, size);
- }
- if (typeof properbility !== 'number') {
- throw TypeError('properbility must be a number');
- }
- if (typeof size !== 'number') {
- throw TypeError('size must be a number');
- }
- if (size <= 0.0) {
- throw TypeError('size must be positive');
- }
- if (properbility < 0.0 || properbility > 1) {
- throw TypeError('properbility must be between 0 and 1');
- }
- this._properbility = properbility;
- this._size = size;
- }
- module.exports = BinomialDistribution;
- BinomialDistribution.prototype.pdf = function (x) {
- var n = this._size;
- var p = this._properbility;
- // choose(n, x)
- var binomialCoefficent = cephes.gamma(n + 1) / (
- cephes.gamma(x + 1) * cephes.gamma(n - x + 1)
- )
- return binomialCoefficent * Math.pow(p, x) * Math.pow(1 - p, n - x);
- };
- BinomialDistribution.prototype.cdf = function (x) {
- return cephes.bdtr(x, this._size, this._properbility);
- };
- BinomialDistribution.prototype.inv = function (p) {
- throw new Error('Inverse CDF of binomial distribution is not implemented');
- };
- BinomialDistribution.prototype.median = function () {
- return Math.round(this._properbility * this._size);
- };
- BinomialDistribution.prototype.mean = function () {
- return this._properbility * this._size;
- };
- BinomialDistribution.prototype.variance = function () {
- return this._properbility * this._size * (1 - this._properbility);
- };
|