# introduccion-a-la-estadistica-bayesiana-con-pymc
Introducción a la estadística bayesiana con PyMC
Volver a la portada del cursoDiferencias con Estadística Frecuentista
Sección 1 · Fundamentos de la Estadística Bayesiana
En este video
- Contrastas qué es aleatorio en cada enfoque y comparas la estimación puntual: máxima verosimilitud 0.7 frente a la media posterior 0.643
- Comparas el intervalo de confianza de Wald con el intervalo creíble y ves que se leen de forma distinta
- Simulas treinta repeticiones y mil réplicas, y ves que Wald cubre solo 89 por ciento frente a 96 por ciento del creíble
- Comparas el p valor de binomtest, 0.172, con la probabilidad posterior de que theta supere un medio, 0.87
- Pruebas tres priors y ves que con suficientes datos la verosimilitud domina, aunque un prior informativo debe justificarse
from scipy import stats
k, n = 7, 10
mle = k / n
post = stats.beta(2 + k, 2 + n - k)
print(mle)
print(round(post.mean(), 3))
print(round(post.median(), 3))
from scipy import stats
k, n = 7, 10
p = k / n
z = stats.norm.ppf(0.97)
se = (p * (1 - p) / n) ** 0.5
print(round(p - z * se, 2), round(p + z * se, 2))
post = stats.beta(2 + k, 2 + n - k)
print(post.ppf([0.03, 0.97]).round(2))
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(4)
p = rng.binomial(10, 0.65, size=30) / 10
se = np.sqrt(p * (1 - p) / 10) * 1.88
ok = (p - se <= 0.65) & (0.65 <= p + se)
c = np.where(ok, "#2563EB", "#F59E0B")
plt.hlines(range(30), p - se, p + se, c, lw=4)
plt.axvline(0.65, color="#22D3EE", lw=3)
rng = np.random.default_rng(42)
k = rng.binomial(10, 0.65, size=1000)
p, z = k / 10, stats.norm.ppf(0.97)
se = np.sqrt(p * (1 - p) / 10)
print(np.mean(abs(p - 0.65) <= z * se))
lo = stats.beta.ppf(0.03, 2 + k, 12 - k)
hi = stats.beta.ppf(0.97, 2 + k, 12 - k)
print(np.mean((lo <= 0.65) & (0.65 <= hi)))
print(np.sum(se == 0))
from scipy import stats
res = stats.binomtest(7, 10, 0.5, "greater")
print(round(res.pvalue, 3))
post = stats.beta(9, 5)
print(round(post.sf(0.5), 3))
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import beta
x = np.linspace(0, 1, 500)
fig, ax = plt.subplots(2, 1, sharex=True)
for i, n in enumerate([10, 200]):
ax[i].set_title(f"n = {n}")
for a, b in [(1, 1), (2, 2), (20, 80)]:
ax[i].plot(x, beta.pdf(x, a + 0.7 * n, b + 0.3 * n))