Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a model program for Python to find the varience of the wolf\'s dice. My i

ID: 3747393 • Letter: I

Question

I have a model program for Python to find the varience of the wolf's dice.

My instructor asks me to do this:

2. Add the variable tail_w to keep track of how often the simulated variance is at least as large as Vw.

this 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

Explanation / Answer

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 tail_w = 0 # change made 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 if v >= Vw: # when simulated variance is at least as large as Vw. tail_w += 1 # increment tail_w print "Repetition %d: var=%f" % (rep, v) # REMOVE ME later # 32 print 'Number of times simulated variance was at least as large as Vw= %d' % (tail_w) # change made