Compute Levene's test F statistic for equal group variances in SQL
Quick Overview
Write a PostgreSQL query that runs Levene's test for equal variances across groups, turning each score into its absolute deviation from the group mean and returning the ANOVA F statistic with its degrees of freedom. It tests chained window functions and careful handling of two different sets of group means.
Compute Levene's test F statistic for equal group variances in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `scores` table records a numeric `value` for subjects in three groups (`grp` = `'A'`, `'B'` or `'C'`). Run a Levene-style test of equal variances. First transform each observation to $z = |x - \bar{x}_g|$, the absolute distance of its `value` from its own group's mean. Then run a one-way ANOVA on z: compute the between-group sum of squares SSB and the within-group sum of squares SSW of the z values, and
$$F = \frac{SSB / (k - 1)}{SSW / (N - k)}$$
where $k$ is the number of groups and $N$ the number of rows. Return one row with `f_statistic` rounded to 4 decimal places, `df_between` = k - 1 and `df_within` = N - k.
For reference, with $n_g$ rows in group $g$, $\bar{z}_g$ the mean of z in group $g$ and $\bar{z}$ the mean of z over all rows:
$$SSB = \sum_g n_g (\bar{z}_g - \bar{z})^2, \qquad SSW = \sum_g \sum_{i \in g} (z_i - \bar{z}_g)^2$$
### Input Table(s)
`scores`
| Column | Type | Meaning |
|---|---|---|
| grp | VARCHAR(5) | Group label: 'A', 'B' or 'C' |
| value | DECIMAL(10,4) | The subject's measured value |
There is no primary key. Each row is one subject's observation, and two rows can be identical.
### Output Contract
One row with exactly these columns, in this order:
| Column | Type | Meaning |
|---|---|---|
| f_statistic | NUMERIC | The F statistic of the ANOVA on z, rounded to 4 decimal places |
| df_between | INTEGER | k - 1 |
| df_within | INTEGER | N - k |
The result always has exactly one row, so it needs no ordering.
### Example
Input `scores`:
| grp | value |
|---|---|
| B | 26.6 |
| C | 21.2 |
| A | 10.3 |
| A | 15 |
| A | 15.9 |
| B | 32.9 |
| C | 27.7 |
| C | 6.8 |
| A | 16.4 |
| B | 21.6 |
| C | 43.3 |
| B | 15.2 |
Output:
| f_statistic | df_between | df_within |
|---|---|---|
| 2.5249 | 2 | 9 |
The group means of `value` are 14.4 (A), 24.075 (B) and 24.75 (C). Group A's z values are 4.1, 0.6, 1.5 and 2.0, with mean 2.05; B's mean z is 5.675 and C's is 10.75, and the mean of all twelve z values is 6.15833. The ANOVA on z gives SSB = 152.78167 and SSW = 272.295, so F = (152.78167 / 2) / (272.295 / 9) = 2.52490. With 3 groups and 12 rows the degrees of freedom are 2 and 9.
### Constraints and Clarifications
- Neither column contains NULL.
- $k$ is the number of distinct `grp` values present, and $N$ counts every row.
- Center each value on the mean of its own group, not the grand mean or the median.
- Identical rows are separate subjects. Each one counts toward the group means, $n_g$, $N$ and both sums.
- Every input has at least two groups, more rows than groups, and a positive SSW for the z values, so F is defined.
- Round only the final F. The degrees of freedom are integers.
```hint Two different group means
After the transform you have a new column z, and the second step is an ordinary one-way ANOVA on it. The group means used to build z and the group means of z are different numbers; keep them apart.
```
Overview: Write a PostgreSQL query that runs Levene's test for equal variances across groups, turning each score into its absolute deviation from the group mean and returning the ANOVA F statistic with its degrees of freedom. It tests chained window functions and careful handling of two different sets of group means.
The scores table records a numeric value for subjects in three groups (grp = 'A', 'B' or 'C'). Run a Levene-style test of equal variances. First transform each observation to z=∣x−xˉg∣, the absolute distance of its value from its own group's mean. Then run a one-way ANOVA on z: compute the between-group sum of squares SSB and the within-group sum of squares SSW of the z values, and
F=SSW/(N−k)SSB/(k−1)
where k is the number of groups and N the number of rows. Return one row with f_statistic rounded to 4 decimal places, df_between = k - 1 and df_within = N - k.
For reference, with ng rows in group g, zˉg the mean of z in group g and zˉ the mean of z over all rows:
SSB=∑gng(zˉg−zˉ)2,SSW=∑g∑i∈g(zi−zˉg)2
Input Table(s)
scores
Column
Type
Meaning
grp
VARCHAR(5)
Group label: 'A', 'B' or 'C'
value
DECIMAL(10,4)
The subject's measured value
There is no primary key. Each row is one subject's observation, and two rows can be identical.
Output Contract
One row with exactly these columns, in this order:
Column
Type
Meaning
f_statistic
NUMERIC
The F statistic of the ANOVA on z, rounded to 4 decimal places
df_between
INTEGER
k - 1
df_within
INTEGER
N - k
The result always has exactly one row, so it needs no ordering.
Example
Input scores:
grp
value
B
26.6
C
21.2
A
10.3
A
15
A
15.9
B
32.9
C
27.7
C
6.8
A
16.4
B
21.6
C
43.3
B
15.2
Output:
f_statistic
df_between
df_within
2.5249
2
9
The group means of value are 14.4 (A), 24.075 (B) and 24.75 (C). Group A's z values are 4.1, 0.6, 1.5 and 2.0, with mean 2.05; B's mean z is 5.675 and C's is 10.75, and the mean of all twelve z values is 6.15833. The ANOVA on z gives SSB = 152.78167 and SSW = 272.295, so F = (152.78167 / 2) / (272.295 / 9) = 2.52490. With 3 groups and 12 rows the degrees of freedom are 2 and 9.
Constraints and Clarifications
Neither column contains NULL.
k
is the number of distinct
grp
values present, and
N
counts every row.
Center each value on the mean of its own group, not the grand mean or the median.
Identical rows are separate subjects. Each one counts toward the group means,
ng
,
N
and both sums.
Every input has at least two groups, more rows than groups, and a positive SSW for the z values, so F is defined.
Round only the final F. The degrees of freedom are integers.