Last active
August 29, 2015 14:07
-
-
Save abner-math/e51ffc7453acf0af3426 to your computer and use it in GitHub Desktop.
Statistical software to calculate over some models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*-coding=utf-8 -*- | |
#----------------------------------------------------------------- | |
# Mesquita v1.0 | |
# Created by: Abner Matheus | |
# E-mail: [email protected] | |
# Github: http://github.com/picoledelimao | |
#----------------------------------------------------------------- | |
import math | |
class MatematicaException(Exception): pass | |
class ProbabilidadeException(Exception): pass | |
class BinomialException(ProbabilidadeException): pass | |
class HipergeometricoException(ProbabilidadeException): pass | |
class GeometricoException(ProbabilidadeException): pass | |
class PoissonException(ProbabilidadeException): pass | |
class UniformeException(ProbabilidadeException): pass | |
class ExponencialException(ProbabilidadeException): pass | |
class NormalException(ProbabilidadeException): pass | |
class Matematica: | |
#combina dois numeros inteiros, n > p | |
@staticmethod | |
def combinacao(n, p): | |
if n < p or p < 0 or not(isinstance(n, int) and isinstance(p, int)): | |
raise MatematicaException("Parâmetros inválidos na combinação") | |
n_fatorado = reduce(lambda x, y: x * y, [x for x in range(n, n-p, -1)] + [1]) | |
try: | |
return float(n_fatorado)/math.factorial(p) | |
except: return n_fatorado/math.factorial(p) | |
#---------------------------------------------------------------- | |
# Modelos discretos | |
#---------------------------------------------------------------- | |
class ModeloDiscreto: | |
#variacao do 'x' na reta real | |
def variacao(self, x): | |
raise NotImplementedError() | |
#funcao de probabilidade | |
def f(self): | |
raise NotImplementedError() | |
#funcao de probabilidade acumulada | |
def F(self): | |
raise NotImplementedError() | |
#esperanca da v.a. | |
def e(self): | |
raise NotImplementedError() | |
#variancia da v.a. | |
def var(self): | |
raise NotImplementedError() | |
#================================================================= | |
class Bernoulli(ModeloDiscreto): | |
#p: chance de sucesso | |
def __init__(self, p): | |
try: | |
self.p = float(p) | |
if not 0 <= self.p <= 1: | |
raise ProbabilidadeException("Parametro p não é uma probabilidade") | |
except: | |
raise ProbabilidadeException("Parametro p não é uma probabilidade") | |
def fp(self): | |
return self.p | |
def e(self): | |
return self.p | |
def var(self): | |
return self.p*(1-self.p) | |
class Binomial(Bernoulli): | |
#n: numero de repeticoes, p: chance de sucesso | |
def __init__(self, n, p): | |
try: | |
self.n = int(n) | |
except: | |
raise BinomialException("Parâmetro n deve ser um numero inteiro") | |
Bernoulli.__init__(self, p) | |
def variacao(self): | |
return [x for x in range(0, self.n + 1)] | |
def fp(self, x): | |
if not x in self.variacao(): | |
raise BinomialException("x está fora da variação da v.a.") | |
combinacao = Matematica.combinacao(self.n, x) | |
chance_sucesso = math.pow(self.p, x) | |
chance_fracasso = math.pow(1 - self.p, self.n - x) | |
return combinacao*chance_sucesso*chance_fracasso | |
def F(self, x): | |
if x < 0: | |
return 0 | |
elif x >= self.n: | |
return 1 | |
count = 0 | |
for p in range(0, int(x)+1): | |
count += self.fp(p) | |
return count | |
def e(self): | |
return self.n*self.p | |
def var(self): | |
return self.n*self.p*(1-self.p) | |
class Hipergeometrico(ModeloDiscreto): | |
#N: tamanho da populacao | |
#n: tamanho da amostra | |
#r: numero de elementos favoraveis | |
def __init__(self, N, r, n): | |
try: | |
self.N = int(N) | |
self.r = int(r) | |
self.n = int(n) | |
if self.N < self.n or self.N < self.r: | |
raise HipergeometricoException("Parâmetros inválidos") | |
except: | |
raise HipergeometricoException("Parâmetros inválidos") | |
def variacao(self): | |
return [x for x in range(max(0, self.n-(self.N-self.r)), min(self.r, self.n) + 1)] | |
def fp(self, x): | |
if not x in self.variacao(): | |
raise HipergeometricoException("x está fora da variação da v.a.") | |
a = Matematica.combinacao(self.r, x) | |
b = Matematica.combinacao(self.N - self.r, self.n - x) | |
c = Matematica.combinacao(self.N, self.n) | |
return a*b/c | |
def F(self, x): | |
if x < max(0, self.n-(self.N-self.r)): | |
return 0 | |
elif x >= min(self.r, self.n): | |
return 1 | |
count = 0 | |
for p in range(max(0, self.n-(self.N-self.r)), int(x)+1): | |
count += self.fp(p) | |
return count | |
def e(self): | |
return self.n*(self.r/float(self.N)) | |
def var(self): | |
return self.n*(self.r/float(self.N))*(1-self.r/float(self.N))*(self.N-self.n)/float(self.N-1) | |
class Geometrico(Bernoulli): | |
#p: chance de sucesso | |
def __init__(self, p): | |
Bernoulli.__init__(self, p) | |
def variacao(self, x): | |
return isinstance(x, int) and x >= 1 | |
def fp(self, x): | |
if not self.variacao(x): | |
raise GeometricoException("x está fora da variação da v.a.") | |
return math.pow(1-self.p, x-1)*self.p | |
def F(self, x): | |
if x <= 1: | |
return 0 | |
count = 0 | |
for p in range(1, int(x)+1): | |
count += self.fp(p) | |
return count | |
def e(self): | |
return 1/float(self.p) | |
def var(self): | |
return (1-self.p)/float(math.pow(self.p, 2)) | |
class Poisson(ModeloDiscreto): | |
#alfa: media | |
def __init__(self, alfa): | |
try: | |
self.alfa = float(alfa) | |
except: | |
raise PoissonException("Parâmetro inválido") | |
def variacao(self, x): | |
return isinstance(x, int) and x >= 0 | |
def fp(self, x): | |
if not self.variacao(x): | |
raise PoissonException("x está fora da variação da v.a.") | |
return math.pow(math.e, -self.alfa)*math.pow(self.alfa, x)/float(math.factorial(x)) | |
def F(self, x): | |
if x <= 0: | |
return 0 | |
count = 0 | |
for p in range(0, int(x)+1): | |
count += self.fp(p) | |
return count | |
def e(self): | |
return self.alfa | |
def var(self): | |
return self.alfa | |
#---------------------------------------------------------------- | |
# Modelos continuos | |
#---------------------------------------------------------------- | |
class ModeloContinuo: | |
#funcao distribuicao de probabilidade entre dois pontos | |
def fdp(self, x, y): | |
raise NotImplementedError() | |
#funcao de probabilidade acumulada de -infinito ate o ponto | |
def F(self, x): | |
raise NotImplementedError() | |
#esperanca da v.a. | |
def e(self): | |
raise NotImplementedError() | |
#variancia da v.a. | |
def var(self): | |
raise NotImplementedError() | |
#================================================================= | |
class Uniforme(ModeloContinuo): | |
def __init__(self, a, b): | |
try: | |
self.a = float(a) | |
self.b = float(b) | |
if self.a >= self.b: | |
raise UniformeException("Parametro a é maior ou igual que o b") | |
except: raise UniformeException("Parâmetros inválidos") | |
def F(self, x): | |
if x <= self.a: | |
return 0 | |
elif x >= self.b: | |
return 1 | |
return (x-self.a)/float(self.b-self.a) | |
def fdp(self, x, y): | |
if y < x: | |
raise UniformeException("Ponto final é menor que ponto inicial") | |
return self.F(y) - self.F(x) | |
def e(self): | |
return (self.a + self.b)/2.0 | |
def var(self): | |
return math.pow(self.b - self.a, 2)/12.0 | |
class Exponencial(ModeloContinuo): | |
#alfa: o inverso da media | |
def __init__(self, alfa): | |
try: | |
self.alfa = float(alfa) | |
except: raise ExponencialException("Parâmetro inválido") | |
if self.alfa < 0: | |
raise ExponencialException("Parâmetro deve ser maior que 0") | |
def F(self, x): | |
if x <= 0: | |
return 0 | |
return 1 - math.pow(math.e, -self.alfa*x) | |
def fdp(self, x, y): | |
if y < x: | |
raise ExponencialException("Ponto final é menor que o ponto inicial") | |
return self.F(y) - self.F(x) | |
def e(self): | |
return 1/float(self.alfa) | |
def var(self): | |
return 1/float(math.pow(self.alfa, 2)) | |
class Normal(ModeloContinuo): | |
#u: media | |
#o2: variancia | |
def __init__(self, u, o2): | |
try: | |
self.u = float(u) | |
self.o2 = float(o2) | |
except: | |
raise NormalException("Parâmetros inválidos") | |
#tabela abaixo | |
self.tabela = {0.0: 0.5, 0.25: 0.5987, 2.0: 0.9772, 3.0: 0.9987, 0.5: 0.6915, 1.0: 0.8413, 3.37: 0.9996, 0.75: 0.7734, 2.83: 0.9977, 1.17: 0.879, 0.67: 0.7486, 2.28: 0.9887, 3.29: 0.9995, 3.21: 0.9993, 0.83: 0.7967, 0.6: 0.7257, 2.91: 0.9982, 3.5: 0.9998, 1.34: 0.9099, 2.25: 0.9878, 0.85: 0.8023, 3.51: 0.9998, 0.96: 0.8315, 0.66: 0.7454, 3.27: 0.9995, 0.78: 0.7823, 3.45: 0.9997, 1.67: 0.9525, 1.43: 0.9236, 2.65: 0.996, 2.03: 0.9788, 3.54: 0.9998, 2.36: 0.9909, 3.46: 0.9997, 0.14: 0.5557, 0.37: 0.6443, 1.2: 0.8849, 0.4: 0.6554, 2.62: 0.9956, 3.55: 0.9998, 2.04: 0.9793, 1.29: 0.9015, 0.46: 0.6772, 3.49: 0.9998, 0.56: 0.7123, 1.73: 0.9582, 1.51: 0.9345, 0.36: 0.6406, 3.41: 0.9997, 0.22: 0.5871, 1.09: 0.8621, 0.95: 0.8289, 0.26: 0.6026, 1.61: 0.9463, 3.24: 0.9994, 1.84: 0.9671, 2.06: 0.9803, 0.63: 0.7357, 1.81: 0.9649, 3.3: 0.9995, 1.54: 0.9382, 2.02: 0.9783, 1.95: 0.9744, 2.57: 0.9949, 2.4: 0.9918, 2.73: 0.9968, 1.9: 0.9713, 2.31: 0.9896, 0.42: 0.6628, 3.58: 0.9998, 2.52: 0.9941, 2.76: 0.9971, 2.84: 0.9977, 2.64: 0.9959, 2.17: 0.985, 2.97: 0.9985, 3.23: 0.9994, 1.94: 0.9738, 1.88: 0.9699, 3.33: 0.9996, 2.75: 0.997, 1.68: 0.9535, 2.43: 0.9925, 1.33: 0.9082, 1.85: 0.9678, 2.35: 0.9906, 2.27: 0.9884, 2.19: 0.9857, 1.77: 0.9616, 0.31: 0.6217, 1.96: 0.975, 3.14: 0.9992, 2.51: 0.994, 2.22: 0.9868, 0.73: 0.7673, 1.04: 0.8508, 1.49: 0.9319, 1.18: 0.881, 1.58: 0.9429, 1.01: 0.8438, 1.62: 0.9474, 0.1: 0.5398, 2.6: 0.9953, 1.72: 0.9573, 2.15: 0.9842, 1.76: 0.9608, 0.47: 0.6808, 1.05: 0.8531, 2.92: 0.9982, 1.1: 0.8643, 1.47: 0.9292, 3.13: 0.9991, 3.28: 0.9995, 0.09: 0.5359, 3.31: 0.9995, 0.76: 0.7764, 2.47: 0.9932, 1.63: 0.9484, 0.15: 0.5596, 3.07: 0.9989, 1.37: 0.9147, 0.7: 0.758, 2.85: 0.9978, 2.54: 0.9945, 1.23: 0.8907, 2.32: 0.9898, 0.97: 0.834, 0.74: 0.7704, 0.51: 0.695, 0.64: 0.7389, 2.55: 0.9946, 0.45: 0.6736, 2.78: 0.9973, 0.54: 0.7054, 1.44: 0.9251, 0.55: 0.7088, 0.01: 0.504, 3.39: 0.9997, 2.89: 0.9981, 2.81: 0.9975, 1.14: 0.8729, 1.8: 0.9641, 3.18: 0.9993, 1.22: 0.8888, 1.86: 0.9686, 3.16: 0.9992, 1.99: 0.9767, 3.02: 0.9987, 1.98: 0.9761, 3.26: 0.9994, 0.04: 0.516, 3.08: 0.999, 2.07: 0.9808, 0.34: 0.6331, 0.61: 0.7291, 3.11: 0.9991, 2.14: 0.9838, 3.03: 0.9988, 2.41: 0.992, 0.16: 0.5636, 0.41: 0.6591, 0.48: 0.6844, 1.36: 0.9131, 0.98: 0.8365, 3.19: 0.9993, 3.06: 0.9989, 3.43: 0.9997, 2.98: 0.9986, 3.35: 0.9996, 2.9: 0.9981, 1.5: 0.9332, 0.89: 0.8133, 1.64: 0.9495, 0.24: 0.5948, 0.52: 0.6985, 2.05: 0.9798, 1.59: 0.9441, 1.25: 0.8944, 0.3: 0.6179, 2.56: 0.9948, 2.63: 0.9957, 0.21: 0.5832, 2.71: 0.9966, 0.23: 0.591, 2.79: 0.9974, 2.12: 0.983, 0.71: 0.7611, 0.19: 0.5753, 1.87: 0.9693, 2.45: 0.9929, 1.45: 0.9265, 3.52: 0.9998, 1.89: 0.9706, 3.44: 0.9997, 3.36: 0.9996, 3.22: 0.9994, 0.07: 0.5279, 1.06: 0.8554, 3.53: 0.9998, 2.59: 0.9952, 1.53: 0.937, 3.47: 0.9997, 0.58: 0.719, 2.7: 0.9965, 3.56: 0.9998, 2.94: 0.9984, 0.28: 0.6103, 0.99: 0.8389, 1.41: 0.9207, 3.48: 0.9997, 1.7: 0.9554, 0.38: 0.648, 1.74: 0.9591, 1.78: 0.9625, 0.11: 0.5438, 1.38: 0.9162, 2.38: 0.9913, 1.92: 0.9726, 0.92: 0.8212, 1.82: 0.9656, 1.71: 0.9564, 0.35: 0.6368, 1.11: 0.8665, 0.13: 0.5517, 0.94: 0.8264, 1.12: 0.8686, 2.46: 0.9931, 1.55: 0.9394, 1.75: 0.9599, 0.68: 0.7517, 1.97: 0.9756, 0.44: 0.67, 2.68: 0.9963, 1.15: 0.8749, 2.09: 0.9817, 0.77: 0.7794, 1.28: 0.8997, 2.01: 0.9778, 0.53: 0.7019, 3.4: 0.9997, 3.57: 0.9998, 1.26: 0.8962, 2.3: 0.9893, 2.33: 0.9901, 1.31: 0.9049, 2.39: 0.9916, 0.82: 0.7939, 2.99: 0.9986, 0.84: 0.7995, 0.79: 0.7852, 0.81: 0.791, 0.59: 0.7224, 0.33: 0.6293, 2.26: 0.9881, 2.42: 0.9922, 0.86: 0.8051, 1.79: 0.9633, 1.03: 0.8485, 0.88: 0.8106, 2.34: 0.9904, 1.66: 0.9515, 0.05: 0.5199, 2.74: 0.9969, 2.21: 0.9864, 1.93: 0.9732, 2.61: 0.9955, 1.57: 0.9418, 2.2: 0.9861, 0.06: 0.5239, 3.17: 0.9992, 0.49: 0.6879, 0.2: 0.5793, 2.29: 0.989, 0.62: 0.7324, 1.13: 0.8708, 2.37: 0.9911, 2.13: 0.9834, 2.5: 0.9938, 3.2: 0.9993, 0.8: 0.7881, 1.3: 0.9032, 0.29: 0.6141, 2.48: 0.9934, 2.24: 0.9875, 2.44: 0.9927, 1.19: 0.883, 0.69: 0.7549, 0.87: 0.8078, 2.08: 0.9812, 2.82: 0.9976, 1.21: 0.8869, 2.95: 0.9984, 1.39: 0.9177, 1.91: 0.9719, 3.04: 0.9988, 3.15: 0.9992, 0.18: 0.5714, 1.52: 0.9357, 0.39: 0.6517, 0.93: 0.8238, 2.53: 0.9943, 1.24: 0.8925, 2.11: 0.9826, 2.16: 0.9846, 2.96: 0.9985, 2.49: 0.9936, 0.91: 0.8186, 2.67: 0.9962, 0.27: 0.6064, 0.72: 0.7642, 2.69: 0.9964, 2.18: 0.9854, 2.77: 0.9972, 2.87: 0.9979, 1.65: 0.9505, 2.1: 0.9821, 2.93: 0.9983, 0.65: 0.7422, 0.9: 0.8159, 1.07: 0.8577, 1.08: 0.8599, 1.32: 0.9066, 3.38: 0.9996, 3.09: 0.999, 2.8: 0.9974, 0.02: 0.508, 3.01: 0.9987, 2.23: 0.9871, 0.17: 0.5675, 0.43: 0.6664, 3.1: 0.999, 1.27: 0.898, 1.4: 0.9192, 2.66: 0.9961, 1.16: 0.877, 3.12: 0.9991, 2.58: 0.9951, 1.46: 0.9279, 0.08: 0.5319, 0.12: 0.5478, 1.69: 0.9545, 1.48: 0.9306, 1.42: 0.9222, 1.02: 0.8461, 1.56: 0.9406, 1.83: 0.9664, 3.42: 0.9997, 2.88: 0.998, 0.03: 0.512, 3.32: 0.9995, 3.25: 0.9994, 3.34: 0.9996, 3.05: 0.9989, 1.6: 0.9452, 2.86: 0.9979, 0.32: 0.6255, 1.35: 0.9115, 0.57: 0.7157, 2.72: 0.9967} | |
def F(self, x): | |
xNormalizada = (x - self.u)/float(math.sqrt(self.o2)) | |
if xNormalizada < -3.5: | |
return 0 | |
elif xNormalizada > 3.5: | |
return 1 | |
chave = float("%.2f" % math.fabs(xNormalizada)) | |
if xNormalizada < 0: | |
return 1 - self.tabela[chave] | |
else: | |
return self.tabela[chave] | |
def fdp(self, x, y): | |
if y < x: | |
raise NormalException("Ponto final é menor que o ponto inicial") | |
return self.F(y) - self.F(x) | |
def e(self): | |
return self.u | |
def var(self): | |
return self.o2 | |
#---------------------------------------------------------------- | |
# Interface gráfica | |
#---------------------------------------------------------------- | |
if __name__ == '__main__': | |
from Tkinter import * | |
import tkMessageBox | |
class JanelaPrincipal: | |
def __init__(self, top_level): | |
self.frame1 = Frame(top_level) | |
self.frame1.pack(side=LEFT) | |
self.frame2 = Frame(top_level) | |
self.frame2.pack(side=RIGHT) | |
width = 50 | |
height = 5 | |
Label(self.frame1, text="Modelos discretos:").pack() | |
self.binomial = Button(self.frame1, text="Modelo binomial") | |
self.binomial['width'] = width | |
self.binomial['height'] = height | |
self.hipergeometrico = Button(self.frame1, text="Modelo hipergeométrico") | |
self.hipergeometrico['width'] = width | |
self.hipergeometrico['height'] = height | |
self.geometrico = Button(self.frame1, text="Modelo geométrico") | |
self.geometrico['width'] = width | |
self.geometrico['height'] = height | |
self.poisson = Button(self.frame1, text="Modelo de Poisson") | |
self.poisson['width'] = width | |
self.poisson['height'] = height | |
Label(self.frame2, text="Modelos contínuos:").pack() | |
self.uniforme = Button(self.frame2, text="Modelo uniforme") | |
self.uniforme['width'] = width | |
self.uniforme['height'] = height | |
self.exponencial = Button(self.frame2, text="Modelo exponencial") | |
self.exponencial['width'] = width | |
self.exponencial['height'] = height | |
self.normal = Button(self.frame2, text="Modelo normal") | |
self.normal['width'] = width | |
self.normal['height'] = height | |
self.binomial.pack() | |
self.hipergeometrico.pack() | |
self.geometrico.pack() | |
self.poisson.pack() | |
self.uniforme.pack() | |
self.exponencial.pack() | |
self.normal.pack() | |
self.binomial.bind("<Button-1>", self.clica_botao) | |
self.hipergeometrico.bind("<Button-1>", self.clica_botao) | |
self.geometrico.bind("<Button-1>", self.clica_botao) | |
self.poisson.bind("<Button-1>", self.clica_botao) | |
self.uniforme.bind("<Button-1>", self.clica_botao) | |
self.exponencial.bind("<Button-1>", self.clica_botao) | |
self.normal.bind("<Button-1>", self.clica_botao) | |
def clica_botao(self, event): | |
janela = Tk() | |
if event.widget == self.binomial: | |
janela.title("Modelo binomial") | |
JanelaBinomial(janela) | |
elif event.widget == self.hipergeometrico: | |
janela.title("Modelo hipergeométrico") | |
JanelaHipergeometrico(janela) | |
elif event.widget == self.geometrico: | |
janela.title("Modelo geométrico") | |
JanelaGeometrico(janela) | |
elif event.widget == self.poisson: | |
janela.title("Modelo de Poisson") | |
JanelaPoisson(janela) | |
elif event.widget == self.uniforme: | |
janela.title("Modelo uniforme") | |
JanelaUniforme(janela) | |
elif event.widget == self.exponencial: | |
janela.title("Modelo exponencial") | |
JanelaExponencial(janela) | |
elif event.widget == self.normal: | |
janela.title("Modelo normal") | |
JanelaNormal(janela) | |
janela.mainloop() | |
class Grafico: | |
def __init__(self, raiz): | |
self.width = 400 | |
self.height = 400 | |
self.canvas = Canvas(raiz, width=self.width, height=self.height) | |
self.canvas.pack() | |
def set_modelo(self, modelo_discreto, funcao, limiteX1, limiteX2, acumulada): | |
self.modelo = modelo_discreto | |
self.funcao = funcao | |
self.desenha(limiteX1, limiteX2, acumulada) | |
class GraficoDiscreto(Grafico): | |
def desenha(self, limiteX1, limiteX2, acumulada): | |
self.canvas.delete("all") | |
try: | |
variacao = self.modelo.variacao() | |
except: | |
if isinstance(self.modelo, Geometrico): | |
i = 1 | |
while(self.modelo.F(i) < 0.95): | |
i += 1 | |
if i > 100: | |
break | |
variacao = [x for x in range(1, i + 1)] | |
elif isinstance(self.modelo, Poisson): | |
i = 0 | |
while(self.modelo.F(i) < 0.95): | |
i += 1 | |
if i > 100: | |
break | |
variacao = [x for x in range(0, i + 1)] | |
variacao.insert(0, variacao[0] - 1) | |
variacao.insert(0, variacao[0] - 2) | |
variacao.append(variacao[-1] + 1) | |
variacao.append(variacao[-1] + 2) | |
#retas x e y, respectivamente | |
reta_y_posicao_x = self.width/float(len(variacao)) * (math.fabs(0 - variacao[0])) + 20 | |
self.canvas.create_line(0, self.height - 20, self.width, self.height - 20) | |
if 0 in variacao: | |
self.canvas.create_line(reta_y_posicao_x, 0, reta_y_posicao_x, self.height) | |
else: | |
reta_y_posicao_x = 20 | |
#desenha os pontos | |
for ponto in variacao: | |
try: | |
probabilidade = self.funcao(ponto) | |
x = self.width/float(len(variacao)) * (math.fabs(ponto - variacao[0])) + 20 | |
y = (self.height - 40) * probabilidade | |
if limiteX1 != None and limiteX2 != None: | |
if limiteX1 <= ponto <= limiteX2: | |
self.canvas.create_oval(x - 2, self.height - y - 22, x + 2, self.height - y - 18, fill='red') | |
self.canvas.create_line(x, self.height - y - 20, x, self.height - 20, fill='red') | |
else: | |
self.canvas.create_oval(x - 2, self.height - y - 22, x + 2, self.height - y - 18, fill='black') | |
self.canvas.create_line(x, self.height - y - 20, x, self.height - 20, fill='black') | |
else: | |
self.canvas.create_oval(x - 2, self.height - y - 22, x + 2, self.height - y - 18, fill='black') | |
self.canvas.create_line(x, self.height - y - 20, x, self.height - 20, fill='black') | |
except: pass | |
limite = 11 | |
if limite > len(variacao): | |
limite = len(variacao) | |
#desenha metricas na reta x | |
for i in range(1, limite): | |
index = i * (len(variacao) / float(limite)) | |
if index != int(index): | |
index += 1 | |
index = int(index) | |
x = self.width/float(len(variacao)) * (math.fabs(variacao[index] - variacao[0])) + 20 | |
self.canvas.create_text(x, self.height - 10, text=str(variacao[index])) | |
#desenha metricas probabilidade | |
for i in range(1, 11): | |
y = self.height * i/11.0 | |
self.canvas.create_text(reta_y_posicao_x - 10, self.height - y - 20, text=str(0.1 * i)) | |
class JanelaDiscreta: | |
def __init__(self, top_level, *parametros): | |
self.top_level = top_level | |
self.frame_left = Frame(top_level) | |
self.frame_left.pack(side=LEFT) | |
self.frame_right = Frame(top_level) | |
self.frame_right.pack(side=RIGHT) | |
self.frame_params = {} | |
self.entries = {} | |
for i in range(len(parametros)): | |
parametro = parametros[i] | |
self.frame_params[parametro] = Frame(self.frame_left) | |
self.frame_params[parametro].pack() | |
Label(self.frame_params[parametro], text=parametro).pack(side=LEFT) | |
self.entries[parametro] = Entry(self.frame_params[parametro]) | |
self.entries[parametro].pack() | |
self.frame_botao_ok = Frame(self.frame_left) | |
self.frame_botao_ok.pack() | |
self.botao_ok = Button(self.frame_botao_ok, text="Gerar") | |
self.botao_ok['width'] = 30 | |
self.botao_ok.pack() | |
self.botao_ok.bind("<Button-1>", self.clica_botao_ok) | |
self.frame_fp = Frame(self.frame_right) | |
self.frame_fp.pack(side=LEFT) | |
self.frame_fda = Frame(self.frame_right) | |
self.frame_fda.pack(side=LEFT) | |
Label(self.frame_fp, text="Função de probabilidade:").pack() | |
self.graficoFd = GraficoDiscreto(self.frame_fp) | |
Label(self.frame_fda, text="Função de distribuição acumulada:").pack() | |
self.graficoF = GraficoDiscreto(self.frame_fda) | |
self.frame_1 = Frame(self.frame_left) | |
self.frame_2 = Frame(self.frame_1) | |
self.frame_2.pack() | |
Label(self.frame_2, text=" P(X = x):").pack(side=LEFT) | |
self.x = Entry(self.frame_2) | |
self.x.pack() | |
self.frame_3 = Frame(self.frame_1) | |
self.frame_3.pack() | |
Label(self.frame_3, text=" F(x):").pack(side=LEFT) | |
self.F = Entry(self.frame_3) | |
self.F.pack() | |
self.frame_4 = Frame(self.frame_1) | |
self.frame_4.pack() | |
self.botao_calcula = Button(self.frame_4, text="Calcular") | |
self.botao_calcula['width'] = 30 | |
self.botao_calcula.pack() | |
self.botao_calcula.bind("<Button-1>", self.clica_botao_calcula) | |
self.frame_5 = Frame(self.frame_1) | |
self.frame_5.pack() | |
self.labelE = Label(self.frame_5, text="E(X): ") | |
self.labelE.pack() | |
self.labelVar = Label(self.frame_5, text="Var(X): ") | |
self.labelVar.pack() | |
self.labelP = Label(self.frame_5) | |
self.labelP.pack() | |
self.labelF = Label(self.frame_5) | |
self.labelF.pack() | |
def clica_botao_ok(self, event): | |
self.graficoFd.set_modelo(self.modelo, self.modelo.fp, None, None, False) | |
self.graficoF.set_modelo(self.modelo, self.modelo.F, None, None, True) | |
self.frame_1.pack() | |
try: | |
self.labelE.setParent(None) | |
self.labelVar.setParent(None) | |
except: pass | |
self.labelE['text'] = "E(X): %.5f" % self.modelo.e() | |
self.labelVar['text'] = "Var(X): %.5f" % self.modelo.var() | |
def clica_botao_calcula(self, event): | |
try: | |
x = int(self.x.get()) | |
try: | |
self.labelP['text'] = "P(X = %d) = %.5f" % (x, self.modelo.fp(x)) | |
self.graficoFd.set_modelo(self.modelo, self.modelo.fp, x, x, False) | |
except Exception, e: | |
tkMessageBox.showwarning("Ops...", e[0], parent=self.top_level) | |
except: | |
if self.x.get().replace(" ","") != "": | |
tkMessageBox.showwarning("Ops...", "x deve ser um número inteiro", parent=self.top_level) | |
self.graficoFd.set_modelo(self.modelo, self.modelo.fp, None, None, False) | |
self.labelP['text'] = "" | |
try: | |
x = float(self.F.get()) | |
self.labelF['text'] = "F(%.2f) = %.5f" % (x, self.modelo.F(x)) | |
self.graficoF.set_modelo(self.modelo, self.modelo.F, 0, x, True) | |
except: | |
self.graficoF.set_modelo(self.modelo, self.modelo.F, None, None, True) | |
self.labelF['text'] = "" | |
class JanelaBinomial(JanelaDiscreta): | |
def __init__(self, top_level): | |
JanelaDiscreta.__init__(self, top_level, "Parâmetro n", "Parâmetro p") | |
def clica_botao_ok(self, event): | |
n = self.entries["Parâmetro n"].get() | |
p = self.entries["Parâmetro p"].get() | |
try: | |
self.modelo = Binomial(n, p) | |
JanelaDiscreta.clica_botao_ok(self, event) | |
except Exception, e: | |
tkMessageBox.showwarning("Ops...", e[0], parent=self.top_level) | |
class JanelaHipergeometrico(JanelaDiscreta): | |
def __init__(self, top_level): | |
JanelaDiscreta.__init__(self, top_level, "Parâmetro N", "Parâmetro r", "Parâmetro n") | |
def clica_botao_ok(self, event): | |
N = self.entries["Parâmetro N"].get() | |
r = self.entries["Parâmetro r"].get() | |
n = self.entries["Parâmetro n"].get() | |
try: | |
self.modelo = Hipergeometrico(N, r, n) | |
JanelaDiscreta.clica_botao_ok(self, event) | |
except Exception, e: | |
tkMessageBox.showwarning("Ops...", e[0], parent=self.top_level) | |
class JanelaGeometrico(JanelaDiscreta): | |
def __init__(self, top_level): | |
JanelaDiscreta.__init__(self, top_level, "Parâmetro p") | |
def clica_botao_ok(self, event): | |
p = self.entries["Parâmetro p"].get() | |
try: | |
self.modelo = Geometrico(p) | |
JanelaDiscreta.clica_botao_ok(self, event) | |
except Exception, e: | |
tkMessageBox.showwarning("Ops...", e[0], parent=self.top_level) | |
class JanelaPoisson(JanelaDiscreta): | |
def __init__(self, top_level): | |
JanelaDiscreta.__init__(self, top_level, "Parâmetro gama") | |
def clica_botao_ok(self, event): | |
alfa = self.entries["Parâmetro gama"].get() | |
try: | |
self.modelo = Poisson(alfa) | |
JanelaDiscreta.clica_botao_ok(self, event) | |
except Exception, e: | |
tkMessageBox.showwarning("Ops...", e[0], parent=self.top_level) | |
class GraficoContinuo(Grafico): | |
def desenha(self, limiteX1, limiteX2, acumulada): | |
self.canvas.delete("all") | |
if isinstance(self.modelo, Uniforme): | |
a = self.modelo.a | |
b = self.modelo.b + 1 | |
elif isinstance(self.modelo, Exponencial): | |
a = -1 | |
b = 0 | |
while self.modelo.F(b) < 0.995: | |
b += 1 | |
if b > 100: | |
break | |
elif isinstance(self.modelo, Normal): | |
a = 0 | |
while self.modelo.F(a) > 0: | |
a -= 1 | |
b = 0 | |
while self.modelo.F(b) < 1: | |
b += 1 | |
l = math.fabs(b-a)/1000.0 | |
tamanho = math.fabs(b-a) | |
variacao = [x*l+a for x in range(0, 1000)] | |
#retas x e y, respectivamente | |
reta_y_posicao_x = self.width/float(tamanho) * (math.fabs(0 - variacao[0])) + 20 | |
self.canvas.create_line(0, self.height - 20, self.width, self.height - 20) | |
if variacao[0] <= 0 <= variacao[-1]: | |
self.canvas.create_line(reta_y_posicao_x, 0, reta_y_posicao_x, self.height) | |
else: | |
reta_y_posicao_x = 20 | |
#desenha os pontos | |
for indice, ponto in enumerate(variacao): | |
try: | |
if acumulada: | |
probabilidade = self.funcao(ponto) | |
else: | |
probabilidade = self.funcao(variacao[indice], variacao[indice+1]) * 100 | |
x = self.width/float(tamanho) * (math.fabs(ponto - variacao[0])) + 20 | |
y = (self.height - 40) * probabilidade | |
if limiteX1 != None and limiteX2 != None: | |
if limiteX1 <= ponto <= limiteX2: | |
self.canvas.create_line(x, self.height - y - 20, x + 1, self.height - y - 20, fill='red') | |
self.canvas.create_line(x, self.height - y - 20, x, self.height - 20, fill='red') | |
else: | |
self.canvas.create_line(x, self.height - y - 20, x + 1, self.height - y - 20, fill='black') | |
self.canvas.create_line(x, self.height - y - 20, x, self.height - 20, fill='black') | |
else: | |
self.canvas.create_line(x, self.height - y - 20, x + 1, self.height - y - 20, fill='black') | |
self.canvas.create_line(x, self.height - y - 20, x, self.height - 20, fill='black') | |
except: pass | |
limite = 11 | |
#desenha metricas na reta x | |
for i in range(1, limite): | |
index = i * len(variacao) / float(limite) | |
index = int(index) | |
tamanhoInicial = math.fabs(variacao[index] - variacao[0]) | |
tamanhoTotal = math.fabs(variacao[-1] - variacao[0]) | |
factor = tamanhoInicial / float(tamanhoTotal) | |
x = self.width * factor + 20 | |
self.canvas.create_text(x, self.height - 10, text=str(variacao[index])) | |
#desenha metricas probabilidade | |
for i in range(1, 11): | |
y = self.height * i/11.0 | |
self.canvas.create_text(reta_y_posicao_x - 10, self.height - y - 20, text=str(0.1 * i)) | |
def desenha22(self, limiteX1, limiteX2, acumulada): | |
self.canvas.delete("all") | |
if isinstance(self.modelo, Uniforme): | |
variacao = [x for x in range(int(self.modelo.a * 100), int((self.modelo.b + 1) * 100 ))] | |
elif isinstance(self.modelo, Exponencial): | |
variacao = [x for x in range(0, 10 * 100)] | |
elif isinstance(self.modelo, Normal): | |
inflexao = 3.5 * self.modelo.o2 + self.modelo.u | |
inicial = (self.modelo.u - inflexao) * 100 | |
final = (self.modelo.u + inflexao) * 100 | |
variacao = [x for x in range(int(inicial), int(final))] | |
#retas x e y, respectivamente | |
reta_y_posicao_x = (self.width - 20)/float(len(variacao)) * (math.fabs(0 - variacao[0])) + 40 | |
self.canvas.create_line(0, self.height - 20, self.width, self.height - 20) | |
self.canvas.create_line(reta_y_posicao_x, 0, reta_y_posicao_x, self.height) | |
#desenha os pontos | |
for ponto in variacao: | |
if acumulada: | |
probabilidade = self.funcao(ponto/100.0) | |
else: | |
probabilidade = self.funcao(ponto/100.0, ponto/100.0 + 1) | |
x = (self.width+40)/float(len(variacao)) * (math.fabs(ponto - variacao[0])) + 40 | |
y = (self.height - 30) * probabilidade | |
if limiteX1 != None and limiteX2 != None: | |
if limiteX1 <= ponto/100.0 + 0.5 <= limiteX2: | |
self.canvas.create_line(x, self.height - y - 20, x + 1, self.height - 20, fill='red') | |
else: | |
self.canvas.create_line(x, self.height - y - 20, x + 1, self.height - 20, fill='black') | |
else: | |
self.canvas.create_line(x, self.height - y - 20, x + 1, self.height - 20, fill='black') | |
limite = 11 | |
if limite > len(variacao): | |
limite = len(variacao) | |
#desenha metricas na reta x | |
for i in range(1, limite): | |
index = i * (len(variacao) / float(limite)) | |
if index != int(index): | |
index += 1 | |
index = int(index) | |
x = (self.width - 20)/float(len(variacao)) * (math.fabs(variacao[index] - variacao[0])) + 40 | |
self.canvas.create_text(x, self.height - 10, text="%.2f" % (variacao[index]/100.0)) | |
#desenha metricas probabilidade | |
for i in range(1, 11): | |
y = self.height / 11.0 * i | |
self.canvas.create_text(reta_y_posicao_x - 20, self.height - y - 10, text=str(0.1 * i)) | |
class JanelaContinua: | |
def __init__(self, top_level, *parametros): | |
self.top_level = top_level | |
self.frame_left = Frame(top_level) | |
self.frame_left.pack(side=LEFT) | |
self.frame_right = Frame(top_level) | |
self.frame_right.pack(side=RIGHT) | |
self.frame_params = {} | |
self.entries = {} | |
for i in range(len(parametros)): | |
parametro = parametros[i] | |
self.frame_params[parametro] = Frame(self.frame_left) | |
self.frame_params[parametro].pack() | |
Label(self.frame_params[parametro], text=" " +parametro).pack(side=LEFT) | |
self.entries[parametro] = Entry(self.frame_params[parametro]) | |
self.entries[parametro].pack() | |
self.frame_botao_ok = Frame(self.frame_left) | |
self.frame_botao_ok.pack() | |
self.botao_ok = Button(self.frame_botao_ok, text="Gerar") | |
self.botao_ok['width'] = 35 | |
self.botao_ok.pack() | |
self.botao_ok.bind("<Button-1>", self.clica_botao_ok) | |
self.frame_fp = Frame(self.frame_right) | |
self.frame_fp.pack(side=LEFT) | |
self.frame_fda = Frame(self.frame_right) | |
self.frame_fda.pack(side=LEFT) | |
Label(self.frame_fp, text="Função distribuição de probabilidade:").pack() | |
self.graficoFd = GraficoContinuo(self.frame_fp) | |
Label(self.frame_fda, text="Função de distribuição acumulada:").pack() | |
self.graficoF = GraficoContinuo(self.frame_fda) | |
self.frame_1 = Frame(self.frame_left) | |
self.frame_2 = Frame(self.frame_1) | |
self.frame_2.pack() | |
Label(self.frame_2, text="P(a <= x <= b):").pack(side=LEFT) | |
self.x1 = Entry(self.frame_2) | |
self.x1.pack() | |
self.x2 = Entry(self.frame_2) | |
self.x2.pack(side=RIGHT) | |
self.frame_3 = Frame(self.frame_1) | |
self.frame_3.pack() | |
Label(self.frame_3, text=" F(x):").pack(side=LEFT) | |
self.F = Entry(self.frame_3) | |
self.F.pack() | |
self.frame_4 = Frame(self.frame_1) | |
self.frame_4.pack() | |
self.botao_calcula = Button(self.frame_4, text="Calcular") | |
self.botao_calcula['width'] = 35 | |
self.botao_calcula.pack() | |
self.botao_calcula.bind("<Button-1>", self.clica_botao_calcula) | |
self.frame_5 = Frame(self.frame_1) | |
self.frame_5.pack() | |
self.labelE = Label(self.frame_5, text="E(X): ") | |
self.labelE.pack() | |
self.labelVar = Label(self.frame_5, text="Var(X): ") | |
self.labelVar.pack() | |
self.labelP = Label(self.frame_5) | |
self.labelP.pack() | |
self.labelF = Label(self.frame_5) | |
self.labelF.pack() | |
def clica_botao_ok(self, event): | |
self.graficoFd.set_modelo(self.modelo, self.modelo.fdp, None, None, False) | |
self.graficoF.set_modelo(self.modelo, self.modelo.F, None, None, True) | |
self.frame_1.pack() | |
try: | |
self.labelE.setParent(None) | |
self.labelVar.setParent(None) | |
except: pass | |
self.labelE['text'] = "E(X): %.5f" % self.modelo.e() | |
self.labelVar['text'] = "Var(X): %.5f" % self.modelo.var() | |
def clica_botao_calcula(self, event): | |
try: | |
x = float(self.x1.get()) | |
y = float(self.x2.get()) | |
try: | |
self.labelP['text'] = "P(%.2f <= X <= %.2f) = %.5f" % (x, y, self.modelo.fdp(x, y)) | |
self.graficoFd.set_modelo(self.modelo, self.modelo.fdp, x, y, False) | |
except Exception, e: | |
tkMessageBox.showwarning("Ops...", e[0], parent=self.top_level) | |
except: | |
if self.x1.get().replace(" ","") != "" or self.x2.get().replace(" ","") != "": | |
tkMessageBox.showwarning("Ops...", "x deve ser um número inteiro", parent=self.top_level) | |
self.graficoFd.set_modelo(self.modelo, self.modelo.fdp, None, None, False) | |
self.labelP['text'] = "" | |
try: | |
x = float(self.F.get()) | |
self.labelF['text'] = "F(%.2f) = %.5f" % (x, self.modelo.F(x)) | |
self.graficoF.set_modelo(self.modelo, self.modelo.F, -9999999999999, x, True) | |
except: | |
self.graficoF.set_modelo(self.modelo, self.modelo.F, None, None, True) | |
self.labelF['text'] = "" | |
class JanelaUniforme(JanelaContinua): | |
def __init__(self, top_level): | |
JanelaContinua.__init__(self, top_level, "Parâmetro a", "Parâmetro b") | |
def clica_botao_ok(self, event): | |
a = self.entries["Parâmetro a"].get() | |
b = self.entries["Parâmetro b"].get() | |
try: | |
self.modelo = Uniforme(a, b) | |
JanelaContinua.clica_botao_ok(self, event) | |
except Exception, e: | |
tkMessageBox.showwarning("Ops...", e[0], parent=self.top_level) | |
class JanelaExponencial(JanelaContinua): | |
def __init__(self, top_level): | |
JanelaContinua.__init__(self, top_level, "Parâmetro alfa") | |
def clica_botao_ok(self, event): | |
a = self.entries["Parâmetro alfa"].get() | |
try: | |
self.modelo = Exponencial(a) | |
JanelaContinua.clica_botao_ok(self, event) | |
except Exception, e: | |
tkMessageBox.showwarning("Ops...", e[0], parent=self.top_level) | |
class JanelaNormal(JanelaContinua): | |
def __init__(self, top_level): | |
JanelaContinua.__init__(self, top_level, "Média", "Variância") | |
def clica_botao_ok(self, event): | |
u = self.entries["Média"].get() | |
o2 = self.entries["Variância"].get() | |
try: | |
self.modelo = Normal(u, o2) | |
JanelaContinua.clica_botao_ok(self, event) | |
except Exception, e: | |
tkMessageBox.showwarning("Ops...", e[0], parent=self.top_level) | |
raiz = Tk() | |
raiz.title("Mesquita 1.0") | |
JanelaPrincipal(raiz) | |
raiz.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment