CommencerCommencer gratuitement

The conditional urn

As we've learned, conditional probability is defined as the probability of an event given another event. To illustrate this concept, let's turn to an urn problem.

We have an urn that contains 7 white and 6 black balls. Four balls are drawn at random. We'd like to know the probability that the first and third balls are white, while the second and the fourth balls are black.

Upon completion, you will learn to manipulate simulations to calculate simple conditional probabilities.

Cet exercice fait partie du cours

Statistical Simulation in Python

Afficher le cours

Instructions

  • Initialize the counter success to 0 and sims to 5000.
  • Define a list, urn, with 7 white balls ('w') and 6 black balls ('b').
  • Draw 4 balls without replacement and check to see if the first and third are white and second and fourth are black.
  • Increment success if the above criterion is met.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Initialize success, sims and urn
success, sims = ____, ____
urn = ____

for _ in range(sims):
    # Draw 4 balls without replacement
    draw = np.random.choice(____, replace=____, size=4)
    # Count the number of successes
    if ____: 
        success +=1

print("Probability of success = {}".format(success/sims))
Modifier et exécuter le code