連續型隨機變數的產生

Python 的 Monte Carlo 模擬

Izzy Weber

Curriculum Manager, DataCamp

常態分佈

鐘形,中心在平均數(或 loc);寬度由標準差(或 scale)決定

美國成年男性的身高近似常態分佈: 常態分佈的理論機率密度函數

Python 的 Monte Carlo 模擬

變更 scale(標準差)

不同標準差下的常態分佈理論機率密度函數

Python 的 Monte Carlo 模擬

變更 loc(平均數)

不同平均數下的常態分佈理論機率密度函數

Python 的 Monte Carlo 模擬

同時變更 scale 與 loc

不同標準差與平均數下的常態分佈理論機率密度函數

Python 的 Monte Carlo 模擬

從常態分佈取樣

  • 美國成年男性身高近似常態分佈;平均數=177 cm;標準差=8 cm

  • 身高高於 190 或低於 165 的比例是多少?

heights = st.norm.rvs(loc=177, scale=8, size=10000)

qualified = (heights < 165) | (heights > 190)
print(np.sum(qualified) * 100/10000)
12.28
Python 的 Monte Carlo 模擬

繪製模擬結果

heights_dict = {"heights":heights}
sns.histplot(x="heights", data=heights_dict)
plt.axvline(x=165, color="red")
plt.axvline(x=190, color="red")

從常態分佈取樣的長條圖

Python 的 Monte Carlo 模擬

更多連續機率分佈

  • 連續均勻分佈(st.uniform
    • 離散均勻分佈的連續版本
  • 指數分佈(st.expon
    • 幾何分佈的連續版本
1 https://docs.scipy.org/doc/scipy/tutorial/stats/continuous.html
Python 的 Monte Carlo 模擬

一起來練習吧!

Python 的 Monte Carlo 模擬

Preparing Video For Download...