Enforce Hierarchical Consistency on Probabilities
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Write `enforce_hierarchy(coarse_proba, fine_proba, parent, coarse_weight=0.5)`. Given `coarse_proba` (n×k), `fine_proba` (n×m) and an integer array `parent` (length m, mapping each fine class to its coarse class), return `(coarse_out, fine_out, conditional)` with shapes n×k, n×m and n×m. First row-normalize both inputs (an all-zero row becomes uniform). Let `mass[:, g]` be the normalized fine probability summed over the children of coarse class g.
The outputs are defined as follows, with `w = coarse_weight`:
- `coarse_out = w * coarse_norm + (1 - w) * mass`.
- `conditional[:, j] = fine_norm[:, j] / mass[:, parent[j]]`. If that group mass is 0, use `1 / number_of_children(parent[j])`.
- `fine_out[:, j] = coarse_out[:, parent[j]] * conditional[:, j]`.
Raise `ValueError` for any of these:
- non-2-D inputs or mismatched row counts
- k or m equal to 0
- `parent` that is not an integer array of shape (m,), or that has values outside [0, k)
- non-finite or negative probabilities
- `coarse_weight` outside [0, 1]
- a coarse class with no children
Example:
```python
coarse = [[0.7, 0.3], [2.0, 2.0]]
fine = [[0.1, 0.2, 0.6, 0.1], [0.0, 0.0, 1.0, 3.0]]
parent = [0, 0, 1, 1]
coarse_out, fine_out, conditional = enforce_hierarchy(coarse, fine, parent, coarse_weight=0.5)
# coarse_out = [[0.5, 0.5], [0.25, 0.75]]
# conditional = [[1/3, 2/3, 6/7, 1/7], [0.5, 0.5, 0.25, 0.75]]
# fine_out = [[1/6, 1/3, 3/7, 1/14], [0.125, 0.125, 0.1875, 0.5625]]
```
In the second row the fine scores give coarse class 0 no mass, so its two children fall back to the uniform conditional 0.5.
```hint Group sums
The per-group mass is a sum over the fine columns that share a parent. Look for a way to compute it for all rows at once without a Python loop over groups.
```
```hint Zero-mass groups
Watch the division when a whole group has zero normalized fine mass. NumPy evaluates both branches of `np.where`, so the denominator must be safe before you divide.
```
### Constraints & Assumptions
- Inputs may be unnormalized non-negative scores. Outputs are NumPy float arrays.
- Results are checked with `rtol=1e-7`, `atol=1e-10`.
### What a Strong Answer Covers
- Row normalization that handles all-zero rows, and per-group mass computed in vectorized form
- The blend, the conditionals and the fine reconstruction, including the zero-mass fallback
- Why the outputs are consistent: children sum to their parent and every row sums to 1
- The full set of input checks, run before any arithmetic
- The time and memory cost of the aggregation step
### Follow-up Questions
- How would you generalize this to a tree of arbitrary depth?
- Does argmax decoding over `fine_out` always agree with argmax over `coarse_out`?
- How would you choose `coarse_weight`?
Overview: Write a NumPy function that combines coarse and fine classifier probabilities so every fine class sums exactly to its parent class. It tests row normalization, vectorized per-group aggregation, zero-mass edge cases and careful input validation.