Title: | Rcpp-Based Truncated Normal Distribution RNG and Family |
---|---|
Description: | R-level and C++-level functionality to generate random deviates from and calculate moments of a Truncated Normal distribution using the algorithm of Robert (1995) <DOI:10.1007/BF00143942>. In addition to RNG, functions for calculating moments, densities, and entropies are provided at both levels. |
Authors: | Jonathan Olmsted [aut, cre] |
Maintainer: | Jonathan Olmsted <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.2-2 |
Built: | 2024-11-06 03:56:17 UTC |
Source: | https://github.com/olmjo/rcpptn |
Calculate density of Truncated Normal distributions
dtn(.x = 0, .mean = rep(0, length(.x)), .sd = rep(1, length(.x)), .low = rep(-Inf, length(.x)), .high = rep(Inf, length(.x)), .checks = TRUE)
dtn(.x = 0, .mean = rep(0, length(.x)), .sd = rep(1, length(.x)), .low = rep(-Inf, length(.x)), .high = rep(Inf, length(.x)), .checks = TRUE)
.x |
Length K vector of the points at which to evaluate the density |
.mean |
Length K vector with the means of the K Normal distributions *prior* to truncation |
.sd |
Length K vector with the standard deviations of the K Normal distributions *prior* to truncation |
.low |
Length K vector with the lower truncation bound of the K Normal distributions *prior* to truncation |
.high |
Length K vector with the upper truncation bound of the K Normal distributions *prior* to truncation |
.checks |
Logical indicating whether inputs and outputs should be checked and either stop (for bad inputs) or warn (for likely bad outputs) |
Length K vector with the entropies associated with each of the K Truncated Normal distributions
Jonathan Olmsted
lows <- c(-1, 5, -100, 4, 4, -100, 7) highs <- c(1, 100, 10, 7, 4.1, 100, 100) dtn(.x = rep(0, length(lows)), .mean = rep(0, length(lows)), .sd = rep(1, length(lows)), .high = highs )
lows <- c(-1, 5, -100, 4, 4, -100, 7) highs <- c(1, 100, 10, 7, 4.1, 100, 100) dtn(.x = rep(0, length(lows)), .mean = rep(0, length(lows)), .sd = rep(1, length(lows)), .high = highs )
Calculate entropy of Truncated Normal distributions
enttn(.mean = rep(0, 1), .sd = rep(1, length(.mean)), .low = rep(-Inf, length(.mean)), .high = rep(Inf, length(.mean)))
enttn(.mean = rep(0, 1), .sd = rep(1, length(.mean)), .low = rep(-Inf, length(.mean)), .high = rep(Inf, length(.mean)))
.mean |
Length K vector with the means of the K Normal distributions prior to truncation |
.sd |
Length K vector with the standard deviations of the K Normal distributions prior to truncation |
.low |
Length K vector with the lower truncation bound of the K Normal distributions prior to truncation |
.high |
Length K vector with the upper truncation bound of the K Normal distributions prior to truncation |
Length K vector with the entropies associated with each of the K Truncated Normal distributions
Jonathan Olmsted
lows <- c(-1, 5, -100, 4, 4, -100, 7) highs <- c(1, 100, 10, 7, 4.1, 100, 100) enttn(.mean = rep(0, length(lows)), .sd = rep(1, length(lows)), .low = lows, .high = highs )
lows <- c(-1, 5, -100, 4, 4, -100, 7) highs <- c(1, 100, 10, 7, 4.1, 100, 100) enttn(.mean = rep(0, length(lows)), .sd = rep(1, length(lows)), .low = lows, .high = highs )
Calculate expectation of Truncated Normal distributions
etn(.mean = rep(0, 1), .sd = rep(1, length(.mean)), .low = rep(-Inf, length(.mean)), .high = rep(Inf, length(.mean)), .checks = TRUE)
etn(.mean = rep(0, 1), .sd = rep(1, length(.mean)), .low = rep(-Inf, length(.mean)), .high = rep(Inf, length(.mean)), .checks = TRUE)
.mean |
Length K vector with the means of the K Normal distributions prior to truncation |
.sd |
Length K vector with the standard deviations of the K Normal distributions prior to truncation |
.low |
Length K vector with the lower truncation bound of the K Normal distributions prior to truncation |
.high |
Length K vector with the upper truncation bound of the K Normal distributions prior to truncation |
.checks |
Length 1 logical vector indicating whether to perform checks (safer) or not (faster) on the input parameters |
The special values of -Inf and Inf are valid values in the .low and .high arguments, respectively.
A length K vector of expectations corresponding to the Truncated Normal distributions. NAs are returned (with a warning) for invalid parameter values.
Jonathan Olmsted
etn() ## 0 etn(0, 1, -Inf, Inf) ## 0 etn(0, 1, -9999, 9999) ## 0 etn(0, 1, 0, Inf) ## 0.798 etn(0, 1, Inf, -Inf) ## NA with warning etn(c(0, 0), c(1, 1), c(-Inf, 5), c(1, Inf) ) ## multiple expectations
etn() ## 0 etn(0, 1, -Inf, Inf) ## 0 etn(0, 1, -9999, 9999) ## 0 etn(0, 1, 0, Inf) ## 0.798 etn(0, 1, Inf, -Inf) ## NA with warning etn(c(0, 0), c(1, 1), c(-Inf, 5), c(1, Inf) ) ## multiple expectations
Sample from Truncated Normal distributions
rtn(.mean = rep(0, 1), .sd = rep(1, length(.mean)), .low = rep(-Inf, length(.mean)), .high = rep(Inf, length(.mean)), .checks = TRUE)
rtn(.mean = rep(0, 1), .sd = rep(1, length(.mean)), .low = rep(-Inf, length(.mean)), .high = rep(Inf, length(.mean)), .checks = TRUE)
.mean |
Length K vector with the means of the K Normal distributions prior to truncation |
.sd |
Length K vector with the standard deviations of the K Normal distributions prior to truncation |
.low |
Length K vector with the lower truncation bound of the K Normal distributions prior to truncation |
.high |
Length K vector with the upper truncation bound of the K Normal distributions prior to truncation |
.checks |
Length 1 logical vector indicating whether to perform checks (safer) or not (faster) on the input parameters |
The special values of -Inf and Inf are valid values in the .low and .high arguments, respectively. The implementation is from Robert (1995). The computation is written in Rcpp-based C++ code, but respects R's RNG state. The draws from this function are reproducible because it respects R's RNG state. Draws using this algorithm (whether implemented in R code or C++) will be the same if seeded correctly. However, you should not expect these draws to match those from another algorithm.
A length K vector of expectations corresponding to the Truncated Normal distributions. NAs are returned (with a warning) for invalid parameter values.
Jonathan Olmsted
Robert, Christian P. “Simulation of truncated normal variables”. Statistics and Computing 5.2 (1995): 121-125. http://dx.doi.org/10.1007/BF00143942
set.seed(1) rtn(0, 1, -Inf, Inf) # single draw from a single distribution ## [1] -0.6264538 set.seed(1) rtn(0, 1, -Inf, Inf) # again, because it respects the RNG state ## [1] -0.6264538 rtn(rep(0, 3), rep(1, 3), rep(-Inf, 3), rep(Inf, 3) ) # multiple draws from a single distribution ## [1] 0.1836433 -0.8356286 1.5952808 rtn(c(0, 0), c(1, 1), c(-Inf, 5), c(1, Inf) ) # multiple draws, each from a different distribution ## [1] 0.3295078 5.3917301
set.seed(1) rtn(0, 1, -Inf, Inf) # single draw from a single distribution ## [1] -0.6264538 set.seed(1) rtn(0, 1, -Inf, Inf) # again, because it respects the RNG state ## [1] -0.6264538 rtn(rep(0, 3), rep(1, 3), rep(-Inf, 3), rep(Inf, 3) ) # multiple draws from a single distribution ## [1] 0.1836433 -0.8356286 1.5952808 rtn(c(0, 0), c(1, 1), c(-Inf, 5), c(1, Inf) ) # multiple draws, each from a different distribution ## [1] 0.3295078 5.3917301
Calculate variance of Truncated Normal distributions
vtn(.mean = rep(0, 1), .sd = rep(1, length(.mean)), .low = rep(-Inf, length(.mean)), .high = rep(Inf, length(.mean)), .checks = TRUE)
vtn(.mean = rep(0, 1), .sd = rep(1, length(.mean)), .low = rep(-Inf, length(.mean)), .high = rep(Inf, length(.mean)), .checks = TRUE)
.mean |
Length K vector with the means of the K Normal distributions prior to truncation |
.sd |
Length K vector with the standard deviations of the K Normal distributions prior to truncation |
.low |
Length K vector with the lower truncation bound of the K Normal distributions prior to truncation |
.high |
Length K vector with the upper truncation bound of the K Normal distributions prior to truncation |
.checks |
Length 1 logical vector indicating whether to perform checks (safer) or not (faster) on the input parameters |
The special values of -Inf and Inf are valid values in the .low and .high arguments, respectively.
A length K vector of expectations corresponding to the Truncated Normal distributions. NAs are returned (with a warning) for invalid. parameter values.
Jonathan Olmsted
vtn() ## 1 vtn(0, 1, -Inf, Inf) ## 1 vtn(0, 1, -9999, 9999) ## 1 vtn(0, 1, 0, Inf) ## 0.36338 vtn(0, 1, Inf, -Inf) ## NA with warning vtn(c(0, 0), c(1, 1), c(-Inf, 5), c(1, Inf) ) ## multiple variances
vtn() ## 1 vtn(0, 1, -Inf, Inf) ## 1 vtn(0, 1, -9999, 9999) ## 1 vtn(0, 1, 0, Inf) ## 0.36338 vtn(0, 1, Inf, -Inf) ## NA with warning vtn(c(0, 0), c(1, 1), c(-Inf, 5), c(1, Inf) ) ## multiple variances