saush

Umbrellas to work, or why engineers should get married

Posted in Ruby by sausheong on March 30, 2010

When I was working in Yahoo at Suntec City, I used to take the MRT to work every day and my route is completely sheltered right to the doorsteps of the office so the weather didn’t really have much effect on me. I still take the MRT to the office now that I am at Garena, but there is a short distance from the MRT station to the office, about 5 minutes of walking, where it is subject to the elements.

Now I don’t take any umbrellas along with me, so with the recent change in weather (it has been raining cats and dogs and all sorts of mammals in between) I have been caught a few times in some unpleasant wetness. It is during one of these wet commutes from home to office I was thinking of ways of making sure that will not happen again.

The idea was like this. I will place some umbrellas at the office and also at home. Whenever I leave the house or the office and if it is raining, I will take the umbrella to the office or back home respectively. Naturally if it’s not raining then I wouldn’t want to look like a dork and carry one.

Therein lies the problem. Let’s say I keep 1 umbrella at the office and another at home. If it rains when I’m leaving the office, I will carry that one home, leaving 2 at home and none at the office. If it rains the next day when I leave home it’s ok, I will just take an umbrella to the office, making it 1 – 1 again. If it doesn’t rain and therefore I don’t take any umbrellas to the office, I will be left with 2 – 0. If it happens to rain when I’m leaving the office I’m stuck with getting wet again.

What if I have 2 umbrellas at the office and 2 at home? In this case I will be caught wet only if the same occurrence (rain when leaving the office followed by no rain when leaving the house) happens 2 days in a row, which probability is lower than if it happens just once. Still, that can happen and I want to get assurance that the risk of getting wet is really minimal. In that case, the question becomes, how many umbrellas should I leave at the office and similarly at home such that the probability of getting wet is negligible? Note that it doesn’t really matter if one place has more umbrellas than the other since it will be the same as the lower number of umbrellas in the long run.

To find out, I used a popular (and really pragmatic and therefore very ‘engineering’) method of finding probabilities — the Monte Carlo simulation method. The name sounds fancy but it’s really just an algorithm that uses repeated random samples to find the answer. In our case, what we want to do is to find out, over large sample, the average number of trips I will make before I run the risk of getting wet.

To find this, I will need to inspect the probability that it will rain at any given day. For easy of calculations, I use a probability between 1% chance that it will rain and 99% chance that it will rain. Why don’t I include 0% or 100%? This is because if it is 0% it means it will never rain and so I will never need to use an umbrella, and if it 100% it will always rain and therefore I will always bring an umbrella back or forth.

On top of that, I will run the simulation for the case where I have 2 umbrellas at home and at the office, up to 50 umbrellas at home and at the office. To give a large enough representation, I run this over 1000 iterations (more will be better but it will take too long to run). This is the Ruby code for the simulation.

require 'rubygems'
require 'faster_csv'

umbrellas_range = 2..50
probability_range = 1..99
@location = :home

total_trips = {}
iterations = 1000

def walks_to(loc)
 location = loc
 @num_of_trips += 1
end

def office?()  @location == :office; end
def home?() @location == :home; end
def raining?(probability) rand(99) < probability; end

FasterCSV.open('umbrellas.csv', 'w') do |csv|
 csv << [''] + probability_range.to_a
 umbrellas_range.each do |umbrellas|
 probability_range.each do |probability|
 total_trips[probability] = 0
 iterations.times do
 @num_of_trips = 0
 wet = false
 home = umbrellas
 office = umbrellas

 while not wet
 if home?
   walks_to :office
   if raining?(probability)
     if home > 0
       home -= 1
       office += 1
     else
       wet = true
     end
   end

 elsif office?
   walks_to :home
   if raining?(probability)
     if office > 0
       office -= 1
       home += 1
     else
       wet = true
     end
   end
 end
 end

 total_trips[probability] += @num_of_trips
 end
 end

 row = [umbrellas]
 total_trips.sort.each { |pair| row << pair[1]/iterations }
 csv << row
 end
end

The Ruby program writes to a simple CSV file called umbrellas.csv with the first row being the probability, and the first column the number of umbrellas at each location. If you chart each row with the y-axis being the number of trips and the x-axis being the probability of rain from 1% to 99%, you will find a chart like this:

Now that I have the average number of trips that I will make before I get wet, I want to actually see what that means for Singapore. To do this, I went to the National Environment Agency’s website and dug out the weather statistics for Singapore.  There is a statistic that gives the average number of rainy days in any given month for the past 118 years. I take that for each month and divide that with the number of days in that month to derive the monthly probability of rain as in the following table:


Month Probability of rain
January 48%
February 39%
March 45%
April 50%
May 45%
June 43%
July 42%
August 45%
September 47%
October 52%
November 63%
December 61%

Finally I match that with the Monte Carlo simulation I made earlier. The average number of trips for each month, must be between 56 to 62 days (each trip is one way only, so every day is 2 trips). For example, the statistics shows that the wettest months are in November and December which is 63% and 61% probability of rain respectively. This means I will need to have 38 umbrellas at each location for each of these 2 months (please check your umbrellas.csv file; open it up with Excel). In February where the probability of rain is the lowest (39%), I will only need 23 umbrellas at each location to almost guarantee that I will not get wet (the whole deal assumes at the end of each month, I replenish each location with the necessary number of umbrellas, of course).

That evening when I proudly told my wife my findings at home, she stared at me for while (frostily if I might add) then gave me a small folding umbrella, which I now carry in my laptop bag.

And that is the reason why engineers should get married.

(I have been inspired by this book — Digital Dice : Computational Solutions to Practical Probability Problems, by Paul J. Nahin)

About these ads

21 Responses

Subscribe to comments with RSS.

  1. Arun said, on March 30, 2010 at 5:57 am

    lol awesome experiment! Agree with your conclusions too :)

  2. jeromE said, on March 30, 2010 at 9:39 am

    lol.. your findings are good in a comic strip :D

  3. Vivian said, on March 30, 2010 at 9:44 am

    Interesting, the eyes of technical..

  4. shweta said, on March 30, 2010 at 11:26 am

    You are a genius sir.

  5. jf said, on March 30, 2010 at 11:31 am

    heh. I have thought about that (especially during the rainy months!!!) – but never went to that extent to do the math… Good on u!!! Now i know i just need 30+ umbrellas….

    OR, how about a “group umbrella pool”? Although i guess the only difference for that would be if people leave at different times – and there is a variability in the rain/no rain factor because of that….

  6. Jason said, on March 30, 2010 at 12:40 pm

    I was just thinking of the folding umbrella while reading the article.

    I”ve a habit of carrying one since long time ago. My wife doesn’t and says she can always count on a friendly stranger to ferry you across, to which I stared at her blankly and said “just carry one and stop being a liability to others…”. Well, she’s still not carrying one.

  7. BL said, on March 30, 2010 at 1:41 pm

    Nice simulation. The use of Monte Carlo sampling will help you to localize around a global maximum and hence you are getting close to observation data. If the seasons of rain becomes irregular, then other forms of sampling or additional heuristic will be required.

  8. Hu Shunjie said, on March 30, 2010 at 1:53 pm

    Or, you can call your colleague to come down and fetch you an umbrella, given that next time your colleague is caught in the rain, you will be the hero.

    But then you got to calculate the probability of your colleague being earlier than you in the office * probability colleague is available . Deviate that with the probability of colleague getting sick and going for oversea trips, that will do the trick!

    Okay, get married is easier.

  9. Ian Timothy said, on March 30, 2010 at 2:49 pm

    Nice story. My gf will probably do the same thing.

    But seriously, real man just walk in the rain.

  10. […] Umbrellas to work, or why engineers should get married: Now I don’t take any umbrellas along with me, so with the recent change in weather (it has been raining cats and dogs and all sorts of mammals in between) I have been caught a few times in some unpleasant wetness. It is during one of these wet commutes from home to office I was thinking of ways of making sure that will not happen again. […]

  11. TH Lim said, on March 30, 2010 at 5:01 pm

    I suppose this is jsut an excuse for u to write codes. :)

  12. Sacha Chua said, on March 30, 2010 at 10:42 pm

    I gave up on umbrellas. The folding umbrellas are too flimsy to stand up to gusts.

    Instead, I wear a hat and a good coat. On rainy days, I’ll take a backpack with a rain cover. I have both hands free, and I don’t have to fuss around with opening and closing umbrellas. Yay!

    On the downside, I’ve chased my hat across a sidewalk before. You may want a sturdy hat with a strap for the wind. =)

    Nice simulation, though!

  13. shikhap said, on March 31, 2010 at 5:00 pm

    good one :)

  14. cobra said, on April 3, 2010 at 1:20 am

    Very interesting indeed. I liked the way you converted your thoughts into a program which I think is very appreciative and thoughtful.

    :)

  15. JN said, on April 3, 2010 at 4:59 pm

    Omg. Things that run inside an engineer’s head…

  16. LBN said, on May 17, 2010 at 3:30 pm

    What if when you start going to work it’s still sunny but when you reach your work place it rains?

  17. goodwind89 said, on May 17, 2010 at 10:49 pm

    Isn’t the probability a bit high? Usually there’s at most 2 rains a day, so 3hrs/24hrs, which is <<50%.

    Now we take some smaller probability, like 12%, i think u'll need less umbrellas, and the solution is feasible.

    But if you walk under rain and think about your wife who gave you the umbrella, then it should be more fun than some calculation.

  18. 2010 in review « saush said, on January 2, 2011 at 7:57 pm

    […] The busiest day of the year was March 30th with 731 views. The most popular post that day was Umbrellas to work, or why engineers should get married. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 450 other followers

%d bloggers like this: