document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. from Gnuplot import Gnuplot
  2. from random import uniform
  3. from Gnuplot import PlotItems
  4.  
  5. class PruebaSeries:
  6.  
  7.     def __init__(self, lista, n):
  8.         self.lista = lista
  9.         self.parejas = self.listaAParejas(lista)
  10.         self.n = float(n)
  11.         return
  12.  
  13.     def graficar(self, serie=\'Parejas\'):
  14.         gp = Gnuplot(persist=1)
  15.         gp(\'set title "Prueba de Series (Aleatoriedad)"\')
  16.         plot = PlotItems.Data(self.parejas, with_="dots", title=serie)
  17.         gp.plot(plot)
  18.         return
  19.    
  20.     def listaAParejas(self,lista):
  21.         parejas = []
  22.         for i in range(len(lista)-1):
  23.             parejas.append([lista[i], lista[i+1]])
  24.         return parejas
  25.  
  26.     def frecuenciaEsperada(self):
  27.         return((len(self.lista)-1)/(self.n**2))
  28.  
  29.     def rangos(self):
  30.         intervalos = 1/self.n
  31.         lista = [0]
  32.         for i in range(1,int(self.n)+1):
  33.             lista.append(lista[i-1]+intervalos)
  34.         return lista
  35.  
  36.     def cantidadEnRangos(self):
  37.         rangos = self.rangos()
  38.         conteo = {}
  39.         for x in range(len(rangos)):
  40.             for y in range(len(rangos)):
  41.                 key = str(rangos[x-1])+\',\'+str(rangos[y-1])
  42.                 conteo[key] = 0
  43.         for i in self.parejas:
  44.             for x in range(len(rangos)-1):
  45.                 for y in range(len(rangos)-1):
  46.                     if(i[0] > rangos[x] and i[0] <= rangos[x+1] and i[1] > rangos[y] and i[1] <= rangos[y+1]):
  47.                         key = str(rangos[x])+\',\'+str(rangos[y])
  48.                         conteo[key] += 1
  49.         for i in rangos:   # par contabilizar correctamente los intervalos se usan valores de 0 a 1 donde as posiciones de 1 se eliminan para no afectar al calculo
  50.             conteo.pop(\'1.0,\'+str(i), 0) # se elimina todas las combinaciones de 1 con otro valor
  51.             conteo.pop(str(i)+\',1.0\', 0)
  52.         return conteo
  53.  
  54.     def estadisticoCalculado(self):
  55.         cantidades = self.cantidadEnRangos()
  56.         FE = self.frecuenciaEsperada()
  57.         Xo2 = (self.n**2/float(len(self.lista)-1.0))
  58.         sumatoria = 0.0
  59.         print cantidades
  60.         for i in cantidades.values():
  61.                 print (i-FE)**2
  62.                 sumatoria += (i - FE)**2
  63.         print sumatoria
  64.         return (Xo2*sumatoria)
  65.  
  66. if __name__=="__main__":
  67.     lista = [0.25163, 0.65251, 0.36815, 0.64397, 0.04515, 0.83751, 0.14387, 0.51321, 0.72472, 0.05466]
  68.     series = PruebaSeries(lista, 2)
  69.     print series.estadisticoCalculado()
  70.     series.graficar()
');