(+30) Write a python program per the following specifications: Populate an array
ID: 3905409 • Letter: #
Question
(+30) Write a python program per the following specifications:
Populate an array(list) of size 50 randomly with only integers 0 and 1
Repeat step1 n = 1000 times using either a while loop or a for loop
At this point you should have a total of 50000 observations
Display the number of 0’s (use the count() function from prior labs)
Display the number of 1’s (use the count() function from prior labs)
Using the Binomial distribution formulas
Display the expected mean (average) of the 1’s
Calculate and display the standard deviation (SD)
NOTE: sd should be > 100
Display the range as follows: mean + SD mean - SD
Answer the question: is the total number of 1’s from 4 above. within the range as calculated in 5.c by printing
‘Yes’ if it is within the range calculated in 5c
‘No’ if it is not
SAMPLE code Template (available in the source code tab Lab06Template.py MODIFY as needed )
# CSC120
# Lab 06 Binomial distribution
import random
# Initialize array
myArray = []
a = 0
b = 1
size = 25 # make == 50
j = 0
n =10 # make equal to 1000
# Populate array with size = 25 random integers in the range 0 - 1
#and repeat for n times
while (j < n):
for k in range(size):
randNum = random.randint(a,b)
myArray.insert(k, randNum)
j = j + 1
# Display array size
print("size of my array is :", len(myArray))
# printmyArray 10 values per line
k = 0
sub = []
while (k < len(myArray) ):
sub = myArray[k: k+10]
print("...",k, sub)
k = k+10
#place holder code only FIX
zeros = -8 # place holder code FIX
print(" number of 1 is: ", ones)
print(" number of 0 is: ", zeros)
# mean and standard deviation (sd) calculations here
#EXAMPLE ONLY
mean = 100 # FIX
sd = 5 # FIX
print("=====================")
print('mean of My Array:', mean, " sd (standard deviation: ", sd)
print (" mean +-sd: ",mean + sd, " " , mean - sd )
print ("the mean number of 1's is within mean +- sd ??? YES/NO ")
# need to answer this
# compare the number of 1’s (variable zeros calculated above
# to see if it is within the range mean +– sd….
OUTPUT of the sample code
size of my array is : 250
... 0 [0, 1, 0, 0, 0, 0, 0, 1, 1, 1]
... 10 [0, 1, 1, 1, 0, 1, 1, 1, 1, 0]
... 20 [0, 0, 0, 1, 1, 0, 0, 0, 1, 1]
... 30 [1, 1, 1, 1, 1, 1, 0, 0, 1, 0]
... 40 [1, 0, 1, 1, 1, 0, 0, 1, 0, 0]
... 50 [0, 0, 1, 1, 0, 1, 1, 1, 1, 0]
... 60 [1, 0, 0, 0, 1, 0, 0, 0, 0, 1]
... 70 [1, 1, 0, 1, 1, 0, 1, 1, 0, 0]
... 80 [0, 0, 0, 0, 1, 1, 1, 1, 0, 0]
... 90 [1, 1, 1, 0, 0, 1, 1, 0, 0, 1]
... 100 [0, 1, 1, 0, 0, 1, 1, 0, 1, 0]
... 110 [1, 1, 0, 0, 1, 0, 1, 0, 0, 0]
... 120 [1, 0, 0, 0, 0, 0, 0, 1, 1, 1]
... 130 [0, 1, 0, 0, 1, 1, 0, 1, 1, 1]
... 140 [1, 0, 0, 0, 1, 0, 0, 1, 0, 0]
... 150 [0, 0, 1, 1, 0, 1, 0, 1, 1, 1]
... 160 [0, 1, 1, 1, 0, 1, 0, 1, 1, 0]
... 170 [1, 1, 0, 1, 1, 1, 1, 0, 1, 0]
... 180 [0, 1, 1, 1, 0, 1, 0, 0, 0, 1]
... 190 [1, 1, 1, 1, 0, 0, 1, 1, 0, 1]
... 200 [0, 1, 1, 1, 1, 0, 0, 0, 1, 1]
... 210 [1, 0, 1, 1, 1, 1, 1, 0, 0, 0]
... 220 [1, 1, 0, 0, 1, 0, 1, 1, 0, 1]
... 230 [0, 0, 1, 1, 1, 1, 0, 0, 1, 0]
... 240 [1, 0, 0, 1, 1, 0, 1, 0, 1, 0]
number of 1 is: 33 # FIX
number of 0 is: -8 # FIX
=====================
mean of My Array: 100 sd (standard deviation: 5
mean +-sd: 105 95
the mean number of 1's is within mean +- sd ???
Explanation / Answer
import random
import math
myArray=[]
a=0
b=1
size=50
j=0
n=1000
#random 1 and 0 in array
while (j<n):
for k in range(size):
randNum=random.randint(a,b) #select rendomly from 1 amd 0
myArray.insert(j,randNum) # insert randomly selected number in array
j=j+1
k=0
sub=[]
#print array , 10 number in a row
while(k < len(myArray)):
sub=myArray[k:k+10] #sub array of length of 10
print("...",k,sub) #print sub aray of lentgh 10
k=k+10
#Count ones and zeros
> zeros=0
k=0
while (k<len(myArray)):
if (myArray[k]==0):
zeros=zeros+1
else:
> k=k+1
print("Number of 1 is : ",ones)
print("Number of 0 is : ",zeros)
#expected mean(average) of> # N => total number of ones
#P => probability of ones
P=ones/50000.0 #probability of 1's
mean=ones*P #mean calculation using bionomial distribution
sd=math.sqrt(ones*P*(1-P)) #standard deviation using bionomial deviation
print(" ===========================================")
print("mean of Array: %.2f" % mean,"sd(standard deviation) : %.2f" %sd)
#%.2f is used to print upto 2 decimal places
print("mean+-sd :%.2f" %(mean+sd) ," %.2f" %(mean-sd))
print("Mean number of 1's is within mean +-sd ???")
if(ones >=(mean-sd) and ones < (mean-sd)):
print("YES")
else:
print("NO")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.