LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Statistical Simulation in Python

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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))
Code bearbeiten und ausführen