Problem. A gambler with units bets 1 unit per round, winning with probability (losing with ), until reaching or . What is the probability of reaching ?
double ruinWin(int i, int N, double p) { // P(reach N before 0) from i, win each step with probability p
if (fabs(p - 0.5) < 1e-12) return (double)i / N;
double r = (1 - p) / p;
return (1 - pow(r, i)) / (1 - pow(r, N));
}Proof: the ruin probability and duration
satisfies with , . Rewrite as , so the differences form a geometric sequence with ratio and . For this is , and gives ; otherwise and normalizing at gives the formula. For the fair duration, with is solved by (second difference ). Checked against value iteration for .