-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW3-ch8q6.py
More file actions
58 lines (45 loc) · 1.46 KB
/
HW3-ch8q6.py
File metadata and controls
58 lines (45 loc) · 1.46 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-6, 6, 1001)
dx = x[1] - x[0]
def gaussian(x, mean, std_dev):
return (1 / (std_dev * np.sqrt(2 * np.pi))) * np.exp(-((x - mean) ** 2) / (2 * std_dev**2))
# create 2 pdfs
pdf1 = gaussian(x, mean=-2.7, std_dev=1)
pdf2 = gaussian(x, mean=2.7, std_dev=1)
pdf = pdf1 + pdf2
# create cdf by pdf
cdf = np.cumsum(pdf) * dx
# normalized cdf and pdf
pdf_normalized = pdf / np.sum(pdf * dx)
cdf_normalized = np.cumsum(pdf_normalized) * dx
# plot them
plt.figure(figsize=(12, 8))
plt.subplot(3, 1, 1)
plt.plot(x, pdf, label="PDF (Unnormalized)", color='blue')
plt.plot(x, pdf_normalized, label="PDF (Normalized)", color='green', linestyle='dashed')
plt.title("PDF (Original and Normalized)")
plt.xlabel("x")
plt.ylabel("Density")
plt.legend()
plt.grid(True)
plt.subplot(3, 1, 2)
plt.plot(x, cdf, label="CDF (Unnormalized)", color='red')
plt.title("CDF (Unnormalized)")
plt.xlabel("x")
plt.ylabel("Cumulative Probability")
plt.legend()
plt.grid(True)
plt.subplot(3, 1, 3)
plt.plot(x, cdf_normalized, label="CDF (Normalized)", color='purple')
plt.title("CDF (Normalized)")
plt.xlabel("x")
plt.ylabel("Cumulative Probability")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
print("Unnormalized CDF:")
print(f"Final value of unnormalized CDF: {cdf[-1]:.4f}")
print("\nNormalized CDF:")
print(f"Final value of normalized CDF: {cdf_normalized[-1]:.4f}")