Rank column pairs by covariance vs correlation and flag where the rankings differ
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `covariance_vs_correlation_ranking(df)` taking a DataFrame whose numeric columns have very different scales. For every unordered pair of distinct numeric columns, compute covariance and Pearson correlation, rank pairs both ways, and flag where rankings differ.
Requirements:
- The columns are exactly `['col_a', 'col_b', 'cov', 'r', 'rank_by_cov', 'rank_by_r', 'rank_changed']` with dtypes `object, object, float64, float64, int64, int64, bool`.
- One row per pair, with `col_a` preceding `col_b` alphabetically.
- `rank_by_cov` and `rank_by_r` are 1-based dense ranks (ties share a rank, and the next rank is consecutive) by absolute `cov` and absolute `r`, descending.
- `rank_changed` is `rank_by_cov != rank_by_r`.
- Sort by `rank_by_r`, then `col_a`, then `col_b` ascending, then `reset_index(drop=True)`.
Example:
```python
df = pd.DataFrame({
"revenue": [1000.0, 3000.0, 2000.0, 4000.0],
"age": [20, 30, 40, 50],
"score": [2.0, 4.0, 5.0, 9.0],
})
```
Expected output:
```text
col_a col_b cov r rank_by_cov rank_by_r rank_changed
0 age score 36.6667 0.9648 3 1 True
1 revenue score 3333.3333 0.8771 2 2 False
2 age revenue 13333.3333 0.8000 1 3 True
```
`age` and `score` move together most tightly, but `score` is small in scale, so their covariance is the smallest of the three. `revenue` is measured in thousands, which puts its pair with `age` first by covariance even though it has the weakest correlation.
```hint Scale and covariance
Write covariance as the correlation times the two standard deviations. That product tells you which pairs will rise to the top of the covariance ranking.
```
```hint Pair orientation
Enumerating pairs in DataFrame column order does not guarantee that `col_a` sorts before `col_b`.
```
### Constraints & Assumptions
- Numeric means int or float columns, including the nullable `Int64` and `Float64` dtypes. Other columns are ignored.
- Column names are unique strings.
- `cov` is the sample covariance, the pandas default (`ddof=1`). When values are missing, both statistics use the rows where both columns are present.
- With k numeric columns the output has $k(k-1)/2$ rows.
### Clarifying Questions to Ask
- How should a pair with an undefined correlation, such as one involving a constant column, be ranked?
- Should values that differ only by floating point rounding count as ties in the dense rank?
- Is the sample covariance (`ddof=1`) the expected value for `cov`, or the population covariance?
### What a Strong Answer Covers
- Covariance and correlation for every unordered pair, with `col_a` before `col_b` alphabetically.
- Dense, descending ranks on the absolute values, so strong negative relationships rank high and tied pairs share a rank with no gap after it.
- The exact columns, dtypes and sort order.
- Why the two rankings disagree, in terms of the units and scales of the columns.
- Behavior when a correlation is undefined or there are fewer than two numeric columns.
### Follow-up Questions
- Why can ranking by absolute covariance be misleading even within one dataset?
- How would you extend this to flag statistically insignificant correlations?
- If you standardized every column first, what would the covariance ranking look like?
Overview: Write a pandas function that ranks every pair of numeric columns by absolute covariance and by absolute Pearson correlation, then flags the pairs whose rank changes. It tests dense ranking, exact output dtypes and sort order, and why covariance depends on the scale of the columns.