Skip to content

Instantly share code, notes, and snippets.

@dsetzer
Created July 29, 2022 13:02
Show Gist options
  • Select an option

  • Save dsetzer/b3743620a4efad0d24178b515bfbc000 to your computer and use it in GitHub Desktop.

Select an option

Save dsetzer/b3743620a4efad0d24178b515bfbc000 to your computer and use it in GitHub Desktop.
Probability functions (codex generated)
// Cumulative Geometric Probability
function getGeoProb(p, n) {
return (1 - Math.pow(p, n)) / (1 - p);
}
// Cumulative Hypergeometric Probability
function getHyperProb(N, n, K, k) {
return getCombin(K, k) * getCombin(N - K, n - k) / getCombin(N, n);
}
// Cumulative Poisson Probability
function getPoissonProb(l, k) {
return Math.pow(l, k) * Math.exp(-l) / getFactorial(k);
}
// Cumulative Binomial Probability
function getBinomialProb(n, p, k) {
return getCombin(n, k) * Math.pow(p, k) * Math.pow(1 - p, n - k);
}
// Cumulative Negative Binomial Probability
function getNegBinomialProb(n, p, k) {
return getCombin(k - 1, n - 1) * Math.pow(p, n) * Math.pow(1 - p, k - n);
}
// Cumulative Uniform Probability
function getUniformProb(a, b, x) {
return (x - a) / (b - a);
}
// Cumulative Exponential Probability
function getExpProb(l, x) {
return 1 - Math.exp(-l * x);
}
// Cumulative Normal Probability
function getNormalProb(x, m, s) {
return 0.5 * (1 + erf((x - m) / (s * Math.sqrt(2))));
}
// Cumulative Student's t-distribution Probability
function getTProb(v, t) {
return 0.5 * (1 + erf(t / Math.sqrt(2 * v)));
}
// Cumulative Chi-squared Probability
function getChiProb(v, x) {
return getGammaProb(v / 2, x / 2);
}
// Cumulative F-distribution Probability
function getFProb(v1, v2, f) {
return getBetaProb(v1 / 2, v2 / 2, v1 / (v1 * f + v2));
}
// Cumulative Beta Probability
function getBetaProb(a, b, x) {
return getGammaProb(a, x) / (getGammaProb(a, 1) * getGammaProb(b, 1));
}
// Cumulative Gamma Probability
function getGammaProb(a, x) {
return getGamma(a) * getP(a, x);
}
// Gamma Function
function getGamma(a) {
return getFactorial(a - 1);
}
// P Function
function getP(a, x) {
var sum = 1 / a;
var ap = a;
var del = sum;
while (Math.abs(del) > 1e-6) {
ap++;
del *= x / ap;
sum += del;
}
return sum * Math.exp(-x + a * Math.log(x) - getLnGamma(a));
}
// Factorial Function
function getFactorial(n) {
var f = 1;
for (var i = 2; i <= n; i++) {
f *= i;
}
return f;
}
// Log Gamma Function
function getLnGamma(x) {
var cof = [
76.18009172947146,
-86.50532032941677,
24.01409824083091,
-1.231739572450155,
0.1208650973866179e-2,
-0.5395239384953e-5
];
var y = x;
var tmp = x + 5.5;
tmp -= (x + 0.5) * Math.log
var ser = 1.000000000190015;
for (var j = 0; j <= 5; j++) {
ser += cof[j] / ++y;
}
return -tmp + Math.log(2.5066282746310005 * ser / x);
}
// Error Function
function erf(x) {
var cof = [
-1.3026537197817094,
6.4196979235649026e-1,
1.9476473204185836e-2,
-9.561514786808631e-3,
-9.46595344482036e-4,
3.66839497852761e-4,
4.2523324806907e-5,
-2.0278578112534e-5,
-1.624290004647e-6,
1.303655835580e-6,
1.5626441722e-8,
-8.5238095915e-8,
6.529054439e-9,
5.059343495e-9,
-9.91364156e-10,
-2.27365122e-10,
9.6467911e-11,
2.394038e-12,
-6.886027e-12,
8.94487e-13,
3.13092e-13,
-1.12708e-13,
3.81e-16,
7.106e-15,
-1.523e-15,
-9.4e-17,
1.21e-16,
-2.8e-17
];
var j = cof.length - 1;
var isneg = false;
var d = 0;
var dd = 0;
var t, ty, tmp, res;
if (x < 0) {
x = -x;
isneg = true;
}
t = 2 / (2 + x);
ty = 4 * t - 2;
for (; j > 0; j--) {
tmp = d;
d = ty * d - dd + cof[j];
dd = tmp;
}
res = t * Math.exp(-x * x + 0.5 * (cof[0] + ty * d) - dd);
return isneg ? res - 1 : 1 - res;
}
// Inverse Error Function
function erfInv(p) {
var j = 0;
var x, err, t, pp;
if (p >= 2) {
return -100;
}
if (p <= 0) {
return 100;
}
pp = (p < 1) ? p : 2 - p;
t = Math.sqrt(-2 * Math.log(pp / 2));
x = -0.70711 * ((2.30753 + t * 0.27061) / (1 + t * (0.99229 + t * 0.04481)) - t);
for (; j < 2; j++) {
err = erf(x) - pp;
x += err / (1.12837916709551257 * Math.exp(-x * x) - x * err);
}
return (p < 1) ? x : -x;
}
@ruzli
Copy link
Copy Markdown

ruzli commented Mar 17, 2023

very handy, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment