#Aniket Shah #Jan 30, 2-15, Assignment hw2.txt #OK to Post #1 seq(sin(i*pi/2),i=0..10); y:=1,2,3; y:=op([op([y])]); S:= table([(a)=1,(x)=4,(3)=5]); f:=(x,z) -> (x*z)^2; F:=array(1..2,1..3,[[seq(diff(f(i,z),z),i=1..3)],[1,2,3]]); #2 #MHg(q): simulates ONE instance of a variation on the Monty Hall #game show where there is a car behind one of the doors #(w.l.o.g door #1) and the two other doors have goats. #implementing the switching strategy MHg:=proc(q) local p, i, door1, UnpickedDoors, MontyDoors,MontyDoor,ND : p:=convert(q,fraction): i:=rand(1..denom(p))(): if i<=numer(p) then door1:=1: print(`You picked door `, door1 ): else door1:=rand(2..3)(): print(`You picked door `, door1 ): fi; UnpickedDoors:={1,2,3} minus {door1}: MontyDoors:=UnpickedDoors minus {1}: MontyDoor:=MontyDoors[rand(1..nops(MontyDoors))()]: print(`Monty shows you that door`, MontyDoor, `has a goat `): print(`and he kindly (or "kindly") offers you to switch your`): print(`choice, and you agree`): ND:={1,2,3} minus {door1, MontyDoor}: ND:=ND[1]: print(`You decided to change your guessed door to door`, ND): if ND=1 then print(`Congratulations! You won a Porche! `): else print(`Monty tricked you, have fun with the goat.`): fi: end: #3 #MHgs(q): same as MHg(p) but silent MHgs:=proc(q) local p, i, door1, UnpickedDoors, MontyDoors,MontyDoor,ND : p:=convert(q,fraction): i:=rand(1..denom(p))(): if i<=numer(p) then door1:=1: else door1:=rand(2..3)(): fi; UnpickedDoors:={1,2,3} minus {door1}: MontyDoors:=UnpickedDoors minus {1}: MontyDoor:=MontyDoors[rand(1..nops(MontyDoors))()]: ND:={1,2,3} minus {door1, MontyDoor}: ND:=ND[1]: if ND=1 then true: else false: fi: end: #MHgsN(p,N): Runs MHgsN(p) N times, calculates the number of #wins to losses MHgsN:=proc(p,N) local i, numPorsche, propWins: numPorsche:=0: for i from 1 to N do if MHgs(p) then numPorsche:=numPorsche+1: fi: od: propWins:= evalf(numPorsche/N): end: #SDsim(N): simulates scenario where 1 person gets sick and another #gets falsely diagnosed, out of N people SDsim:=proc(N) local rs, fs, people, healthyPeople: people:={seq(i,i=1..N)}: rs:=rand(1..N)(): healthyPeople:=people minus {rs}: fs:=healthyPeople[rand(1..(N-1))()]: [rs,fs]: end: #6 #If k*(2*r+1)>N, then the probability of BDg(k,N,r) is 0, #basically by pigeonhole #if k*(2*r+1)<=N, then the probability is binomial(N-2*r*k,k)*k!/N^k.