Programming Assignment 3: Drawing Balls From an Urn
Programming Assignment 3: Drawing Balls From an Urn
Note: If your computer does not render math equations properly, you can do a right-click and then change the rendering method (Math Settings > Math Renderer
) to be HTML-CSS
.
CourseNana.COM
tl;dr: There are 3 tasks and 1 conclusion task waiting for you (scroll down for more information). CourseNana.COM
Introduction
You have an urn. Initially there are R red balls and B blue balls in the urn. You would like to randomly draw a ball from an urn, for exactly N times. CourseNana.COM
# Just in case you need to import some packages.
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
plt.style.use("seaborn-v0_8")
Task 1: With Replacement (1 point)
Suppose each time after you draw a ball from an urn, you put the ball back to the urn. Please write a function drawing_balls_with_replacement(R, B, N, rep)
that simulates the above experiment for rep times. For each experiment, you record the number of red balls being drawn. Then, your function plots a histogram on the number of red balls being drawn for the scenario.
CourseNana.COM
Return Value
Your function returns the average number of red balls being drawn in an experiment. CourseNana.COM
def drawing_balls_with_replacement(R = 100, B = 100, N = 100, rep = 10000):
# a sample implementation, feel free to re-write the whole thing.
def one_experiment(R, B, N):
"""Your code here."""
return np.random.randint(1, N) # change me!
exps = [one_experiment(R, B, N) for _ in range(rep)]
plt.hist(exps, bins=range(min(exps)-10, max(exps)+10), edgecolor='k', alpha=0.5)
return np.mean(exps)
Testing
We will test your function with the following parameters. CourseNana.COM
# Test 1.1
drawing_balls_with_replacement(100, 100, 100, 10000)
# Test 1.2
drawing_balls_with_replacement(100, 300, 100, 10000)
Task 2: Without Replacement (1 point)
Suppose each time after you draw a ball from an urn, you DO NOT put the ball back to the urn. Please write a function drawing_balls_without_replacement(R, B, N, rep)
that simulates the above experiment for rep times. For each experiment, you record the number of red balls being drawn. Then, your function plots a histogram on the number of red balls being drawn for the scenario.
CourseNana.COM
Return Value
Your function returns the average number of red balls being drawn in an experiment. CourseNana.COM
def drawing_balls_without_replacement(R = 100, B = 100, N = 100, rep = 10000):
"""Your code here."""
return 0.0
Testing
Now, we compare the experiments on whether or not we put the ball back into the urn. CourseNana.COM
# Test 2.1
avg1 = drawing_balls_with_replacement(100, 100, 100, 10000)
avg2 = drawing_balls_without_replacement(100, 100, 100, 10000)
print(avg1, avg2)
# Test 2.2
avg1 = drawing_balls_with_replacement(100, 300, 200, 10000)
avg2 = drawing_balls_without_replacement(100, 300, 200, 10000)
print(avg1, avg2)
# Test 2.3
avg1 = drawing_balls_with_replacement(100, 300, 300, 10000)
avg2 = drawing_balls_without_replacement(100, 300, 300, 10000)
print(avg1, avg2)
Task 3: With Duplication (1 point)
Suppose each time after you draw a ball from an urn, you put an additional ball of the same color back to the urn. Please write a function drawing_balls_with_duplication(R, B, N, rep)
that simulates the above experiment for rep times. For each experiment, you record the number of red balls being drawn. Then, your function plots a histogram on the number of red balls being drawn for the scenario.
CourseNana.COM
Return Value
Your function returns the average number of red balls being drawn in an experiment. CourseNana.COM
def drawing_balls_with_duplication(R = 100, B = 100, N = 100, rep = 10000):
"""Your code here."""
return 0.0
Testing
Please design 3 meaningful tests, the choice of the parameters is up to you. CourseNana.COM
# Test 3.1
avg1 = drawing_balls_with_replacement(100, 100, 100, 10000)
avg2 = drawing_balls_without_replacement(100, 100, 100, 10000)
avg3 = drawing_balls_with_duplication(100, 100, 100, 10000)
print(avg1, avg2, avg3)
# Test 3.2
# Test 3.3
Task 4: Conclusion (1 point)
What do you observe from the above figures? Are there possible explanations about you observation? (Any reasonable observation deserves full credits.) CourseNana.COM