Paul's Python CoursePPC

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.

About an hour and a half, in 15 steps.

Sign up free to open itLog in

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 of after minus the mean of before.
  • shuffled_differences(before, after, rng, shuffles=10_000): an array of shuffles differences. For each, put all the readings together, shuffle them with rng.permutation, deal the first len(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.

What it prints
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

  1. Dice you can roll again
  2. The same dice twice
  3. How often, over many tries
  4. One or two
  5. If nothing were changing
  6. Could luck have done it?
  7. At least as big
  8. Rising and falling together
  9. The sun's height
  10. Together isn't because
  11. The Clocktower is warming the city
  12. How Paul would do it
  13. Make the station's answer
  14. Ship it