-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pruebas al sistema de creación de distribuciones PyMC.
- Loading branch information
1 parent
5a77964
commit 33a499d
Showing
5 changed files
with
320 additions
and
41 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import numpy as np | ||
import scipy.stats as estad | ||
import pymc | ||
import matplotlib.pyplot as dib | ||
from warnings import warn as avisar | ||
|
||
import Matemáticas.NuevoIncert as Inc | ||
import Matemáticas.Distribuciones as Ds | ||
|
||
""" | ||
Un módulo de pruebas, un poco inelegante. | ||
""" | ||
|
||
# Pruebas para el convertidor de distribuciones SciPy a PyMC en NuevoIncert | ||
|
||
n_rep = 10000 | ||
|
||
dibujar = False | ||
|
||
con_errores = [] | ||
|
||
for nombre, dist in sorted(Ds.dists.items()): | ||
|
||
if dist['tipo'] != 'cont': | ||
continue | ||
|
||
if dist['scipy'] is None or dist['pymc'] is None: | ||
continue | ||
|
||
mín, máx = dist['límites'] | ||
if mín == -np.inf: | ||
if máx == np.inf: | ||
núms = estad.norm.rvs(10, 20, size=100) | ||
else: | ||
avisar('No se pudo comprobar la distribución %s' % nombre) | ||
continue | ||
|
||
else: | ||
if máx == np.inf: | ||
núms = estad.gamma.rvs(1, loc=mín, size=100) | ||
else: | ||
núms = estad.uniform.rvs(mín, máx-mín, size=100) | ||
|
||
dist_scipy = Inc.ajustar_dist(datos=núms, límites=dist['límites'], cont=True, | ||
lista_dist=[nombre])[0] | ||
puntos_scipy = dist_scipy.rvs(size=n_rep) | ||
|
||
dist_pymc = Inc.ajustar_dist(datos=núms, límites=dist['límites'], cont=True, usar_pymc=True, | ||
nombre=nombre, lista_dist=[nombre])[0] | ||
if isinstance(dist_pymc, pymc.Stochastic): | ||
puntos_pymc = np.array([dist_pymc.rand() for x in range(n_rep)]) | ||
elif isinstance(dist_pymc, pymc.Deterministic): | ||
puntos_pymc = np.empty(n_rep) | ||
dist_pariente = min(dist_pymc.extended_parents) | ||
for i in range(n_rep): | ||
dist_pariente.rand() | ||
puntos_pymc[i] = dist_pymc.value | ||
else: | ||
raise ValueError | ||
|
||
scipy_de_pymc = Inc.ajustar_dist(datos=puntos_pymc, límites=dist['límites'], cont=True, | ||
lista_dist=[nombre])[0] | ||
|
||
if dibujar: | ||
|
||
dib.hist(núms, normed=True, color='red', histtype='stepfilled', alpha=0.2, bins=100) | ||
|
||
dib.hist(puntos_scipy, normed=True, color='blue', histtype='stepfilled', alpha=0.2, bins=100) | ||
|
||
dib.hist(puntos_pymc, normed=True, color='green', histtype='stepfilled', alpha=0.2, bins=100) | ||
|
||
x = np.linspace(dist_scipy.ppf(0.01), dist_scipy.ppf(0.99), 100) | ||
dib.plot(x, dist_scipy.pdf(x), 'b-', lw=2, alpha=0.6) | ||
dib.plot(x, scipy_de_pymc.pdf(x), 'g-', lw=2, alpha=0.6) | ||
|
||
dib.title(nombre) | ||
dib.show() | ||
|
||
error = 0 | ||
for n, i in enumerate(scipy_de_pymc.args): | ||
if i == 0: | ||
e = i - dist_scipy.args[n] | ||
else: | ||
e = (i - dist_scipy.args[n]) / dist_scipy.args[n] | ||
error = max(error, abs(e)) | ||
|
||
if error >= 0.05: | ||
mensaje = '¡¡¡!!!' | ||
con_errores.append(nombre) | ||
else: | ||
mensaje = 'Todo bien.' | ||
|
||
print(nombre, error, mensaje, dist_scipy.args, scipy_de_pymc.args) | ||
|
||
if len(con_errores): | ||
print('Verificar:', con_errores) | ||
else: | ||
print('¡Todas las distribuciones pasaron!') | ||
|
||
# Arreglar: Errores con T, TNo, etc... |
Oops, something went wrong.