Python array counter -
the assignment
write function
collect_sims(nsim,n,d,p=0.5,nmax=10000)
runsrun_sim
functionnsim
times (with parametersn
,d
,p
) , returns numpy array of lengthnmax
giving number of times simulation took specified number of steps stop.for example, suppose
nsim
8 , successive runs ofrun_sim
gave 3,4,4,3,6,5,4,4.
tabulate “two 3s, 4 4s, 1 5, 1 6, 0 7s, 0 8s …”
the function function calls
def run_sim(n=20, d=6, p=0.5, itmax=5000): counter = n sum = 0 = 0 while counter != 0: if i>itmax: raise runtimeerror c = np.random.randint(1,d+1,1)[0] # method returns array 1 value coin = np.random.binomial(1,p,1)[0] # if =1 heads, =0 tails print(i,"|",c,"|",counter) if coin ==1: if (counter + c)>n: i+=1 else: i=+0 counter = counter +c elif coin ==0: print("coint == 0: counter - c:", counter - c) if(counter - c)<0: i+=1 else: i+=1 counter = counter -c print("coint ==0: counter:", counter) if counter==0: break return(i) print(run_sim(n=20, d=6, p=0.5,itmax=500)) print(run_sim(n=20, d=12, p=0.5, itmax=500))
i wrote:
def collect_sims(nsim, n, d, p=0.5, nmax=10000): run_sim(n=n, d=d, p=0.5, itmax=5000) a=np.zeros(nmax) a.reshape(nsim,2) in range (nsim): a[run_sim(n,d,p),2] += 1 return(a)
it isn't giving me error isn't returning or printing when run it, doing wrong?
Comments
Post a Comment