Doug’s MATLAB Video Tutorials
April 10th, 2009
MATLAB Puzzler: Professor and the Rain
Being a company full of geeks, a fair amount of brainteasers get passed around. One that recently came around got my attention. I knew there was probably math that governed it elegantly, but I did not know how to do that. I do know how to brute force something in MATLAB, so I wrote a monte carlo simulation.
I am against premature optimization. I think you should write clear, easy to maintain code and then and only then optimize if it really needs it. In this case, I needed to optimize to get the code to run in a reasonable time. Once you all have had a chance to solve this puzzler in the comments, I will make a video showing the progression from code that was written quickly to code that was optimized for speed. Until then, try this puzzler out:
A professor has an umbrella in the office and another one at home.
If it is raining when the professor goes out, he takes an umbrella with him,
if one is available. If it is not raining, he doesn't carry any umbrellas
with him. Whenever the professor walks with an umbrella, he stays dry.
Whenever he walks without an umbrella in the rain, he gets wet.
Given p = probability of rain (same on every trip), what is the (steady
state) probability of getting wet?
I.e., what is the probability of rain & no available umbrella?
--------------------
References:
I heard about this problem during Jeff Buzen's talk at the joint IEEE/ACM
meeting, The Improbable Success of Probabilistic Models
The problem is taken from the Introduction to Probability textbook by MIT
professors D.P. Bertsekas and J. N. Tsitsklis.
Here is a link to the slides from an earlier talk by Jeff Buzen on the same
topic:
As always with these puzzlers, be sure to use the proper tags in the comments.
<pre> <code>
[deltaX, deltaY] = puzzler(inMatrix)
%% All the code so someone can just copy and paste it from the comments.
</code> </pre>
19:19 UTC |
Posted in Topic: Puzzler |
Permalink |
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
Leave a Reply
|
 |
Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.
|
 |
|
I worked that formula out by hand. I’d be interested to know if the brute force methods agree with it, or whether anybody comes up with a different analytic solution.
Oliver
Markov-chain solution:
Blast - I made a mistake in my analytical solution. I still think it is possible to evaluate, though. I may come back with a correction.
Nice solution, Petter.
Oliver
There is an analytical solution. It is possible to analytically calculate the left eigenvector of the matrix A in my code.
Maybe something like this, using the matrix from before:
I get (1-p)/(3-p)*p as the analytical formula.
OK, I got there in the end and also get p(1-p)/(3-p).
The way I did it was write an expression for q(0) as a function of p and q(2), and an expression for q(2) as a function of p and q(0), where q(x) is the probability of having x umbrellas (these expressions are both geometric series). I then substituted out q(2), giving an expression for q(0), which I then just multiply by p to give the answer. I wouldn’t call it elegant :)
What’s the rest of the story? Don’t leave us hanging here, with just the bare facts.
Is the professor upset at getting wet, and if so does he walk faster? Does he wear jeans or polyester; the latter taking longer to dry and feels worse when he gets wet? Is he bald or not? (Since it’s well know that a full head of hair increases the chance of rain, due to an increase static electricity discharged into the atmoshere).
Does the professor meet her true love at a rainy bus stop when a passerby offers her his own umbrella? If so, what is the probability that they will have four children, two boys and two girls, as compared with a different gender mix of four children? (Hint: assuming 4 children, the answer is not 50%).
Have to inked a contract for the book rights, and when will the DVD come out?
Inquiring MATLAB’ers Want to Know…
;-)
Everyone,
These are much better answers than I came up with. I knew there was cleaner math than what I used. Twenty minutes programming will save you two minutes in a library… :)
Later this week, I will post the Monte Carlo code optimization and a graph of the solution.
-Thanks,
Doug
PS, Tim- Pretty sure the professor wore polyester, most of mine did at least! :)
Here is my Monte Carlo like simulation.
João,
Wow, that is lightning quick. Much faster than what I came up with (video to be posted this week). I think this is the perfect example of optimizing for speed. You left your original code as reference and commentary and managed to come up with an obfuscated but equivalent and faster set of code. In this case, the speed was worth the extra time and effort it takes to maintain the code.
Thanks for a great solution!
Doug
that’s pretty good Petter.