MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Statistical Simulation in Python

Lihat Kursus

Petunjuk latihan

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

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# 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))
Edit dan Jalankan Kode