You have a table of counts — say rows for "saw the ad" / "didn't" and columns for "bought" / "didn't buy". The question is whether the two things are related, or whether the pattern is what you'd expect from pure chance.
The chi-square test answers it by comparing what you observed against what you'd expect if the rows and columns had nothing to do with each other.
For the cell in row i, column j:
Then add up, over every cell:
Task: write chi2_statistic(table) returning [chi2, dof] — the statistic rounded to 4 decimal places, and the degrees of freedom as a plain integer:
table is a list of rows of counts, at least 2×2, and every row has the same length.A big statistic means observation and expectation disagree, so the two variables probably are related.