Compare Pearson, Spearman and Kendall correlation with a tie diagnostic in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `rank_correlation_methods(df, col_a, col_b)`. It takes a DataFrame and the names of two numeric columns, and returns a `pd.Series` of dtype `float64` named `value` whose index is exactly `['pearson', 'spearman', 'kendall', 'tie_fraction']`, in that order.
- `pearson`, `spearman` and `kendall` are the correlation between `col_a` and `col_b` under each method.
- `tie_fraction` is `1 - (distinct values / total values)` computed over the concatenation of both columns.
- All computations use non-null values only.
You may rely on `df[col].corr(other, method=...)` (pandas calls scipy for the rank methods).
```python
df = pd.DataFrame({"a": [1, 2, 2, 3, 5], "b": [2, 2, 3, 4, 5]})
rank_correlation_methods(df, "a", "b")
# pearson 0.935585
# spearman 0.921053
# kendall 0.888889
# tie_fraction 0.500000
# Name: value, dtype: float64
```
The concatenated vector holds ten values with five distinct ones (1 through 5), so the tie fraction is 0.5.
```hint Missing values
`Series.corr` skips every row where either value is missing. The tie fraction works on the two columns stacked end to end, so it needs its own null handling.
```
```hint Output shape
Integer inputs or a dict built in a different order can change what the returned Series looks like. Pass the index, name and dtype explicitly.
```
### Constraints & Assumptions
- Both columns are numeric and may contain NaN.
- The three correlations use the rows where both values are present. The tie fraction uses every non-null value from either column, so a row missing one value still contributes the other.
- Any quantity that is undefined (fewer than two complete rows, a constant column, no non-null values at all) comes back as NaN.
### What a Strong Answer Covers
- Null handling that matches each output: pairwise deletion for the correlations and per-column dropping for the stacked tie vector.
- A Series with the exact index order, the name `value` and dtype `float64`, whatever the input dtypes are.
- Edge cases such as all-null columns, a constant column and fewer than two complete rows.
- How Spearman (average ranks) and Kendall (tied pairs and the tau-b correction) treat ties, and how to read the coefficients when the tie fraction is high.
### Follow-up Questions
- Spearman usually comes out larger than Kendall on the same data. Is that caused by ties, and which coefficient would you report when ties are heavy?
- What is the difference between Kendall's tau-a and tau-b, and which one does pandas return?
- How would you attach a p-value or a confidence interval to each coefficient?
Overview: Write a pandas function that returns the Pearson, Spearman and Kendall correlations between two columns along with a tie-fraction diagnostic. It tests careful null handling, an exact Series output, and how rank-based correlation methods treat tied values.