Wednesday, August 12, 2009

Psenti-Sem

The following are the activities that will constitute most of my final semester on campus
  • Getting bit by LGMF's and other wildlife in my bhawan
  • Convincing people that Microsoft Windows actually sucks
  • Attending first year classes and hitting on first year girls
  • Conning my project professor
  • Being under the influence every alternate day (at the least)
  • Taking a "trip"
  • Campaigning in elections for the "gults"
  • Coding a virtual pet
  • Plan "the road trip" with Andy
People who know me might wanna add more :)

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.

Saturday, June 27, 2009

Steps to Create a Successful Web App

This is the fourth blog I am starting and I am bored of my usual hello world posts.So let me just talk about what I want to write in this blog (if I actually write!). The answer is simple, the title says it all.Just random things that are in my mind.But I warn the readers that this blog may contain some posts which are inappropriate for non-geeks.

I read this article by Kevin Rose on how to promote a web app.After working on chirplines and trying to market a web app, here are my views on creating a successful web application.

Find your market
Internet has a diverse set of people and you cannot satisfy everyone with your application.So before you begin your first line of code have a clear idea as to whom are you targeting.For example Vimeo targets an artistic crowd compared to youtube which has a general crowd.Each feature that goes into your app should make it more usable for your target users.

Keep It Simple Stupid! (aka KISS)
Da Vinci said "Simplicity is the ultimate sophistication".Keep your application as simple as possible.Don't get carried away by the options available at every stage.You need not incorporate mail updates just because they can be done.Stick to the basics and in case you need to add something, take the easiest path even if the result is marginally less.This applies to the back end as well.Don't use a complex algorithm for something which a binary sort can solve.

Get Real
The first thing I was asked to do when we were designing chirplines was to design the CSS for the home page.I was really excited and wanted to get it pixel perfect.I spent 2 days on it without going anywhere.The most important thing while developing a web app is to get real and start making the first draft without giving too much importance to details.More importantly get the functionality right.CSS and color combinations can be changed in minutes but what your app does can't be done so quickly.So get it working first and worry about the details later.37 Signals has an excellent book about this which you can find here.

Usability Usability Usability
Yes you probably heard about it a zillion times but usability is crucial.Web has too many applications coming up too fast to make your users come back for a second time.So you just have a single chance to make an impression.Avoid unnecessary email conformations and login screens.tumblr for example doesn't even wait for an email conformation before you can start posting.Reduce the number of steps that it takes for the user to do what you want him to.

Selling your app
Even though you have a kick ass app, you can't get users without reaching out to a lot of people and the web is too big for your sales division.The only way you can get a number of people is through other people who already used it.Things like Twitter, Facebook, Digg offer limitless possibilities to market your app.

Social Media Bitches (SMBs)
Thats right!These are the people that you want to get hold of while selling your product.How to spot them?Simple.Anyone with more than 10,000 twitter followers come under this category.These people can get your site a number of users and it's how good your app is that decides how many of them stick to it.As Kevin Rose says in the article offer them something valuable to get them use your app.

Show the way
Show people what all can they do with your app.Make a cool podcast on how to use the app though it might be obvious for you (because YOU created it).Get the users excited about it the same way as you do.As my friend rishabh says "Show two solid applications of your app and people will come up with a thousand".

One can write a book about the "right" things about making web apps but at the end of the day there is no secret recipe to make a successful app.Whatever I talked about are things I noticed from my experience of developing chirplines.I would love to hear different views and comments on the ideas expressed above.