interval: Constrain to Interval

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/interval.R


This function constrains the value(s) of a scalar, vector, matrix, or array to a specified interval, [a,b]. In Bayesian inference, it is often used both to truncate a parameter to an interval, such as p(theta) in [a,b]. The interval function is often used in conjunction with the dtrunc function to truncate the prior probability distribution associated with the constrained parameter. While dtrunc prevents assigning density outside of its interval and re-estimates density within the interval, the interval function is used to prevent the parameter from moving outside of the interval in the first place.

After the parameter is constrained to an interval in IterativeQuadrature, LaplaceApproximation, LaplacesDemon, PMC, or VariationalBayes, the constrained parameter should be updated back into the parm vector, so the algorithm knows it has been constrained.

This is unrelated to the probability interval (see p.interval and LPL.interval).


interval(x, a=-Inf, b=Inf, reflect=TRUE)



This required argument is a scalar, vector, matrix or array, and its elements will be constrained to the interval [a,b].


This optional argument allows the specification of the lower bound of the interval, and defaults to -Inf.


This optional argument allows the specification of the upper bound of the interval, and defaults to Inf.


Logical. When TRUE, a value outside of the constrained interval is reflected or bounced back into the interval. When FALSE, a value outside of the interval is assigned the nearest boundary of the interval. This argument defaults to TRUE.


It is common for a parameter to be constrained to an interval. The interval function provides two methods of constraining proposals. The default is to reflect an out-of-bounds proposal off of the boundaries until the proposal is within the specified interval. This is rare in the literature but works very well in practice. The other method does not reflect off of boundaries, but sets the value equal to the violated boundary. This is also rare in the literature and is not generally recommended.

If the interval function is unacceptable, then there are several alternatives.

It is common to re-parameterize by transforming the constrained parameter to the real line. For example, a positive-only scale parameter may be log-transformed. A parameter that is re-parameterized to the real line often mixes better in MCMC, exhibiting a higher effective sample size (ESS), and each evaluation of the model specification function is faster as well. However, without a hard constraint, it remains possible for the transformed parameter still become problematic, such as a log-transformed scale parameter that reaches negative infinity. This is much more common in the literature.

Another method is to allow the parameters to move outside of the desired, constrained interval in MCMC during the model update, and when the model update is finished, to discard any samples outside of the constraint boundaries. This is a method of rejecting unacceptable proposals in regions of zero probability. However, it is possible for parameters to remain outside of acceptable bounds long enough to be problematic.

In LaplacesDemon, the Gibbs sampler allows more control in the FC function, where a user can customize how constraints are handled.


The interval function returns a scalar, vector, matrix, or array in accord with its argument, x. Each element is constrained to the interval [a,b].


Statisticat, LLC. [email protected]

See Also

dtrunc, ESS, IterativeQuadrature, LaplaceApproximation, LaplacesDemon, LPL.interval, PMC, p.interval, VariationalBayes.


#See the Examples vignette for numerous examples.
x <- 2
X <- matrix(runif(25,-2,2),5,5)

LaplacesDemon documentation built on July 1, 2018, 9:02 a.m.