# Tactiverge Academy — companion code for "Repeatability, trials and sample size"
# Lesson: https://tactiverge.win/academy/research-methodology/experimental-design/trials-and-sample-size/
# Licence: MIT (see https://tactiverge.win/open-engineering/). Examples appear in lesson order.

# ── Example 1 ────────────────────────────────────────
import statistics

# Made-up slip masses in grams, for illustration only.
slip_g = [212, 198, 225, 205, 190, 218, 230, 201, 209, 214]

n = len(slip_g)
mean = statistics.mean(slip_g)
sd = statistics.stdev(slip_g)   # sample standard deviation
se = sd / n ** 0.5              # standard error of the mean

print(f"n = {n}")
print(f"mean = {mean:.1f} g")
print(f"standard deviation = {sd:.1f} g")
print(f"standard error = {se:.1f} g")


# ── Example 2 ────────────────────────────────────────
import random

random.seed(1)
TRUE_RATE = 0.7   # pretend robot: succeeds 70% of the time

def run_trials(n):
    return sum(1 for _ in range(n) if random.random() < TRUE_RATE)

for n in (10, 50):
    results = [run_trials(n) / n for _ in range(1000)]
    print(f"{n} trials: observed rate ranged {min(results):.2f} to {max(results):.2f}")
