Để vẽ CDF (Cumulative Distribution Function) từ dữ liệu mẫu, ta tính histogram -> PDF -> tích lũy thành CDF.
Các bước
- Thiết lập figure size và autolayout.
- Khởi tạo số mẫu N và tạo dữ liệu ngẫu nhiên.
- Tính histogram: np.histogram(data, bins=10).
- Tính PDF: count / sum(count).
- Tính CDF: np.cumsum(pdf).
- Vẽ CDF bằng plot() với label.
- Thêm legend và hiển thị.
Ví dụ
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
N = 500
data = np.random.randn(N)
count, bins_count = np.histogram(data, bins=10)
pdf = count / sum(count)
cdf = np.cumsum(pdf)
plt.plot(bins_count[1:], cdf, label="CDF")
plt.legend()
plt.show()
Kết quả
