Inferencia y A/B testing
1.10.2kata 2 de 4

Type I, Type II Errors & Power

Falsos positivos, falsos negativos y potencia estadística

Error Tipo I (α) es un falso positivo: rechazas H0 cuando H0 es verdadera. Ejemplo: concluyes que el nuevo botón aumenta conversiones cuando en realidad no hay efecto. α=0.05 significa tolerar 5% de falsos positivos. Si ejecutas 20 tests, esperas ~1 falso positivo por azar.

Error Tipo II (β) es un falso negativo: no rechazas H0 cuando H0 es falsa. Ejemplo: no detectas que el nuevo botón realmente aumenta conversiones. β depende del tamaño del efecto real, la varianza de los datos y el tamaño de muestra. Potencia = 1 - β = capacidad de detectar efecto real.

Los errores son un tradeoff: bajar α (ser más estricto sobre H0) requiere más muestra; subir potencia requiere más muestra. Diseño de experimento balancea estos: típicamente α=0.05, potencia=0.80, lo que da un ratio 1:4 de falsos positivos a falsos negativos.

structure.txt
import numpy as np
from scipy import stats

# Type I error (false positive)
alpha = 0.05  # probability of rejecting H0 when H0 is true
print(f'Type I error (α): {alpha}')
print(f'If H0 is true, we have {alpha*100}% chance of false positive')

# Type II error and Power
beta = 0.20  # probability of NOT rejecting H0 when H0 is false
power = 1 - beta
print(f'Type II error (β): {beta}')
print(f'Power (1 - β): {power}')
print(f'If H1 is true, we have {power*100}% chance of detecting it')

# Power analysis: how much sample do we need?
from scipy.stats import norm
baseline_cr = 0.05  # 5% conversion
expected_lift = 0.25  # expect 25% relative increase
target_cr = baseline_cr * (1 + expected_lift)  # 6.25%
alpha = 0.05
power = 0.80

# Cohen's h for proportions
h = 2 * (np.arcsin(np.sqrt(target_cr)) - np.arcsin(np.sqrt(baseline_cr)))
z_alpha = norm.ppf(1 - alpha/2)  # two-sided
z_beta = norm.ppf(power)
n_per_group = ((z_alpha + z_beta) / h) ** 2
print(f'Need {n_per_group:.0f} samples per group')

Debugging lab

Detecta y corrige el error en el código.

0/2 tests passing0%
  1. 01

    alpha = 0.05 power = 0.80 print(f'Type II error = {power}') # WRONG

  2. 02

    # Need more power, what to do?