Py之pymc:pymc的簡(jiǎn)介、安裝、使用方法之詳細(xì)攻略
目錄
pymc的簡(jiǎn)介
pymc的安裝
pymc的使用方法
1、時(shí)序性任務(wù)
(1)、使用 Euler-Maruyama 方案推斷 SDE 的參數(shù)
pymc的簡(jiǎn)介
? ? ? ?PyMC(以前稱為PyMC3)是一個(gè)專注于高級(jí)馬爾科夫鏈蒙特卡洛(MCMC)和變分推斷(VI)算法的Python包,用于貝葉斯統(tǒng)計(jì)建模。其靈活性和可擴(kuò)展性使其適用于各種問題。PyMC是一個(gè)功能強(qiáng)大的貝葉斯建模工具,提供了豐富的特性和算法,適用于各種統(tǒng)計(jì)建模和推斷任務(wù)。包括(廣義)線性模型和層次線性模型案例研究、因果推斷、診斷和模型評(píng)估、高斯過程、ODE模型推斷、馬爾科夫鏈蒙特卡洛方法、混合模型、生存分析、時(shí)間序列、變分推斷。其特點(diǎn)如下:
>> 直觀的模型規(guī)范語(yǔ)法,例如,x ~ N(0,1) 可以翻譯為 x = Normal('x',0,1)
>> 強(qiáng)大的采樣算法,例如 No U-Turn Sampler,可以處理具有成千上萬個(gè)參數(shù)的復(fù)雜模型,>> 而無需特殊的擬合算法知識(shí)。
>> 變分推斷:提供快速近似后驗(yàn)估計(jì)的ADVI以及用于大型數(shù)據(jù)集的小批量ADVI。
>> 依賴于PyTensor提供:
>> 計(jì)算優(yōu)化和動(dòng)態(tài)的C或JAX編譯
>> NumPy廣播和高級(jí)索引
>> 線性代數(shù)運(yùn)算符
>> 簡(jiǎn)單的可擴(kuò)展性
>> 透明支持缺失值填充
GitHub鏈接:GitHub - pymc-devs/pymc: Bayesian Modeling in Python
文檔:Introductory Overview of PyMC — PyMC dev documentation
pymc的安裝
pip install pymc
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pymc
?文章來源:http://www.zghlxwxcb.cn/news/detail-722305.html
?
pymc的使用方法
更多案例:PyMC Example Gallery — PyMC example gallery文章來源地址http://www.zghlxwxcb.cn/news/detail-722305.html
1、時(shí)序性任務(wù)
(1)、使用 Euler-Maruyama 方案推斷 SDE 的參數(shù)
%pylab inline
import arviz as az
import pymc as pm
import scipy
import theano.tensor as tt
from pymc.distributions.timeseries import EulerMaruyama
%config InlineBackend.figure_format = 'retina'
az.style.use("arviz-darkgrid")
# parameters
λ = -0.78
σ2 = 5e-3
N = 200
dt = 1e-1
# time series
x = 0.1
x_t = []
# simulate
for i in range(N):
x += dt * λ * x + sqrt(dt) * σ2 * randn()
x_t.append(x)
x_t = array(x_t)
# z_t noisy observation
z_t = x_t + randn(x_t.size) * 5e-3
figure(figsize=(10, 3))
subplot(121)
plot(x_t[:30], "k", label="$x(t)$", alpha=0.5), plot(z_t[:30], "r", label="$z(t)$", alpha=0.5)
title("Transient"), legend()
subplot(122)
plot(x_t[30:], "k", label="$x(t)$", alpha=0.5), plot(z_t[30:], "r", label="$z(t)$", alpha=0.5)
title("All time")
tight_layout()
def lin_sde(x, lam):
return lam * x, σ2
with pm.Model() as model:
# uniform prior, but we know it must be negative
lam = pm.Flat("lam")
# "hidden states" following a linear SDE distribution
# parametrized by time step (det. variable) and lam (random variable)
xh = EulerMaruyama("xh", dt, lin_sde, (lam,), shape=N, testval=x_t)
# predicted observation
zh = pm.Normal("zh", mu=xh, sigma=5e-3, observed=z_t)
with model:
trace = pm.sample(2000, tune=1000)
figure(figsize=(10, 3))
subplot(121)
plot(percentile(trace[xh], [2.5, 97.5], axis=0).T, "k", label=r"$\hat{x}_{95\%}(t)$")
plot(x_t, "r", label="$x(t)$")
legend()
subplot(122)
hist(trace[lam], 30, label=r"$\hat{\lambda}$", alpha=0.5)
axvline(λ, color="r", label=r"$\lambda$", alpha=0.5)
legend();
# generate trace from posterior
ppc_trace = pm.sample_posterior_predictive(trace, model=model)
# plot with data
figure(figsize=(10, 3))
plot(percentile(ppc_trace["zh"], [2.5, 97.5], axis=0).T, "k", label=r"$z_{95\% PP}(t)$")
plot(z_t, "r", label="$z(t)$")
legend()
到了這里,關(guān)于Py之pymc:pymc的簡(jiǎn)介、安裝、使用方法之詳細(xì)攻略的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!