Exercises — Likelihood and log-likelihood
Self-check questions, exercises, and worked answers
← Back to the lesson: Likelihood and log-likelihood
Check your understanding
Calculate likelihood for the running example
In the overview example, the model assigns \(P(\text{They}\mid q)=0.6\), then \(P(\text{built}\mid q,\text{They})=0.5\). Calculate the likelihood and log-likelihood of the observed continuation “They built”, then its NLL.
Show the worked answer
By the chain rule, \(P(\text{They},\text{built}\mid q)=0.6\times0.5=0.3\). The log-likelihood is \(\ln(0.3)\approx-1.204\) nats and the NLL is \(-\ln(0.3)\approx1.204\) nats. Token independence is not assumed: the second term is conditioned on the first.
Practice with the mathematical foundations collection, including worked exercises on negative log-likelihood and perplexity.
- Why use log-likelihood rather than the direct product?
- Which rule factorizes a sequence probability into conditional probabilities? Does it require token independence?
- Why document the tokenizer and NLL reduction when comparing models?
Show answers
- A product of many probabilities can become extremely small and difficult to represent numerically. The logarithm turns it into a sum, which is more stable and easier to optimize. Because the logarithm is increasing, maximizing log-likelihood preserves the ranking induced by likelihood.
- The chain rule factorizes any joint probability into successive conditional probabilities. It does not require independence between tokens. Each term is conditioned on the preceding prefix. An independence assumption between separate dataset sequences, sometimes used when constructing total likelihood, is a different issue.
- The tokenizer determines which events are evaluated and their granularity. Summed NLL also depends on token count; averaging per token changes the scale and must be distinguished from the sum. Comparisons are meaningful only when these conventions and the evaluated data are compatible.
Numerical transfer: for the example probabilities \((0.5, 0.25, 0.8)\), likelihood is \(0.1\), total NLL is approximately \(2.303\) nats, and mean NLL per token is approximately \(0.768\) nat. This mean is the average of the one-hot target cross-entropies at the three positions; a batch loss then adds an aggregation and masking convention.
To see how likelihood connects to the training objective, read Cross-entropy and Loss.
Generate or score from the same prefix
Keep the model fixed. Start from prefix \(c\) and compare two operations over two tokens. Generation uses greedy decoding. The observed continuation to score is \((A,C)\). The table gives the model’s predicted probabilities.
| Prefix | \(A\) | \(B\) | \(C\) |
|---|---|---|---|
| \(c\) | 0.30 | 0.60 | 0.10 |
| \(cA\) | 0.05 | 0.05 | 0.90 |
| \(cB\) | 0.40 | 0.35 | 0.25 |
- Which tokens does greedy decoding generate? What prefix does it use for the second token?
- Which prefix is used to score the second observed token \(C\)? Compute the likelihood of \((A,C)\) conditional on \(c\).
- Compute the likelihood of the generated continuation under the model too. Does the greedy continuation maximize two-token likelihood in this example?
Show the worked answer
- The decoder selects \(B\) under \(c\), then \(A\) under \(cB\). The generated continuation is \((B,A)\).
- Scoring follows the observed continuation. For the second token it uses \(cA\). The likelihood is \(p(A\mid c)\,p(C\mid cA)=0.30\times0.90=0.27\).
- The generated continuation has likelihood \(p(B\mid c)\,p(A\mid cB)=0.60\times0.40=0.24\), below \(0.27\). Greedy decoding maximizes each conditional probability along the path it follows. Selecting \(B\) at the first step commits it to a different path.
Return to the review plan · Run inference and generate text · Score observed tokens