Pointwise mutual information (PMI)Mediumnumpynlpinformation-theorystatisticspmi
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 .
Python
Loading...
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reference example: -0.263
- •Independent events give PMI 0
- •Zero joint count -> -inf
- •Stronger-than-chance co-occurrence is positive