In [35]:
def generadorCongruencial(n):
m = 91
a = 11
b = 4
Xo = 17
lastXn = Xo
Xi = []
Ui = []
ls = 1
first = 0
print "Used parameters: "
print "a = "+str(a)+" b = "+str(b)+" m = "+str(m)
print "Random numbers generated: "
for i in range(n):
Xn = float((a*lastXn + b)% m)
lastXn = Xn
Xi.append(Xn)
random = float(Xn/m)
Ui.append(random)
if (i == 0):
first = random
print 'Xi: ', Xi
print ""
print 'Ui: ', Ui
for i in range(len(Ui)):
if(i > 0):
if(Ui[i] != first):
ls += 1
else:
break
print ''
print 'LONGITUD DE LA SECUENCIA: ', ls
def main():
generadorCongruencial(100)
main()
In [36]:
def generadorCongruencial(n):
m = 271
a = 139
b = 17
Xo = 9
lastXn = Xo
Xi = []
Ui = []
print "Used parameters: "
print "a = "+str(a)+" b = "+str(b)+" m = "+str(m)
print "Random numbers generated: "
for i in range(n):
Xn = float((a*lastXn + b)% m)
lastXn = Xn
Xi.append(Xn)
random = float(Xn/m)
Ui.append(random)
print 'Xi: ', Xi
print ""
print 'Ui: ', Ui
print ""
print 'Los primeros cinco numeros aleatorios son: ',Ui[0:5]
def main():
generadorCongruencial(100)
main()
In [38]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
def generadorCongruencial(n):
m = 317
a = 16645
b = 13467
Xo = 999
lastXn = Xo
Xi = []
Ui = []
print "Used parameters: "
print "a = "+str(a)+" b = "+str(b)+" m = "+str(m)
print "Random numbers generated: "
for i in range(n):
Xn = float((a*lastXn + b)% m)
lastXn = Xn
Xi.append(Xn)
random = float(Xn/m)
Ui.append(random)
return Ui
def chicuadrado():
n = 2000
x = generadorCongruencial(n)
k = 100
fi, clases, nadaimportante = plt.hist(x, k)
# fi = observaciones encontradas, ei= observaciones esperadas
ei = np.ones_like(clases)*(n/k)
plt.plot(clases, ei, linewidth=3, color = '#FA58F4')
plt.show()
chi2 = 0
ei = n/k
for i in fi:
chi2 += (i-ei)**2
print "El estadistico de Chi-cuadrado para este generador es: ", chi2
def main():
chicuadrado()
main()
Dados los siguientes valores de la distribución de probabilidad discreta de la variable aleatoria X de un dado: X = {1,2,3,4,5,6}, p(X) = {0.2,0.1,0.05,0.2,0.05,0.4}, ¿Qué valores retornaría el generador de variables aleatorias por el método de la inversión si los números aleatorios generados son: U = {0.4280, 0.5609, 0.0258, 0.6531, 0.8487, 0.0332, 0.6790, 0.4391}?
In [40]:
def genVAD(u, x, px):
dAcum = distAcum (x,px)
rList = u
xList = []
for i in rList:
for j in range(len(dAcum)):
if(i <= dAcum[j]):
xList.append(x[j])
break
print "Generated numbers: "
print xList
plt.hist(xList)
def distAcum(x, px):
Px = []
suma = 0
for i in range(len(x)):
Px.append(suma + px[i])
suma = suma + px[i]
return Px
def main():
x = [1,2,3,4,5,6]
px = [0.2,0.1,0.05,0.2,0.05,0.4]
u = [0.4280, 0.5609, 0.0258, 0.6531, 0.8487, 0.0332, 0.6790, 0.4391]
genVAD(u,x,px)
main()
In [41]:
def generadorCongruencial(n):
m = 317
a = 16645
b = 13467
Xo = 999
lastXn = Xo
Xi = []
Ui = []
print "Used parameters: "
print "a = "+str(a)+" b = "+str(b)+" m = "+str(m)
print "Random numbers generated: "
for i in range(n):
Xn = float((a*lastXn + b)% m)
lastXn = Xn
Xi.append(Xn)
random = float(Xn/m)
Ui.append(random)
return Ui
def kolmogorov():
n = 2000
x = generadorCongruencial(n)
u = [1.0] * n
# Graficar distribucion empirica f(x)
count, bins, ignored = plt.hist(x, 10, normed=True)
plt.plot(bins, np.ones_like(bins), linewidth=3, color = '#FA58F4')
plt.show()
# Generamos la distribucion acumulada de una distribucion uniforme con parametros 0 y 1, para n valores.
x1 = np.arange(1/(n+0.0),1+1/(n+0.0),1/(n+0.0))
y1 = np.cumsum(np.sort(u)/np.max(np.cumsum(u)))
#Generamos distribucion acumulada del generador que estamos evaluando para N valores.
x2 = np.sort(x)
y2 = np.cumsum(np.sort(x)/np.max(np.cumsum(x)))
De = np.absolute(y2-y1)
# Plotting
plt.plot(x1,y1,label='Uniforme')
plt.step(x2,y2,label='Aleatoria')
x3 = [x1[De.argmax()],x1[De.argmax()]]
y3 = [np.min([y1[De.argmax()],y2[De.argmax()]]),np.max([y1[De.argmax()],y2[De.argmax()]])]
plt.step(x3,y3,label='De')
plt.legend()
plt.xlabel('X')
plt.ylabel('Probabilidad acumulada P(X)')
De = np.max(De)
print 'El valor del estadistico de Kolmogorv Smirnov para este generador es: ', De
def main():
kolmogorov()
main()
In [ ]: