⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 schools_demo.m

📁 matlab中实现openbugs或winbugs的功能调用
💻 M
字号:
% An example illustrating how to call matbugs.m% We use the schools data/model from p140/ p592 of Gelman's book% Written by Maryam Mahdaviani, August 2005% Modified by Kevin Murphy (murphyk@cs.ubc.ca), 11 October 2007dataStruct = struct('J', 8, ...                    'y', [28, 8, -3, 7, -1, 1, 18, 12], ...                   'sigma_y', [15, 10, 16, 11, 9, 11, 10, 18]);Nchains = 3;% we initialize the params to the observed data values, but with decreasing% confidence, as suggested on p593 of Gelmanclear initStructsfor i=1:Nchains  S.theta = dataStruct.y;  S.mu_theta = 0;  S.sigma_theta = 10^i; % each chain becomes more over-dispersed  initStructs(i) = S;end[samples, stats] = matbugs(dataStruct, ...		fullfile(pwd, 'schools_model.txt'), ...		'init', initStructs, ...		'view', 0, 'nburnin', 1000, 'nsamples', 500, ...		'thin', 10, ...		'monitorParams', {'theta', 'mu_theta', 'sigma_theta'}, ...		'Bugdir', 'C:/Program Files/WinBUGS14');stats.Rhatstats.mean% Traceplotsfigure(1);clfcolors = 'rgb';for j=1:8  subplot(3,3,j); hold on  for c=1:Nchains    %plot(s(c).theta(:,j), colors(c));    plot(samples.theta(c,:,j), colors(c));  end  title(sprintf('theta %d', j));endsubplot(3,3,9); hold onfor c=1:Nchains  plot(samples.mu_theta(c,:), colors(c));endtitle(sprintf('mu.theta'))% Posterior summaries - kernel density estimationfigure(1); clffor j=1:8  subplot(3,3,j); hold on  %dat = samples.theta(:,:,j);  for c=1:Nchains    [p, x] = ksdensity(samples.theta(c,:,j));    plot(x, p, colors(c));  end  title(sprintf('theta %d', j));endsubplot(3,3,9); hold onfor c=1:Nchains  [p, x] = ksdensity(samples.mu_theta(c,:));  plot(x, p, colors(c));endtitle(sprintf('mu.theta'))% Posterior summaries - intervalsfigure(1); clfhold onfor j=1:8  for c=1:Nchains    q = quantile(samples.theta(c,:,j), [0.1 0.9]);    h = line([q(1) q(2)], [j j]+c*0.1); set(h, 'color', colors(c));    q = quantile(samples.theta(c,:,j), [0.5]);    h=plot(q,j+c*0.1,'*'); set(h, 'color', colors(c));  end  legendstr{j} = sprintf('theta %d', j);endj = 9;for c=1:Nchains  q = quantile(samples.mu_theta(c,:), [0.1 0.9]);  h = line([q(1) q(2)], [j j]+c*0.1); set(h, 'color', colors(c));  q = quantile(samples.mu_theta(c,:), [0.5]);  h=plot(q,j+c*0.1,'*'); set(h, 'color', colors(c));endlegendstr{j} = sprintf('mu.theta');set(gca,'yticklabel',[])xlim = get(gca, 'xlim');for i=1:j  text(xlim(1), i, legendstr{i});endtitle('80pc posterior intervals (*=median)')

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -