Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconFundamentos del Análisis de Datos con Python
Fundamentos del Análisis de Datos con Python

Chapter 11: Probability Theory

11.5 Practical Exercises for Chapter 11: Probability Theory

Ah, the moment you've all been waiting for—the practical exercises! We promise, the hands-on work is not just immensely educational but also lots of fun. These exercises will help you grasp the concept of probability and the Bayesian Theory more deeply. Let's jump in!  

Exercise 1: Roll the Die

Simulate the roll of a fair six-sided die 1,000 times. Plot a histogram to show the distribution of the results. 

import matplotlib.pyplot as plt
import random

results = [random.randint(1, 6) for _ in range(1000)]

plt.hist(results, bins=6, edgecolor='black')
plt.xlabel('Die Face')
plt.ylabel('Frequency')
plt.title('Frequency Distribution of 1,000 Die Rolls')
plt.show()

Exercise 2: Bayesian Inference for a Coin Toss

You have a biased coin that lands heads 60% of the time. If the coin is tossed 10 times, what is the probability that it lands heads exactly 7 times?

You can use the binomial probability formula for this:


P(x=k) = \binom{n}{k} \times p^k \times (1-p)^{(n-k)}

Where \binom{n}{k} is the combination \frac{n!}{k! \times (n-k)!}.


from scipy.stats import binom

n = 10  # Number of trials
k = 7  # Number of successes
p = 0.6  # Probability of success

# Compute the binomial probability
prob = binom.pmf(k, n, p)

print(f"The probability of getting exactly 7 heads in 10 tosses is {prob:.4f}")

Exercise 3: Bayesian Disease Diagnosis

Extend the disease diagnosis example from the Bayesian Theory section to handle multiple test results. Assume you have two tests with the following probabilities:

  • Test 1: P(E_1|H) = 0.98
  • Test 2: P(E_2|H) = 0.95

Compute the posterior probability P(H|E_1, E_2).

# Prior probability
P_H = 0.001

# Likelihoods
P_E1_given_H = 0.98
P_E2_given_H = 0.95

# Total probabilities of positive tests
P_E1 = (0.98 * 0.001) + (0.02 * 0.999)
P_E2 = (0.95 * 0.001) + (0.05 * 0.999)

# Compute the joint probability of both tests
P_E1_and_E2_given_H = P_E1_given_H * P_E2_given_H

# Compute the joint total probability of positive tests
P_E1_and_E2 = P_E1 * P_E2

# Posterior probability using Bayes' Theorem
P_H_given_E1_and_E2 = (P_E1_and_E2_given_H * P_H) / P_E1_and_E2

print(f"The probability of actually having the disease if both tests are positive is {P_H_given_E1_and_E2:.4f}")

Feel free to give these exercises a whirl, and don't worry if you don't get them right the first time. The beauty of learning is in the journey itself. Happy coding! 

11.5 Practical Exercises for Chapter 11: Probability Theory

Ah, the moment you've all been waiting for—the practical exercises! We promise, the hands-on work is not just immensely educational but also lots of fun. These exercises will help you grasp the concept of probability and the Bayesian Theory more deeply. Let's jump in!  

Exercise 1: Roll the Die

Simulate the roll of a fair six-sided die 1,000 times. Plot a histogram to show the distribution of the results. 

import matplotlib.pyplot as plt
import random

results = [random.randint(1, 6) for _ in range(1000)]

plt.hist(results, bins=6, edgecolor='black')
plt.xlabel('Die Face')
plt.ylabel('Frequency')
plt.title('Frequency Distribution of 1,000 Die Rolls')
plt.show()

Exercise 2: Bayesian Inference for a Coin Toss

You have a biased coin that lands heads 60% of the time. If the coin is tossed 10 times, what is the probability that it lands heads exactly 7 times?

You can use the binomial probability formula for this:


P(x=k) = \binom{n}{k} \times p^k \times (1-p)^{(n-k)}

Where \binom{n}{k} is the combination \frac{n!}{k! \times (n-k)!}.


from scipy.stats import binom

n = 10  # Number of trials
k = 7  # Number of successes
p = 0.6  # Probability of success

# Compute the binomial probability
prob = binom.pmf(k, n, p)

print(f"The probability of getting exactly 7 heads in 10 tosses is {prob:.4f}")

Exercise 3: Bayesian Disease Diagnosis

Extend the disease diagnosis example from the Bayesian Theory section to handle multiple test results. Assume you have two tests with the following probabilities:

  • Test 1: P(E_1|H) = 0.98
  • Test 2: P(E_2|H) = 0.95

Compute the posterior probability P(H|E_1, E_2).

# Prior probability
P_H = 0.001

# Likelihoods
P_E1_given_H = 0.98
P_E2_given_H = 0.95

# Total probabilities of positive tests
P_E1 = (0.98 * 0.001) + (0.02 * 0.999)
P_E2 = (0.95 * 0.001) + (0.05 * 0.999)

# Compute the joint probability of both tests
P_E1_and_E2_given_H = P_E1_given_H * P_E2_given_H

# Compute the joint total probability of positive tests
P_E1_and_E2 = P_E1 * P_E2

# Posterior probability using Bayes' Theorem
P_H_given_E1_and_E2 = (P_E1_and_E2_given_H * P_H) / P_E1_and_E2

print(f"The probability of actually having the disease if both tests are positive is {P_H_given_E1_and_E2:.4f}")

Feel free to give these exercises a whirl, and don't worry if you don't get them right the first time. The beauty of learning is in the journey itself. Happy coding! 

11.5 Practical Exercises for Chapter 11: Probability Theory

Ah, the moment you've all been waiting for—the practical exercises! We promise, the hands-on work is not just immensely educational but also lots of fun. These exercises will help you grasp the concept of probability and the Bayesian Theory more deeply. Let's jump in!  

Exercise 1: Roll the Die

Simulate the roll of a fair six-sided die 1,000 times. Plot a histogram to show the distribution of the results. 

import matplotlib.pyplot as plt
import random

results = [random.randint(1, 6) for _ in range(1000)]

plt.hist(results, bins=6, edgecolor='black')
plt.xlabel('Die Face')
plt.ylabel('Frequency')
plt.title('Frequency Distribution of 1,000 Die Rolls')
plt.show()

Exercise 2: Bayesian Inference for a Coin Toss

You have a biased coin that lands heads 60% of the time. If the coin is tossed 10 times, what is the probability that it lands heads exactly 7 times?

You can use the binomial probability formula for this:


P(x=k) = \binom{n}{k} \times p^k \times (1-p)^{(n-k)}

Where \binom{n}{k} is the combination \frac{n!}{k! \times (n-k)!}.


from scipy.stats import binom

n = 10  # Number of trials
k = 7  # Number of successes
p = 0.6  # Probability of success

# Compute the binomial probability
prob = binom.pmf(k, n, p)

print(f"The probability of getting exactly 7 heads in 10 tosses is {prob:.4f}")

Exercise 3: Bayesian Disease Diagnosis

Extend the disease diagnosis example from the Bayesian Theory section to handle multiple test results. Assume you have two tests with the following probabilities:

  • Test 1: P(E_1|H) = 0.98
  • Test 2: P(E_2|H) = 0.95

Compute the posterior probability P(H|E_1, E_2).

# Prior probability
P_H = 0.001

# Likelihoods
P_E1_given_H = 0.98
P_E2_given_H = 0.95

# Total probabilities of positive tests
P_E1 = (0.98 * 0.001) + (0.02 * 0.999)
P_E2 = (0.95 * 0.001) + (0.05 * 0.999)

# Compute the joint probability of both tests
P_E1_and_E2_given_H = P_E1_given_H * P_E2_given_H

# Compute the joint total probability of positive tests
P_E1_and_E2 = P_E1 * P_E2

# Posterior probability using Bayes' Theorem
P_H_given_E1_and_E2 = (P_E1_and_E2_given_H * P_H) / P_E1_and_E2

print(f"The probability of actually having the disease if both tests are positive is {P_H_given_E1_and_E2:.4f}")

Feel free to give these exercises a whirl, and don't worry if you don't get them right the first time. The beauty of learning is in the journey itself. Happy coding! 

11.5 Practical Exercises for Chapter 11: Probability Theory

Ah, the moment you've all been waiting for—the practical exercises! We promise, the hands-on work is not just immensely educational but also lots of fun. These exercises will help you grasp the concept of probability and the Bayesian Theory more deeply. Let's jump in!  

Exercise 1: Roll the Die

Simulate the roll of a fair six-sided die 1,000 times. Plot a histogram to show the distribution of the results. 

import matplotlib.pyplot as plt
import random

results = [random.randint(1, 6) for _ in range(1000)]

plt.hist(results, bins=6, edgecolor='black')
plt.xlabel('Die Face')
plt.ylabel('Frequency')
plt.title('Frequency Distribution of 1,000 Die Rolls')
plt.show()

Exercise 2: Bayesian Inference for a Coin Toss

You have a biased coin that lands heads 60% of the time. If the coin is tossed 10 times, what is the probability that it lands heads exactly 7 times?

You can use the binomial probability formula for this:


P(x=k) = \binom{n}{k} \times p^k \times (1-p)^{(n-k)}

Where \binom{n}{k} is the combination \frac{n!}{k! \times (n-k)!}.


from scipy.stats import binom

n = 10  # Number of trials
k = 7  # Number of successes
p = 0.6  # Probability of success

# Compute the binomial probability
prob = binom.pmf(k, n, p)

print(f"The probability of getting exactly 7 heads in 10 tosses is {prob:.4f}")

Exercise 3: Bayesian Disease Diagnosis

Extend the disease diagnosis example from the Bayesian Theory section to handle multiple test results. Assume you have two tests with the following probabilities:

  • Test 1: P(E_1|H) = 0.98
  • Test 2: P(E_2|H) = 0.95

Compute the posterior probability P(H|E_1, E_2).

# Prior probability
P_H = 0.001

# Likelihoods
P_E1_given_H = 0.98
P_E2_given_H = 0.95

# Total probabilities of positive tests
P_E1 = (0.98 * 0.001) + (0.02 * 0.999)
P_E2 = (0.95 * 0.001) + (0.05 * 0.999)

# Compute the joint probability of both tests
P_E1_and_E2_given_H = P_E1_given_H * P_E2_given_H

# Compute the joint total probability of positive tests
P_E1_and_E2 = P_E1 * P_E2

# Posterior probability using Bayes' Theorem
P_H_given_E1_and_E2 = (P_E1_and_E2_given_H * P_H) / P_E1_and_E2

print(f"The probability of actually having the disease if both tests are positive is {P_H_given_E1_and_E2:.4f}")

Feel free to give these exercises a whirl, and don't worry if you don't get them right the first time. The beauty of learning is in the journey itself. Happy coding!