Can estimations go beyond outside the range of the inits?

Yes, the inits and inits control the range of the initial values, but the chain is still allowed to move freely after this initial step, as shown in the following example.

Please report to the next question to learn how you can specify hard limits for the chains.

## a log-pdf to sample from
p.log <- function(x) {
  B <- 0.03 # controls 'bananacity'
  -x[1]^2 / 200 - 1 / 2 * (x[2] + B * x[1]^2 - 100 * B)^2
}

unif_inits <- data.frame(
  a = runif(10, min = -10, max = -5),
  b = runif(10, min = -10, max = -5)
)

set.seed(20201209)

res1 <- MCMCEnsemble(
  p.log,
  inits = unif_inits,
  max.iter = 3000, n.walkers = 10,
  method = "stretch",
  coda = TRUE
)
#> Using stretch move with 10 walkers.

summary(res1$samples)
#> 
#> Iterations = 1:300
#> Thinning interval = 1 
#> Number of chains = 10 
#> Sample size per chain = 300 
#> 
#> 1. Empirical mean and standard deviation for each variable,
#>    plus standard error of the mean:
#> 
#>     Mean     SD Naive SE Time-series SE
#> a -4.660 11.095   0.2026         1.1048
#> b -1.653  6.241   0.1139         0.6989
#> 
#> 2. Quantiles for each variable:
#> 
#>     2.5%     25%     50%   75%  97.5%
#> a -27.45 -11.908 -4.9494 2.327 19.054
#> b -20.50  -4.833  0.8194 2.600  4.453
plot(res1$samples)

res2 <- MCMCEnsemble(
  p.log,
  inits = unif_inits,
  max.iter = 3000, n.walkers = 10,
  method = "differential.evolution",
  coda = TRUE
)
#> Using differential.evolution move with 10 walkers.

summary(res2$samples)
#> 
#> Iterations = 1:300
#> Thinning interval = 1 
#> Number of chains = 10 
#> Sample size per chain = 300 
#> 
#> 1. Empirical mean and standard deviation for each variable,
#>    plus standard error of the mean:
#> 
#>     Mean    SD Naive SE Time-series SE
#> a 0.4583 9.288   0.1696         0.9237
#> b 0.2161 3.489   0.0637         0.3069
#> 
#> 2. Quantiles for each variable:
#> 
#>      2.5%    25%     50%   75%  97.5%
#> a -15.517 -6.483 -0.0924 7.129 19.159
#> b  -8.662 -1.363  1.2419 2.622  4.239
plot(res2$samples)

How to restrict the possible parameter range?

There is no built-in way to define hard limits for the parameter and make sure they never go outside of this range.

The recommended way to address this issue is to handle these cases in the function f you provide.

For example, to keep parameters in the 0-1 range:

p.log.restricted <- function(x) {
  
  if (any(x < 0, x > 1)) {
    return(-Inf)
  }
  
  B <- 0.03 # controls 'bananacity'
  -x[1]^2 / 200 - 1 / 2 * (x[2] + B * x[1]^2 - 100 * B)^2
}

unif_inits <- data.frame(
  a = runif(10, min = 0, max = 1),
  b = runif(10, min = 0, max = 1)
)

res <- MCMCEnsemble(
  p.log.restricted,
  inits = unif_inits,
  max.iter = 3000, n.walkers = 10,
  method = "stretch",
  coda = TRUE
)
#> Using stretch move with 10 walkers.
summary(res$samples)
#> 
#> Iterations = 1:300
#> Thinning interval = 1 
#> Number of chains = 10 
#> Sample size per chain = 300 
#> 
#> 1. Empirical mean and standard deviation for each variable,
#>    plus standard error of the mean:
#> 
#>     Mean     SD Naive SE Time-series SE
#> a 0.4893 0.2933 0.005354        0.02960
#> b 0.6585 0.2659 0.004854        0.02577
#> 
#> 2. Quantiles for each variable:
#> 
#>      2.5%    25%    50%    75%  97.5%
#> a 0.01970 0.2284 0.4886 0.7428 0.9728
#> b 0.05949 0.4930 0.7391 0.8683 0.9904
plot(res$samples)

This might seem inconvenient but in most cases, users will define their posterior probability as the product of a prior probability and the likelihood. In this situation, values that are not contained in the log-prior density automatically return -Inf in the log-posterior and it is not necessary to define it explicitly:

prior.log <- function(x) {
 dunif(x, log = TRUE)
}

lkl.log <- function(x) {
  B <- 0.03 # controls 'bananacity'
  -x[1]^2 / 200 - 1 / 2 * (x[2] + B * x[1]^2 - 100 * B)^2
}

posterior.log <- function(x) {
  sum(prior.log(x)) + lkl.log(x)
}

res <- MCMCEnsemble(
  posterior.log,
  inits = unif_inits,
  max.iter = 5000, n.walkers = 10,
  method = "stretch",
  coda = TRUE
)
#> Using stretch move with 10 walkers.
summary(res$samples)
#> 
#> Iterations = 1:500
#> Thinning interval = 1 
#> Number of chains = 10 
#> Sample size per chain = 500 
#> 
#> 1. Empirical mean and standard deviation for each variable,
#>    plus standard error of the mean:
#> 
#>     Mean     SD Naive SE Time-series SE
#> a 0.5154 0.2902 0.004104        0.02478
#> b 0.6838 0.2356 0.003332        0.02035
#> 
#> 2. Quantiles for each variable:
#> 
#>      2.5%    25%    50%    75%  97.5%
#> a 0.02785 0.2611 0.5357 0.7700 0.9854
#> b 0.12319 0.5347 0.7367 0.8734 0.9893
plot(res$samples)