Contents/ Part XXVII · Capstone Projects: Statistical Testing/ Chapter 168

Is Web Traffic Really Spread Evenly?

Capstone 9. One categorical variable against an expected distribution. The arithmetic is the easiest in the part; the judgment is the hardest, because the expected distribution is not handed to you by the data. It is a claim you supply, and the test can only answer the question you actually asked.

⏱️ ~16 min read
🧪 Chi-square goodness-of-fit
📊 Chapter 168

The previous capstone had two categorical variables and asked whether they were related. This one has only one, and asks whether it matches a distribution somebody claimed. That sounds simpler, and mechanically it is. But it hides a trap that the two-variable test does not have: you choose the thing you are testing against, and that choice can manufacture a finding out of nothing.

The brief
Setting
A company has one quarter of website traffic totaled by day of week, seven counts in all.
The question
Is traffic really spread evenly across the week, as marketing has asserted?
Why it matters
Staffing, ad scheduling and release timing are all built on the assumption of an even week. If the assumption is wrong, all three are mistimed.
What we do
Run a chi-square goodness-of-fit test against the uniform claim, then ask the harder question of whether uniform was ever the right benchmark, and finish by estimating which days differ and by how much.
The chi-square goodness-of-fit test compares the observed counts of one categorical variable against an expected distribution that you specify. Degrees of freedom are k − 1, every expected count should be about 5 or more, and the effect size is Cohen's w.
📈
The finding, up front

Traffic is emphatically not uniform across the week (χ²(6) = 78.3, p ≈ 8 × 10−15), with weekends far below a flat line. But tested against last year's day-of-week pattern the very same numbers give p = 0.999. Same data, opposite verdicts. The expected distribution is the hypothesis.

1

One Variable, One Claim

A company has a quarter of website traffic totaled by day of week, and someone in marketing has asserted that traffic is spread evenly across the week. The data are seven counts, and the assertion is a distribution.

Two chi-square tests, two different questions GOODNESS-OF-FIT (this chapter) ONE variable, k categories an expected shape YOU supply this df = k − 1 INDEPENDENCE (Capstone 8) TWO variables, cross-tabulated expected computed FROM the table's own margins · df = (r−1)(c−1)

That difference is the whole point. In a test of independence the expected counts come from the table itself. In a goodness-of-fit test you bring the expected distribution with you, which means you are testing your own assumption as much as the data.

2

The Obvious Test, and Its Obvious Answer

With 3,514 visits over seven days, a uniform claim expects about 502 per day. Every expected count is far above the minimum of 5, so the approximation is safe.

A bar chart of website visits for each day of the week, with a dashed red line at the uniform expectation of about 502 visits. Monday through Friday bars sit above the line, with Wednesday highest at 578, while Saturday at 406 and Sunday at 373 fall well below it.
Traffic by day against a flat expectation. Weekdays cluster above the line; both weekend days fall clearly below it. This is a systematic split, not scatter.
DayObservedExpected (uniform)Standardized residual
Monday525502+1.03
Tuesday569502+2.99
Wednesday578502+3.39
Thursday553502+2.28
Friday510502+0.36
Saturday406502−4.28
Sunday373502−5.76
Chi-square
78.3
on 6 degrees of freedom
p-value
≈ 8 × 10−15
reject uniform
Cohen's w
0.15
a small effect
Total visits
3,514
across 7 days

Note the tension already visible in those four numbers. The p-value is astronomically small, and the effect size is small. With 3,514 visits the test can detect a modest departure easily, so the p-value is reporting the sample size as much as the size of the pattern.

3

But Was Uniform Ever the Right Claim?

Here is the trap. We rejected "traffic is uniform", but nobody who runs a website expects uniform traffic. Weekends are quieter for most business sites. Rejecting a claim no one believed is a technically valid result that carries no information, and it is one of the easiest ways to manufacture a finding.

So test a claim someone would actually make: that this quarter matches last year's day-of-week shares, which already build in the weekend dip.

Two side-by-side panels using the identical observed traffic bars. On the left, observed against a flat uniform expectation, where the bars diverge visibly and the p-value is 8 times ten to the minus fifteen, rejecting the claim. On the right, observed against last year's day-of-week pattern, where the bars sit almost exactly on the expectation and the p-value is 0.999, not rejecting.
The identical observed bars, tested twice. Against a uniform claim (left) the data look dramatically irregular. Against last year's pattern (right) they look utterly ordinary. Nothing about the data changed between the panels, only the expected distribution.
Expected modelχ²(6)pCohen's wVerdict
Uniform (one seventh per day)78.328 × 10−150.149reject
Last year's day-of-week shares0.410.9990.011do not reject
The lesson of this capstone

A goodness-of-fit test does not tell you whether data are "normal" or "fine". It tells you whether they match the specific distribution you handed it. A rejection means the data disagree with that model, and the value of the result depends entirely on whether the model was worth testing. State and justify the expected distribution before running the test, or the test becomes a formality wrapped around a foregone conclusion.

4

The Verdict

Both statements below are true, and only one is worth putting in a report:

The honest one-line summary is that traffic is uneven across the week in exactly the way it always has been. Weekdays run about a fifth above the weekend, Sunday is the quietest day, and nothing this quarter departs from precedent.

5

Ethics, Bias, and Limits

6

Goodness-of-Fit in Data Science & AI

Comparing an observed distribution against an expected one is a routine production task, usually under a different name.

Where it appearsThe expected distribution
Data-drift monitoringThis week's category mix against the training-set mix.
A/B assignment checksObserved split across arms against the intended 50/50 or 33/33/33.
Survey weightingSample demographics against known census shares, before raking.
Fraud and anomaly screensObserved leading digits against Benford's law.
Practice note

Drift monitors are where this test most often goes wrong in production. At scale every distribution differs from its reference, so a chi-square alarm fires constantly and gets ignored, which is worse than no alarm. The fix is the discipline from this chapter: choose a reference distribution that reflects genuine expectation, alert on an effect size threshold rather than a p-value, and pre-commit to how large a shift has to be before anyone should care.

7

Estimate: Which Days, and by How Much

Two chi-square tests gave two verdicts. Neither said how far any particular day sits from its expectation, and that is the only form in which this result reaches a planning meeting. A Wilson interval on each day's share does the job the omnibus test cannot.

DayVisitsShare95% CI (Wilson)Against an even split (14.29%)
Monday52514.94%13.80% to 16.16%contains 14.29%
Tuesday56916.19%15.01% to 17.45%above
Wednesday57816.45%15.26% to 17.71%above
Thursday55315.74%14.57% to 16.98%above
Friday51014.51%13.39% to 15.72%contains 14.29%
Saturday40611.55%10.54% to 12.65%below
Sunday37310.61%9.64% to 11.68%below
The weekday story is really a midweek story

Only three days sit entirely above an even share, and two sit entirely below. Monday and Friday are on the fence: both intervals contain 14.29 percent, so the two ends of the working week cannot be distinguished from an even split this quarter. The omnibus test could never have told us that, because it collapses seven deviations into one number. If capacity planning is the goal, this table is the output and the p-value is not.

EACH DAY'S SHARE, WITH ITS 95% INTERVAL an even split: 14.29% 10% 12% 16% 18% Monday Tuesday Wednesday Thursday Friday Saturday Sunday
Green clears the line, red falls short, gray straddles it. Reading intervals against a reference value is how a goodness-of-fit result becomes actionable. The single chi-square statistic says only that the seven bars are not all centered on the dashed line.

The effect size gets an interval too. Cohen's w against the uniform expectation is 0.149, with a 95 percent interval of roughly 0.12 to 0.19. The whole of that range sits in "small" territory. This matters for how the result is sold: traffic is not evenly spread and the departure is real, but it is not dramatic. A p-value of 8 × 10−15 describes how confident we are that the pattern is not chance, and it is routinely misread as describing how big the pattern is.

🐍

The full project, step by step

The companion notebook runs the framework end to end on aggregated data: it validates the seven categories, checks the expected counts, runs the goodness-of-fit test against the uniform claim with scipy, computes Cohen's w and the standardized residuals, then re-runs the identical test against last year's shares and plots the two comparisons side by side. Every number here comes from its output, with a plain-language note after each result.

📓 View Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub
Read the reports & get the data

The dataset (capstone-website-visits-by-weekday.xlsx) holds the seven daily totals on the visits sheet and last year's shares on the PriorYear sheet, so you can run both tests yourself. Two written reports accompany it: a plain-language brief for a marketing lead, and a technical report with both models, residuals, effect sizes, and references.

🎓 Key Takeaways

  • Goodness-of-fit tests one variable against an expected distribution, with df = k − 1. Independence tests two variables and derives its expectation from the table.
  • You supply the expected distribution, so it is a hypothesis, not a fact. Declare and justify it before testing.
  • Same data, opposite verdicts: uniform gave χ²(6) = 78.3, p ≈ 8 × 10−15; last year's shares gave χ²(6) = 0.41, p = 0.999.
  • Report the effect size: Cohen's w of 0.15 is small, and the vanishing p-value mostly reflects 3,514 visits.
  • Rejecting a claim nobody held is not a finding. "Traffic is uneven" was true and useless; "traffic matches last year" was the informative answer.
8

Quiz: Test Yourself

Eight questions on this capstone, from degrees of freedom to choosing the null. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved.