#C20.txt: April 6, 2020 ##Strategic Gambling Help:=proc(): print(` StatAnal(P,t,K), CheckStra(L),TiS(N), BoS(N), RaS(N), GRsim1(L,p,n) , GRsim(L,p,n,M) `): print(`ProbWinning(L,p), ExpDuration(L,p), PGF(L,p,t)`): end: with(Statistics): #from C16.txt #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: #End from C16.txt #TiS(N): The timid strategy with maximum capital N TiS:=proc(N) local i: [1$(N-1)]: end: #BoS(N): The bold strategy with maximum capital N BoS:=proc(N) local i: [seq(min(i,N-i),i=1..N-1)]: end: #RaS(N): a random legal strategy maximum capital N RaS:=proc(N) local i,L: L:=BoS(N): [seq(rand(1..L[i])(),i=1..N-1)]: end: #CheckStra(L): inputs a list L (of length nops(L)) snf lry N:=nops(L)+1, of positive integers indicating a gambling strategy #such that that if you currently have i dollars how much you would stake (it has to be less than min(i,N-i)). Try: #CheckStra([1,2,3,2,1]); #CheckStra([1,2,4,2,1]); CheckStra:=proc(L) local N,i: if not type(L,list) then RETURN(false): fi: N:=nops(L)+1: for i from 1 to nops(L) do if L[i]>min(L[i],N-L[i]) or L[i]<1 then RETURN(false): fi: od: true: end: #GRsim1(L,p,n): Simulating ONE Gambler's game with initial capital of n dollars, and exit capital N #and gambling strategy L, where nops(L)=N-1 and L[i] is the amount you would stake if you had i dollars #At each turn the gambler's win the staked amount with probability p and loses it 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([1$9],1/2,5); #GRsim1(10,10/21,5); GRsim1:=proc(L,p,n) local N,x,U,coin,count: N:=nops(L)+1: if not (type(p,numeric) and p>=0 and p<=1 and type(n, integer) and n>=1 and n<=N) then print(`Bad input`): RETURN(FAIL): fi: if not CheckStra(L) then print(`Bad input`): RETURN(FAIL): fi: U:=RandomVariable(Bernoulli(p)): x:=n: count:=0: while x>0 and x