The Moment generating function is used to derive the first and second moments of the binomial and geometric distributions. The moment generating function for a binomial is given syms t k n p q kf=sym('k!');
M=exp(t*k)*subs(kf,k,n)*p^k*q^(n-k)/(subs(kf,k,k)*subs(kf,k,n-k)); pretty(M)
k (n - k)
exp(t k) n! p q
-----------------------
k! (n - k)!
M=simplify(symsum(M,k,0,n)); pretty(M)
n
(q + exp(t) p)
Matlab performed the simplification of the summation. We now use Equation 3.3-7 to find the moments
MP=diff(M,t);
M1=limit(MP,t,0);
M1=simplify(M1); pretty(M1)
(n - 1)
(q + p) n p
Now we substitute t = 0 into the expression to find as example 3.2 that when we use the fact p + q = 1 we have E[T] = n p
The second moment is found by differentiating again
MPP=diff(MP,t)
MPP =
(q+exp(t)*p)^n*n^2*exp(t)^2*p^2/(q+exp(t)*p)^2+(q+exp(t)*p)^n*n*exp(t)* p/(q+exp(t)*p)-(q+exp(t)*p)^n*n*exp(t)^2*p^2/(q+exp(t)*p)^2
Substitution for t = 0 and simplifying
M2=limit(MPP,t,0); pretty(simplify(M2))
(n - 2)
(q + p) n p (n p + q)
Using the fact that (p + q ) = 1 we have
E[T2] = pretty(subs(M2,q,1-p))
n p (n p + 1 - p)
The variance is computed in the usual manner VAR[X] = E[T2] - E[T]2 =
VAR=subs(M2,q,1-p) - subs(M1,q,1-p)^2; pretty(simplify(VAR))
2
n p - n p
The process is repeated for the geometric distribution. First we obtain a closed form for the moment generating function using Matlab
ML=simplify(symsum(exp(t*k)*p^k*q,k,0,inf)) pretty(ML)
ML =
-q/(-1+exp(t)*p)
q
- -------------
-1 + exp(t) p
The first and second moments are generated by differentiation and substitution for t = 0 in the resultant expression
E[K] =
MLP=limit(diff(ML),t,0)
MLP =
1/(p-1)^2*q*p
We repeat the process for the second moment
E[K 2 ]=
MLPP=limit(diff(ML,2),t,0); pretty(MLPP)
q p (p + 1)
- -----------
3
(p - 1)
The variance is computed and simplified by noting q = 1 - p and by substitution VAR[K] = E[K2] - E[K]2
=
VARL=subs(MLPP,q,1-p) - subs(MLP,q,1-p)^2 pretty( simplify(VARL))
VARL =
-(1-p)*p*(p+1)/(p-1)^3-1/(p-1)^4*(1-p)^2*p^2
p
--------
2
(p - 1)