from statistics import mean, stdev
from random import Random
from math import comb, sqrt

def success_rate(matrix):
    if len(matrix) < 2 or any(not row for row in matrix):
        raise ValueError('Se necesitan al menos dos tareas con ejecuciones')
    task_means = [mean(row) for row in matrix]
    return mean(task_means), stdev(task_means)/sqrt(len(task_means))

def paired_bootstrap(a, b, n_boot=2000, seed=42):
    if len(a) != len(b) or len(a) < 2 or n_boot < 40 or any(not x for x in a+b):
        raise ValueError('Matrices o número de réplicas no válidos')
    differences = [mean(y)-mean(x) for x,y in zip(a,b)]
    rng = Random(seed)
    means = sorted(mean(rng.choice(differences) for _ in differences) for _ in range(n_boot))
    return means[int(.025*n_boot)], means[min(n_boot-1,int(.975*n_boot))]

def pass_at_k(n, c, k):
    if not 0 <= c <= n or not 1 <= k <= n: raise ValueError('n, c o k no válidos')
    return 1-(comb(n-c,k)/comb(n,k) if n-c>=k else 0)

def pass_hat_k(n, c, k):
    if not 0 <= c <= n or not 1 <= k <= n: raise ValueError('n, c o k no válidos')
    return comb(c,k)/comb(n,k) if c>=k else 0

def mcnemar_exact(a, b):
    if len(a) != len(b) or not a or any(x not in (0,1) for x in a+b):
        raise ValueError('Pares binarios no válidos')
    b_only = sum(x == 0 and y == 1 for x,y in zip(a,b))
    a_only = sum(x == 1 and y == 0 for x,y in zip(a,b))
    n = a_only + b_only
    return min(1.0, 2*sum(comb(n,i)*0.5**n for i in range(min(a_only,b_only)+1))) if n else 1.0

def pareto(points):
    # Cada elemento es (nombre, coste, precisión).
    return [a for a in points if not any(b[1] <= a[1] and b[2] >= a[2]
            and (b[1] < a[1] or b[2] > a[2]) for b in points)]

