# Tactiverge Academy — companion code for "Recognising p-hacking, cherry-picking and the garden of forking paths"
# Lesson: https://tactiverge.win/academy/research-methodology/scientific-integrity/p-hacking-and-cherry-picking/
# Licence: MIT (see https://tactiverge.win/open-engineering/). Examples appear in lesson order.

# ── Example 1 ────────────────────────────────────────
import random

random.seed(1)
STUDIES = 10000
MEASURES = 20       # number of things checked per study
FALSE_ALARM = 0.05  # chance one check looks "significant" by luck alone

lucky = 0
for _ in range(STUDIES):
    hits = sum(1 for _ in range(MEASURES) if random.random() < FALSE_ALARM)
    if hits >= 1:
        lucky += 1

print(f"Studies with at least one 'significant' result: {lucky / STUDIES:.1%}")
print(f"Exact value: {1 - (1 - FALSE_ALARM) ** MEASURES:.1%}")
