rolls has one row per throw of a six-sided die (face 1-6). expected_dist maps each face to a weight, and the expected proportion for a face is its weight divided by the sum of all weights. Return one row with the chi-square goodness-of-fit statistic and a verdict on whether the die is fair.
-
chi_square
= SUM((observed - expected)^2 / expected) over the six faces, where expected = total rolls * proportion. Round it to 4 decimals.
-
A face with zero observed rolls still contributes to the sum, so the counts must be joined from
expected_dist
with a
LEFT JOIN
.
-
verdict
is
'loaded'
when
chi_square
exceeds 11.07 (the critical value for 5 degrees of freedom), otherwise
'fair'
.
Input Table(s)
Table rolls:
| Column | Type | Meaning |
|---|
| face | INT | The face shown on one throw, 1 to 6 |
No key. Each row is one throw, so the same face appears many times.
Table expected_dist:
| Column | Type | Meaning |
|---|
| face | INT | A die face, 1 to 6 (primary key) |
| weight | INT | Relative weight of that face |
Output Contract
-
Exactly one row with columns
chi_square
and
verdict
, in that order.
-
chi_square
is the statistic summed over all six faces in
expected_dist
, rounded to 4 decimals.
-
verdict
is
'loaded'
when the unrounded statistic is strictly greater than 11.07, otherwise
'fair'
.
Example
Input:
rolls
+------+
| face |
+------+
| 4 |
| 2 |
| 5 |
| 4 |
| 4 |
| 4 |
| 1 |
| 2 |
| 3 |
| 2 |
| 3 |
| 6 |
| 5 |
| 5 |
| 2 |
| 3 |
| 2 |
| 6 |
| 5 |
| 1 |
| 4 |
+------+
expected_dist
+------+--------+
| face | weight |
+------+--------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+------+--------+
Expected output:
+------------+---------+
| chi_square | verdict |
+------------+---------+
| 2.7143 | fair |
+------------+---------+
There are 21 rolls and equal weights, so each face expects 3.5. The observed counts for faces 1 to 6 are 2, 5, 3, 5, 4 and 2. The squared gaps add up to 9.5, and 9.5 / 3.5 = 2.7143, which is below 11.07.
Constraints and Clarifications
-
rolls
has at least one row, and every
face
in it is between 1 and 6. Neither table has NULLs.
-
expected_dist
has exactly six rows, one per face 1 to 6, and every weight is a positive integer. The weights do not have to sum to anything in particular.
-
A face that never appears in
rolls
has an observed count of 0.