Hastings - Metropolis Algorithm implementation

by Naser Abbasi. Mathematics 504, Spring 2008. CSUF

This below is an implementation of the Hastings - Metropolis algorithm. A simple GUI interface allows the user to specify the number of steps to run the algorithm for. At each step, the current P matrix and the current calculated stationary distribution for this P matrix are shown to help observe the convergence.

The input to this run below is that of example 8.3.1 from Lecture notes of Math 504 by Professor B. Gearhart, CSUF. Below I show the user interface, and few screen shots showing the progress of the convergence to the final P matrix.

Few seed the random number generator and display the q and the π distribution used

In[1]:=

"nma_hastings_1.gif"

Out[2]//MatrixForm=

"nma_hastings_2.gif"

In[3]:=

"nma_hastings_3.gif"

Out[3]//MatrixForm=

"nma_hastings_4.gif"

In[9]:=

"nma_hastings_5.gif"

Out[9]=

"nma_hastings_6.gif"

This a snap shot of the above output for number of iterations

"nma_hastings_7.gif"

"nma_hastings_8.gif"

Appending

Algorithm implementation

In[4]:=

"nma_hastings_9.gif"

Function to calculate β (x, y)

In[5]:=

"nma_hastings_10.gif"

Function called at the end of the run to generate P from the path of states travelled

In[6]:=

"nma_hastings_11.gif"

Function to sample from q using uniform distribution

In[7]:=

"nma_hastings_12.gif"

The Hastings algorithm main loop

In[8]:=

"nma_hastings_13.gif"


Created by Wolfram Mathematica 6.0 for Students - Personal Use Only  (10 April 2008) Valid XHTML 1.1!