Pointwise mutual information (PMI)
Background
Pointwise Mutual Information (PMI) measures how much more — or less — two events co-occur than if they were independent. In NLP it scores word associations: high PMI means two words appear together far more than chance, the basis of collocation detection and early count-based word embeddings (PPMI matrices).
Problem statement
Implement compute_pmi(joint_counts, total_counts_x, total_counts_y, total_samples) returning the PMI in bits:
with , , . Round to 3 decimals.
Input
joint_counts—int: number of times x and y co-occur.total_counts_x—int: number of times x occurs.total_counts_y—int: number of times y occurs.total_samples—int: total observations .
Output
Returns a float (PMI in bits, rounded to 3 decimals); when the joint count is 0.
Examples
Example 1
Input: compute_pmi(50, 200, 300, 1000)
Output: -0.263
Explanation: and ; , so x and y co-occur slightly less than chance.
Constraints
- Probabilities are counts divided by
total_samples. - means more-than-chance association, less, independent.
- Return if
joint_countsis 0; otherwise round to 3 decimals. - Inputs are non-negative integers with
joint_counts <= min(count_x, count_y).
Notes
- PMI is unbounded below (rare co-occurrences go very negative); NLP often uses PPMI for an interpretable, sparse association matrix.
- It is the per-event term whose expectation (weighted by ) is the mutual information .
▶ 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: slightly-below-chance pair
- •Reference: independent events give zero
- •Sample: stronger-than-chance association