#!/usr/bin/env python3
# Source: q1-ml-fundamentals.html
import numpy as np

def focal_loss(p, y, gamma=2.0, alpha=0.25, eps=1e-7):
    """Binary focal loss. p: predicted P(y=1). y: 0/1 labels."""
    p, y = np.asarray(p, dtype=float), np.asarray(y)
    if p.shape != y.shape or p.size == 0 or not np.isin(y, [0, 1]).all():
        raise ValueError("Use matching nonempty probabilities and binary labels")
    if not np.isfinite(p).all() or ((p < 0) | (p > 1)).any():
        raise ValueError("Probabilities must be finite and within [0, 1]")
    if not 0 < eps < 0.5 or gamma < 0 or not 0 <= alpha <= 1:
        raise ValueError("Use valid focal loss parameters")
    p = np.clip(p, eps, 1 - eps)
    p_t = np.where(y == 1, p, 1 - p)              # prob given to the true class
    a_t = np.where(y == 1, alpha, 1 - alpha)      # class balance weight
    return np.mean(-a_t * (1 - p_t) ** gamma * np.log(p_t))

def best_threshold(p, y, c_fp=1.0, c_fn=9.0):
    """Pick the threshold that minimises total cost on a validation set."""
    p, y = np.asarray(p, dtype=float), np.asarray(y)
    if p.ndim != 1 or p.shape != y.shape or p.size == 0:
        raise ValueError("Use matching nonempty probability and label arrays")
    if not np.isfinite(p).all() or ((p < 0) | (p > 1)).any():
        raise ValueError("Probabilities must be finite and within [0, 1]")
    if not np.isin(y, [0, 1]).all() or not np.isfinite([c_fp, c_fn]).all() or min(c_fp, c_fn) < 0:
        raise ValueError("Use binary labels and nonnegative costs")
    # Include the choice that predicts every item negative.
    grid = np.append(np.unique(p), np.nextafter(p.max(), np.inf))
    costs = [c_fp * np.sum((p >= t) & (y == 0)) + c_fn * np.sum((p < t) & (y == 1))
             for t in grid]
    return grid[int(np.argmin(costs))]
print(best_threshold(np.array([0.2, 0.9]), np.array([0, 0])))
