scipy.stats —— 機率、隨機變數與分布_numpy

來源:互聯網
上載者:User
import numpy as npimport scipy.stats as st

幾率(odds)

p(B)=120 :二十場比賽只贏一場
odds against B winning: o(B)=1−p(B)p(B)=19 :
A贏19場比賽,B才會贏一場 1. 建立隨機變數(rv:random variable)

泊松分布:

F_true = 1000N = 50F = st.poisson(F_true).rvs(N)                # 泊松分布為離散型機率分布

也可以這樣:

mu_true, sigma_true = 1000, 15N = 100F_true = st.norm(mu_true, sigma_true).rvs(N)F = st.poisson(F_true).rvs()

二項分布

# python>>> import scipy.stats as st>>> n, p = 100, .5>>> X = st.binom(n, p)                        # 隨機變數X:投100次硬幣正面出現的個數                        # 用二項分布表示>>> X.mean()50.0                        # mu = n*p = 100*.5 >>> X.std()5.0                         # sigma = sqrt(n*p*q)=sqrt(100*.5*.5)

st.binom(100, .5).rvs() ⇒ 採樣(trial); 2. 連續性機率分布函數:pdf

pdf 表示的是函數,給一定輸入值,就會得到一個輸出值,而不是隨機變數。

st.norm.pdf(0, loc=0, scale=1) ⇒ 12π√

如下代碼繪製出 f(x)=12π√exp(−(x−1)22)

mu, sigma = 1, 1xs = np.linspace(-5, 5, 1000)plt.plot(x, st.norm.pdf(xs, loc=mu, scale=sigma))plt.show()

st.multivariate_normal:多元常態分佈;
scipy.stats.multivariate_normal 直接傳遞 x ,根據機率密度函數(pdf)獲得其值;

x = np.linspace(0, 5, 10, endpoint=False)y = st.multivariate_normal.pdf(x, mean=2.5, cov=.5)
首先定義隨機變數,再取得 pdf 在各個位置上的值;
x, y = np.mgrid[-1:1:.01, -1:1:.01]pos = np.empty(x.shape + (2,))pos[:, :, 0] = x; pos[:, :, 1] = yrv = multivariate_normal([0, 0], [[1, 0], [0, 1]])plt.contourf(x, y, rv.pdf(pos))

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.