In this question asked on Stackoverflow, the asker gives a Java function similar to this:
public int f(){
if(Math.random() >= 0.5){
return 1;
}
else{
return 1 + Math.max(f(), f());
}
}
In math-speak, that would be written something like this:
$$f = \left\{ \begin{array}{ll} 50\%\ chance : & 1 \\ 50\%\ chance : & 1 + max(f, f) \end{array} \right.$$
So basically, $f()$ is a probabilistic function that calls itself twice with 50% probability, and returns the maximum depth of its calls.
If you draw the call-tree and label the first case "tails" and the second case "heads," you can see that the question "What is the probability that this function eventually returns" is the same as the question "What is the probability of eventually getting more heads than tails.". Thus, this function terminates with probability 1. This also allows you to calculate the expected number of total times that $f()$ is called.
However, the one question I haven't been able to answer is, what is the expected return value of $f()$?
My first thought was that the expected value $E_f$ should be $$E_f = 0.5 * 1 + 0.5 * (1 + max(E_f, E_f))$$ However, that is clearly wrong; that would mean that, no matter how many calls $f()$ makes to itself in the second case, $E_f = 2$ !
So, does anyone know how to calculate the expected return value of $f()$?