Skip to content

Commit

Permalink
add normal distribution functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zdimension committed May 8, 2018
1 parent 9192ee4 commit f87c36d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/maths/lib/algobox.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def wrapper(lst, p, n):
ALGOBOX_NB_COMBINAISONS = binomial
ALGOBOX_COEFF_BINOMIAL = binomial
ALGOBOX_LOI_BINOMIAL = d_binomial
ALGOBOX_LOI_NORMALE_CR = None
ALGOBOX_LOI_NORMALE = None
ALGOBOX_INVERSE_LOI_NORMALE_CR = None
ALGOBOX_INVERSE_LOI_NORMALE = None
ALGOBOX_LOI_NORMALE_CR = d_normal_std_cdf
ALGOBOX_LOI_NORMALE = d_normal_cdf
ALGOBOX_INVERSE_LOI_NORMALE_CR = d_normal_std_cdf_inv
ALGOBOX_INVERSE_LOI_NORMALE = d_normal_cdf_inv
ALGOBOX_FACTORIELLE = fact
ALGOBOX_SOMME = algobox_listfunc(sum)
ALGOBOX_MOYENNE = algobox_listfunc(arithm_mean)
Expand Down
78 changes: 76 additions & 2 deletions src/maths/lib/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import statistics
import numpy as np

from maths.lib import basic
from util import translate
from . import basic, trig
from .docs import *

__desc__ = translate("Docs", "Statistics")
Expand Down Expand Up @@ -535,4 +535,78 @@ def irange(start, end=None, step=None):
step = -1
else:
step = 1
return np.arange(start, end + step, step)
return np.arange(start, end + step, step)


doc("d_normal_pdf",
[
("mu", "Real"),
("sigma", "Real"),
("x", "Real")
],
translate("Docs",
"Returns the probability for {{x}} with the normal distribution of parameters µ={{mu}} and σ={{sigma}}."))


def d_normal_pdf(mu, sigma, x):
return 1 / (sigma * math.sqrt(2 * trig.c_pi)) * math.exp(
- (math.pow(x - mu, 2) / (2 * math.pow(sigma, 2))))


doc("d_normal_std_pdf",
[
("x", "Real")
],
translate("Docs", "Returns the probability for {{x}} with the standard normal distribution (µ=0 and σ=1)."))


def d_normal_std_pdf(x):
return 1 / math.sqrt(2 * trig.c_pi) * math.exp(-pow(x, 2) / 2)


doc("d_normal_cdf",
[
("mu", "Real"),
("sigma", "Real"),
("x", "Real")
],
translate("Docs", "Returns the cumulative probability for {{x}} with the normal distribution of parameters µ={{mu}} and σ={{sigma}}."))


def d_normal_cdf(mu, sigma, x):
return d_normal_std_cdf((x - mu) / sigma)


doc("d_normal_std_cdf",
[
("x", "Real")
],
translate("Docs", "Returns the cumulative probability for {{x}} with the standard normal distribution (µ=0 and σ=1)."))


def d_normal_std_cdf(x):
return 1 - 0.5 * erfc(x / math.sqrt(2))


doc("d_normal_cdf_inv",
[
("mu", "Real"),
("sigma", "Real"),
("p", "Real")
],
translate("Docs", "Returns the number with cumulative probability {{p}} with the normal distribution of parameters µ={{mu}} and σ={{sigma}}."))


def d_normal_cdf_inv(mu, sigma, p):
return d_normal_std_cdf_inv(p) * sigma + mu


doc("d_normal_std_cdf_inv",
[
("p", "Real")
],
translate("Docs", "Returns the number with cumulative probability {{p}} with the standard normal distribution (µ=0 and σ=1)."))


def d_normal_std_cdf_inv(p):
return erfcinv((1 - p) * 2) * math.sqrt(2)

0 comments on commit f87c36d

Please sign in to comment.