Mô phỏng Monte Carlo với Python
Izzy Weber
Curriculum Manager, DataCamp
.nnlf() dùng để tính hàm khả năng âm.nnlf() càng thấp, độ khớp càng tốtsns.histplot(dia["age"])

distributions = [st.laplace, st.norm, st.expon]

mles = []for distribution in distributions: pars = distribution.fit(dia["age"])mle = distribution.nnlf(pars, dia["age"])mles.append(mle)print(mles)
[1797.8467779878652, 1764.0693689033028, 1938.171599681118]
for var in ["age", "bmi", "bp", "tc", "ldl", "hdl", "tch", "ltg", "glu"]:distributions = [st.laplace, st.norm, st.expon] mles = []for distribution in distributions: pars = distribution.fit(dia[var]) mle = distribution.nnlf(pars, dia[var]) mles.append(mle)best_fit = sorted(zip(distributions, mles), key=lambda d: d[1])[0] print(f"Best fit reached using {best_fit[0].name}, \ MLE value: {best_fit[1]}, for variable {var}")
Khớp tốt nhất: dùng norm, giá trị MLE: 1764.0693689033028, biến age
Khớp tốt nhất: dùng norm, giá trị MLE: 1283.356127017369, biến bmi
Khớp tốt nhất: dùng norm, giá trị MLE: 1787.7746251622739, biến bp
Khớp tốt nhất: dùng norm, giá trị MLE: 2193.1564373753627, biến to
Khớp tốt nhất: dùng norm, giá trị MLE: 2136.0440476305284, biến ldl
Khớp tốt nhất: dùng norm, giá trị MLE: 1758.1350738323013, biến hdl
Khớp tốt nhất: dùng norm, giá trị MLE: 739.3762494786798, biến tch
Khớp tốt nhất: dùng norm, giá trị MLE: 339.6620870566908, biến ltg
Khớp tốt nhất: dùng norm, giá trị MLE: 1706.0467588930867, biến glu
Mô phỏng Monte Carlo với Python