Hypothesis Testing Framework


Hypothesis Testing Framework

Simon
Alright Michael, I've got a bold claim for you. In twenty minutes, we can make hypothesis testing click forever. And I mean the core algorithm that powers every AB test, clinical trial, and quantitative trading strategy.

Michael
That's a big promise, Simon. But I've heard about p-values and all that jargon, and it always felt like a maze. Where do we even start?

Simon
We start with the language. It's actually pretty simple. First, you've got H0, the null hypothesis—the boring status quo. Then H1, the alternative—the exciting new theory you want to prove. Next is alpha, your BS threshold, usually 0.05. And finally, the p-value: the probability of seeing your data if the null is true.

Michael
Okay, so that's the vocabulary. But how do these pieces fit together? Is there like a step-by-step process?

Simon
Exactly! It's a universal algorithm. Four steps. Step one: formulate your hypotheses (H0 vs H1). Step two: calibrate, set your alpha. Step three: compute a test statistic. Step four: decide—compare p-value to alpha. If p < alpha, reject H0. That's the whole game.

Michael
So the p-value is the key. But I need an intuitive example. How do we actually see this in action?

Simon
Great question. Let's use the classic: a potentially rigged coin. Your friend claims the coin is fair, but you flip it 10 times and get 9 heads. Your internal BS detector is screaming. So we follow the steps. Step one: H0 says the coin is fair (p=0.5), H1 says it's rigged. Step two: set alpha at 0.05. Step three: compute the probability of getting 9 or more heads under the fair assumption.

Michael
And how do we compute that? It feels like we need to sum probabilities, right?

Simon
Exactly. The chance of exactly 10 heads is 0.5^10, about 0.001. For 9 heads, it's 10 times that, 0.0098. Sum them up, and you get a p-value of about 0.0107. That's 1.07%. Step four: is 1.07% less than our 5% alpha? Yes! So we reject H0—your friend is cheating.

Michael
I see! So the p-value is the probability of getting a result this extreme if the null is true. And when it's small enough, we call BS. Now, in practice, we don't compute these by hand. How would we code that?

Simon
You'd use Python. With scipy.stats, you run `binom_test` with k=9, n=10, p=0.5, and alternative='greater'. It prints the same p-value: 0.0107. The logic is identical, just automated.

Michael
Perfect. But that was for discrete data like coin flips. What about continuous data, like revenue or time on site? How do we handle that?

Simon
Enter the Z test. Imagine a potato chip factory targeting 100g per bag, with a known standard deviation of 5g. You sample 50 bags and get an average of 102g. Is the machine broken? The key insight is the central limit theorem: even if individual bag weights are weird, the average of 50 bags follows a normal distribution. So we can use a simple formula: Z = (sample mean - target) / (sigma / sqrt(n)).

Michael
Signal over noise—I like that. So the signal is the deviation from the target, and the noise is the standard error. Let's plug in: (102 - 100) / (5 / sqrt(50)) = 2 / 0.707 ≈ 2.83. That's our Z-score. Then we convert to a p-value?

Simon
Right. A Z-score of 2.83 corresponds to a p-value of about 0.0047. That's less than our alpha of 0.05, so we reject H0—the machine is broken. In Python, you'd use `norm.cdf` with that Z, times two for a two-tailed test, and you get the same p-value.

Michael
But wait—there's a catch. In the chip factory, we knew sigma, the population standard deviation. In real AB tests, you don't know that. So the Z test isn't always applicable, is it?

Simon
Exactly right. That's where the t test comes in—the workhorse of AB testing. When you don't know the population sigma, you estimate it from your sample, which adds uncertainty. The t test accounts for that with a slightly different distribution. The formula is still signal over noise, but now the noise uses the sample standard deviation s instead of sigma.

Michael
So imagine testing a new button to increase time on site. You have 10 users in each group. Group A (old button) averages 45 seconds with s=15, Group B (new button) averages 55 seconds with s=15. The signal is 10 seconds. What's the noise?

Simon
We combine the uncertainties: noise = sqrt( (15^2/10) + (15^2/10) ) = sqrt(22.5 + 22.5) = sqrt(45) ≈ 6.71. So the t-statistic is 10 / 6.71 ≈ 1.49. That yields a p-value around 0.15. That's above 0.05, so we fail to reject H0. The 10-second improvement could easily be random noise.

Michael
So small samples create huge noise. To win, you either need a huge signal or—more likely—more data. That makes sense. In code, you'd just use `scipy.stats.ttest_ind` on the two lists, and it gives you t and p.

Simon
Precisely. And that's how you handle continuous data. But what about categorical data, like conversion rates—did they click or not? You can't average a yes/no. That's where the chi-square test comes in.

Michael
Ah, I've seen that. So for a red button versus blue button test, you have observed counts. How does chi-square work?

Simon
It's a different mental model: observed versus expected. The null hypothesis says the button color doesn't affect conversion. You build a table of what you observed and what you would expect under the null. For example, with 200 total visitors and 30 sales overall, the expected conversion is 15%. So you'd expect 15 sales in each group if color didn't matter.

Michael
So for the blue group, you observed 10 sales but expected 15. For red, observed 20, expected 15. That's a gap. How do we quantify that gap?

Simon
We use the chi-square statistic: sum over all cells of (O - E)^2 / E. For the buyers in blue: (10-15)^2/15 = 1.67. Similarly for red buyers: 1.67. Add in the non-buyers and you get a total chi-square of about 3.92. That converts to a p-value of about 0.0477, just below 0.05. So we reject H0—the red button is better.

Michael
Barely significant though. And what's scary is that if one fewer person in the red group had bought, you'd get 19 sales instead of 20, and the p-value would jump to 0.076, making it insignificant. One person can flip the whole test. That's why categorical data needs huge sample sizes.

Simon
Exactly. In code, you'd use `chi2_contingency` from scipy with a 2x2 table of the counts. Now, once you've seen Z, t, and chi-square, you realize the algorithm never changes. Only the formula in step three changes. That's the secret.

Michael
So we need a decision matrix. For continuous data, unknown sigma? Use t test. For continuous data, known sigma? Use Z test. For categorical data? Use chi-square. That's the playbook.

Simon
But knowing the tools isn't enough. You have to avoid the traps. First: p-hacking. If you peek at your p-value every hour and stop as soon as it drops below 0.05, you're cheating. P-values bounce around early on. You must pre-commit to a sample size, run the test, and only check at the end.

Michael
And the second trap—I've heard this called the Google problem. When your sample size is enormous, even a tiny effect becomes statistically significant. A 0.001-second speed improvement with millions of users will give a p-value of practically zero. But is it practically significant?

Simon
That's exactly it. Statistical significance says an effect is real; practical significance says it matters. You always need to check both: the p-value for certainty, and the effect size for magnitude. A tiny p-value doesn't mean a big effect—it means we're very sure a small effect exists.

Michael
So the rule for production decisions: look at p-value and effect size together. That's the key takeaway. I think this twenty-minute journey really did make it click. We've covered the universal four-step algorithm, the specific tests, and the pitfalls.

Simon
Exactly. And remember: hypothesis testing isn't about being a human calculator. It's about making sound decisions under uncertainty. You now have the framework to do that.

Ask anything
We've read this whole study set, so questions about it get the best answers.