#C16.txt: March 23, 2020 virtual class ##Discrete Prob. generating functions, and starting The Gambler's ruin's problem using simulation Help:=proc(): print(` StatAnal(P,t,K), GRsim1(N,p,n) , GRsim(N,p,n,M,s,t) , PH(N,p,n), Roullete(L) `): end: with(Statistics): #StatAnal(P,t,K): Inputs a polynomial (or even a function whose Taylor expansion has positive coefficients) #describing the (not-necessarily normalized) probability generating function of some discrete random variable #outputs a list of length K whose #(i) First entry is the expectation (alias mean, alias average) #(ii) Second entry is the variance (aka as second moment about the mean) #(iii) third-through K entries are the scaled moments about the mean #in particular the third entry is the skewness and the fourth moment is the kurtosis. For example, try: #StatAnal((1+t)^1000,t,6); StatAnal:=proc(P,t,K) local P1,L,k,mu: #First let's normalize it (just in case) if subs(t=1,P)=0 then RETURN(FAIL): else P1:=P/subs(t=1,P): fi: #mu is the average mu:=subs(t=1,diff(P1,t)): L:=[mu]: #Now let's CENTRALIZE, get the prob. gen. function for the "deviation from the mean" P1:=P1/t^mu: #We will now apply the operation td/dt repeatedly and plug-in t=1 P1:=t*diff(P1,t): #The expectation of the "deviation from the mean" should be 0, let's check it if subs(t=1,P1)<>0 then print(`Something is wrong`): RETURN(FAIL): fi: for k from 2 to K do P1:=t*diff(P1,t): #We append the current moment-about the mean L:=[op(L),subs(t=1,P1)]: od: #Finally we adjust the third-through the K-th moment by dividing the k-th momend by #the L[2]^(k/2) (the k-th power of the standard deviation) if L[2]=0 then print(`The variance is 0, the unscaled moments are`): print(L): RETURN(FAIL): else [L[1],L[2],seq(L[k]/L[2]^(k/2),k=3..K)]: fi: end: #GRsim1(N,p,n): Simulating ONE Gambler's ruin game with initial capital of n dollars, #At each turn the gambler's win a dollar with probability p and loses a dollar with probability 1-p #the game ends as soon as it reaches N dollars or 0 dollars (when the gambler's is ruined). #The output is a pair [0,m] or [1,m], according to whether the gambler's left the casino broke or #rich, and m is the number of rounds. Try: #GRsim1(10,1/2,5); #GRsim1(10,10/21,5); GRsim1:=proc(N,p,n) local x,U,coin,count: U:=RandomVariable(Bernoulli(p)): x:=n: count:=0: while x>0 and x0 and x0 and x