-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp2b.py
More file actions
35 lines (25 loc) · 1.09 KB
/
p2b.py
File metadata and controls
35 lines (25 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom
# Probability of getting a 2 in a single roll
p_success = (0.10) ** 3 # Cube because we need three twos
# Number of rolls in each experiment (n)
n = 1000
# Theoretical probability distribution using the Binomial formula
x = np.arange(0, n + 1) # Possible values of X
pmf = binom.pmf(x, n, p_success) # Probability mass function
# Experimental data (from Problem 1)
experimental_data = successes # Make sure 'successes' is the list from Problem 1
# Create plots
plt.figure(figsize=(10, 5))
# Theoretical Binomial PMF
plt.stem(x, pmf, linefmt='r-', markerfmt='ro', basefmt='r-', label='Theoretical Binomial PMF')
# Experimental data (from Problem 1)
unique_values, counts = np.unique(experimental_data, return_counts=True)
plt.stem(unique_values, counts / N, linefmt='b-', markerfmt='bo', basefmt='b-', label='Experimental Data (Problem 1)')
plt.xlabel('Number of Successes (X)')
plt.ylabel('Probability')
plt.title('Comparison of Theoretical Binomial PMF and Experimental Data')
plt.legend()
plt.grid(True)
plt.show()