-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistriBinomialPoisson.py
More file actions
59 lines (49 loc) · 1.84 KB
/
distriBinomialPoisson.py
File metadata and controls
59 lines (49 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import analiCombi as comb
import math
def distriNormalInv(probabilidad, media, desviacion):
return norm.ppf(probabilidad, loc=madia, scale=desviacion)
def distriBinomial(num_exitos, ensayos, prob_exito, acumulado):
combinat = comb.combSinRep(ensayos, num_exitos)
pk = prob_exito ** num_exitos
qN_K = (1 - prob_exito) ** (ensayos - num_exitos)
resul = pk * combinat * qN_K
num_exitos = num_exitos - 1
if acumulado and num_exitos >= 0:
return resul + distriBinomial(num_exitos, ensayos, prob_exito, acumulado)
else:
return resul
def distriPoison(x, media, acumulado):
uK = media ** x
expMenU = math.e ** -media
factK = math.factorial(x)
resul = uK * expMenU / factK
x -= 1
if acumulado and x >= 0:
return resul + distriPoison(x, media, acumulado)
else:
return resul
def distriNormal(x, mu, sigma, acumulado):
if sigma <= 0:
raise ValueError("La desviación estándar debe ser mayor que cero.")
# Cálculo de la PDF
densidad = (1 / (sigma * math.sqrt(2 * math.pi))) * math.exp(-((x - mu) ** 2) / (2 * sigma ** 2))
if acumulado:
# Cálculo de la CDF usando la aproximación de la función error (erf)
z = (x - mu) / (sigma * math.sqrt(2))
cdf = 0.5 * (1 + math.erf(z))
return cdf
else:
return densidad
if __name__ == "__main__":
n = 5
disB = distriPoison(4, 7, True)
disB = round(disB * 100, 2)
print(disB)
# Ejemplo de uso de la distribución normal
x = 5
mu = 10
sigma = 2
disN = distriNormal(x, mu, sigma, acumulado=False) # PDF
disN_acumulada = distriNormal(x, mu, sigma, acumulado=True) # CDF
print(f"Densidad normal (PDF) para x={x}, mu={mu}, sigma={sigma}: {disN}")
print(f"Probabilidad acumulada (CDF) para x={x}, mu={mu}, sigma={sigma}: {disN_acumulada}")