Build a Market Concentration Report (HHI) for a Categorical Series

Quick Overview

Write a pandas function that turns a categorical Series into a market concentration report: the number of levels, the Herfindahl-Hirschman index on the 0 to 10,000 scale, the top-k concentration ratio and the largest share. It tests share calculations, NaN handling and exact output formatting.

Build a Market Concentration Report (HHI) for a Categorical Series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `concentration_report(s: pd.Series, top_k: int = 4) -> pd.Series` computing market-concentration metrics for a categorical Series. Return a Series named `concentration` with index name `metric` and these entries, in order, all `float64`: `n_levels`, `hhi`, `cr_k`, `top_share`. Requirements: - Exclude NaN. Shares are percentages (0 to 100), not proportions. - `n_levels`: number of distinct levels. - `hhi`: sum of squared percentage shares (DOJ 0 to 10,000 scale: 10,000 when one level holds everything). - `cr_k`: combined share of the `top_k` largest levels (`100.0` when `top_k` exceeds the level count). - `top_share`: the largest level's share. Example with four levels holding 30, 30, 20 and 20 percent: ```python s = pd.Series(["a", "a", "a", "b", "b", "b", "c", "c", "d", "d"]) # hhi = 30**2 + 30**2 + 20**2 + 20**2 = 2600.0 concentration_report(s, top_k=4) # metric # n_levels 4.0 # hhi 2600.0 # cr_k 100.0 # top_share 30.0 # Name: concentration, dtype: float64 ``` ```hint Pick the scale first Work out what scale the shares must be on for a single dominant level to give an HHI of 10,000, then build all four metrics from that one vector of shares. ``` ### Clarifying Questions to Ask - What should the function return when the Series is empty or entirely NaN? - If the input has a pandas `category` dtype, should categories that never occur count toward `n_levels`? - Can `top_k` be 0 or negative, or may I assume it is a positive integer? ### What a Strong Answer Covers - Computing percentage shares after dropping NaN, so HHI lands on the 0 to 10,000 scale. - Returning the four metrics in the stated order as `float64`, with the Series name and index name set. - Handling `top_k` at or above the number of levels, and noting that ties at the k-th position do not change `cr_k`. - Edge cases: empty or all-NaN input, unused categories in a categorical dtype, and float tolerance when testing. ### Follow-up Questions - How does HHI relate to Shannon entropy as a concentration measure? - How would you extend this to report the HHI change from a proposed merger of two levels? - How would you compute HHI for every group of a DataFrame (say, per region) without a Python loop?

Overview: Write a pandas function that turns a categorical Series into a market concentration report: the number of levels, the Herfindahl-Hirschman index on the 0 to 10,000 scale, the top-k concentration ratio and the largest share. It tests share calculations, NaN handling and exact output formatting.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement concentration_report(s: pd.Series, top_k: int = 4) -> pd.Series computing market-concentration metrics for a categorical Series. Return a Series named concentration with index name metric and these entries, in order, all float64: n_levels, hhi, cr_k, top_share.

Requirements:

  • Exclude NaN. Shares are percentages (0 to 100), not proportions.
  • n_levels : number of distinct levels.
  • hhi : sum of squared percentage shares (DOJ 0 to 10,000 scale: 10,000 when one level holds everything).
  • cr_k : combined share of the top_k largest levels ( 100.0 when top_k exceeds the level count).
  • top_share : the largest level's share.

Example with four levels holding 30, 30, 20 and 20 percent:

s = pd.Series(["a", "a", "a", "b", "b", "b", "c", "c", "d", "d"])
# hhi = 30**2 + 30**2 + 20**2 + 20**2 = 2600.0
concentration_report(s, top_k=4)
# metric
# n_levels        4.0
# hhi          2600.0
# cr_k          100.0
# top_share      30.0
# Name: concentration, dtype: float64

Clarifying Questions to Ask Guidance

  • What should the function return when the Series is empty or entirely NaN?
  • If the input has a pandas category dtype, should categories that never occur count toward n_levels ?
  • Can top_k be 0 or negative, or may I assume it is a positive integer?

What a Strong Answer Covers Guidance

  • Computing percentage shares after dropping NaN, so HHI lands on the 0 to 10,000 scale.
  • Returning the four metrics in the stated order as float64 , with the Series name and index name set.
  • Handling top_k at or above the number of levels, and noting that ties at the k-th position do not change cr_k .
  • Edge cases: empty or all-NaN input, unused categories in a categorical dtype, and float tolerance when testing.

Follow-up Questions Guidance

  • How does HHI relate to Shannon entropy as a concentration measure?
  • How would you extend this to report the HHI change from a proposed merger of two levels?
  • How would you compute HHI for every group of a DataFrame (say, per region) without a Python loop?