【发布时间】:2020-01-03 00:16:08
【问题描述】:
我正在尝试使用带有随机游走采样器的 Metropolis Hastings 算法来模拟来自 matlab 中函数 $$ 的样本,但我的代码有问题。提议密度是椭圆上的均匀 PDF 2s^2 + 3t^2 ≤ 1/4。我可以使用接受拒绝方法从提案密度中抽样吗?
N=5000;
alpha = @(x1,x2,y1,y2) (min(1,f(y1,y2)/f(x1,x2)));
X = zeros(2,N);
accept = false;
n = 0;
while n < 5000
accept = false;
while ~accept
s = 1-rand*(2);
t = 1-rand*(2);
val = 2*s^2 + 3*t^2;
% check acceptance
accept = val <= 1/4;
end
% and then draw uniformly distributed points checking that u< alpha?
u = rand();
c = u < alpha(X(1,i-1),X(2,i-1),X(1,i-1)+s,X(2,i-1)+t);
X(1,i) = c*s + X(1,i-1);
X(2,i) = c*t + X(2,i-1);
n = n+1;
end
figure;
plot(X(1,:), X(2,:), 'r+');
【问题讨论】:
标签: matlab random mcmc random-walk