Wednesday 19 February 2014

Generating functions for random walk

Here is Mathematica code for the generating functions for the step at which the random walk first hits a barrier.
$U_z(s)=\sum_{n=0}^\infty P(\text{hit $0$ at $n$}\mid\text{start at $z$})s^n$,
$V_z(s)=\sum_{n=0}^\infty P(\text{hit $a$ at $n$}\mid\text{start at $z$})s^n$.

As one would expect, $U_z(1)=q_z$, $V_z(1)=p_z$ and $U_z'(1)+V_z'(1)=D_z$.

I used these to compute the table on page 60 of the notes. You could use these to find the variance of the duration of the game. By the way, notice in this table that in lines 2-4 the duration of the game is 10 times the expected loss. This is because the expected loss per game is $EX_1=-0.1$. So we are seeing $E[G]=E[X_1+\cdots+X_N]=EN\cdot EX_1=D_z\cdot EX_1$, where the r.v. $N$ is the length of the game.
L1[s_] = (1 + Sqrt[1 - 4 p q s^2])/(2 p s);
L2[s_] = (1 - Sqrt[1 - 4 p q s^2])/(2 p s);

U[s_] = (q/p)^z (L1[s]^(a - z) - L2[s]^(a - z))/(L1[s]^a - L2[s]^a) 
U[s] /. {a -> 10, z -> 9, p -> 45/100, q -> 55/100};
Series[%, {s, 0, 15}] // Simplify

M1[s_] = (1 + Sqrt[1 - 4 p q s^2])/(2 q s);
M2[s_] = (1 - Sqrt[1 - 4 p q s^2])/(2 q s);
V[s_] = (p/q)^(a - z) (M1[s]^z - M2[s]^z)/(M1[s]^a - M2[s]^a) 
V[s] /. {a -> 10, z -> 9, p -> 45/100, q -> 55/100};
Series[%, {s, 0, 15}] // Simplify