Friday, July 3, 2009

Prisoner's Paradox : Solution

Here is an interesting math puzzle that I found here:

Three condemned prisoners share a cell. A guard arrives and tells them that one has been pardoned.

"Which is it?" they ask.

"I can't tell you that," says the guard. "I can't tell a prisoner his own fate."

Prisoner A takes the guard aside. "Look," he says. "Of the three of us, only one has been pardoned. That means that one of my cellmates is still sure to die. Give me his name. That way you're not telling me my own fate, and you're not identifying the pardoned man."

The guard thinks about this and says, "Prisoner B is sure to die."

Prisoner A rejoices that his own chance of survival has improved from 1/3 to 1/2. But how is this possible? The guard has given him no new information. Has he?

This is a classic example of a paradox usually encountered in the theory of probability. Like most other paradoxes it might seem a bit confusing initially but there is a simple technique to come to the correct conclusion.We studied it in school and it is called Baye's theorem.Let us take a closer look at the problem:

The objective:

We need to find out what is the probability that prisoner A lives before and after getting the information from the guard.Let LA denote the event that prisoner A lives.Before getting the information from the guard it is fairly simple, since only one of them is going to live it is P(LA) = 1/3.

Now consider the event of guard telling prisoner A that prisoner B is going to die.Let us denote the event as GAB.Let us assume that A actually is freed (I shall tell you why a little later) then the probability of the guard giving the information is P(GAB/LA) which is 1/2.

For those of you who are wondering why it is not 1,this is the tricky part.If prisoner A lives then the guard can tell either the name of B or C because both of them are going to die.The probability that he told A that B is going to die is 1/2 (remember, given the condition!).Now for the fact why we computed P(GAB/LA)? You must have already solved loads of problems where to find a conditional probability you will find its posterior probability and apply Baye's theorem.The case is no different here.It is crucial to note that we are trying to find the probability that A lives after getting the info so the desired probability is P(LA/GAB) which can be computed using the standard formula.

Therefore
P(LA/GAB) = P(GAB/LA)*P(LA)/P(GAB)
To evaluate P(GAB) we need to evaluate all possible cases (if you don't know what I am referring to refer Wikipedia on Baye's theorem).Let us look at the following cases:

We know P(GAB/LA) and P(LA) which are 1/2 and 1/3 respectively.So the product is 1/6.
The second case is P(GAB/LB) and P(LB) which is zero because he cannot tell A that B will die when B will actually live (he cannot lie!)
The third case is P(GAB/LC) which is 1.Confused again? It goes like this... When C is actually living that means both A and B are going to die.Since the guard cannot tell A about his own fate he HAS to tell him about the death of B.So hence it is 1 and its product with P(LC) is 1/3.
Finally the sum is P(GAB) = 1/3+1/6 = 1/2.

Now substituting it in the Baye's formula we get P(LA/GAB) = 1/6/(1/2) = 1/3.You can observe that the probability is infact the same as that was before asking the question.So technically there is no new information that A received.Secondly, A incorrectly estimates that his chances after asking the guard have become 1/2.His chances actually remained 1/3 before and after asking the guard.So the paradox is just superficial and the law of probability still prevails.

Going further, isn't it paradoxical that the probability of C living increased without him actually doing anything?Well...if you followed the steps properly it should not be difficult figuring out the answer.And also note that none of the other prisoners have the same information as prisoner A.So they would still be clueless and assume that their probability of survival is 1/3.And if prisoner C also approaches and asks the guard and both A and B share the information with each other.Then what would the probability of their survival be?

This problem is also called the three prisoners problem and available in Wikipedia for more reading.One of the methods shown there is similar to this with slight change of details.I took me a couple of hours to come up with a sound explanation of what was going on in the problem and put it on paper.

Any flaws, corrections or comments are welcome.

Wednesday, July 1, 2009

Stuck without Javascript

A couple of days back I was working on a mobile optimized website.Since Javascript is not supported in mobile phone browsers (though Opera Mini and some others do but most of the default browsers don't!) I had to figure out a way to display tiny bits of a large result without querying the server every time.I was facing the following problem...A couple of days back I was working on a mobile optimized website.Since Javascript is not supported in mobile phone browsers (though Opera Mini and some others do but most of the default browsers don't!) I had to figure out a way to display tiny bits of a large result without querying the server every time.I was facing the following problem...

I had a huge array that contained search results from a database and I had to display one result at a time without querying the server every time.Well, if I had Javascript I would have put each result in a container and kept all of them invisible except the one that I wanted to display.Since I don't have that option I had to use the following PHP hack.

I queried the server for the results and got an array.After getting the array, I serialized it using serialize() function and passed it along with a display index using GET to a display URL.I unserialized() the string in the display page, thereby recovering the array.Using the display index I could display the desired element.So I could display each element without querying the server each time for results.

This is not an uncommon technique but it will help you if you are stuck without Javascript like me :)

Note:There are other ways of passing information using curl etc, but you will need support from the server for using them.