Wolfram Blog
Jon McLoone

Is the Weather Biased?

July 29, 2010 — Jon McLoone, International Business & Strategic Development

My mother has a theory: “The nicest weather is when you are at work, and then it rains on the weekend.” Hearing this from her once again, I think it is time to expose her theory to the facts and prove her wrong.

We’ll start by setting up some tools to help retrieve and categorize the data in terms of the type of day. In the United Kingdom, the weekend is Saturday and Sunday.

WeekendQ[date_List] := MatchQ[DateString[date, "DayName"], "Saturday" | "Sunday"]

Now we can take the available weather data and split it into weekend values and weekday values, and discard dates where no value was recorded…

Splitting the weather data into weekend and weekday categories with actual values

Finally, we need to average the data and present it clearly. We’ll do this as a separate step, as we’ll also want to access the raw data for further analysis.

AverageReport[data_] := Grid[{{"Weekend", "Weekday"}, Mean /@ data}, Frame -> All, ItemStyle -> {Automatic, {"Subsubsection"}}]

OK, now I am ready to start. In the UK, the definition of “nice” weather is simple: the dryer the better and the hotter the better. Let’s look at the rain first…

AverageReport[SplitWeatherData["Oxford", "TotalPrecipitation"]]

Average rainfall: 0.389826mm on weekend days, 0.373604mm on weekdays

Oh dear, she might be on to something. There has been on average nearly 0.02mm more rain on weekend days. Before conceding defeat, we have to know whether that is a significant result. Fortunately, Mathematica has all the statistics we need to test this.

Needs["HypothesisTesting`"]

If you take two sample sets from the same data then they are unlikely to have exactly the same mean. The question is, how likely is it that the means would be as different as these are purely because of the randomness of sampling? If the difference is very unlikely (the standard test is 5% of the time) then we conclude that it is significantly likely that the difference is because there is another cause (i.e., that they are being sampled from different data that actually has different means).

My hypothesis is that the weather has no bias for weekends, and therefore both sets of data are from the same data. So we give the test parameter for the difference in means as zero, and say the variances of both populations is equal and it is a TwoSided test, as the hypothesis is symmetrical.

We’ll create a shortcut function for this as we will do this test several times.

Shortcut function for significance test

Shortcut function for significance test

Phew! This tells us that the difference isn’t significant. Randomly dividing the data into two sets will produce this much difference in mean about 20% of the time.

What about temperature?

AverageReport[SplitWeatherData["Oxford", "MeanTemperature"]]

Average weekend temperature 9.94533°C, average weekday temperature 9.92313°C

The average weekend temperature is 0.03°C higher—the opposite of her prediction—but again, let’s see if that is significant…

SignificanceTest[SplitWeatherData["Oxford", "MeanTemperature"]]

{TwoSidedPValue -> 0.810665, Fail to reject null hypothesis at significance level -> 0.05}

Not surprisingly, this isn’t significant either. But “niceness” is a combination of both values, and we don’t like it too windy either. Let’s factor in windiness; perhaps there is some complexity about combining the three data sets.

The weighting between the three values is somewhat subjective, but here is my formula. You will have to come up with your own to suit your climate and tastes.

Niceness[temperature_?NumberQ, precipitation_?NumberQ, wind_?NumberQ] := temperature - 10*precipitation - wind/2; Niceness[___, _Missing, ___] := Missing[];

Unfortunately, the weather stations that Mathematica is querying sometimes have missing data points when individual instruments or weather stations have been offline. We have to collect the data by day, reject any days where any of the values is missing, apply the “niceness” metric, and categorize as weekend or not. This is the kind of complicated data analysis that is made easy by Mathematica.

We are retrieving three data sets with around 20,000 points in each. My first naive approach was to search each set nearly 20,000 times. This was a bit slow, so instead we’ll tag the data with the type of data that it is, throw it all in together, and let the built-in Mathematica command GatherBy match up the dates. As a rule of thumb, if there is a built-in command close to what you want, try to use it rather than replicating it with something similar. This version takes a couple of seconds.

Tagging the data and letting Mathematica sort it out

AverageReport[NicenessComparisonData["Oxford"]]

Weekend niceness value 1.61292, weekday niceness value 1.58002

SignificanceTest[NicenessComparisonData["Oxford"]]

{TwoSidedPValue -> 0.80175, Fail to reject null hypothesis at significance level -> 0.05}

Not significant again.

At this point, I present the results. Peer review does not go well. My mother alleges selective choice of Oxford, a place where she has never lived.

I sense that it is time to change the goal of my research. Where is her theory MOST true? Let’s scan the largest 68 cities in the UK to see if it applies somewhere…

cityNicenessData = Sort[Map[{Apply[Subtract, Mean /@ NicenessComparisonData[#]], #} &, CityData[{Large, "UnitedKingdom"}]]];

The places with the greatest variance are at the ends of the sorted list.

First[cityNicenessData]

{-1.3892, {SouthendOnSea, SouthendOnSea, UnitedKingdom}}

Last[cityNicenessData]

{0.595845, {Brighton, UnitedKingdom, UnitedKingdom}}

AverageReport[NicenessComparisonData["SouthendOnSea"]]

Southend On Sea's average weekend niceness: 2.84839; average weekday niceness: 4.23759

AverageReport[NicenessComparisonData["Brighton"]]

Brighton's average weekend niceness: 1.35719; average weekday niceness: 0.761341

While both of these are significant with the 5% test, doing so on data which we have systematically selected for being the greatest outliers of 68 points is a sufficient abuse of statistical method to be meaningless. And I point out that Brighton, the place where her theory is least true, IS somewhere that she has lived.

We agree that the definitive answer would be to take into account every single UK weather station (or perhaps all those that she has lived near, weighted by how long she was there). It will only take a few more lines of Mathematica code, but I have concluded that if I want to still get birthday cards, it would be best to leave it as a mystery.

Download the notebook

Leave a Comment

11 Comments


republicofmath

Your statement “Randomly dividing the data into two sets will produce this much difference in mean about 20% of the time.” is not what the p-value tells us. It tells us that IF the assumption of no difference in means is correct then 20% of the time we will see a test statistic at least this large. That’s all it tells us.

It’s a bit like saying an 80% confidence interval for the means tells us we will see this difference in means lying in the CI 80% of the time we sample – absolutely incorrect, and easily seen to be so by a simple (Mathematica !) simulation. The CI tells us NOTHING about the observed differences in means. It says that 80% of the time we sample we will see the true difference in means lying in that – freshly re-calculated – CI. But it says nothing about an 80% chance of lying in a particular CI. Similar interpretation of p-values.

Posted by republicofmath    July 29, 2010 at 5:04 pm
George Kangas

Something I’ve noticed, is that precipitation seems to have a one week periodicity: e.g., if it’s rainy this Monday, it seems especially likely to be rainy next Monday.

Care to whip up a test for this?

Posted by George Kangas    July 30, 2010 at 10:20 pm
    Brian

    Weather reports are popular. Everyone wants to know what to expect tomorrow. If you happen to miss the weather forecast I would like to suggest a simple rule for forecasting tomorrows weather . The rule is: the weather tomorrow will be very similar to what it has been today. Obviously the rule is not always correct but I guess that it is correct a surprisingly high proportion of the time. It would be interesting for someone to evaluate the rule or variants of it using Matematica.

    Posted by Brian    May 17, 2012 at 5:18 am
Michael

Your mother’s theory is correct. It had been proven by experimetal data already. The explanation is quite simple. In order to form rain drops condensation cores are needed. Normally some piece of dirt works fine. During the week there is lots of dirt in the air from exhaust fumes. During the weekend it will clear up. It leads to less rain in the beginning of the week.

Posted by Michael    July 31, 2010 at 7:36 am
oelewapperke

Why is the Brighton value rejected ? Surely you can select data all you want, as long as you select data based on a variable that doesn’t have anything to do with what you’re searching for.

Perhaps one could make the argument that selecting in your sorted set was not independant of the variable. However I doubt it’ll cheer up the weekends of the people in Bristol.

Posted by oelewapperke    July 31, 2010 at 9:34 am
JonMcLoone

@ republicofmath
You are, of course, correct. I apologise for the over-simplified description. The test statistic is StudentT and so is based on the difference in means, but also on the observed variance, and the number of sample points.

Posted by JonMcLoone    August 2, 2010 at 5:32 am
JonMcLoone

@ George
The other item I wrote about whether is probably closer to your question:

http://blog.wolfram.com/2010/01/15/mathematica-tests-the-st-swithuns-day-proverb/

Posted by JonMcLoone    August 4, 2010 at 5:06 am
Chris

Michael’s theory seems to be validated by Jon’s data. Prevailing wind across southern England is from the south-west. Southend is directly in the lee of London, the UK’s largest centre of popultaion, and close enough for the weekend effect to be felt almost instantaneously on Saturday/Sunday, rather than Sunday/Monday. Brighton is on the south coast, so is not normally exposed to airborne pollution from other English towns. The major pollutants coming over from the French coast are emissions from the string of nuclear power stations and would not have the same effect on climate. It would be interesting to test this theory by looking at the location of other cities at the extremes of [cityNicenessData]. Are Swansea and Plymouth close to Brighton in the distribution? Are cities grouped with Southend in the lee of other conurbations?

Posted by Chris    August 19, 2010 at 6:17 am
nikki

john hangman game only has 8 spaces for letters. how can you give words for more than 8 spaces and call them winning words?

Posted by nikki    August 20, 2010 at 12:31 am
Kevin

With this amount of data, you might collect some small effects, e.g. weather stations are more prone to defects in very hot weather and are less likely to be repaired immediately on weekends

Posted by Kevin    July 31, 2011 at 2:15 pm
Dimiter Tsvetkov

Well done!

Posted by Dimiter Tsvetkov    January 7, 2014 at 1:49 pm


Leave a comment

Loading...

Or continue as a guest (your comment will be held for moderation):