MCMC: The Gibbs Sampler

多元高斯分布的边缘概率和条件概率

Marginal and conditional distributions of multivariate normal distribution

clear, clc
rng('default') num_samples = 5000;
num_dims = 2; mu = [0, 0];
rho(1) = .8; rho(2) = .8; prop_sigma = 1;
minn = [-3, -3]; maxx = [3, 3];
x = zeros(num_samples, num_dims); x(1, 1) = unifrnd(minn(1), maxx(1));
x(1, 2) = unifrnd(minn(2), maxx(2)); t = 1;
dims = 1:num_dims;
while t < num_samples
t = t + 1;
T = [t-1, t]; % 时刻信息的维护,T(1):上一时刻,T(2):下一时刻
for iD = 1:num_dims
not_idx = (dims ~= iD);
mu_cond = mu(iD) + rho(iD)*(x(T(iD), not_idx) - mu(not_idx));
sigma_cond = sqrt(1-rho(iD)^2);
x(t, iD) = normrnd(mu_cond, sigma_cond);
end
end figure;
h1 = scatter(x(:, 1), x(:, 2), 'r.');
hold on for t=1:50
plot([x(t, 1), x(t+1, 1)], [x(t, 2), x(t, 2)], 'k-'); % x 轴方向移动,
plot([x(t+1, 1), x(t+1, 1)], [x(t, 2), x(t+1, 2)], 'k-'); % y 轴方向移动;
h2 = plot(x(t+1, 1), x(t+1, 2), 'ko');
end h3 = scatter(x(1, 1), x(1, 2), 'go', 'linewidth', 3);
legend([h1, h2, h3], {'Samples', '1st 50 samples', 'x(t=0)'}, 'location', 'northwest');
hold off;
xlabel('x_1'); ylabel('x_2')
axis square

最新文章

  1. .NET Core采用的全新配置系统[7]: 将配置保存在数据库中
  2. HTML 兼容性
  3. Loadrunner中关联的作用:
  4. (四)、 nodejs中Async详解之一:流程控制
  5. 第二百五十天 how can I 坚持
  6. SQL Server中时间段查询
  7. C#功能扩张方式
  8. 第三篇:web之前端之JavaScript基础
  9. -_-#【Node】Express 400 Error: ENOENT, open
  10. C# DateTime的ToString()方法的使用
  11. 6.Hibernate单向的多对一 关联映射
  12. 最近项目用到Dubbo框架,分享一下~
  13. 超强、超详细Redis数据库入门教程(转载)
  14. java项目编码格式转换(如GBK转UTF-8)
  15. xBIM IFC 墙壁案例
  16. disjoint set
  17. 求矩形面积(问题来自PythonTip)
  18. public private protect
  19. light oj 1007 Mathematically Hard (欧拉函数)
  20. hihocoder 1322 - 树结构判定 - [hiho一下161周][模板题/水题]

热门文章

  1. RFID的基本组织构成
  2. drawable-图片绘制
  3. [selenium]-处理滚动条
  4. app 自动化测试 Appium+Java可以运行的代码
  5. 配置PL/SQL Developer连接server数据库
  6. GO语言学习(十一)Go 语言循环语句
  7. GDB中创建要素数据集
  8. [React Intl] Render Content with Markup Using react-intl FormattedHTMLMessage
  9. 卫星网络中使用TCP协议的劣势(所以才有TCP优化版用来卫星通信啊,比如TCP-Peach和ADolar)
  10. netty reactor线程模型分析