truncatedNormalMoment: Calculate k-th order moment of truncated normal distribution

View source: R/RcppExports.R

truncatedNormalMomentR Documentation

Calculate k-th order moment of truncated normal distribution

Description

This function recursively calculates k-th order moment of truncated normal distribution.

Usage

truncatedNormalMoment(
  k = 1L,
  x_lower = numeric(0),
  x_upper = numeric(0),
  mean = 0,
  sd = 1,
  pdf_lower = numeric(0),
  cdf_lower = numeric(0),
  pdf_upper = numeric(0),
  cdf_upper = numeric(0),
  cdf_difference = numeric(0),
  return_all_moments = FALSE,
  is_validation = TRUE,
  is_parallel = FALSE,
  diff_type = "NO"
)

Arguments

k

non-negative integer moment order.

x_lower

numeric vector of lower truncation points.

x_upper

numeric vector of upper truncation points.

mean

numeric expected value.

sd

positive numeric standard deviation.

pdf_lower

non-negative numeric matrix of precalculated normal density functions with mean mean and standard deviation sd at points given by x_lower.

cdf_lower

non-negative numeric matrix of precalculated normal cumulative distribution functions with mean mean and standard deviation sd at points given by x_lower.

pdf_upper

non-negative numeric matrix of precalculated normal density functions with mean mean and standard deviation sd at points given by x_upper.

cdf_upper

non-negative numeric matrix of precalculated normal cumulative distribution functions with mean mean and standard deviation sd at points given by x_upper.

cdf_difference

non-negative numeric matrix of precalculated cdf_upper-cdf_lower values.

return_all_moments

logical; if TRUE, function returns the matrix of moments of normally distributed random variable with mean = mean and standard deviation = sd under lower and upper truncation points x_lower and x_upper correspondingly. Note that element in i-th row and j-th column of this matrix corresponds to the i-th observation (j-1)-th order moment.

is_validation

logical value indicating whether function input arguments should be validated. Set it to FALSE for slight performance boost (default value is TRUE).

is_parallel

if TRUE then multiple cores will be used for some calculations. It usually provides speed advantage for large enough samples (about more than 1000 observations).

diff_type

string value indicating the type of the argument the moment should be differentiated respect to. Default value is "NO" so the moments itself will be returned. Alternative values are "mean" and "sd". Also "x_lower" and "x_upper" values are available for truncatedNormalMoment.

Details

This function estimates k-th order moment of normal distribution which mean equals to mean and standard deviation equals to sd truncated at points given by x_lower and x_upper. Note that the function is vectorized so you can provide x_lower and x_upper as vectors of equal size. If vectors values for x_lower and x_upper are not provided then their default values will be set to -(.Machine$double.xmin * 0.99) and (.Machine$double.xmax * 0.99) correspondingly.

Note that parameter k value automatically converts to integer. So passing non-integer k value will not cause any errors but the calculations will be performed for rounded k value only.

If there is precalculated density or cumulative distribution functions at standardized truncation points (subtract mean and then divide by sd) then it is possible to provide them through pdf_lower, pdf_upper, cdf_lower and cdf_upper arguments in order to decrease number of calculations.

Value

This function returns vector of k-th order moments for normally distributed random variable with mean = mean and standard deviation = sd under x_lower and x_upper truncation points x_lower and x_upper correspondingly. If return_all_moments is TRUE then see this argument description above for output details.

Examples

## Calculate 5-th order moment of three truncated normal random  
## variables (x1, x2, x3) which mean is 5 and standard deviation is 3. 
## These random variables truncation points are given 
## as follows:-1<x1<1, 0<x2<2, 1<x3<3.
k <- 3
x_lower <- c(-1, 0, 1, -Inf, -Inf)
x_upper <- c(1, 2 , 3, 2, Inf)
mean <- 3
sd <- 5

# get the moments
truncatedNormalMoment(k, x_lower, x_upper, mean, sd)

# get matrix of (0-5)-th moments (columns) for each variable (rows)
truncatedNormalMoment(k, x_lower, x_upper, 
                      mean, sd, 
                      return_all_moments = TRUE)

# get the moments derivatives respect to mean
truncatedNormalMoment(k, x_lower, x_upper, 
                      mean, sd, 
                      diff_type = "mean")

# get the moments derivatives respect to standard deviation
truncatedNormalMoment(k, x_lower, x_upper, 
                      mean, sd, 
                      diff_type = "sd")


hpa documentation built on May 31, 2023, 8:25 p.m.