% % Example on epage 445 % % Problem on epage 471 % % Written by: % -- % John L. Weatherwax 2008-02-20 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- clear all; close all; clc; addpath('../../Code/CSTool'); randn('seed',0); rand('seed',0); % a constant: b = 3; % store the von Mies distribution: vm = inline( 'exp(b*cos(x))/(2*pi*besseli(0,b))', 'b', 'x' ); % generate "n" iterations of the chain: n = 10000; % set up some memory: X = zeros(1,n); % get a starting value: X(1) = 1; for i=2:n, % generate a variable from the proposal distribution: y = unifrnd(-1,+1); %y = unifrnd(-4,+4); % calculate alpha: alpha = min([ 1, vm(b,y)/vm(b,X(i-1)) ]); if( rand(1,1) < alpha ) X(i)=y; % set the chain to y else X(i)=X(i-1); end end figure; plot( X, 'x' ); xlabel('iteration value'); ylabel('X(i)'); saveas( gcf, '../../WriteUp/Graphics/Chapter11/prob_11_1_chain', 'epsc' ); figure; %hist( X, 20 ); cshistden( X, 20 ); title( 'univariate density histogram' ); saveas( gcf, '../../WriteUp/Graphics/Chapter11/prob_11_1_hist', 'epsc' ); clear functions;