#C10.txt, finishing up (for now) gambling theory #Feb. 23, 2015 Help:=proc(): print(` fpnN(p,n,N), LpnN(p,n,N), EDE(N,p) `): print(` PGFL(p,n,N,x) `): end: #fpnNscaffold(p,n,N): the closed-form expression for the prob. of #winning in a gambler's ruin problem with Pr(SingleWin)=p #entering capital n, and exit capital 0 or N, done from scratch, via Maple fpnNscaffold:=proc(p,n,N) local A,f,a: A:=rsolve({f(n)=p*f(n+1)+(1-p)*f(n-1), f(0)=0,f(1)=a},f(n)): normal(subs(a=solve(subs(n=N,A)=1,a),A)): end: #fpnN(p,n,N): the output of the above, kept for posterity fpnN:=proc(p,n,N):(-1+(-(p-1)/p)^n)/((-(p-1)/p)^N-1): end: #LpnNscaffold(p,n,N): Letting Maple derive, from sctratch #the closed-form expression for the expected #duration of a gambler's ruin problem with Pr(SingleWin)=p #entering capital n, and exit capital 0 or N LpnNscaffold:=proc(p,n,N) local A,f,a: A:=rsolve({f(n)=p*f(n+1)+(1-p)*f(n-1)+1, f(0)=0,f(1)=a},f(n)): normal(subs(a=solve(subs(n=N,A)=0,a),A)): end: #LpnN(p,n,N): the output of the above, kept for posterity LpnN:=proc(p,n,N): normal((N*(-(p-1)/p)^n-(-(p-1)/p)^N*n-N+n)/((2*p-1)*((-(p-1)/p)^N-1))): end: #code, courtesy of Abigail Raz EDE:=proc(N,p) local P,eq, var, i: # enter with a dollars. Probability p you win a dollar and then have a+1. Probability # 1-p you lose a dollar and have a-1. So expected number of days is p*P[a+1]+ #(1-p)*P[a-1]+1. var:={seq(P[i],i=1..N-1)}: eq:={seq(P[i]=p*(P[i+1]+1)+(1-p)*(P[i-1]+1),i=1..N-1)}: eq:=subs({P[0]=0,P[N]=0},eq): var:=solve(eq,var): normal([ seq(subs(var,P[i]),i=1..N-1)]): end: #PGFLscaffold(p,n,N,x): the closed-form expression for the probability #generating function for the r.v. "duration of game" #if you enter with n dollars and exit with 0 or N dollars #and prob. of winning a dollar at each round is p #and losing a dollar is 1-p PGFLscaffold:=proc(p,n,N,x) local A,f,a: A:=rsolve({f(n)=x*(p*f(n+1)+(1-p)*f(n-1)), f(0)=1,f(1)=a},f(n)): normal(subs(a=solve(subs(n=N,A)=1,a),A)): end: #The output of the above kept for posterity PGFL:=proc(p,n,N,x): -4*((-2*x*(p-1)/(1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^n*(2*x*(p-1)/(-1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^N*p-(2*x*(p-1)/(-1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^n*(-2*x*(p-1)/(1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^N*p-(-2*x*(p-1)/(1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^n*(2*x*(p-1)/(-1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^N-(-2*x*(p-1)/(1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^n*p+(2*x*(p-1)/(-1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^n*(-2*x*(p-1)/(1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^N+(2*x*(p-1)/(-1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^n*p+(-2*x*(p-1)/(1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^n-(2*x*(p-1)/(-1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^n)*p*x^2/((-1+(4*p^2*x^2-4*p*x^2+1)^(1/2))*(1+(4*p^2*x^2-4*p*x^2+1)^(1/2))*((-2*x*(p-1)/(1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^N-(2*x*(p-1)/(-1+(4*p^2*x^2-4*p*x^2+1)^(1/2)))^N)) : end: