#C19.txt: Discrete Stochastic processes (con't), and martingales Help:=proc(): print(` ParentList(M) , NewMart(M,phi,Z0)`): print(`NewMart1(M,phi,Z0,n) `): end: ####old stuff #C18.txt: March 30, 2015 #general discrete martingales Help18:=proc(): print(`RandTree(k,M), RandLD(f,M), RandMar(k,M,U,K)`): end: #RandTree(k,M): a random tree with k generations #where each vertex may have from 0 to M children #it returns #[NumberOfVertices, ListOfListOfChildren,ListOfGenerations] #T[i]: the list (from youngest to oldest of the children #of vertex i RandTree:=proc(k,M) local ra,T,co,G,v,NuC,i,i1: co:=0: ra:=rand(1..M): G[0]:={0}: for i from 1 to k do G[i]:={}: for v in G[i-1] do NuC:=ra(): T[v]:=[seq(co+i1,i1=1..NuC)]: co:=co+NuC: G[i]:=G[i] union {op(T[v])}: od: od: [co+1,[seq(T[v],v=0..co-nops(G[k]))],[seq(G[v],v=0..k)]]: end: #RandLD(f,K): generates a random loaded die #in other words a list of length f of #numbers between 0 and 1 that add up to 1 #by first picking a random list of positive integer RandLD:=proc(f,K) local ra,i,q,su: ra:=rand(1..K): q:=[seq(ra(),i=1..f)]: su:=convert(q,`+`): [seq(q[i]/su,i=1..nops(q))]: end: #RandMar(k,M,U,K): generates a random martingale #on a random tree with k generations and #nu. of children from 1 to M, and integer #values of youngest generation from 0 to U #It returns the triple for the tree #[NuVertices,ListOfListOfChildren,ListOfGenerations] #followed by the list of probability distrubtions for each non-leaf #followed by the values of a discrete stochastic process that us a martingale RandMar:=proc(k,M,U,K) local T,q,NuV,C,G,NonLeaves,Mar,ra,KH,LD,v,i,j: T:=RandTree(k,M): ra:=rand(1..U): NuV:=T[1]: C:=T[2]: G:=T[3]: NonLeaves:= {seq(op(G[i]),i=1..nops(G)-1)}: for v in NonLeaves do q[v]:=RandLD( nops(C[v+1]) , K ): od: for v in G[nops(G)] do Mar[v]:=ra(): od: for i from k by -1 to 1 do for v in G[i] do KH:=C[v+1]: LD:=q[v]: Mar[v]:=add(LD[j]*Mar[KH[j]],j=1..nops(LD)): od: od: [op(T), [seq(q[i],i=0..nops(NonLeaves)-1)] , [seq(Mar[i],i=0..NuV-1)] ]: end: ####end old stuff #ParentList(M)Pa: inputs a discrete stochastic process M #and outputs the list,P, of length of M[1]-1 #such that P[i] is the unique parent of vertex i ParentList:=proc(M) local P,C,j,NuNR,c: NuNR:=M[1]-1: C:=M[2]: for j from 1 to nops(C) do for c in C[j] do P[c]:=j-1: od: od: [seq(P[j],j=1..NuNR)]: end: #NewMart1(M,phi,Z0,n) #Inputs a discrete martingale #[NuVertices,ListOfChildren,ListOfGenerations,q,X] #phi a "predictable" ( a function defined on internal vertices) #and Z0, the value of the new martingale at 0 (the top) #outputs the value of brand-new martingale with the same vertices #and the same prob. distibution but different values #at vertex NewMart1:=proc(M,phi,Z0,n) local X,NuV,C,path,P,cu: NuV:=M[1]: C:=M[2]: X:=M[5]: if nops(C)<>nops(phi) then RETURN(FAIL): fi: if n<0 or n>NuV-1 then RETURN(FAIL): fi: path:=[n]: P:=ParentList(M): cu:=n: while cu<>0 do cu:=P[cu]: path:=[cu,op(path)]: od: Z0+add(phi[path[j+1]+1]*(X[path[j+2]+1]-X[path[j+1]+1]), j=0..nops(path)-2): end: #NewMart(M,phi,Z0) #Inputs a discrete martingale #[NuVertices,ListOfChildren,ListOfGenerations,q,X] #phi a "predictable" ( a function defined on internal vertices) #and Z0, the value of the new martingale at 0 (the top) #outputs the brand-new martingale with the same vertices #and the same prob. distibution but different values NewMart:=proc(M,phi,Z0) local n: if nops(M[2])<>nops(phi) then RETURN(FAIL): fi: [op(1..4,M), [seq(NewMart1(M,phi,Z0,n),n=0..M[1]-1)] ]: end: