From a964199ee40f63f6cb29fa039f3a49d144837fb5 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Tue, 23 Mar 2021 01:18:08 -0500 Subject: [PATCH] Initial import --- README.md | 7 +++++ distributions.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 README.md create mode 100644 distributions.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..154d066 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# math + +These are some miscellaneous math functions implemented in Javascript that helped me with various assignments. + +## Distributions +The `distributions.js` file has some functions relating to Binomial and Poisson distributions of random variables. + diff --git a/distributions.js b/distributions.js new file mode 100644 index 0000000..81498dc --- /dev/null +++ b/distributions.js @@ -0,0 +1,76 @@ +/** + * Some functions for calculating distributions of random variables. + */ + +/** + * Takes the factorial of n. + * @param {number} n + * @return {number} + */ +const fact = n => { + if ( n == 0 || n == 1 ) return 1 + + for ( let i = n - 1; i >= 1; i -= 1 ) { + n = n * i + } + + return n +} + +/** + * Compute the {n}\choose{k} value. + * @param {number} n the total number of items + * @param {number} k the number to take at a time + * @return {number} + */ +const choose = (n,k) => (fact(n))/(fact(k)*(fact(n-k))) + +/** + * Calculate the binomial distribution of a random variable: + * + * b(x; n,p) for lower_x <= x <= upper_x + */ +const binom_range = (lower_x, upper_x, n, p) => { + let sum = 0 + + for ( let i = lower_x; i <= upper_x; i += 1 ) { + sum += choose(n, i) * Math.pow(p, i) * Math.pow(1-p, n-i) + } + + return sum +} + +/** + * Calculate the binomial distribution for some specific x. + * + * b(x; n,p) + */ +const binom_dist = (x, n, p) => choose(n, x) * Math.pow(p, x) * Math.pow(1-p, n-x) + +/** + * Calculate the poisson distribution for some specific x. + * + * p(x, lambda * t) + */ +const poisson_dist = (x, lambda, t) => { + const numerator = Math.pow(Math.E, -1 * lambda * t) * Math.pow(lambda * t, x) + const denom = fact(x) + return numerator / denom +} + +/** + * Calculate the poisson range for some specific x: + * + * p(x, lambda * t) for lower_x <= x <= upper_x. + */ +const poisson_range = (lower_x, upper_x, lambda, t) => { + let sum = 0 + + for ( let i = lower_x; i <= upper_x; i += 1 ) { + sum += poisson_dist(i, lambda, t) + } + + return sum +} + +module.exports = exports = {fact, choose, binom_range, binom_dist, poisson_dist, poisson_range}