Update distribution.py
This commit is contained in:
parent
ee9a752d7f
commit
e3566d1c21
2
.idea/.gitignore
vendored
2
.idea/.gitignore
vendored
@ -6,3 +6,5 @@
|
|||||||
# Datasource local storage ignored files
|
# Datasource local storage ignored files
|
||||||
/dataSources/
|
/dataSources/
|
||||||
/dataSources.local.xml
|
/dataSources.local.xml
|
||||||
|
# GitHub Copilot persisted chat sessions
|
||||||
|
/copilot/chatSessions
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module type="PYTHON_MODULE" version="4">
|
<module type="PYTHON_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<content url="file://$MODULE_DIR$" />
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.idea/copilot/chatSessions" />
|
||||||
|
</content>
|
||||||
<orderEntry type="jdk" jdkName="Pipenv (casio-calculator)" jdkType="Python SDK" />
|
<orderEntry type="jdk" jdkName="Pipenv (casio-calculator)" jdkType="Python SDK" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
</component>
|
</component>
|
||||||
|
1
Pipfile
1
Pipfile
@ -9,4 +9,3 @@ name = "pypi"
|
|||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.11"
|
python_version = "3.11"
|
||||||
python_full_version = "3.11.7"
|
|
||||||
|
3
Pipfile.lock
generated
3
Pipfile.lock
generated
@ -1,11 +1,10 @@
|
|||||||
{
|
{
|
||||||
"_meta": {
|
"_meta": {
|
||||||
"hash": {
|
"hash": {
|
||||||
"sha256": "bc82cd27f07d4e24b750064464bbc233a141778868b9a387125705e2d4e8a830"
|
"sha256": "ed6d5d614626ae28e274e453164affb26694755170ccab3aa5866f093d51d3e4"
|
||||||
},
|
},
|
||||||
"pipfile-spec": 6,
|
"pipfile-spec": 6,
|
||||||
"requires": {
|
"requires": {
|
||||||
"python_full_version": "3.11.7",
|
|
||||||
"python_version": "3.11"
|
"python_version": "3.11"
|
||||||
},
|
},
|
||||||
"sources": [
|
"sources": [
|
||||||
|
744
distribution.py
744
distribution.py
@ -1,420 +1,536 @@
|
|||||||
import math
|
import math
|
||||||
|
import statistics
|
||||||
|
|
||||||
|
|
||||||
def factorial(n):
|
def factorial(n):
|
||||||
"""
|
"""
|
||||||
Computes the factorial of a number.
|
Computes the factorial of a number.
|
||||||
:param n: The number to compute the factorial of.
|
:param n: The number to compute the factorial of.
|
||||||
:return: Returns the factorial of the number.
|
:return: Returns the factorial of the number.
|
||||||
"""
|
"""
|
||||||
if n == 0:
|
if n == 0:
|
||||||
return 1
|
return 1
|
||||||
for i in range(1, n):
|
for i in range(1, n):
|
||||||
n *= i
|
n *= i
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
|
||||||
def combination(n, r):
|
def combination(n, r):
|
||||||
"""
|
"""
|
||||||
Computes the combination of n choose r.
|
Computes the combination of n choose r.
|
||||||
:param n: The number of items.
|
:param n: The number of items.
|
||||||
:param r: The number of items to choose.
|
:param r: The number of items to choose.
|
||||||
:return: Returns the number of ways to choose r items from n items.
|
:return: Returns the number of ways to choose r items from n items.
|
||||||
"""
|
"""
|
||||||
return factorial(n) / (factorial(r) * factorial(n - r))
|
return factorial(n) / (factorial(r) * factorial(n - r))
|
||||||
|
|
||||||
|
|
||||||
def bnd(x, n, p):
|
def bnd(x, n, p):
|
||||||
"""
|
"""
|
||||||
Computes the binomial distribution.
|
Computes the binomial distribution.
|
||||||
:param x: Number of successes.
|
:param x: Number of successes.
|
||||||
:param n: Number of trials.
|
:param n: Number of trials.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:return: Returns the probability of getting x successes in n trials.
|
:return: Returns the probability of getting x successes in n trials.
|
||||||
"""
|
"""
|
||||||
return combination(n, x) * p ** x * (1 - p) ** (n - x)
|
return combination(n, x) * p ** x * (1 - p) ** (n - x)
|
||||||
|
|
||||||
|
|
||||||
def bnd_mean(n, p):
|
def bnd_mean(n, p):
|
||||||
"""
|
"""
|
||||||
Computes the mean of the binomial distribution.
|
Computes the mean of the binomial distribution.
|
||||||
:param n: Number of trials.
|
:param n: Number of trials.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:return: Returns the mean of the binomial distribution.
|
:return: Returns the mean of the binomial distribution.
|
||||||
"""
|
"""
|
||||||
return n * p
|
return n * p
|
||||||
|
|
||||||
|
|
||||||
def bnd_var(n, p):
|
def bnd_var(n, p):
|
||||||
"""
|
"""
|
||||||
Computes the variance of the binomial distribution.
|
Computes the variance of the binomial distribution.
|
||||||
:param n: Number of trials.
|
:param n: Number of trials.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:return: Returns the variance of the binomial distribution.
|
:return: Returns the variance of the binomial distribution.
|
||||||
"""
|
"""
|
||||||
return n * p * (1 - p)
|
return n * p * (1 - p)
|
||||||
|
|
||||||
|
|
||||||
def bnd_std(n, p):
|
def bnd_std(n, p):
|
||||||
"""
|
"""
|
||||||
Computes the standard deviation of the binomial distribution.
|
Computes the standard deviation of the binomial distribution.
|
||||||
:param n: Number of trials.
|
:param n: Number of trials.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:return: Returns the standard deviation of the binomial distribution.
|
:return: Returns the standard deviation of the binomial distribution.
|
||||||
"""
|
"""
|
||||||
return bnd_var(n, p) ** 0.5
|
return bnd_var(n, p) ** 0.5
|
||||||
|
|
||||||
|
|
||||||
def bnd_leq(x, n, p):
|
def bnd_leq(x, n, p):
|
||||||
"""
|
"""
|
||||||
Computes the cumulative probability less than or equal to 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 x: Number of successes.
|
||||||
:param n: Number of trials.
|
:param n: Number of trials.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:return: Returns the cumulative probability less than or equal to 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))
|
return sum(bnd(i, n, p) for i in range(x + 1))
|
||||||
|
|
||||||
|
|
||||||
def bnd_lt(x, n, p):
|
def bnd_lt(x, n, p):
|
||||||
"""
|
"""
|
||||||
Computes the cumulative probability less than x successes in n trials.
|
Computes the cumulative probability less than x successes in n trials.
|
||||||
:param x: Number of successes.
|
:param x: Number of successes.
|
||||||
:param n: Number of trials.
|
:param n: Number of trials.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:return: Returns the cumulative probability less than x successes in n trials.
|
:return: Returns the cumulative probability less than x successes in n trials.
|
||||||
"""
|
"""
|
||||||
return sum(bnd(i, n, p) for i in range(x))
|
return sum(bnd(i, n, p) for i in range(x))
|
||||||
|
|
||||||
|
|
||||||
def bnd_geq(x, n, p):
|
def bnd_geq(x, n, p):
|
||||||
"""
|
"""
|
||||||
Computes the cumulative probability greater than or equal to x successes in n trials.
|
Computes the cumulative probability greater than or equal to x successes in n trials.
|
||||||
:param x: Number of successes.
|
:param x: Number of successes.
|
||||||
:param n: Number of trials.
|
:param n: Number of trials.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:return: Returns the cumulative probability greater than or equal to x successes in n trials.
|
:return: Returns the cumulative probability greater than or equal to x successes in n trials.
|
||||||
"""
|
"""
|
||||||
return 1 - bnd_lt(x, n, p)
|
return 1 - bnd_lt(x, n, p)
|
||||||
|
|
||||||
|
|
||||||
def bnd_gt(x, n, p):
|
def bnd_gt(x, n, p):
|
||||||
"""
|
"""
|
||||||
Computes the cumulative probability greater than x successes in n trials.
|
Computes the cumulative probability greater than x successes in n trials.
|
||||||
:param x: Number of successes.
|
:param x: Number of successes.
|
||||||
:param n: Number of trials.
|
:param n: Number of trials.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:return: Returns the cumulative probability greater than x successes in n trials.
|
:return: Returns the cumulative probability greater than x successes in n trials.
|
||||||
"""
|
"""
|
||||||
return 1 - bnd_leq(x, n, p)
|
return 1 - bnd_leq(x, n, p)
|
||||||
|
|
||||||
|
|
||||||
def gd(x, p, q=None):
|
def gd(x, p, q=None):
|
||||||
"""
|
"""
|
||||||
Computes the geometric distribution.
|
Computes the geometric distribution.
|
||||||
:param x: Number of trials until the first success.
|
:param x: Number of trials until the first success.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:param q: Probability of failure.
|
:param q: Probability of failure.
|
||||||
:return: Returns the probability of getting the first success on the xth trial.
|
:return: Returns the probability of getting the first success on the xth trial.
|
||||||
"""
|
"""
|
||||||
if q is None:
|
if q is None:
|
||||||
q = 1 - p
|
q = 1 - p
|
||||||
return q ** (x - 1) * p
|
return q ** (x - 1) * p
|
||||||
|
|
||||||
|
|
||||||
def gd_mean(p):
|
def gd_mean(p):
|
||||||
"""
|
"""
|
||||||
Computes the mean of the geometric distribution.
|
Computes the mean of the geometric distribution.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:return: Returns the mean of the geometric distribution.
|
:return: Returns the mean of the geometric distribution.
|
||||||
"""
|
"""
|
||||||
return 1 / p
|
return 1 / p
|
||||||
|
|
||||||
|
|
||||||
def gd_var(p):
|
def gd_var(p):
|
||||||
"""
|
"""
|
||||||
Computes the variance of the geometric distribution.
|
Computes the variance of the geometric distribution.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:return: Returns the variance of the geometric distribution.
|
:return: Returns the variance of the geometric distribution.
|
||||||
"""
|
"""
|
||||||
return (1 - p) / p ** 2
|
return (1 - p) / p ** 2
|
||||||
|
|
||||||
|
|
||||||
def gd_std(p):
|
def gd_std(p):
|
||||||
"""
|
"""
|
||||||
Computes the standard deviation of the geometric distribution.
|
Computes the standard deviation of the geometric distribution.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:return: Returns the standard deviation of the geometric distribution.
|
:return: Returns the standard deviation of the geometric distribution.
|
||||||
"""
|
"""
|
||||||
return gd_var(p) ** 0.5
|
return gd_var(p) ** 0.5
|
||||||
|
|
||||||
|
|
||||||
def gd_leq(x, p, q=None):
|
def gd_leq(x, p, q=None):
|
||||||
"""
|
"""
|
||||||
Computes the cumulative probability of getting upto x trials until the first success.
|
Computes the cumulative probability of getting upto x trials until the first success.
|
||||||
:param x: Number of trials until the first success.
|
:param x: Number of trials until the first success.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:param q: Probability of failure.
|
:param q: Probability of failure.
|
||||||
:return: Returns the cumulative probability of getting upto x trials until the first success.
|
:return: Returns the cumulative probability of getting upto x trials until the first success.
|
||||||
"""
|
"""
|
||||||
if q is not None:
|
if q is not None:
|
||||||
return sum(gd(i, p, q) for i in range(1, x + 1))
|
return sum(gd(i, p, q) for i in range(1, x + 1))
|
||||||
return sum(gd(i, p) for i in range(1, x + 1))
|
return sum(gd(i, p) for i in range(1, x + 1))
|
||||||
|
|
||||||
|
|
||||||
def gd_lt(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.
|
Computes the cumulative probability of getting less than x trials until the first success.
|
||||||
:param x: Number of trials until the first success.
|
:param x: Number of trials until the first success.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:param q: Probability of failure.
|
:param q: Probability of failure.
|
||||||
:return: Returns the cumulative probability of getting less than x trials until the first success.
|
:return: Returns the cumulative probability of getting less than x trials until the first success.
|
||||||
"""
|
"""
|
||||||
if q is not None:
|
if q is not None:
|
||||||
return sum(gd(i, p, q) for i in range(1, x))
|
return sum(gd(i, p, q) for i in range(1, x))
|
||||||
return sum(gd(i, p) for i in range(1, x))
|
return sum(gd(i, p) for i in range(1, x))
|
||||||
|
|
||||||
|
|
||||||
def gd_geq(x, p, q=None):
|
def gd_geq(x, p, q=None):
|
||||||
"""
|
"""
|
||||||
Computes the cumulative probability of getting from x trials until the first success.
|
Computes the cumulative probability of getting from x trials until the first success.
|
||||||
:param x: Number of trials until the first success.
|
:param x: Number of trials until the first success.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:param q: Probability of failure.
|
:param q: Probability of failure.
|
||||||
:return: Returns the cumulative probability of getting from x trials until the first success.
|
:return: Returns the cumulative probability of getting from x trials until the first success.
|
||||||
"""
|
"""
|
||||||
if q is not None:
|
if q is not None:
|
||||||
return 1 - gd_lt(x, p, q)
|
return 1 - gd_lt(x, p, q)
|
||||||
return 1 - gd_leq(x, p)
|
return 1 - gd_leq(x, p)
|
||||||
|
|
||||||
|
|
||||||
def gd_gt(x, p, q=None):
|
def gd_gt(x, p, q=None):
|
||||||
"""
|
"""
|
||||||
Computes the cumulative probability of getting from x trials until the first success.
|
Computes the cumulative probability of getting from x trials until the first success.
|
||||||
:param x: Number of trials until the first success.
|
:param x: Number of trials until the first success.
|
||||||
:param p: Probability of success.
|
:param p: Probability of success.
|
||||||
:param q: Probability of failure.
|
:param q: Probability of failure.
|
||||||
:return: Returns the cumulative probability of getting from x trials until the first success.
|
:return: Returns the cumulative probability of getting from x trials until the first success.
|
||||||
"""
|
"""
|
||||||
if q is not None:
|
if q is not None:
|
||||||
return 1 - gd_leq(x, p, q)
|
return 1 - gd_leq(x, p, q)
|
||||||
return 1 - gd_leq(x, p)
|
return 1 - gd_leq(x, p)
|
||||||
|
|
||||||
|
|
||||||
def hgd(x, N, n, k):
|
def hgd(x, N, n, k):
|
||||||
"""
|
"""
|
||||||
Computes the hyper geometric distribution.
|
Computes the hyper geometric distribution.
|
||||||
:param x: Number of successes in the sample.
|
:param x: Number of successes in the sample.
|
||||||
:param N: Number of items in the population.
|
:param N: Number of items in the population.
|
||||||
:param n: Number of draws.
|
:param n: Number of draws.
|
||||||
:param k: Number of successes in the population.
|
: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: Returns the probability of getting x successes in n draws from a population of size N with k successes.
|
||||||
"""
|
"""
|
||||||
return (combination(k, x) * combination(N - k, n - x)) / combination(N, n)
|
return (combination(k, x) * combination(N - k, n - x)) / combination(N, n)
|
||||||
|
|
||||||
|
|
||||||
def hgd_mean(N, n, k):
|
def hgd_mean(N, n, k):
|
||||||
"""
|
"""
|
||||||
Computes the mean of the hyper geometric distribution.
|
Computes the mean of the hyper geometric distribution.
|
||||||
:param N: Number of items in the population.
|
:param N: Number of items in the population.
|
||||||
:param n: Number of draws.
|
:param n: Number of draws.
|
||||||
:param k: Number of successes in the population.
|
:param k: Number of successes in the population.
|
||||||
:return: Returns the mean of the hyper geometric distribution.
|
:return: Returns the mean of the hyper geometric distribution.
|
||||||
"""
|
"""
|
||||||
return n * (k / N)
|
return n * (k / N)
|
||||||
|
|
||||||
|
|
||||||
def hgd_var(N, n, k):
|
def hgd_var(N, n, k):
|
||||||
"""
|
"""
|
||||||
Computes the variance of the hyper geometric distribution.
|
Computes the variance of the hyper geometric distribution.
|
||||||
:param N: Number of items in the population.
|
:param N: Number of items in the population.
|
||||||
:param n: Number of draws.
|
:param n: Number of draws.
|
||||||
:param k: Number of successes in the population.
|
:param k: Number of successes in the population.
|
||||||
:return: Returns the variance of the hyper geometric distribution.
|
:return: Returns the variance of the hyper geometric distribution.
|
||||||
"""
|
"""
|
||||||
return (n * k * (N - k) * (N - n)) / (N ** 2 * (N - 1))
|
return (n * k * (N - k) * (N - n)) / (N ** 2 * (N - 1))
|
||||||
|
|
||||||
|
|
||||||
def hgd_std(N, n, k):
|
def hgd_std(N, n, k):
|
||||||
"""
|
"""
|
||||||
Computes the standard deviation of the hyper geometric distribution.
|
Computes the standard deviation of the hyper geometric distribution.
|
||||||
:param N: Number of items in the population.
|
:param N: Number of items in the population.
|
||||||
:param n: Number of draws.
|
:param n: Number of draws.
|
||||||
:param k: Number of successes in the population.
|
:param k: Number of successes in the population.
|
||||||
:return: Returns the standard deviation of the hyper geometric distribution.
|
:return: Returns the standard deviation of the hyper geometric distribution.
|
||||||
"""
|
"""
|
||||||
return hgd_var(N, n, k) ** 0.5
|
return hgd_var(N, n, k) ** 0.5
|
||||||
|
|
||||||
|
|
||||||
def hgd_leq(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.
|
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 x: Number of successes in the sample.
|
||||||
:param N: Number of items in the population.
|
:param N: Number of items in the population.
|
||||||
:param n: Number of draws.
|
:param n: Number of draws.
|
||||||
:param k: Number of successes in the population.
|
: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: 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))
|
return sum(hgd(i, N, n, k) for i in range(x + 1))
|
||||||
|
|
||||||
|
|
||||||
def hgd_lt(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.
|
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 x: Number of successes in the sample.
|
||||||
:param N: Number of items in the population.
|
:param N: Number of items in the population.
|
||||||
:param n: Number of draws.
|
:param n: Number of draws.
|
||||||
:param k: Number of successes in the population.
|
: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: 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))
|
return sum(hgd(i, N, n, k) for i in range(x))
|
||||||
|
|
||||||
|
|
||||||
def hgd_geq(x, N, n, k):
|
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.
|
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 x: Number of successes in the sample.
|
||||||
:param N: Number of items in the population.
|
:param N: Number of items in the population.
|
||||||
:param n: Number of draws.
|
:param n: Number of draws.
|
||||||
:param k: Number of successes in the population.
|
: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: Returns the cumulative probability of getting from x successes in n draws from a population of size N with k successes.
|
||||||
"""
|
"""
|
||||||
return 1 - hgd_lt(x, N, n, k)
|
return 1 - hgd_lt(x, N, n, k)
|
||||||
|
|
||||||
|
|
||||||
def hgd_gt(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.
|
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 x: Number of successes in the sample.
|
||||||
:param N: Number of items in the population.
|
:param N: Number of items in the population.
|
||||||
:param n: Number of draws.
|
:param n: Number of draws.
|
||||||
:param k: Number of successes in the population.
|
: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: 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)
|
return 1 - hgd_leq(x, N, n, k)
|
||||||
|
|
||||||
|
|
||||||
def pd(x, l):
|
def pd(x, l):
|
||||||
"""
|
"""
|
||||||
Computes the poisson distribution.
|
Computes the poisson distribution.
|
||||||
:param x: Number of occurrences.
|
:param x: Number of occurrences.
|
||||||
:param l: Average number of occurrences.
|
:param l: Average number of occurrences.
|
||||||
:return: Returns the probability of getting x occurrences.
|
:return: Returns the probability of getting x occurrences.
|
||||||
"""
|
"""
|
||||||
return (l ** x * math.e ** -l) / factorial(x)
|
return (l ** x * math.e ** -l) / factorial(x)
|
||||||
|
|
||||||
|
|
||||||
def pd_mean(l):
|
def pd_mean(l):
|
||||||
"""
|
"""
|
||||||
Computes the mean of the poisson distribution.
|
Computes the mean of the poisson distribution.
|
||||||
:param l: Average number of occurrences.
|
:param l: Average number of occurrences.
|
||||||
:return: Returns the mean of the poisson distribution.
|
:return: Returns the mean of the poisson distribution.
|
||||||
"""
|
"""
|
||||||
return l
|
return l
|
||||||
|
|
||||||
|
|
||||||
def pd_var(l):
|
def pd_var(l):
|
||||||
"""
|
"""
|
||||||
Computes the variance of the poisson distribution.
|
Computes the variance of the poisson distribution.
|
||||||
:param l: Average number of occurrences.
|
:param l: Average number of occurrences.
|
||||||
:return: Returns the variance of the poisson distribution.
|
:return: Returns the variance of the poisson distribution.
|
||||||
"""
|
"""
|
||||||
return l
|
return l
|
||||||
|
|
||||||
|
|
||||||
def pd_std(l):
|
def pd_std(l):
|
||||||
"""
|
"""
|
||||||
Computes the standard deviation of the poisson distribution.
|
Computes the standard deviation of the poisson distribution.
|
||||||
:param l: Average number of occurrences.
|
:param l: Average number of occurrences.
|
||||||
:return: Returns the standard deviation of the poisson distribution.
|
:return: Returns the standard deviation of the poisson distribution.
|
||||||
"""
|
"""
|
||||||
return l ** 0.5
|
return l ** 0.5
|
||||||
|
|
||||||
|
|
||||||
def pd_leq(x, l):
|
def pd_leq(x, l):
|
||||||
"""
|
"""
|
||||||
Computes the cumulative probability of getting upto x occurrences.
|
Computes the cumulative probability of getting upto x occurrences.
|
||||||
:param x: Number of occurrences.
|
:param x: Number of occurrences.
|
||||||
:param l: Average number of occurrences.
|
:param l: Average number of occurrences.
|
||||||
:return: Returns the cumulative probability of getting upto x occurrences.
|
:return: Returns the cumulative probability of getting upto x occurrences.
|
||||||
"""
|
"""
|
||||||
return sum(pd(i, l) for i in range(x + 1))
|
return sum(pd(i, l) for i in range(x + 1))
|
||||||
|
|
||||||
|
|
||||||
def pd_lt(x, l):
|
def pd_lt(x, l):
|
||||||
"""
|
"""
|
||||||
Computes the cumulative probability of getting less than x occurrences.
|
Computes the cumulative probability of getting less than x occurrences.
|
||||||
:param x: Number of occurrences.
|
:param x: Number of occurrences.
|
||||||
:param l: Average number of occurrences.
|
:param l: Average number of occurrences.
|
||||||
:return: Returns the cumulative probability of getting less than x occurrences.
|
:return: Returns the cumulative probability of getting less than x occurrences.
|
||||||
"""
|
"""
|
||||||
return sum(pd(i, l) for i in range(x))
|
return sum(pd(i, l) for i in range(x))
|
||||||
|
|
||||||
|
|
||||||
def pd_geq(x, l):
|
def pd_geq(x, l):
|
||||||
"""
|
"""
|
||||||
Computes the cumulative probability of getting from x occurrences.
|
Computes the cumulative probability of getting from x occurrences.
|
||||||
:param x: Number of occurrences.
|
:param x: Number of occurrences.
|
||||||
:param l: Average number of occurrences.
|
:param l: Average number of occurrences.
|
||||||
:return: Returns the cumulative probability of getting from x occurrences.
|
:return: Returns the cumulative probability of getting from x occurrences.
|
||||||
"""
|
"""
|
||||||
return 1 - pd_lt(x, l)
|
return 1 - pd_lt(x, l)
|
||||||
|
|
||||||
|
|
||||||
def pd_gt(x, l):
|
def pd_gt(x, l):
|
||||||
"""
|
"""
|
||||||
Computes the cumulative probability of getting from x occurrences.
|
Computes the cumulative probability of getting from x occurrences.
|
||||||
:param x: Number of occurrences.
|
:param x: Number of occurrences.
|
||||||
:param l: Average number of occurrences.
|
:param l: Average number of occurrences.
|
||||||
:return: Returns the cumulative probability of getting from x occurrences.
|
:return: Returns the cumulative probability of getting from x occurrences.
|
||||||
"""
|
"""
|
||||||
return 1 - pd_leq(x, l)
|
return 1 - pd_leq(x, l)
|
||||||
|
|
||||||
|
|
||||||
|
def sample_mean_e(u):
|
||||||
|
"""
|
||||||
|
Computes the expected value of the sample mean.
|
||||||
|
:param u: The population mean.
|
||||||
|
:return: Returns the expected value of the sample mean.
|
||||||
|
"""
|
||||||
|
return u
|
||||||
|
|
||||||
|
|
||||||
|
def sample_mean_std(u, n):
|
||||||
|
"""
|
||||||
|
Computes the standard deviation of the sample mean.
|
||||||
|
:param u: The population mean.
|
||||||
|
:param n: The sample size.
|
||||||
|
:return: Returns the standard deviation of the sample mean.
|
||||||
|
"""
|
||||||
|
return u / n ** 0.5
|
||||||
|
|
||||||
|
|
||||||
|
def sample_mean_var(u, n):
|
||||||
|
"""
|
||||||
|
Computes the variance of the sample mean.
|
||||||
|
:param u: The population mean.
|
||||||
|
:param n: The sample size.
|
||||||
|
:return: Returns the variance of the sample mean.
|
||||||
|
"""
|
||||||
|
return (sample_mean_std(u, n) ** 2) / n
|
||||||
|
|
||||||
|
|
||||||
|
def z_score(x, u, s):
|
||||||
|
"""
|
||||||
|
Computes the z-score of a sample.
|
||||||
|
:param x: The sample mean.
|
||||||
|
:param u: The population mean.
|
||||||
|
:param s: The standard deviation of the sample mean.
|
||||||
|
:return: Returns the z-score of the sample.
|
||||||
|
"""
|
||||||
|
return (x - u) / s
|
||||||
|
|
||||||
|
|
||||||
|
def z_to_p(z):
|
||||||
|
"""
|
||||||
|
Computes the probability of a z-score.
|
||||||
|
:param z: The z-score.
|
||||||
|
:return: Returns the probability of the z-score.
|
||||||
|
"""
|
||||||
|
nd = statistics.NormalDist()
|
||||||
|
return nd.cdf(z)
|
||||||
|
|
||||||
|
|
||||||
|
def p_to_z(p):
|
||||||
|
"""
|
||||||
|
Computes the z-score of a probability.
|
||||||
|
:param p: The probability.
|
||||||
|
:return: Returns the z-score of the probability.
|
||||||
|
"""
|
||||||
|
nd = statistics.NormalDist()
|
||||||
|
return nd.inv_cdf(p)
|
||||||
|
|
||||||
|
|
||||||
|
def gamma(u, n):
|
||||||
|
"""
|
||||||
|
Computes the gamma of a sample.
|
||||||
|
:param u: The population mean.
|
||||||
|
:param n: The sample size.
|
||||||
|
:return: Returns the gamma of the sample.
|
||||||
|
"""
|
||||||
|
return sample_mean_var(u, n) / sample_mean_e(u)
|
||||||
|
|
||||||
|
|
||||||
|
def alpha(u, n):
|
||||||
|
"""
|
||||||
|
Computes the alpha of a sample.
|
||||||
|
:param u: The population mean.
|
||||||
|
:param n: The sample size.
|
||||||
|
:return: Returns the alpha of the sample.
|
||||||
|
"""
|
||||||
|
return sample_mean_e(u) / gamma(u, n)
|
||||||
|
|
||||||
|
|
||||||
|
def margin_of_error(a, s, n):
|
||||||
|
"""
|
||||||
|
Computes the margin of error of a sample.
|
||||||
|
:param a: The alpha of the sample.
|
||||||
|
:param s: The standard deviation of the sample mean.
|
||||||
|
:param n: The sample size.
|
||||||
|
:return: Returns the margin of error of the sample.
|
||||||
|
"""
|
||||||
|
return abs((p_to_z(a / 2)) * (s / (n ** 0.5)))
|
||||||
|
|
||||||
|
|
||||||
|
def confidence_interval(x, a, s, n):
|
||||||
|
"""
|
||||||
|
Computes the confidence interval of a sample.
|
||||||
|
:param x: The sample mean.
|
||||||
|
:param a: The alpha of the sample.
|
||||||
|
:param s: The standard deviation of the sample mean.
|
||||||
|
:param n: The sample size.
|
||||||
|
:return: Returns the confidence interval of the sample.
|
||||||
|
"""
|
||||||
|
return x - margin_of_error(a, s, n), x + margin_of_error(a, s, n)
|
||||||
|
|
||||||
|
|
||||||
def man():
|
def man():
|
||||||
"""
|
"""
|
||||||
Prints the manual for the module.
|
Prints the manual for the module.
|
||||||
"""
|
"""
|
||||||
seperator = "-" * 20
|
separator = "-" * 20
|
||||||
print("This module contains functions for computing the total probability of events.")
|
print("This module contains functions for computing the total probability of events.")
|
||||||
print("The functions are:")
|
print("The functions are:")
|
||||||
print(seperator)
|
print(separator)
|
||||||
print("Binomial Distribution")
|
print("Binomial Distribution")
|
||||||
print("bnd(x, n, p)")
|
print("bnd(x, n, p)")
|
||||||
print("bnd_mean(n, p)")
|
print("bnd_mean(n, p)")
|
||||||
print("bnd_var(n, p)")
|
print("bnd_var(n, p)")
|
||||||
print("bnd_std(n, p)")
|
print("bnd_std(n, p)")
|
||||||
print("bnd_leq(x, n, p)")
|
print("bnd_leq(x, n, p)")
|
||||||
print("bnd_lt(x, n, p)")
|
print("bnd_lt(x, n, p)")
|
||||||
print("bnd_geq(x, n, p)")
|
print("bnd_geq(x, n, p)")
|
||||||
print("bnd_gt(x, n, p)")
|
print("bnd_gt(x, n, p)")
|
||||||
print(seperator)
|
print(separator)
|
||||||
print("Geometric Distribution")
|
print("Geometric Distribution")
|
||||||
print("gd(x, p, q)")
|
print("gd(x, p, q)")
|
||||||
print("gd_mean(p)")
|
print("gd_mean(p)")
|
||||||
print("gd_var(p)")
|
print("gd_var(p)")
|
||||||
print("gd_std(p)")
|
print("gd_std(p)")
|
||||||
print("gd_leq(x, p, q)")
|
print("gd_leq(x, p, q)")
|
||||||
print("gd_lt(x, p, q)")
|
print("gd_lt(x, p, q)")
|
||||||
print("gd_geq(x, p, q)")
|
print("gd_geq(x, p, q)")
|
||||||
print("gd_gt(x, p, q)")
|
print("gd_gt(x, p, q)")
|
||||||
print(seperator)
|
print(separator)
|
||||||
print("Hyper Geometric Distribution")
|
print("Hyper Geometric Distribution")
|
||||||
print("hgd(x, N, n, k)")
|
print("hgd(x, N, n, k)")
|
||||||
print("hgd_mean(N, n, k)")
|
print("hgd_mean(N, n, k)")
|
||||||
print("hgd_var(N, n, k)")
|
print("hgd_var(N, n, k)")
|
||||||
print("hgd_std(N, n, k)")
|
print("hgd_std(N, n, k)")
|
||||||
print("hgd_leq(x, N, n, k)")
|
print("hgd_leq(x, N, n, k)")
|
||||||
print("hgd_lt(x, N, n, k)")
|
print("hgd_lt(x, N, n, k)")
|
||||||
print("hgd_geq(x, N, n, k)")
|
print("hgd_geq(x, N, n, k)")
|
||||||
print("hgd_gt(x, N, n, k)")
|
print("hgd_gt(x, N, n, k)")
|
||||||
print(seperator)
|
print(separator)
|
||||||
print("Poisson Distribution")
|
print("Poisson Distribution")
|
||||||
print("pd(x, l)")
|
print("pd(x, l)")
|
||||||
print("pd_mean(l)")
|
print("pd_mean(l)")
|
||||||
print("pd_var(l)")
|
print("pd_var(l)")
|
||||||
print("pd_std(l)")
|
print("pd_std(l)")
|
||||||
print("pd_leq(x, l)")
|
print("pd_leq(x, l)")
|
||||||
print("pd_lt(x, l)")
|
print("pd_lt(x, l)")
|
||||||
print("pd_geq(x, l)")
|
print("pd_geq(x, l)")
|
||||||
print("pd_gt(x, l)")
|
print("pd_gt(x, l)")
|
||||||
|
print(separator)
|
||||||
|
print("Sample Mean")
|
||||||
|
print("sample_mean_e(u)")
|
||||||
|
print("sample_mean_std(u, n)")
|
||||||
|
print("sample_mean_var(u, n)")
|
||||||
|
print("z_score(x, u, s)")
|
||||||
|
print("z_to_p(z)")
|
||||||
|
print("p_to_z(p)")
|
||||||
|
print("gamma(u, n)")
|
||||||
|
print("alpha(u, n)")
|
||||||
|
print("margin_of_error(a, s, n)")
|
||||||
|
print("confidence_interval(x, a, s, n)")
|
||||||
|
Loading…
Reference in New Issue
Block a user