From b0e72be1d0e36b6a14b0357e1a198cabc6d9b657 Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Sat, 17 Feb 2024 21:44:21 -0400 Subject: [PATCH] Add more functions --- distribution.py | 154 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 129 insertions(+), 25 deletions(-) diff --git a/distribution.py b/distribution.py index 614d9ec..affcd21 100644 --- a/distribution.py +++ b/distribution.py @@ -42,26 +42,48 @@ def bnd_std(n, p): return bnd_var(n, p) ** 0.5 -def bnd_upto(x, n, p): +def bnd_leq(x, n, p): """ - Computes the cumulative probability of getting upto x successes in n trials. + Computes the cumulative probability less than or equal to x successes in n trials. :param x: Number of successes. :param n: Number of trials. :param p: Probability of success. - :return: Returns the cumulative probability of getting upto x successes in n trials. + :return: Returns the cumulative probability less than or equal to x successes in n trials. """ return sum(bnd(i, n, p) for i in range(x + 1)) -def bnd_from(x, n, p): +def bnd_lt(x, n, p): """ - Computes the cumulative probability of getting from x successes in n trials. + Computes the cumulative probability less than x successes in n trials. :param x: Number of successes. :param n: Number of trials. :param p: Probability of success. - :return: Returns the cumulative probability of getting from x successes in n trials. + :return: Returns the cumulative probability less than x successes in n trials. """ - return 1 - bnd_upto(x - 1, n, p) + return sum(bnd(i, n, p) for i in range(x)) + + +def bnd_geq(x, n, p): + """ + Computes the cumulative probability greater than or equal to x successes in n trials. + :param x: Number of successes. + :param n: Number of trials. + :param p: Probability of success. + :return: Returns the cumulative probability greater than or equal to x successes in n trials. + """ + return 1 - bnd_lt(x, n, p) + + +def bnd_gt(x, n, p): + """ + Computes the cumulative probability greater than x successes in n trials. + :param x: Number of successes. + :param n: Number of trials. + :param p: Probability of success. + :return: Returns the cumulative probability greater than x successes in n trials. + """ + return 1 - bnd_leq(x, n, p) def gd(x, p, q=None): @@ -104,7 +126,7 @@ def gd_std(p): return gd_var(p) ** 0.5 -def gd_upto(x, p, q=None): +def gd_leq(x, p, q=None): """ Computes the cumulative probability of getting upto x trials until the first success. :param x: Number of trials until the first success. @@ -117,7 +139,20 @@ def gd_upto(x, p, q=None): return sum(gd(i, p) for i in range(1, x + 1)) -def gd_from(x, p, q=None): +def gd_lt(x, p, q=None): + """ + Computes the cumulative probability of getting less than x trials until the first success. + :param x: Number of trials until the first success. + :param p: Probability of success. + :param q: Probability of failure. + :return: Returns the cumulative probability of getting less than x trials until the first success. + """ + if q is not None: + return sum(gd(i, p, q) for i in range(1, x)) + return sum(gd(i, p) for i in range(1, x)) + + +def gd_geq(x, p, q=None): """ Computes the cumulative probability of getting from x trials until the first success. :param x: Number of trials until the first success. @@ -126,8 +161,21 @@ def gd_from(x, p, q=None): :return: Returns the cumulative probability of getting from x trials until the first success. """ if q is not None: - return 1 - gd_upto(x - 1, p, q) - return 1 - gd_upto(x - 1, p) + return 1 - gd_lt(x, p, q) + return 1 - gd_leq(x, p) + + +def gd_gt(x, p, q=None): + """ + Computes the cumulative probability of getting from x trials until the first success. + :param x: Number of trials until the first success. + :param p: Probability of success. + :param q: Probability of failure. + :return: Returns the cumulative probability of getting from x trials until the first success. + """ + if q is not None: + return 1 - gd_leq(x, p, q) + return 1 - gd_leq(x, p) def hgd(x, N, n, k): @@ -175,7 +223,7 @@ def hgd_std(N, n, k): return hgd_var(N, n, k) ** 0.5 -def hgd_upto(x, N, n, k): +def hgd_leq(x, N, n, k): """ Computes the cumulative probability of getting upto x successes in n draws from a population of size N with k successes. :param x: Number of successes in the sample. @@ -187,7 +235,19 @@ def hgd_upto(x, N, n, k): return sum(hgd(i, N, n, k) for i in range(x + 1)) -def hgd_from(x, N, n, k): +def hgd_lt(x, N, n, k): + """ + Computes the cumulative probability of getting less than x successes in n draws from a population of size N with k successes. + :param x: Number of successes in the sample. + :param N: Number of items in the population. + :param n: Number of draws. + :param k: Number of successes in the population. + :return: Returns the cumulative probability of getting less than x successes in n draws from a population of size N with k successes. + """ + return sum(hgd(i, N, n, k) for i in range(x)) + + +def hgd_geq(x, N, n, k): """ Computes the cumulative probability of getting from x successes in n draws from a population of size N with k successes. :param x: Number of successes in the sample. @@ -196,7 +256,19 @@ def hgd_from(x, N, n, k): :param k: Number of successes in the population. :return: Returns the cumulative probability of getting from x successes in n draws from a population of size N with k successes. """ - return 1 - hgd_upto(x - 1, N, n, k) + return 1 - hgd_lt(x, N, n, k) + + +def hgd_gt(x, N, n, k): + """ + Computes the cumulative probability of getting from x successes in n draws from a population of size N with k successes. + :param x: Number of successes in the sample. + :param N: Number of items in the population. + :param n: Number of draws. + :param k: Number of successes in the population. + :return: Returns the cumulative probability of getting from x successes in n draws from a population of size N with k successes. + """ + return 1 - hgd_leq(x, N, n, k) def pd(x, l): @@ -236,7 +308,7 @@ def pd_std(l): return l ** 0.5 -def pd_upto(x, l): +def pd_leq(x, l): """ Computes the cumulative probability of getting upto x occurrences. :param x: Number of occurrences. @@ -246,17 +318,38 @@ def pd_upto(x, l): return sum(pd(i, l) for i in range(x + 1)) -def pd_from(x, l): +def pd_lt(x, l): + """ + Computes the cumulative probability of getting less than x occurrences. + :param x: Number of occurrences. + :param l: Average number of occurrences. + :return: Returns the cumulative probability of getting less than x occurrences. + """ + return sum(pd(i, l) for i in range(x)) + + +def pd_geq(x, l): """ Computes the cumulative probability of getting from x occurrences. :param x: Number of occurrences. :param l: Average number of occurrences. :return: Returns the cumulative probability of getting from x occurrences. """ - return 1 - pd_upto(x - 1, l) + return 1 - pd_lt(x, l) + + +def pd_gt(x, l): + """ + Computes the cumulative probability of getting from x occurrences. + :param x: Number of occurrences. + :param l: Average number of occurrences. + :return: Returns the cumulative probability of getting from x occurrences. + """ + return 1 - pd_leq(x, l) def man(): + seperator = "-" * 30 """ Prints the manual for the module. Formatted this way to fit in memory on the calculator. @@ -267,23 +360,34 @@ def man(): print("bnd_mean(n, p) - The mean of the binomial distribution") print("bnd_var(n, p) - The variance of the binomial distribution") print("bnd_std(n, p) - The standard deviation of the binomial distribution") - print("bnd_upto(x, n, p) - The cumulative probability of getting upto x successes in n trials") - print("bnd_from(x, n, p) - The cumulative probability of getting from x successes in n trials") + print("bnd_leq(x, n, p) - The cumulative probability less than or equal to x successes in n trials") + print("bnd_lt(x, n, p) - The cumulative probability less than x successes in n trials") + print("bnd_geq(x, n, p) - The cumulative probability greater than or equal to x successes in n trials") + print("bnd_gt(x, n, p) - The cumulative probability greater than x successes in n trials") + print(seperator) print("gd(x, p, q) - The geometric distribution") print("gd_mean(p) - The mean of the geometric distribution") print("gd_var(p) - The variance of the geometric distribution") print("gd_std(p) - The standard deviation of the geometric distribution") - print("gd_upto(x, p, q) - The cumulative probability of getting upto x trials until the first success") - print("gd_from(x, p, q) - The cumulative probability of getting from x trials until the first success") + print("gd_leq(x, p, q) - The cumulative probability of getting upto x trials until the first success") + print("gd_lt(x, p, q) - The cumulative probability of getting less than x trials until the first success") + print("gd_geq(x, p, q) - The cumulative probability of getting from x trials until the first success") + print("gd_gt(x, p, q) - The cumulative probability of getting from x trials until the first success") + print(seperator) print("hgd(x, N, n, k) - The hyper geometric distribution") print("hgd_mean(N, n, k) - The mean of the hyper geometric distribution") print("hgd_var(N, n, k) - The variance of the hyper geometric distribution") print("hgd_std(N, n, k) - The standard deviation of the hyper geometric distribution") - print("hgd_upto(x, N, n, k) - The cumulative probability of getting upto x successes in n draws from a population of size N with k successes") - print("hgd_from(x, N, n, k) - The cumulative probability of getting from x successes in n draws from a population of size N with k successes") + print("hgd_leq(x, N, n, k) - The cumulative probability of getting upto x successes in n draws from a population of size N with k successes") + print("hgd_lt(x, N, n, k) - The cumulative probability of getting less than x successes in n draws from a population of size N with k successes") + print("hgd_geq(x, N, n, k) - The cumulative probability of getting from x successes in n draws from a population of size N with k successes") + print("hgd_gt(x, N, n, k) - The cumulative probability of getting from x successes in n draws from a population of size N with k successes") + print(seperator) print("pd(x, l) - The poisson distribution") print("pd_mean(l) - The mean of the poisson distribution") print("pd_var(l) - The variance of the poisson distribution") print("pd_std(l) - The standard deviation of the poisson distribution") - print("pd_upto(x, l) - The cumulative probability of getting upto x occurrences") - print("pd_from(x, l) - The cumulative probability of getting from x occurrences") + print("pd_leq(x, l) - The cumulative probability of getting upto x occurrences") + print("pd_lt(x, l) - The cumulative probability of getting less than x occurrences") + print("pd_geq(x, l) - The cumulative probability of getting from x occurrences") + print("pd_gt(x, l) - The cumulative probability of getting from x occurrences")