日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MCMC算法之Metropolis-Hastings(MHs)算法(Matlab代码)

發(fā)布時間:2023/12/9 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MCMC算法之Metropolis-Hastings(MHs)算法(Matlab代码) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. Problem:

An MH step of invariant distribution p(x)p(x) and proposal distribution q(x?|x)q(x?|x) involves sampling a candidate value x?x? given the current value xx according to q(x?|x)q(x?|x). The Markov chain then moves towards x?x? with acceptance probability A(x,x?)=min{1,[p(x)q(x?|x)]?1p(x?)q(x|x?)}A(x,x?)=min{1,[p(x)q(x?|x)]?1p(x?)q(x|x?)}, otherwise it remains at xx. The pseudo code is shown in the figure 1, while the following figures show the results of running the MHs algorithm with a Gaussian proposal distribution q(x?|x(i))=N(x(i),10)q(x?|x(i))=N(x(i),10) and a bimodal target distribution p(x)0.3?exp(?0.2x2)+0.7?exp(?0.2(x?10)2)p(x)∝0.3exp(?0.2x2)+0.7exp(?0.2(x?10)2) for 10000 iterations. As expected, the histogram of the samples approximates the target distribution.

2. Pseudo code:

3. MHs cases:

Case 1: General acceptance probability:
Acceptance probability: A(x,x?)=min{1,p(x?)q(x|x?)p(x)q(x?|x)}A(x,x?)=min{1,p(x?)q(x|x?)p(x)q(x?|x)}
Case 2: Metropolis algorithm:
Acceptance probability: q(x?|x)=q(x|x?)?A(x,x?)=min{1,p(x?)p(x)}q(x?|x)=q(x|x?)?A(x,x?)=min{1,p(x?)p(x)}
Case 3: Independent sampler:
Acceptance probability: q(x?|x)=q(x?)?A(x,x?)=min{1,p(x?)q(x)p(x)q(x?)}q(x?|x)=q(x?)?A(x,x?)=min{1,p(x?)q(x)p(x)q(x?)}

4. Matlab code:

% Metropolis(-Hastings) algorithm % true (target) pdf is p(x) where we know it but can't sample data. % proposal (sample) pdf is q(x*|x)=N(x,10) where we can sample. %% clc clear; X(1)=0; N=1e4; p = @(x) 0.3*exp(-0.2*x.^2) + 0.7*exp(-0.2*(x-10).^2); dx=0.5; xx=-10:dx:20; fp=p(xx); plot(xx,fp) % plot the true p(x) %% MH algorithm sig=(10); for i=1:N-1u=rand;x=X(i); xs=normrnd(x,sig); % new sample xs based on existing x from proposal pdf.pxs=p(xs);px=p(x); qxs=normpdf(xs,x,sig);qx=normpdf(x,xs,sig); % get p,q.if u<min(1,pxs*qx/(px*qxs)) % case 1: pesudo code % if u<min(1,pxs/(px)) % case 2: Metropolis algorithm % if u<min(1,pxs/qxs/(px/qx)) % case 3: independent samplerX(i+1)=xs;elseX(i+1)=x; end end % compare pdf of the simulation result with true pdf. N0=1; close all;figure; %N/5; nb=histc(X(N0+1:N),xx); bar(xx+dx/2,nb/(N-N0)/dx); % plot samples. A=sum(fp)*dx; hold on; plot(xx,fp/A,'r') % compare. % figure(2); plot(N0+1:N,X(N0+1:N)) % plot the traces of x.% compare cdf with true cdf. F1(1)=0; F2(1)=0; for i=2:length(xx) F1(i)=F1(i-1)+nb(i)/(N-N0); F2(i)=F2(i-1)+fp(i)*dx/A; endfigure plot(xx,[F1' F2']) max(F1-F2) % this is the true possible measure of accuracy.

5. Results:

Result of Case 1:

Result of Case 2:

Result of Case 3:


References:

  • An Introduction to MCMC for Machine Learning
  • http://www2.mae.ufl.edu/haftka/vvuq/lectures/MCMC-Metropolis-practice.pptx
  • 總結

    以上是生活随笔為你收集整理的MCMC算法之Metropolis-Hastings(MHs)算法(Matlab代码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。