From dce1cd9d432184e75322d1808cf5fa8b309f691a Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Sat, 17 Feb 2024 19:18:01 -0400 Subject: [PATCH] Add some stats methods --- distribution.py | 254 +++++++++++++++++++++++++++++++++++++++ law_total_probability.py | 37 ++++++ utils.py | 6 + 3 files changed, 297 insertions(+) create mode 100644 distribution.py create mode 100644 law_total_probability.py create mode 100644 utils.py diff --git a/distribution.py b/distribution.py new file mode 100644 index 0000000..e8e14e7 --- /dev/null +++ b/distribution.py @@ -0,0 +1,254 @@ +import math +from utils import * + + +def bnd(x, n, p): + """ + Computes the binomial distribution. + :param x: Number of successes. + :param n: Number of trials. + :param p: Probability of success. + :return: Returns the probability of getting x successes in n trials. + """ + return math.comb(n, x) * p ** x * (1 - p) ** (n - x) + + +def bnd_mean(n, p): + """ + Computes the mean of the binomial distribution. + :param n: Number of trials. + :param p: Probability of success. + :return: Returns the mean of the binomial distribution. + """ + return n * p + + +def bnd_var(n, p): + """ + Computes the variance of the binomial distribution. + :param n: Number of trials. + :param p: Probability of success. + :return: Returns the variance of the binomial distribution. + """ + return n * p * (1 - p) + + +def bnd_std(n, p): + """ + Computes the standard deviation of the binomial distribution. + :param n: Number of trials. + :param p: Probability of success. + :return: Returns the standard deviation of the binomial distribution. + """ + return bnd_var(n, p) ** 0.5 + + +def bnd_upto(x, n, p): + """ + Computes the cumulative probability of getting upto 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 sum(bnd(i, n, p) for i in range(x + 1)) + + +def bnd_from(x, n, p): + """ + Computes the cumulative probability of getting from 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 1 - bnd_upto(x - 1, n, p) + + +def gd(x, p, q=None): + """ + Computes the geometric distribution. + :param x: Number of trials until the first success. + :param p: Probability of success. + :param q: Probability of failure. + :return: Returns the probability of getting the first success on the xth trial. + """ + if q is None: + q = 1 - p + return q ** (x - 1) * p + + +def gd_mean(p): + """ + Computes the mean of the geometric distribution. + :param p: Probability of success. + :return: Returns the mean of the geometric distribution. + """ + return 1 / p + + +def gd_var(p): + """ + Computes the variance of the geometric distribution. + :param p: Probability of success. + :return: Returns the variance of the geometric distribution. + """ + return (1 - p) / p ** 2 + + +def gd_std(p): + """ + Computes the standard deviation of the geometric distribution. + :param p: Probability of success. + :return: Returns the standard deviation of the geometric distribution. + """ + return gd_var(p) ** 0.5 + + +def gd_upto(x, p): + """ + Computes the cumulative probability of getting upto x trials until the first success. + :param x: Number of trials until the first success. + :param p: Probability of success. + :return: Returns the cumulative probability of getting upto x trials until the first success. + """ + return sum(gd(i, p) for i in range(1, x + 1)) + + +def gd_from(x, p): + """ + 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. + :return: Returns the cumulative probability of getting from x trials until the first success. + """ + return 1 - gd_upto(x - 1, p) + + +def hgd(x, N, n, k): + """ + Computes the hyper geometric distribution. + :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 probability of getting x successes in n draws from a population of size N with k successes. + """ + return (math.comb(k, x) * math.comb(N - k, n - x)) / math.comb(N, n) + + +def hgd_mean(N, n, k): + """ + Computes the mean of the hyper geometric distribution. + :param N: Number of items in the population. + :param n: Number of draws. + :param k: Number of successes in the population. + :return: Returns the mean of the hyper geometric distribution. + """ + return n * (k / N) + + +def hgd_var(N, n, k): + """ + Computes the variance of the hyper geometric distribution. + :param N: Number of items in the population. + :param n: Number of draws. + :param k: Number of successes in the population. + :return: Returns the variance of the hyper geometric distribution. + """ + return (n * k * (N - k) * (N - n)) / (N ** 2 * (N - 1)) + + +def hgd_std(N, n, k): + """ + Computes the standard deviation of the hyper geometric distribution. + :param N: Number of items in the population. + :param n: Number of draws. + :param k: Number of successes in the population. + :return: Returns the standard deviation of the hyper geometric distribution. + """ + return hgd_var(N, n, k) ** 0.5 + + +def hgd_upto(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. + :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 upto 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 + 1)) + + +def hgd_from(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_upto(x - 1, N, n, k) + + +def pd(x, l): + """ + Computes the poisson distribution. + :param x: Number of occurrences. + :param l: Average number of occurrences. + :return: Returns the probability of getting x occurrences. + """ + return (l ** x * math.e ** -l) / math.factorial(x) + + +def pd_mean(l): + """ + Computes the mean of the poisson distribution. + :param l: Average number of occurrences. + :return: Returns the mean of the poisson distribution. + """ + return l + + +def pd_var(l): + """ + Computes the variance of the poisson distribution. + :param l: Average number of occurrences. + :return: Returns the variance of the poisson distribution. + """ + return l + + +def pd_std(l): + """ + Computes the standard deviation of the poisson distribution. + :param l: Average number of occurrences. + :return: Returns the standard deviation of the poisson distribution. + """ + return l ** 0.5 + + +def pd_upto(x, l): + """ + Computes the cumulative probability of getting upto x occurrences. + :param x: Number of occurrences. + :param l: Average number of occurrences. + :return: Returns the cumulative probability of getting upto x occurrences. + """ + return sum(pd(i, l) for i in range(x + 1)) + + +def pd_from(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) + + +man(man) diff --git a/law_total_probability.py b/law_total_probability.py new file mode 100644 index 0000000..5d926bb --- /dev/null +++ b/law_total_probability.py @@ -0,0 +1,37 @@ +from utils import * + +def i(A, B): + """ + :param A: First probability + :param B: Second probability + :return: The intersection of A and B + """ + return A * B + + +def u(A, B): + """ + :param A: The first probability + :param B: The second probability + :return: The union of A and B + """ + return A + B - i(A, B) + + +def g(A, B): + """ + :param A: The first probability + :param B: The second probability + :return: The conditional probability of A given B + """ + return g(A, B) / B + + +def n(A): + """ + :param A: The probability + :return: The negation of A + """ + return 1 - A + +man(man) \ No newline at end of file diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..a72b5d7 --- /dev/null +++ b/utils.py @@ -0,0 +1,6 @@ +def man(func): + """ + Prints the manual for the function. + :param func: function + """ + print(func.__doc__) \ No newline at end of file