44: sample_m <- function(ycurrent, pars){
45:   # Extract the parameters from the pars dataframe
46:   a1 = pars$a1; b1 = pars$b1
47:   beta = pars$beta
48:   L = length(beta) # Length of the simplex
49:   
50:   ycand = rgamma(n = 1, shape = a1, rate = b1) # Propose a candidate
51:   # Numerator of MH ratio in log-scale
52:   num = - b1 * ycand + (a1 - 1)*log(ycand) + sum(((ycand/L) - 1) * log(beta)) + lgamma(ycand) - (L * lgamma(ycand/L)) + dgamma(ycurrent, shape = a1, rate = b1, log = TRUE)
53:   # Denominator of MH ratio in log-scale
54:   den = - b1 * ycurrent + (a1 - 1)*log(ycurrent) + sum(((ycurrent/L) - 1) * log(beta)) + lgamma(ycurrent) - (L * lgamma(ycurrent/L)) + dgamma(ycand, shape = a1, rate = b1, log = TRUE)
55:   MH = num - den # MH ratio which become difference in log-scale
56:   if(log(runif(n = 1)) < MH){ 
57:     out = ycand # Accept proposed value if log(U) < MH
58:     accept = 1
59:   }else{
60:     out = ycurrent # Reject proposed value if log(U) < MH
61:     accept = 0
62:   }
63:   return(list(out = out, accept = accept))
64: }
