Pregunta 2:

Sea Xn = ( aX{n-1} + b ) mod m un generador congruencial con los siguientes parámetros: m=91, a=11, b=4 y X_0=17, generar los primeros 100 valores X_i y U_i. ¿Cúal es la longitud de la secuencia?

Respuesta: a. 12


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()


Used parameters: 
a = 11 b = 4 m = 91
Random numbers generated: 
Xi:  [9.0, 12.0, 45.0, 44.0, 33.0, 3.0, 37.0, 47.0, 66.0, 2.0, 26.0, 17.0, 9.0, 12.0, 45.0, 44.0, 33.0, 3.0, 37.0, 47.0, 66.0, 2.0, 26.0, 17.0, 9.0, 12.0, 45.0, 44.0, 33.0, 3.0, 37.0, 47.0, 66.0, 2.0, 26.0, 17.0, 9.0, 12.0, 45.0, 44.0, 33.0, 3.0, 37.0, 47.0, 66.0, 2.0, 26.0, 17.0, 9.0, 12.0, 45.0, 44.0, 33.0, 3.0, 37.0, 47.0, 66.0, 2.0, 26.0, 17.0, 9.0, 12.0, 45.0, 44.0, 33.0, 3.0, 37.0, 47.0, 66.0, 2.0, 26.0, 17.0, 9.0, 12.0, 45.0, 44.0, 33.0, 3.0, 37.0, 47.0, 66.0, 2.0, 26.0, 17.0, 9.0, 12.0, 45.0, 44.0, 33.0, 3.0, 37.0, 47.0, 66.0, 2.0, 26.0, 17.0, 9.0, 12.0, 45.0, 44.0]

Ui:  [0.0989010989010989, 0.13186813186813187, 0.4945054945054945, 0.4835164835164835, 0.3626373626373626, 0.03296703296703297, 0.4065934065934066, 0.5164835164835165, 0.7252747252747253, 0.02197802197802198, 0.2857142857142857, 0.18681318681318682, 0.0989010989010989, 0.13186813186813187, 0.4945054945054945, 0.4835164835164835, 0.3626373626373626, 0.03296703296703297, 0.4065934065934066, 0.5164835164835165, 0.7252747252747253, 0.02197802197802198, 0.2857142857142857, 0.18681318681318682, 0.0989010989010989, 0.13186813186813187, 0.4945054945054945, 0.4835164835164835, 0.3626373626373626, 0.03296703296703297, 0.4065934065934066, 0.5164835164835165, 0.7252747252747253, 0.02197802197802198, 0.2857142857142857, 0.18681318681318682, 0.0989010989010989, 0.13186813186813187, 0.4945054945054945, 0.4835164835164835, 0.3626373626373626, 0.03296703296703297, 0.4065934065934066, 0.5164835164835165, 0.7252747252747253, 0.02197802197802198, 0.2857142857142857, 0.18681318681318682, 0.0989010989010989, 0.13186813186813187, 0.4945054945054945, 0.4835164835164835, 0.3626373626373626, 0.03296703296703297, 0.4065934065934066, 0.5164835164835165, 0.7252747252747253, 0.02197802197802198, 0.2857142857142857, 0.18681318681318682, 0.0989010989010989, 0.13186813186813187, 0.4945054945054945, 0.4835164835164835, 0.3626373626373626, 0.03296703296703297, 0.4065934065934066, 0.5164835164835165, 0.7252747252747253, 0.02197802197802198, 0.2857142857142857, 0.18681318681318682, 0.0989010989010989, 0.13186813186813187, 0.4945054945054945, 0.4835164835164835, 0.3626373626373626, 0.03296703296703297, 0.4065934065934066, 0.5164835164835165, 0.7252747252747253, 0.02197802197802198, 0.2857142857142857, 0.18681318681318682, 0.0989010989010989, 0.13186813186813187, 0.4945054945054945, 0.4835164835164835, 0.3626373626373626, 0.03296703296703297, 0.4065934065934066, 0.5164835164835165, 0.7252747252747253, 0.02197802197802198, 0.2857142857142857, 0.18681318681318682, 0.0989010989010989, 0.13186813186813187, 0.4945054945054945, 0.4835164835164835]

LONGITUD DE LA SECUENCIA:  12

Pregunta 3:

Sea Xn = ( aX{n-1} + b ) mod m un generador congruencial con los siguientes parámetros: m=271, a=139, b=17 y X_0=9, generar los primeros 100 valores X_i y U_i. ¿Cúales son los primeros cinco numeros aleatorios U_i?

Respuesta: a. 0.6790, 0.4391, 0.0996, 0.9114, 0.7528


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()


Used parameters: 
a = 139 b = 17 m = 271
Random numbers generated: 
Xi:  [184.0, 119.0, 27.0, 247.0, 204.0, 189.0, 1.0, 156.0, 21.0, 226.0, 266.0, 135.0, 83.0, 172.0, 77.0, 151.0, 139.0, 97.0, 221.0, 113.0, 6.0, 38.0, 150.0, 0.0, 17.0, 212.0, 217.0, 99.0, 228.0, 2.0, 24.0, 101.0, 235.0, 162.0, 42.0, 164.0, 49.0, 53.0, 67.0, 116.0, 152.0, 7.0, 177.0, 230.0, 9.0, 184.0, 119.0, 27.0, 247.0, 204.0, 189.0, 1.0, 156.0, 21.0, 226.0, 266.0, 135.0, 83.0, 172.0, 77.0, 151.0, 139.0, 97.0, 221.0, 113.0, 6.0, 38.0, 150.0, 0.0, 17.0, 212.0, 217.0, 99.0, 228.0, 2.0, 24.0, 101.0, 235.0, 162.0, 42.0, 164.0, 49.0, 53.0, 67.0, 116.0, 152.0, 7.0, 177.0, 230.0, 9.0, 184.0, 119.0, 27.0, 247.0, 204.0, 189.0, 1.0, 156.0, 21.0, 226.0]

Ui:  [0.6789667896678967, 0.43911439114391143, 0.0996309963099631, 0.9114391143911439, 0.7527675276752768, 0.6974169741697417, 0.0036900369003690036, 0.5756457564575646, 0.07749077490774908, 0.8339483394833949, 0.981549815498155, 0.4981549815498155, 0.3062730627306273, 0.6346863468634686, 0.28413284132841327, 0.5571955719557196, 0.5129151291512916, 0.35793357933579334, 0.8154981549815498, 0.41697416974169743, 0.02214022140221402, 0.14022140221402213, 0.5535055350553506, 0.0, 0.06273062730627306, 0.7822878228782287, 0.8007380073800738, 0.36531365313653136, 0.8413284132841329, 0.007380073800738007, 0.08856088560885608, 0.3726937269372694, 0.8671586715867159, 0.5977859778597786, 0.15498154981549817, 0.6051660516605166, 0.18081180811808117, 0.19557195571955718, 0.24723247232472326, 0.4280442804428044, 0.5608856088560885, 0.025830258302583026, 0.6531365313653137, 0.8487084870848709, 0.033210332103321034, 0.6789667896678967, 0.43911439114391143, 0.0996309963099631, 0.9114391143911439, 0.7527675276752768, 0.6974169741697417, 0.0036900369003690036, 0.5756457564575646, 0.07749077490774908, 0.8339483394833949, 0.981549815498155, 0.4981549815498155, 0.3062730627306273, 0.6346863468634686, 0.28413284132841327, 0.5571955719557196, 0.5129151291512916, 0.35793357933579334, 0.8154981549815498, 0.41697416974169743, 0.02214022140221402, 0.14022140221402213, 0.5535055350553506, 0.0, 0.06273062730627306, 0.7822878228782287, 0.8007380073800738, 0.36531365313653136, 0.8413284132841329, 0.007380073800738007, 0.08856088560885608, 0.3726937269372694, 0.8671586715867159, 0.5977859778597786, 0.15498154981549817, 0.6051660516605166, 0.18081180811808117, 0.19557195571955718, 0.24723247232472326, 0.4280442804428044, 0.5608856088560885, 0.025830258302583026, 0.6531365313653137, 0.8487084870848709, 0.033210332103321034, 0.6789667896678967, 0.43911439114391143, 0.0996309963099631, 0.9114391143911439, 0.7527675276752768, 0.6974169741697417, 0.0036900369003690036, 0.5756457564575646, 0.07749077490774908, 0.8339483394833949]

Los primeros cinco numeros aleatorios son:  [0.6789667896678967, 0.43911439114391143, 0.0996309963099631, 0.9114391143911439, 0.7527675276752768]

Pregunta 5:

¿Cúal es el valor del estadístico de Chi-cuadrado para una secuencia de 2000 numeros aleatorios y un k=100 de un generador congruencial mixto con a = 16645, b = 13467, m = 317, X_0 = 999?

Respuesta: d. 12330


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()


Used parameters: 
a = 16645 b = 13467 m = 317
Random numbers generated: 
El estadistico de Chi-cuadrado para este generador es:  12330.0

Pregunta 6:

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}?

Respuesta: a. [4, 5, 1, 6, 6, 1, 6, 4]


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()


Generated numbers: 
[4, 5, 1, 6, 6, 1, 6, 4]

Pregunta 9:

¿Cúal es el valor del estadístico de Kolmogorov–Smirnov para una secuencia de 2000 numeros aleatorios de un generador congruencial mixto con a = 16645, b = 13467, m = 317, X_0 = 999?

Respuesta: e. 0.22878622683


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()


Used parameters: 
a = 16645 b = 13467 m = 317
Random numbers generated: 
El valor del estadistico  de Kolmogorv Smirnov para este generador es:  0.22878622683

In [ ]: