Subject Computer Science Python Programming

Question

Programming Challenge 1
In python, the random module can generate pseudo random numbers. For the purpose of this course, such numbers can be considered random. In particular, random.randrange(2) produces random bits. To use the random module, it is necessary to import random. Using a loop, store N random bits in a list object.
import random
SampleSpaceSize = 10
NumberTrials = 10
TrialSequence = []
for TrialIndex in range(0, NumberTrials):
TrialSequence.append(random.randrange(SampleSpaceSize))
Then, look at the empirical distribution of the ratios of zeros and ones.
percent = []
for OutcomeIndex in range(0, SampleSpaceSize):
percent.append(TrialSequence.count(OutcomeIndex) / float(NumberTrials))
print percent
Explore how the empirical distribution changes as N increases 10.0, 100.0, 1000.0, 10000.0.

Programming Challenge 2
This challenge, like many other challenges, will use the random module in python to generate pseudo random numbers. Remember to import the module, if needed.
import random
Write a method to generate a Bernoulli random variable with parameter p = 0.75. Then, create a sequence of random variables. Each random variable in the sequence should be the sum of 10 independent Bernoulli random variable. Note that the range X(Ω) for each random variable should be {0, 1, . . . , 10}. Using a for loop, look at the empirical distribution of the proportion of zeros, ones, . . . , tens.
percent = []
for OutcomeIndex in range(0, SampleSpaceSize):
percent.append(TrialSequence.count(OutcomeIndex) / float(NumberTrials))
print percent
Explore how the empirical distribution changes as N grows: 100.0, 1000.0, 10000.0, etc. Can you recognize the distribution that you are getting?
Use matplotlib, or another python 2D plotting library to display empirical distributions.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import random
import matplotlib.pyplot as plt

SampleSpaceSize = 10

#
NumberTrials = 10

TrialSequence = []
for TrialIndex in range(0, NumberTrials):
    TrialSequence.append(random.randrange(SampleSpaceSize))
      
percent = []
for OutcomeIndex in range(0, SampleSpaceSize):
    percent.append(TrialSequence.count(OutcomeIndex) / float(NumberTrials) )
print percent

#
#y1 = percent
#
NumberTrials = 100

TrialSequence = []
for TrialIndex in range(0, NumberTrials):
    TrialSequence.append(random.randrange(SampleSpaceSize))...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Assisting Tutor

Related Homework Solutions

Get help from a qualified tutor
Live Chats