使用pymc進行統計建模

2021-07-22 16:51:28 字數 3574 閱讀 5218

1、乙個統計模型

有這樣乙個資料集,它按照時間順序,收錄了英國從2023年到2023年每年的礦難發生次數。如下圖所示:

我們可以假設,礦難發生的概率服從乙個poisson過程,在某一年泊松過程的引數發生了變化,在時間軸的早些時候,礦難發生的概率較高,後來礦難發生的概率比較低。

我們將上述概念模型轉化為統計模型:

以上模型引數定義如下:

由於在模型中我們定義了d依賴於s,e,l,所以我們把d稱作s,e,l的子變數,類似的,s,e,l稱為d的父變數。

2、變數的兩種型別

pymc包中定義類兩種隨機變數型別,分別為stochastic和deterministic。

模型中唯一的deterministic變數是r,因為當我們知道r的父引數(s,l,e)後,我們可以準確地計算出r的值。

另一方面,s,d(在觀察到資料之前)是stochastic變數,因為即使觀察到他們的父變數,任然不能確定它們的值。

我們將模型寫在乙個名為 disaster_model.py 的python指令碼中:

"""

匯入numpy和pymc

"""from pymc import discreteuniform, exponential, deterministic, poisson, uniform

import numpy as np

"""匯入英國礦難資料集

"""disasters_array = \

np.array([ 4, 5, 4, 0, 1, 4, 3, 4, 0, 6, 3, 3, 4, 0, 2, 6,

3, 3, 5, 4, 5, 3, 1, 4, 4, 1, 5, 5, 3, 4, 2, 5,

2, 2, 3, 4, 2, 1, 3, 2, 2, 1, 1, 1, 1, 3, 0, 0,

1, 0, 1, 1, 0, 0, 3, 1, 0, 3, 2, 2, 0, 1, 1, 1,

0, 1, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 1, 1, 0, 2,

3, 3, 1, 1, 2, 1, 1, 1, 1, 2, 4, 2, 0, 0, 1, 4,

0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1])

"""定義轉折點s:

取值範圍0-110

均勻離散分布

"""switchpoint = discreteuniform('switchpoint', lower=0, upper=110, doc='switchpoint[year]')

"""定義e、l

指數分布

"""early_mean = exponential('early_mean', beta=1.)

late_mean = exponential('late_mean', beta=1.)

"""定義r

"""@deterministic(plot=false)

def rate(s=switchpoint, e=early_mean, l=late_mean):

''' concatenate poisson means '''

out = np.empty(len(disasters_array))

out[:s] = e

out[s:] = l

return out

"""定義礦難發生次數

服從泊松分布

"""

disasters = poisson('disasters', mu=rate, value=disasters_array, observed=true)

3、父變數與子變數

我們已經使用pymc建立了統計模型,pymc中提供方法檢視模型中引數之間的關係,試例**如下:

from pymc.examples import disaster_model

disaster_model.switchpoint.parents #顯示s的父引數

#輸出disaster_model.disasters.parents #顯示disasters的父引數

#輸出disaster_model.rate.children #顯示rate的子引數

#輸出

4、變數的值

所有的pymc變數都具有value屬性,檢視value值示例**如下:

disaster_model.disasters.value

"""輸出

array([4, 5, 4, 0, 1, 4, 3, 4, 0, 6, 3, 3, 4, 0, 2, 6, 3, 3, 5, 4, 5, 3, 1,

4, 4, 1, 5, 5, 3, 4, 2, 5, 2, 2, 3, 4, 2, 1, 3, 2, 2, 1, 1, 1, 1, 3,

0, 0, 1, 0, 1, 1, 0, 0, 3, 1, 0, 3, 2, 2, 0, 1, 1, 1, 0, 1, 0, 1, 0,

0, 0, 2, 1, 0, 0, 0, 1, 1, 0, 2, 3, 3, 1, 1, 2, 1, 1, 1, 1, 2, 4, 2,

0, 0, 1, 4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1])

"""disaster_model.switchpoint.value

#輸出 array(40)

disaster_model.early_mean.value

#輸出 array(1.1444157379406001)

disaster_model.late_mean.value

#輸出 array(0.027985496189503425)

5、使用馬爾科夫鏈蒙特卡洛(mcmc)擬合模型

pymc提供mcmc方法擬合模型,使用方法如下:

from pymc.examples import disaster_model

from pymc import mcmc

m = mcmc(disaster_model)

m.sample(iter=10000, burn=1000, thin=10)

mcmc演算法輸出模型中每個變數的樣本,獲得樣本方法如下:

m.trace('switchpoint')[:]

#輸出array([43,43,44,...44,44])

取樣序列圖可以判斷mcmc是否收斂,如果取樣序列分布近似於白雜訊,那麼可以判斷mcmc已經收斂。

使用erwin進行mysql建模

1,定義資料字典,把需要用到的資料型別建立好 2,在物理模式下,設定資料字典,修改comment為 attname 這樣會預設使用邏輯模式下,實體屬性的名稱作為欄位的注釋 3,物理模式下選擇database pre post script model level,如下圖 新增角本 在生成資料庫時,使...

使用bit進行計數統計

這個類的主要用途是使用bit計數,儲存使用int,乙個int有32位,可以記錄32個資料 public class arraybitmap 判斷num的第pointer位是否為1 param num 待修改的資料 param pointer 從0開始計數,從右到左數,待修改的位置 return bo...

Spark SQL使用window進行統計

def main args array string unit 按照地市進行統計top3課程 param spark param accessdf def citytypeaccesstopnstat spark sparksession,accessdf dataframe unit 執行結果如下...