Skip to course content
Free Python statistics course

Statistical Data Analytics with Python

Unit 05.03: The direction is part of the hypothesis

A one-sided test halves your p-value, which is exactly why it must be chosen before you see the data.

The direction is part of the hypothesis

A two-sided test asks whether the parameter differs from the null value in either direction. A one-sided test asks about one direction only and concentrates the whole rejection region there — which is why the p-value is half.

That halving is legitimate when the direction was specified in advance on substantive grounds, and it is a way of manufacturing significance when the direction is chosen after seeing which way the mean fell. Doing that makes the real false-positive rate 10%, not 5%.

A one-sided test also gives up the ability to detect an effect the other way. If the true effect is opposite to your hypothesis, the test reports a p-value near one and you conclude nothing.

This block runs all three forms on the same sample.

import numpy as np
from scipy import stats

rng = np.random.default_rng(2041)
d = rng.normal(106, 12, 30)

two = stats.ttest_1samp(d, popmean=100)
greater = stats.ttest_1samp(d, popmean=100, alternative="greater")
less = stats.ttest_1samp(d, popmean=100, alternative="less")

print(f"Sample mean: {d.mean():.2f}\n")
print(f"two-sided         p = {two.pvalue:.4f}")
print(f"one-sided greater p = {greater.pvalue:.4f}")
print(f"one-sided less    p = {less.pvalue:.4f}")
print()
print("The one-sided p in the observed direction is exactly half the two-sided:")
print(f"  two-sided / 2 = {two.pvalue / 2:.4f}")
print(f"  greater       = {greater.pvalue:.4f}")
print()
print("That halving is why the direction must be chosen BEFORE the data.")
print("Choosing it after seeing which way the mean fell converts a 5%")
print("false-positive rate into 10%.")
print("A one-sided test also cannot detect an effect the other way: had the")
print(f"true effect been negative, 'greater' would report p near"
      f" {1 - greater.pvalue:.2f}")

The sample mean is 108.36. Two-sided gives p = 0.0053; one-sided 'greater' gives 0.0027, exactly half; one-sided 'less' gives 0.9973. The arithmetic is confirmed directly — two-sided / 2 equals the 'greater' value. And the cost is in the last line: had the true effect been negative, the 'greater' test would report p near 1.00 and find nothing at all.

The mistake this prevents

The mistake is switching to alternative='greater' when the two-sided p comes out at 0.07. It always works, and it is the clearest possible case of letting the data choose the hypothesis.

Takeaway

Default to two-sided. Use one-sided only when the direction was pre-specified for a substantive reason, record it in the analysis plan, and accept that you cannot then report an effect the other way.