MH                package:calibrator                R Documentation

_V_e_r_y _b_a_s_i_c _i_m_p_l_e_m_e_n_t_a_t_i_o_n _o_f _t_h_e _M_e_t_r_o_p_o_l_i_s-_H_a_s_t_i_n_g_s _a_l_g_o_r_i_t_h_m

_D_e_s_c_r_i_p_t_i_o_n:

     Very basic implementation of the Metropolis-Hastings algorithm
     using a multivariate Gaussian proposal distribution.   Useful for
     sampling from 'p.eqn8.supp()'.

_U_s_a_g_e:

     MH(n, start, sigma, pi)

_A_r_g_u_m_e_n_t_s:

       n: Number of samples to take

   start: Start value

   sigma: Variance matrix for kernel

      pi: Functional proportional to the desired sampling pdf

_D_e_t_a_i_l_s:

     This is a *basic* implementation.  The proposal
     distribution~q(X|Y) is q(.|X)=N(X,sigma^2)

_V_a_l_u_e:

     Returns a matrix whose rows are samples from pi().  Note that the
     first few rows will be "burn-in", so should be ignored

_N_o_t_e:

     This function is a little slow because it is not vectorized.

_A_u_t_h_o_r(_s):

     Robin K. S. Hankin

_R_e_f_e_r_e_n_c_e_s:

     W. R. Gilks et al 1996. _Markov Chain Monte Carlo in practice_. 
     Chapman and Hall, 1996.    ISBN 0-412-05551-1

_S_e_e _A_l_s_o:

     'p.eqn8.supp'

_E_x_a_m_p_l_e_s:

     # First, a bivariate Gaussian:
     A <- diag(3) + 0.7
     quad.form <- function(M,x){drop(crossprod(crossprod(M,x),x))}
     pi.gaussian <- function(x){exp(-quad.form(A/2,x))}
     x.gauss <- MH(n=1000, start=c(0,0,0),sigma=diag(3),pi=pi.gaussian)
     cov(x.gauss)/solve(A) # Should be a matrix of 1s.

     # Now something a bit weirder:
     pi.triangle <- function(x){
       1*as.numeric( (abs(x[1])<1.0) & (abs(x[2])<1.0) ) +
       5*as.numeric( (abs(x[1])<0.5) & (abs(x[2])<0.5) ) *
         as.numeric(x[1]>x[2])
     }
     x.tri <- MH(n=100,start=c(0,0),sigma=diag(2),pi=pi.triangle)
     plot(x.tri,main="Try with a higher n")

     # Now a Gaussian mixture model:
     pi.2gauss <- function(x){
       exp(-quad.form(A/2,x)) +
       exp(-quad.form(A/2,x+c(2,2,2)))
     }
     x.2 <- MH(n=100,start=c(0,0,0),sigma=diag(3),pi=pi.2gauss)
     ## Not run: p3d(x.2, theta=44,d=1e4,d0=1,main="Try with more points")

