2/14
Part 1 of 7 · LikelihoodLearn

The likelihood scores each rate by how probable it makes the data

An OpenAI coding round asked candidates to use cross entropy to find a distribution's minimum error (1 source). Cross entropy is a likelihood in disguise, so start with the likelihood.

NIST's handbook puts it loosely: the likelihood of a set of data is the probability of getting that data under the chosen model. The parameter values that maximize it are the maximum likelihood estimates, or MLEs. MacKay adds a warning that's worth saying in an interview: it's the likelihood of the parameters, and the likelihood function is not a probability distribution over them.

The shop wants its return rate. If each order comes back with chance p, one particular run of n orders with k returns has probability pk(1−p)n−kp^k (1-p)^{n-k}. Score every rate that way, and keep the best:

import numpy as np

rng = np.random.default_rng(34)
# 5,000 orders, each returned with chance 12%
returned = (rng.random(5_000) < 0.12).astype(int)
k, n = int(returned.sum()), len(returned)
# every rate from 0.0002 to 0.9998
grid = np.arange(1, 5_000) / 5_000


def best_and_range(outcomes):
    m, j = len(outcomes), int(outcomes.sum())
    # the log of p^j (1 - p)^(m - j), at every rate on the grid
    ll = j * np.log(grid) + (m - j) * np.log(1 - grid)
    # rates within 2 log units of the best
    near = grid[ll >= ll.max() - 2]
    return grid[ll.argmax()], near.min(), near.max()


for m in (50, 5_000):
    best, lo, hi = best_and_range(returned[:m])
    print(f"first {m:>5,} orders: best rate {best:.4f}, good rates {lo:.3f} to {hi:.3f}")
Predict

The first 50 orders and all 5,000 give similar best rates. What happens to the range of good rates?

Pick one to see what it prints.
Pick an answer above to see the rest