METODO DE LA INVERSIÓN

Distribuciones continuas

Distribución Cauchy

Funcion de Probabilidad

$$F(x)=\frac{1} {\pi}arctan(\frac{x-xo}{\gamma})+\frac{1}{2}$$

Funcion Cuantil de la Distribucion Cauchy

$$F(x)=xo+\gamma tan[\pi (p-\frac{1}{2})]$$

In [4]:
def funcc(x,xo,g):
    pr=[]
    pi=[]
    pr=gen(x)
   
    for i in range(x):
        pi.append(xo+g*math.tan(math.pi*(pr[i]-(1/2))))
    return pi

fcc=funcc(x,0,1)

for i in range(len(fcc)):
    print "{0:.2f}".format(fcc[i])


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-16729e43ddf7> in <module>()
      9     return pi
     10 
---> 11 fcc=funcc(3,0,1)
     12 
     13 for i in range(len(fcc)):

<ipython-input-4-16729e43ddf7> in funcc(x, xo, g)
      3     pr=[]
      4     pi=[]
----> 5     pr=gen(x)
      6 
      7     for i in range(x):

NameError: global name 'gen' is not defined

Distribucion Exponencial

Funcion de Probabilidad

$$F(x)=\lambda e^{- \lambda x}$$

Funcion cuantil de la Distribucion exponencial

$$F^{-1}(x)=\frac{-ln(1-p)}{\lambda}$$

In [5]:
lambda_=1

def funexp(l,x):
    lmda=[]
    for i in range(x):
        lmda.append(l*math.exp(-l*i))
    return lmda
fprobe=funexp(lambda_,x)

def funcexp(x,l):
    p=[]
    pi=[]
    p=gen(x)
    
    for i in range(x):
        pi.append(-math.log10(1-p[i])/l)
    return pi

fcue=funcuanexp(x,lambda_)
for i in range(len(fcue)):
    print "{0:.2f}".format(fcue[i])


  File "<ipython-input-5-13a2f3fde2ea>", line 1
    lambda=1
          ^
SyntaxError: invalid syntax

Distribuciones Discretas

Distribucion Binomial

Funcion de Probabilidad

$$F(x)=\frac{n!}{x!(n-x)!}p^x(1-p)^{n-x}$$

In [7]:
%matplotlib inline 

import matplotlib.pyplot as plt
from scipy.integrate import quad
import math
import numpy as np
import scipy as sp



x=10
s1=17
s2=27
def gen(x):
    x0=x*10
    x0=((75*x0))%((2**16)+1)
    Ux=float(x0)/((2**16)+1)
    return Ux

def gena(N):
    Ux=[]
    x=0
    x0=7
    while x<N:
        x0=((5*x0)+3)%607
        x=x+1
        Ux.append(float(x0)/607)
    return Ux

p=gen(s1)

#funcion de probabilidad binomial
def funpb(x,p):
    y=[]
    for i in range(x):
        y.append((math.factorial(x)/(math.factorial(i)*math.factorial(x-i)))*(p**i)*(1-p)**(x-i))
    return y

fpb=funpb(x,p)
#funcion acumulada binomial
def funab():
    y=[]
    y.append(fpb[0])
    for i in range(x-1):
        y.append(fpb[i+1]+y[i])
    return y

fab=funab()

plt.plot(fab)


Out[7]:
[<matplotlib.lines.Line2D at 0xa8635f8>]

In [8]:
#funcion inversa binomial
def fib(x):
    p=[]
    p=gena(x)
    pi=[]
    for i in range(x):
        for j in range(len(fab)):
            if p[i]<fab[j]:
                pi.append(j)
                break
    return pi      

finb=fib(x)
print finb


[0, 1, 2, 5, 4, 2, 2, 1, 4, 2]

Distribucion Geometrica

Funcion de Probabilidad

$$F(x)=p(1-p)^{x-1}$$

In [11]:
p=gen(s2)
def funpg(x,p):
    y=[]
    for i in range(x+1):
        y.append(p*(1-p)**(i))
    return y

fpg=funpg(x,p)

def funag():
    y=[]
    y.append(fpg[0])
    for i in range(x-1):
        y.append(fpg[i+1]+y[i])
    return y

fag=funag()


plt.plot(fag)


Out[11]:
[<matplotlib.lines.Line2D at 0xac2a0f0>]

Distribucion Uniforme Discreta

Funcion de Probabilidad

$$F(x)=\frac{1}{x}$$

In [12]:
#funcion inversa binomial
def fig(x):
    p=[]
    p=gena(x)
    pi=[]
    for i in range(x):
        for j in range(len(fag)):
            if p[i]<fag[j]:
                pi.append(j)
                break
    return pi      

fing=fig(x)
print fing


[0, 1, 2, 6, 1, 1, 1, 7, 2]

In [13]:
#Funcion de probabilidad Uniforme Discreta
def funpun(x):
    fpu=[]
    for i in range(x):
        fpu.append(1/float(x))
    return fpu

fpu=funpun(x)
   
def funaun(x):
    facu=[]
    facu.append(fpu[0])
    for i in range(x-1):
        facu.append(fpu[i+1]+facu[i])
    return facu

facu=funaun(x)
plt.plot(facu)


Out[13]:
[<matplotlib.lines.Line2D at 0x131a91d0>]

In [14]:
#funcion inversa Uniforme
def fiun(x):
    p=[]
    p=gena(x)
    pi=[]
    for i in range(x):
        for j in range(len(facu)):
            if p[i]<facu[j]:
                pi.append(j)
                break
    return pi      

fing=fiun(x)
print fing


[0, 3, 5, 9, 8, 4, 4, 3, 9, 6]

In [ ]: