Compute phi and Cramer's V from a chi-square test of independence in SQL
Quick Overview
Write a SQL query that builds a contingency table from two categorical columns, computes the chi-square statistic of independence, and turns it into the phi coefficient and Cramer's V. It tests marginal and expected counts, handling of empty cells, and numeric rounding in PostgreSQL.
Compute phi and Cramer's V from a chi-square test of independence in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Table `crosstab_data` has one row per subject with categorical `row_cat` (`'A'`, `'B'`, `'C'`) and `col_cat` (`'X'`, `'Y'`, `'Z'`). Compute the chi-square statistic of independence, then
$$\phi = \sqrt{\frac{\chi^2}{n}}, \qquad V = \sqrt{\frac{\chi^2}{n \cdot (\min(\text{numRows}, \text{numCols}) - 1)}}$$
where n is the sample size and V is Cramer's V. Return one row with `phi` and `cramers_v`, each rounded to 4 decimals.
```hint Every cell counts
Check that every combination of a row category and a column category ends up in your chi-square sum, including combinations with no rows at all.
```
### Input Table(s)
Table `crosstab_data`:
| Column | Type | Meaning |
|---|---|---|
| `row_cat` | VARCHAR(5) | Row category of the subject: `'A'`, `'B'` or `'C'` |
| `col_cat` | VARCHAR(5) | Column category of the subject: `'X'`, `'Y'` or `'Z'` |
There is no key. Each row is one subject, and two identical rows are two different subjects.
### Output Contract
One row with these columns, in this order:
| Column | Type | Meaning |
|---|---|---|
| `phi` | NUMERIC | sqrt(chi_square / n), rounded to 4 decimal places |
| `cramers_v` | NUMERIC | sqrt(chi_square / (n * (min(numRows, numCols) - 1))), rounded to 4 decimal places |
The result always has exactly one row, so no ordering is needed.
### Example
Input `crosstab_data`:
| row_cat | col_cat |
|---|---|
| A | X |
| C | Y |
| C | Z |
| A | Z |
| C | Y |
| A | X |
| B | Y |
| B | Z |
| C | Y |
| A | Z |
| B | X |
| C | Z |
| B | Y |
| B | X |
| A | X |
| A | X |
| B | Z |
| C | Y |
Output:
| phi | cramers_v |
|---|---|
| 0.6667 | 0.4714 |
The 18 rows form this contingency table:
| | X | Y | Z | total |
|---|---|---|---|---|
| A | 4 | 0 | 2 | 6 |
| B | 2 | 2 | 2 | 6 |
| C | 0 | 4 | 2 | 6 |
| total | 6 | 6 | 6 | 18 |
Every expected count is 6 * 6 / 18 = 2. The four cells at 4 or 0 each contribute (2)^2 / 2 = 2 and the rest contribute 0, so chi-square is 8. Then phi = sqrt(8 / 18), about 0.6667, and with a 3 by 3 table V = sqrt(8 / 36), about 0.4714.
### Constraints and Clarifications
- `row_cat` and `col_cat` are never NULL.
- numRows and numCols are the numbers of distinct `row_cat` and `col_cat` values that appear in the table (3 and 3 in the example).
- A row and column combination with no subjects is an observed count of 0 and still enters the chi-square sum.
- Each column has at least 2 distinct values, so the denominator of Cramer's V is positive.
- Use the plain Pearson chi-square, with no continuity correction.
- Round only the final values, to 4 decimal places.
Overview: Write a SQL query that builds a contingency table from two categorical columns, computes the chi-square statistic of independence, and turns it into the phi coefficient and Cramer's V. It tests marginal and expected counts, handling of empty cells, and numeric rounding in PostgreSQL.
Table crosstab_data has one row per subject with categorical row_cat ('A', 'B', 'C') and col_cat ('X', 'Y', 'Z'). Compute the chi-square statistic of independence, then
ϕ=nχ2,V=n⋅(min(numRows,numCols)−1)χ2
where n is the sample size and V is Cramer's V. Return one row with phi and cramers_v, each rounded to 4 decimals.
Input Table(s)
Table crosstab_data:
Column
Type
Meaning
row_cat
VARCHAR(5)
Row category of the subject: 'A', 'B' or 'C'
col_cat
VARCHAR(5)
Column category of the subject: 'X', 'Y' or 'Z'
There is no key. Each row is one subject, and two identical rows are two different subjects.
The result always has exactly one row, so no ordering is needed.
Example
Input crosstab_data:
row_cat
col_cat
A
X
C
Y
C
Z
A
Z
C
Y
A
X
B
Y
B
Z
C
Y
A
Z
B
X
C
Z
B
Y
B
X
A
X
A
X
B
Z
C
Y
Output:
phi
cramers_v
0.6667
0.4714
The 18 rows form this contingency table:
X
Y
Z
total
A
4
0
2
6
B
2
2
2
6
C
0
4
2
6
total
6
6
6
18
Every expected count is 6 * 6 / 18 = 2. The four cells at 4 or 0 each contribute (2)^2 / 2 = 2 and the rest contribute 0, so chi-square is 8. Then phi = sqrt(8 / 18), about 0.6667, and with a 3 by 3 table V = sqrt(8 / 36), about 0.4714.
Constraints and Clarifications
row_cat
and
col_cat
are never NULL.
numRows and numCols are the numbers of distinct
row_cat
and
col_cat
values that appear in the table (3 and 3 in the example).
A row and column combination with no subjects is an observed count of 0 and still enters the chi-square sum.
Each column has at least 2 distinct values, so the denominator of Cramer's V is positive.
Use the plain Pearson chi-square, with no continuity correction.