Don't understand the algebraic passage to find the generating function I have a random walk with master equation $$\hat{P}_{i}(n)=p_{i-1}\hat{P}_{i-1}(n-1)+q_{i+1}\hat{P}_{i+1}(n-1)$$
where $\hat{P}_{i}(n)$ is the probability of the random walker of being in position $i$ at time $n$. Also, $p_i$ is the probability of hopping from $i$ to $i+1$ and $q_i$ from $i$ to $i-1$.
The books wants to find the generating function so multiplies by $z^n$ both sides and sums up from $0$ to $\infty$ but I don't understand how he gets from the above, to $$\hat{P}_i(n)=p_{i-1}z\hat{P}_{i-1}(z)+q_{i+1}z\hat{P}_{i+1}(z)+\delta_{i,0}$$ 

How do you get the above equation? The book doesn't even explain what is the delta and why it came up. Can you at least hint me in the right direction?

 A: If you want to become fluent with generating functions, you have to learn "the method". Quoting Wilf (page 8 of Generatingfunctionology), here it is:
THE METHOD
Given: a recurrence formula that is to be solved by the method of
generating functions.


*

*Make sure that the set of values of the free variable (say $n$) for
which the given recurrence relation is true, is clearly delineated.

*Give a name to the generating function that you will look for, and
write out that function in terms of the unknown sequence (e.g.,
call it $A(x)$, and define it to be $\sum_{n\ge0} a_nx^n$).

*Multiply both sides of the recurrence by $x^n$, and sum over all
values of $n$ for which the recurrence holds.

*Express both sides of the resulting equation explicitly in terms of
your generating function $A(x)$.

*Solve the resulting equation for the unknown generating function
$A(x)$.

*If you want an exact formula for the sequence that is defined by
the given recurrence relation, then attempt to get such a formula
by expanding $A(x)$ into a power series by any method you can
think of. In particular, if $A(x)$ is a rational function (quotient
of two polynomials), then success will result from expanding in
partial fractions and then handling each of the resulting terms
separately.


I might add for the sake of clarity that it would be a good idea to change your notation slightly in order to highlight the difference between the quantities involved here. You have $p_i$ and $q_i$ for the probabilities of moving right or left if the walker is at site $i$. I would let $P_i(n)$ without the hat represent the probability that the walker is at site $i$ at time $n$, and then let $\hat P_i(z)=\sum_{n\ge0}P_i(n)z^n$ with the hat represent the generating function of the time sequence of probabilities at site $i$.
Then we can follow Wilf's steps.


*

*Your master equation is good for all times $n\ge1$. You didn't mention any initial condition, but a common assumption would be that the walker starts at the origin. That's where the $\delta$ notation comes in: $\delta_{i,0}$ is 1 if $i=0$, and $\delta_{i,0}$ is 0 if $i\ne0$. This notation is sometimes called the Kronecker delta.


*You actually have a family of generating functions, $\hat P_i(z)=\sum_{n\ge0}P_i(n)z^n$, one at each site $i$. Note that because of the initial condition, the only generating function with a nonzero constant term is $\hat P_0(z)$.

*To turn your master equation into a relation about the family of generating functions, you multiply each instance of the master equation by a appropriate power of $z$ and then sum over the allowed times from step 1, namely $n\ge1$. This gives
$$\sum_{n\ge1}P_i(n)z^n=p_{i-1}\sum_{n\ge1}P_{i-1}(n-1)z^n+q_{i+1}\sum_{n\ge1}P_{i+1}(n-1)z^n.$$

*The term on the left $\sum_{n\ge1}P_i(n)z^n$ coincides with $\hat P_i(z)$, except possibly for the $n=0$ term. From the initial condition, this term is 0, except at the origin, in which case the term on the left is $\hat P_0(z)-1$. You can put both cases into a single expression with $\hat P_i(z)-\delta_{i,0}$. For the terms on the right, there's a factoring and a shift of variables going on. For instance, 
$$\sum_{n\ge1}P_{i-1}(n-1)z^n=z\sum_{n\ge1}P_{i-1}(n-1)z^{n-1}$$
by factoring one $z$ out of the sum. Then, by shifting the limits of the sum $n'=n-1$
$$z\sum_{n\ge1}P_{i-1}(n-1)z^{n-1}=z\sum_{n'\ge0}P_{i-1}(n')z^{n'}=z\hat P_{i-1}(z).$$
Doing the same thing with the other term on the right gives your final equation
$$\hat P_i(z)=p_{i-1}z\hat P_{i-1}(z)+q_{i+1}z\hat P_{i+1}(z)+\delta_{i,0}.$$
I hope that gives you some of the direction you were looking for.
