-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp3c.py
More file actions
37 lines (30 loc) · 897 Bytes
/
p3c.py
File metadata and controls
37 lines (30 loc) · 897 Bytes
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
import numpy as np
import matplotlib.pyplot as plt
N = 10000
U = np.random.rand(N) #random variables
T = -np.log(U) / 2 # Transform to exponentially distributed RVs
#hist bins, counts
nbins = 15
hist, xout = np.histogram(T, bins=nbins)
pdf = hist / N / (xout[1] - xout[0]) # Normalize the histogram
#PDF
def theoretical_pdf(t):
y = np.zeros_like(t) #new empty array like T
for i in range(len(t)):
if t[i] < 0:
y[i] = 0
else:
y[i] = 2 * np.exp(-2 * t[i])
return y
t = np.linspace(min(T), max(T), 100) #PDF line
y = theoretical_pdf(t)
# Plot
plt.figure(figsize=(10, 6))
plt.stairs(xout[:-1], pdf, fill=True, label='Simulated PDF')
plt.plot(t, y, color='red', label='Theoretical PDF')
plt.xlabel('T (seconds)')
plt.ylabel('Probability Density')
plt.title('Exponentially Distributed RV Histogram')
plt.legend()
plt.grid(True)
plt.show()