Skip to contents

Maps unconstrained values to positive values via exp(x). Use this for parameters like standard deviations, rates, or variances.

Usage

positive(x)

Arguments

x

Unconstrained value (scalar, vector, or value object)

Value

Positive value (always > 0)

Details

The transformation is: positive(x) = exp(x)

For optimization, work with the unconstrained parameter and transform:


  result <- fit(
    function(mu, log_sigma) {
      sigma <- positive(log_sigma)  # exp(log_sigma)
      loglik_normal(mu, sigma, data)
    },
    params = c(mu = 0, log_sigma = 0)  # log(1) = 0
  )
  # To recover sigma: exp(coef(result)["log_sigma"])

Examples

if (FALSE) { # \dontrun{
# Parameter that must be positive
log_sigma <- val(-1)
sigma <- positive(log_sigma)  # exp(-1) ≈ 0.368
get_data(sigma)

# Works in optimization
fit(
  function(mu, log_sigma) loglik_normal(mu, positive(log_sigma), x),
  params = c(mu = 0, log_sigma = 0)
)
} # }