Statistics · hard
Asked in Data Scientist interviews, in the Statistics round.
Pre-register hypotheses, correct for multiple comparisons, fix sample size in advance.
P-hacking is any practice that lets you keep testing until something clears 0.05: trying many metrics and reporting the one that worked, slicing by segment until one segment is significant, stopping the test the day it crosses the line, or adjusting the model until p drops. Each is a way of running many tests while reporting one, and at 20 tests you expect one false positive for free.
Avoiding it is mostly about deciding things before seeing data:
from statsmodels.stats.multitest import multipletests
reject, p_adj, *_ = multipletests(p_values, alpha=0.05, method="fdr_bh")
And say the uncomfortable part: the pressure to p-hack is organisational. A team whose tests "have to win" will find wins. The fix is reporting the negatives as routinely as the positives.