"""
Practice: pairwise comparisons. Lab 11's pairwise math (evalkit/pairwise.py and Scripts 1-3): turn a verdict
from each order into a win, loss or tie, summarise a comparison two ways (ties as half, decisive only), check
whether the decisive win rate is clear of 50%, size a comparison, and fit Bradley-Terry strengths.

Fill in the TODOs, then run the tests:
    pytest test_pairwise_practice.py      (locally)
or press "Run tests" on the chapter page.

Scores are always for version A: 1 = A won, 0 = A lost (B won), 0.5 = tie. Standard library only.
"""
import math

Z_95 = 1.959964   # two-sided 95%, as in the lab
Z_80 = 0.841621   # one-sided z for 80% power


def swap_outcome(pick_ab, pick_ba):
    """One ticket judged twice. pick_ab: the version the judge picked ("A" or "B") when A was shown first;
    pick_ba: the one it picked when B was shown first. Same pick both times: that version wins
    (1.0 if A, 0.0 if B). Different picks: a tie (0.5)."""
    # TODO
    raise NotImplementedError


def tally(scores):
    """Summarise A's scores (1 / 0 / 0.5). Return a dict with
    wins, losses, ties, n, win_rate (ties counted as half a win) and decisive (wins / (wins + losses),
    or None when there are no decisive comparisons)."""
    # TODO
    raise NotImplementedError


def wilson_ci(k, n, z=Z_95):
    """Wilson interval for k successes in n trials, as a (low, high) tuple. (0.0, 1.0) when n == 0."""
    # TODO
    raise NotImplementedError


def verdict(wins, losses):
    """The lab's check on the decisive comparisons: if the Wilson interval of wins / (wins + losses) is above
    50%, return "A better"; below 50%, "B better"; if it contains 50%, "can't tell"."""
    # TODO
    raise NotImplementedError


def comparisons_needed(p_win, z_alpha=Z_95, z_power=Z_80):
    """Decisive comparisons needed to show a true decisive win rate p_win is not 50% (80% power, two-sided 5%):
        n = ((z_alpha * 0.5 + z_power * sqrt(p_win * (1 - p_win))) / |p_win - 0.5|) ** 2, rounded UP."""
    # TODO
    raise NotImplementedError


def expected_tie_rate(gap, bias):
    """A simulated judge like the lab's: shown X first and Y second, it picks the first with chance
    sigmoid(strength_X - strength_Y + bias), where sigmoid(x) = 1 / (1 + e^-x). gap = strength_A - strength_B.
    Return the chance a both-orders comparison ends in a tie: the judge picks the first-shown summary both
    times, or the second-shown both times."""
    # TODO
    raise NotImplementedError


def bradley_terry(results, prior=0.5, iters=2000):
    """Fit Bradley-Terry log-strengths, centred so they average 0.

    results: {(a, b): (score_for_a, n)}, where score_for_a sums A's scores (ties count half) over n comparisons.
    As in the lab, add `prior` virtual comparisons to every pair, split as half a win each.
    The MM update (Hunter, 2004), with s = 1.0 for every version to start:
        s_i  <-  wins_i / sum over i's pairs of  n_ij / (s_i + s_j)
    then divide every s by their geometric mean. Repeat `iters` times (or until it stops changing) and
    return {version: log(s)}."""
    # TODO
    raise NotImplementedError


def to_elo(log_strengths, base=1000.0):
    """Log-strengths on the Elo scale: base + 400 * strength / ln(10) (400 points = 10x the odds)."""
    # TODO
    raise NotImplementedError
