interval {LaplacesDemon} | R Documentation |

## Constrain to Interval

### Description

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`

).

### Usage

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

### Arguments

`x` |
This required argument is a scalar, vector, matrix or array,
and its elements will be constrained to the interval
[ |

`a` |
This optional argument allows the specification of the lower
bound of the interval, and defaults to |

`b` |
This optional argument allows the specification of the upper
bound of the interval, and defaults to |

`reflect` |
Logical. When |

### Details

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.

### Value

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`

].

### Author(s)

Statisticat, LLC. software@bayesian-inference.com

### See Also

`dtrunc`

,
`ESS`

,
`IterativeQuadrature`

,
`LaplaceApproximation`

,
`LaplacesDemon`

,
`LPL.interval`

,
`PMC`

,
`p.interval`

,
`VariationalBayes`

.

### Examples

```
#See the Examples vignette for numerous examples.
library(LaplacesDemon)
x <- 2
interval(x,0,1)
X <- matrix(runif(25,-2,2),5,5)
interval(X,-1,1)
```

*LaplacesDemon*version 16.1.6 Index]