. clear //清空工作區 . set seed 20 //設定隨機種子 . set obs 10000 //設定觀測值數量,在這裡 n=10000 . gen y = runiform(0,1) > 0.5 //生成服從伯努利分布的樣本,記為 y . bayesmh y, likelihood(dbernoulli({theta})) prior({theta}, beta(1,1)) //估計命令
Burn-in ... Simulation ...
Model summary -- Likelihood: y ~ bernoulli({theta})
這一操作步驟看似複雜,但在 Stata 中的實現其實很容易,只需要在線性回歸的命令前加上 bayes: 即可,但需要注意的是,此時回歸命令不能縮寫,並且如果要用吉布斯採樣,需要使用 bayes,gibbs:。
. clear //清空工作區 . set seed 20 //設定隨機種子 . sysuse auto //使用系統自帶的 auto 數據集 . bayes, gibbs: regress price mpg weight //執行吉布斯採樣的貝葉斯估計
Burn-in ... Simulation ...
Model summary ---- Likelihood: price ~ normal(xb_price,{sigma2})
Priors: {price:mpg weight _cons} ~ normal(0,10000) (1) {sigma2} ~ igamma(.01,.01) ---- (1) Parameters are elements of the linear form xb_price.
Bayesian linear regression MCMC iterations = 12,500 Gibbs sampling Burn-in = 2,500 MCMC sample size = 10,000 Number of obs = 74 Acceptance rate = 1 Efficiency: min = .9457 avg = .9864 Log marginal likelihood = -696.85658 max = 1
---- | Equal-tailed | Mean Std. Dev. MCSE Median [95% Cred. Interval] ----+---- price | mpg | -5.282435 27.88942 .278894 -5.410969 -59.53671 49.12283 weight | 2.074039 .198759 .001988 2.076242 1.682404 2.464258 _cons | 1.928181 99.76441 .997644 2.99603 -202.2644 194.9716 ----+---- sigma2 | 6448502 1099995 11311.5 6331241 4641478 8928296 ---- Note: Default priors are used for model parameters.
6. 參考資料
溫馨提示: 文中連結在微信中無法生效。請點擊底部「閱讀原文」。
M-H 算法和共軛先驗的維基百科 -Link1- -Link2-Blake A P, Mumtaz H. Applied Bayesian econometrics for central bankers[J]. Technical Books, 2012. -Link-Koop G, Korobilis D. Bayesian multivariate time series methods for empirical macroeconomics[M]. Now Publishers Inc, 2010. -Link-Bill Rising. Bayesian Analysis using Stata. 2015. -Link-
7. 相關推文
Note:產生如下推文列表的命令為: lianxh VAR DSGE 分布, m 安裝最新版 lianxh 命令: ssc install lianxh, replace
溫馨提示: 文中連結在微信中無法生效。請點擊底部「閱讀原文」。
Stata程序:Monte-Carlo-模擬之產生符合特定分布的隨機數VaR 風險價值: Stata 及 Python 實現