Welch-Satterthwaite Degrees of Freedom

Quick Overview

Write a PostgreSQL query that computes the Welch-Satterthwaite degrees of freedom for two samples with unequal variances. It tests conditional aggregation, sample variance and translating a statistics formula into exact SQL arithmetic.

Welch-Satterthwaite Degrees of Freedom

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `measurements` holds two samples with possibly unequal variances: `group_name` is `'control'` (group 1) or `'treatment'` (group 2), and `value` is the observation. Compute the Welch-Satterthwaite degrees of freedom $$ df = \frac{\left( s_1^2/n_1 + s_2^2/n_2 \right)^2}{\dfrac{(s_1^2/n_1)^2}{n_1 - 1} + \dfrac{(s_2^2/n_2)^2}{n_2 - 1}} $$ where $s_1^2$ and $s_2^2$ are the sample variances and $n_1$, $n_2$ are the group sizes. Return one row with `df` rounded to 4 decimals. ```hint Reduce it to two numbers Each group enters the formula only through its sample variance divided by its count. Get those two quantities first and the rest is arithmetic. ``` ### Input Table(s) Table `measurements`: | Column | Type | Meaning | |---|---|---| | group_name | VARCHAR(20) | `'control'` (group 1) or `'treatment'` (group 2) | | value | DECIMAL(10,4) | The measured outcome for one observation | The table has no primary key. Each row is one observation, and identical rows are separate observations. ### Output Contract Exactly one row with one column: | Column | Type | Meaning | |---|---|---| | df | numeric | The Welch-Satterthwaite degrees of freedom, rounded to 4 decimal places | The result always has one row, so no ordering is needed. Keep the fractional value; do not floor it to an integer. ### Example ```text Input: measurements +------------+-------+ | group_name | value | +------------+-------+ | control | 6.3 | | treatment | 10.5 | | control | 6.6 | | control | 5.4 | | treatment | 8.1 | | treatment | 8.9 | | control | 4.6 | | treatment | 12.5 | | treatment | 11.8 | +------------+-------+ Output: +--------+ | df | +--------+ | 6.0137 | +--------+ ``` Control has 4 rows with sample variance 0.8225, so $s_1^2/n_1 = 0.205625$. Treatment has 5 rows with sample variance 3.478, so $s_2^2/n_2 = 0.6956$. The numerator is ${0.901225^2 \approx 0.812207}$, the denominator is ${0.014094 + 0.120965 = 0.135059}$, and the ratio is about 6.0137. ### Constraints and Clarifications - `group_name` only takes the values `'control'` and `'treatment'`. - `value` is never NULL. - Each group has at least two rows, and at least one group has a nonzero variance, so the formula is defined. - Sample variance uses the $n - 1$ denominator. - Duplicate rows all count as observations. - Round only the final result, not the intermediate terms.

Overview: Write a PostgreSQL query that computes the Welch-Satterthwaite degrees of freedom for two samples with unequal variances. It tests conditional aggregation, sample variance and translating a statistics formula into exact SQL arithmetic.

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

Table measurements holds two samples with possibly unequal variances: group_name is 'control' (group 1) or 'treatment' (group 2), and value is the observation. Compute the Welch-Satterthwaite degrees of freedom

df=(s12/n1+s22/n2)2(s12/n1)2n1−1+(s22/n2)2n2−1df = \frac{\left( s_1^2/n_1 + s_2^2/n_2 \right)^2}{\dfrac{(s_1^2/n_1)^2}{n_1 - 1} + \dfrac{(s_2^2/n_2)^2}{n_2 - 1}}

where s12s_1^2 and s22s_2^2 are the sample variances and n1n_1, n2n_2 are the group sizes. Return one row with df rounded to 4 decimals.

Input Table(s)

Table measurements:

ColumnTypeMeaning
group_nameVARCHAR(20)'control' (group 1) or 'treatment' (group 2)
valueDECIMAL(10,4)The measured outcome for one observation

The table has no primary key. Each row is one observation, and identical rows are separate observations.

Output Contract

Exactly one row with one column:

ColumnTypeMeaning
dfnumericThe Welch-Satterthwaite degrees of freedom, rounded to 4 decimal places

The result always has one row, so no ordering is needed. Keep the fractional value; do not floor it to an integer.

Example

Input: measurements
+------------+-------+
| group_name | value |
+------------+-------+
| control    | 6.3   |
| treatment  | 10.5  |
| control    | 6.6   |
| control    | 5.4   |
| treatment  | 8.1   |
| treatment  | 8.9   |
| control    | 4.6   |
| treatment  | 12.5  |
| treatment  | 11.8  |
+------------+-------+

Output:
+--------+
| df     |
+--------+
| 6.0137 |
+--------+

Control has 4 rows with sample variance 0.8225, so s12/n1=0.205625s_1^2/n_1 = 0.205625. Treatment has 5 rows with sample variance 3.478, so s22/n2=0.6956s_2^2/n_2 = 0.6956. The numerator is 0.9012252≈0.812207{0.901225^2 \approx 0.812207}, the denominator is 0.014094+0.120965=0.135059{0.014094 + 0.120965 = 0.135059}, and the ratio is about 6.0137.

Constraints and Clarifications

  • group_name only takes the values 'control' and 'treatment' .
  • value is never NULL.
  • Each group has at least two rows, and at least one group has a nonzero variance, so the formula is defined.
  • Sample variance uses the n−1n - 1 denominator.
  • Duplicate rows all count as observations.
  • Round only the final result, not the intermediate terms.