Logistic regression — likelihood of the whole dataset
Background
Assuming the examples are independent, the probability of observing the entire labelled dataset is the product of each example's label-likelihood:
Maximum likelihood estimation chooses the weights that make this product as large as possible. The lesson's point: for logistic regression this objective (via its log) is convex, so gradient descent finds the single global optimum.
Problem statement
Implement dataset_likelihood(y, p) returning the product over all examples of the probability assigned to each observed label.
Input
y— array-like of true labels (0/1).p— array-like of predicted probabilities , same length.
Output
Returns a Python float: the joint likelihood .
Examples
Example 1
Input: y = [1, 0], p = [0.9, 0.8]
Output: 0.18
Explanation: class-1 contributes , class-0 contributes , and .
Example 2 — a perfect model scores 1
Input: y = [1, 0, 1], p = [1.0, 0.0, 1.0]
Output: 1.0
Explanation: every observed label was given probability , so the product is .
Constraints
- Build each example's label-likelihood (
pify==1else1-p), then take the product. - Return a plain
float.
Notes
- Real implementations sum log-likelihoods instead of multiplying probabilities, because a product of many small numbers underflows to 0. That log form is binary cross-entropy — the next lesson.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example — 0.9 * 0.2 = 0.18
- •Reference — a perfect model scores 1.0
- •Sample — mixed labels 0.8 * 0.7 * 0.6