TABLE OF CONTENTS
MetropolisHastings/acceptreject [ Functions ]
NAME
acceptreject --- accept of reject a M-H step
FUNCTION
Handles accept-reject portion of every Metropolis-Hastings step. Given a base likelihood and a candidate loglikelihood, generates a random number and decides whether to accept or reject the step.
SYNOPSIS
2549 acceptreject <- function(baselik, candlik, ratio = 1)
INPUTS
baselik loglikelihood of the base model candlik loglikelihood of the candidate model ratio additional multiplier (e.g. prior ratio)
OUTPUTS
acc boolean, whether to accept or reject the jump
SOURCE
2552 { 2553 if(is.nan(candlik)) candlik <- -Inf 2554 # compute acceptance probability 2555 r <- exp(candlik - baselik) * ratio 2556 p.accept <- min(1, r) 2557 if(is.nan(p.accept)) p.accept <- 0 2558 # generate uniform and accept or reject 2559 if(runif(1) < p.accept){ 2560 return(TRUE) 2561 }else{ 2562 return(FALSE) 2563 } 2564 }