#308Logistic regression — likelihood of the whole datasetEasy

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:

L=i=1npiyi(1pi)1yi=i=1n{piyi=11piyi=0\mathcal{L} = \prod_{i=1}^{n} p_i^{\,y_i}\,(1 - p_i)^{\,1 - y_i} = \prod_{i=1}^{n} \begin{cases} p_i & y_i = 1 \\ 1 - p_i & y_i = 0 \end{cases}

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 P(y=1)\mathbb{P}(y=1), same length.

Output

Returns a Python float: the joint likelihood L[0,1]\mathcal{L} \in [0, 1].

Examples

Example 1

Input:  y = [1, 0], p = [0.9, 0.8]
Output: 0.18

Explanation: class-1 contributes 0.90.9, class-0 contributes 10.8=0.21-0.8=0.2, and 0.9×0.2=0.180.9 \times 0.2 = 0.18.

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 11, so the product is 11.

Constraints

  • Build each example's label-likelihood (p if y==1 else 1-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.
Python
Loading...

▶ 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