Compare commits

...

4 Commits

Author SHA1 Message Date
4579978900 Fix cdf on micropython 2024-04-22 13:27:55 -03:00
cdc19c0509 Add some cdf functions from std lib 2024-04-22 13:16:50 -03:00
e3566d1c21 Update distribution.py 2024-04-22 12:52:11 -03:00
ee9a752d7f Add net centric 2024-02-26 07:23:13 -04:00
7 changed files with 619 additions and 318 deletions

2
.idea/.gitignore generated vendored
View File

@ -6,3 +6,5 @@
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# GitHub Copilot persisted chat sessions
/copilot/chatSessions

View File

@ -1,7 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<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="sourceFolder" forTests="false" />
</component>

View File

@ -9,4 +9,3 @@ name = "pypi"
[requires]
python_version = "3.11"
python_full_version = "3.11.7"

3
Pipfile.lock generated
View File

@ -1,11 +1,10 @@
{
"_meta": {
"hash": {
"sha256": "bc82cd27f07d4e24b750064464bbc233a141778868b9a387125705e2d4e8a830"
"sha256": "ed6d5d614626ae28e274e453164affb26694755170ccab3aa5866f093d51d3e4"
},
"pipfile-spec": 6,
"requires": {
"python_full_version": "3.11.7",
"python_version": "3.11"
},
"sources": [

79
cdf.py Normal file
View File

@ -0,0 +1,79 @@
import math
def normal_dist_cdf(x, mu=0.0, sigma=1.0):
return 0.5 * (1.0 + math.erf((x - mu) / (sigma * (2 ** 0.5))))
def normal_dist_inv_cdf(p, mu=0.0, sigma=1.0):
# There is no closed-form solution to the inverse CDF for the normal
# distribution, so we use a rational approximation instead:
# Wichura, M.J. (1988). "Algorithm AS241: The Percentage Points of the
# Normal Distribution". Applied Statistics. Blackwell Publishing. 37
# (3): 477484. doi:10.2307/2347330. JSTOR 2347330.
q = p - 0.5
if math.fabs(q) <= 0.425:
r = 0.180625 - q * q
# Hash sum: 55.88319_28806_14901_4439
num = (((((((2.5090809287301226727e+3 * r +
3.3430575583588128105e+4) * r +
6.7265770927008700853e+4) * r +
4.5921953931549871457e+4) * r +
1.3731693765509461125e+4) * r +
1.9715909503065514427e+3) * r +
1.3314166789178437745e+2) * r +
3.3871328727963666080e+0) * q
den = (((((((5.2264952788528545610e+3 * r +
2.8729085735721942674e+4) * r +
3.9307895800092710610e+4) * r +
2.1213794301586595867e+4) * r +
5.3941960214247511077e+3) * r +
6.8718700749205790830e+2) * r +
4.2313330701600911252e+1) * r +
1.0)
x = num / den
return mu + (x * sigma)
r = p if q <= 0.0 else 1.0 - p
r = math.sqrt(-math.log(r))
if r <= 5.0:
r = r - 1.6
# Hash sum: 49.33206_50330_16102_89036
num = (((((((7.74545014278341407640e-4 * r +
2.27238449892691845833e-2) * r +
2.41780725177450611770e-1) * r +
1.27045825245236838258e+0) * r +
3.64784832476320460504e+0) * r +
5.76949722146069140550e+0) * r +
4.63033784615654529590e+0) * r +
1.42343711074968357734e+0)
den = (((((((1.05075007164441684324e-9 * r +
5.47593808499534494600e-4) * r +
1.51986665636164571966e-2) * r +
1.48103976427480074590e-1) * r +
6.89767334985100004550e-1) * r +
1.67638483018380384940e+0) * r +
2.05319162663775882187e+0) * r +
1.0)
else:
r = r - 5.0
# Hash sum: 47.52583317549289671629
num = (((((((2.01033439929228813265e-7 * r +
2.71155556874348757815e-5) * r +
1.24266094738807843860e-3) * r +
2.65321895265761230930e-2) * r +
2.96560571828504891230e-1) * r +
1.78482653991729133580e+0) * r +
5.46378491116411436990e+0) * r +
6.65790464350110377720e+0)
den = (((((((2.04426310338993978564e-15 * r +
1.42151175831644588870e-7) * r +
1.84631831751005468180e-5) * r +
7.86869131145613259100e-4) * r +
1.48753612908506148525e-2) * r +
1.36929880922735805310e-1) * r +
5.99832206555887937690e-1) * r +
1.0)
x = num / den
if q < 0.0:
x = -x
return mu + (x * sigma)

View File

@ -1,420 +1,535 @@
import math
import cdf
def factorial(n):
"""
Computes the factorial of a number.
:param n: The number to compute the factorial of.
:return: Returns the factorial of the number.
"""
if n == 0:
return 1
for i in range(1, n):
n *= i
return n
"""
Computes the factorial of a number.
:param n: The number to compute the factorial of.
:return: Returns the factorial of the number.
"""
if n == 0:
return 1
for i in range(1, n):
n *= i
return n
def combination(n, r):
"""
Computes the combination of n choose r.
:param n: The number of items.
:param r: The number of items to choose.
:return: Returns the number of ways to choose r items from n items.
"""
return factorial(n) / (factorial(r) * factorial(n - r))
"""
Computes the combination of n choose r.
:param n: The number of items.
:param r: The number of items to choose.
:return: Returns the number of ways to choose r items from n items.
"""
return factorial(n) / (factorial(r) * factorial(n - r))
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 combination(n, x) * p ** x * (1 - p) ** (n - x)
"""
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 combination(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
"""
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)
"""
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
"""
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_leq(x, n, p):
"""
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 less than or equal to x successes in n trials.
"""
return sum(bnd(i, n, p) for i in range(x + 1))
"""
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 less than or equal to x successes in n trials.
"""
return sum(bnd(i, n, p) for i in range(x + 1))
def bnd_lt(x, n, p):
"""
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 less than x successes in n trials.
"""
return sum(bnd(i, n, p) for i in range(x))
"""
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 less than x successes in n trials.
"""
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)
"""
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)
"""
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):
"""
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
"""
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
"""
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
"""
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
"""
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_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.
:param p: Probability of success.
:param q: Probability of failure.
:return: Returns the cumulative probability of getting upto x trials until the first success.
"""
if q is not None:
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))
"""
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.
:param q: Probability of failure.
:return: Returns the cumulative probability of getting upto x trials until the first success.
"""
if q is not None:
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))
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))
"""
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.
: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_lt(x, p, q)
return 1 - gd_leq(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.
: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_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)
"""
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):
"""
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 (combination(k, x) * combination(N - k, n - x)) / combination(N, n)
"""
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 (combination(k, x) * combination(N - k, n - x)) / combination(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)
"""
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))
"""
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
"""
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_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.
: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))
"""
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_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))
"""
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.
: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_lt(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_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)
"""
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):
"""
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) / factorial(x)
"""
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) / 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
"""
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
"""
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
"""
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_leq(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))
"""
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_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))
"""
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_lt(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_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)
"""
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 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.
"""
return cdf.normal_dist_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.
"""
return cdf.normal_dist_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():
"""
Prints the manual for the module.
"""
seperator = "-" * 20
print("This module contains functions for computing the total probability of events.")
print("The functions are:")
print(seperator)
print("Binomial Distribution")
print("bnd(x, n, p)")
print("bnd_mean(n, p)")
print("bnd_var(n, p)")
print("bnd_std(n, p)")
print("bnd_leq(x, n, p)")
print("bnd_lt(x, n, p)")
print("bnd_geq(x, n, p)")
print("bnd_gt(x, n, p)")
print(seperator)
print("Geometric Distribution")
print("gd(x, p, q)")
print("gd_mean(p)")
print("gd_var(p)")
print("gd_std(p)")
print("gd_leq(x, p, q)")
print("gd_lt(x, p, q)")
print("gd_geq(x, p, q)")
print("gd_gt(x, p, q)")
print(seperator)
print("Hyper Geometric Distribution")
print("hgd(x, N, n, k)")
print("hgd_mean(N, n, k)")
print("hgd_var(N, n, k)")
print("hgd_std(N, n, k)")
print("hgd_leq(x, N, n, k)")
print("hgd_lt(x, N, n, k)")
print("hgd_geq(x, N, n, k)")
print("hgd_gt(x, N, n, k)")
print(seperator)
print("Poisson Distribution")
print("pd(x, l)")
print("pd_mean(l)")
print("pd_var(l)")
print("pd_std(l)")
print("pd_leq(x, l)")
print("pd_lt(x, l)")
print("pd_geq(x, l)")
print("pd_gt(x, l)")
"""
Prints the manual for the module.
"""
separator = "-" * 20
print("This module contains functions for computing the total probability of events.")
print("The functions are:")
print(separator)
print("Binomial Distribution")
print("bnd(x, n, p)")
print("bnd_mean(n, p)")
print("bnd_var(n, p)")
print("bnd_std(n, p)")
print("bnd_leq(x, n, p)")
print("bnd_lt(x, n, p)")
print("bnd_geq(x, n, p)")
print("bnd_gt(x, n, p)")
print(separator)
print("Geometric Distribution")
print("gd(x, p, q)")
print("gd_mean(p)")
print("gd_var(p)")
print("gd_std(p)")
print("gd_leq(x, p, q)")
print("gd_lt(x, p, q)")
print("gd_geq(x, p, q)")
print("gd_gt(x, p, q)")
print(separator)
print("Hyper Geometric Distribution")
print("hgd(x, N, n, k)")
print("hgd_mean(N, n, k)")
print("hgd_var(N, n, k)")
print("hgd_std(N, n, k)")
print("hgd_leq(x, N, n, k)")
print("hgd_lt(x, N, n, k)")
print("hgd_geq(x, N, n, k)")
print("hgd_gt(x, N, n, k)")
print(separator)
print("Poisson Distribution")
print("pd(x, l)")
print("pd_mean(l)")
print("pd_var(l)")
print("pd_std(l)")
print("pd_leq(x, l)")
print("pd_lt(x, l)")
print("pd_geq(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)")

105
netcentric.py Normal file
View File

@ -0,0 +1,105 @@
# Calculate internet checksum for any arbitrary length variable amount of binary numbers
def checksum(*args):
length = len(bin(args[0])) - 2
result = 0
for arg in args:
result += arg
if result > (2 ** length) - 1:
result = (result & ((2 ** length) - 1)) + 1
return result ^ ((2 ** length) - 1)
def e2e_delay(P, L, N, R):
# P = propagation speed
# L = packet length
# N = number of packets
# R = transmission rate
return (P - 1) * (L / R) + N * (L / R)
def estimate_e2e_delay(PS, T):
# PS = packet size
# T = Throughput
return PS / T
def trans_delay(L, R):
# L = packet length
# R = transmission rate
return L / R
def prop_delay(P, L):
# P = propagation speed
# L = packet length
return P * L
def traffic_intensity(L, pps, R):
# L = packet length
# pps = packets per second
# R = transmission rate
return (L * pps) / R
def cs_time(N, F, U):
# N = Number of copies
# F = File size
# U = Server upload rate
return (N * F) / U
def cs_time_n_clients(N, F, U, D):
# N = Number of copies
# F = File size
# U = Server upload rate
# D = Client download rate
return max(cs_time(N, F, U), F / D)
def p2p_time(N, F, U, CD, D):
# N = Number of copies
# F = File size
# U = Server upload rate
# CU = Client upload rate
# D = Client download rate
return max((F / U), (N * F) / (U + (N * CD)), F / D)
def utilisation(L, R, RTT):
# L = packet length
# R = transmission rate
# RTT = round trip time
return (L / R) / (L / R + RTT)
def utilisation_pipeline(L, R, RTT, N):
# L = packet length
# R = transmission rate
# RTT = round trip time
# N = window size
return N / (1 + (RTT / (L / R)))
def print_byte_tables():
print("Byte Conversion Table")
print("1 B = 8 bits")
print("kB = 1024 bytes")
print("MB = 1024 kB")
print("GB = 1024 MB")
print("TB = 1024 GB")
def man():
print("checksum(0b, 0b,)")
print("e2e_delay(P, L, N, R)")
print("estimate_e2e_delay(PS, T)")
print("trans_delay(L, R)")
print("prop_delay(P, L)")
print("traffic_intensity(L, pps, R)")
print("cs_time(N, F, U)")
print("cs_time_n_clients(N, F, U, D)")
print("p2p_time(N, F, U, CD, D)")
print("utilisation(L, R, RTT)")
print("utilisation_pipeline(L, R, RTT, N)")
print("print_byte_tables()")