Project 9 of 12, for Yahya
Statistics Toolkit
A statistics toolkit for Yahya's bet that it's warmer outside the Clocktower than it used to be: a permutation test that says how often luck alone could have made the difference, and a correlation that isn't a cause.
Act 5 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- YahyaI bet Paul it's warmer now than when the station book started, and he says I have to prove it isn't luck.
- YahyaThe last thirty years are 0.86 of a degree up on the first thirty. Could luck do that?
- PaulFind out how often luck does it, then.
- YahyaFine, but Paul says it's drier now, and he can have the same question.
A statistics toolkit: four functions that work on any two groups of numbers, and a report at the bottom that uses them. station.csv is the station book: one row per month from 1925 to 2024, with year, month, temp (degrees C), rain (mm) and sun (hours).
Write four functions. before and after are arrays or Series of readings; leave them as they were.
difference(before, after): the mean ofafterminus the mean ofbefore.shuffled_differences(before, after, rng, shuffles=10_000): an array ofshufflesdifferences. For each, put all the readings together, shuffle them withrng.permutation, deal the firstlen(before)into one group and the rest into the other, and take the difference of their means, the second group's minus the first's.p_value(before, after, rng, shuffles=10_000): the share of shuffled differences at least as big as the real one.correlation(x, y): the correlation r between two equally long sets of numbers, from -1 to 1, worked out from the formula.
The report compares the first thirty years with the last thirty, then correlates the temperature with the year and with the Clocktower's age: it was built in 1890.
First thirty years: 11.06 degrees C. Last thirty: 11.92. Warmer by 0.86 degrees C. Shuffled 10,000 times, chance did as well 0 times. Drier by 35 mm a year. Shuffled 10,000 times, chance did as well 1,090 times. Year and temperature: r = 0.63 The Clocktower's age and temperature: r = 0.63
What you learn
What's in it
- Dice you can roll again
- The same dice twice
- How often, over many tries
- One or two
- If nothing were changing
- Could luck have done it?
- At least as big
- Rising and falling together
- The sun's height
- Together isn't because
- The Clocktower is warming the city
- How Paul would do it
- Make the station's answer
- Ship it