Question: I have a model python program. I have to make these changes in it. 1. Add a variable called tail_r (for tail of the red-die
I have a model python program. I have to make these changes in it.
1. Add a variable called tail_r (for "tail of the red-die distribution"). This variable should initially equal zero. Each time through the loop, if v is greater than or equal to Vr, then add 1 to tail_r. Thus tail_r will count the number of repetitions in which the simulated variance is at least as large as the observed one. Before you modify the code, think carefully. Where should tail_r be defined and initialized? Inside the inner loop? Before the inner loop but inside the outer one? Or before both loops?
2. Add the variable tail_w to keep track of how often the simulated variance is at least as large as Vw.
3. At the end of the program, and outside of the for loop, print two lines of output, one telling us what fraction of the time the simulated variance exceeded Vr and the other doing the same for Vw. The print statements should include text that explains what is being printed.
4. Once this works, take out the REMOVE ME line (or convert it to a comment), and increase nreps first to 100 (make sure that everything still works) and then to 1000. If you go much over 1000 then your program may take a long time to run. 5. Use your results to figure out whether either or both of Wolf's dice were unfair.
here is the model program:
from random import random # line number 1
# define a function to return the variance of values in xvec # 3 def var(xvec): # 4 m = msq = 0.0 # 5 for x in xvec: # 6 m += x # 7 msq += x*x # 8 n = float(len(xvec)) # 9 m /= n # mean # 10 msq /= n # mean square # 11 return (msq - m*m) # variance # 12
w = [3246, 3449, 2897, 2841, 3635, 3932] # counts from white die # 14 r = [3407, 3631, 3176, 2916, 3448, 3422] # counts from red die # 15
Vw = var(w) # 17 Vr = var(r) # 18 print "Var(white):", Vw # 19 print "Var(red) :", Vr # 20
nreps = 10 # adjust this to do more replicates # 22 for rep in xrange(nreps): # 23 count = [0,0,0,0,0,0] # count[i] accumulates the numbers of # 24 # rolls that showed i+1 spots # 25 for roll in xrange(20000): # do 20000 rolls of the simulated die # 26 spots = int(6.0*random()) # uniform on [0,1,2,3,4,5] # 27 count[spots] += 1 # here's the accumulation # 28
v = var(count) # and here's our function call # 30
print "Repetition %d: var=%f" % (rep, v) # REMOVE ME later # 32
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
