Skip to content

Instantly share code, notes, and snippets.

@mikebirdgeneau
Created December 22, 2016 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikebirdgeneau/a61459aede3a29743c780548753b7fde to your computer and use it in GitHub Desktop.
Save mikebirdgeneau/a61459aede3a29743c780548753b7fde to your computer and use it in GitHub Desktop.
Multivariate Beta Pert Distribution
#' Multivariate Beta PERT distributions
#'
#' @description Generates random deviates from correlated (modified) pert distributions.
#' this is performed by remapping correlated normal distributions to the beta pert
#' distributions using quantiles.
#'
#' @param n Number of observations. If length(n) > 1, the length is taken to be the number required.
#' @param min Vector of minima.
#' @param mode Vector of modes.
#' @param max Vector of maxima.
#' @param sigma covariance matrix, default is diag(ncol(x)).
#'
#' @return Returns a matrix of values of dimensions n x length(mode).
#' @export
#'
#' @examples
mvtbetapert <- function(n, min, mode, max, sigma = diag(length(mode))){
# Generate correlated normal distributions:
mvn <- mvtnorm::rmvnorm(n, mean = rep(0, length(mode)), sigma = sigma)
# Convert to quantiles to be re-mapped to the betaPERT distribution:
for (j in 1:ncol(mvn)){
mvn[, j] <- rank(mvn[, j]) / length(mvn[, j])
}
# Convert quantiles desired betaPERT distributions
for (j in 1:ncol(mvn)){
prt <- rpert(n, min = min[j], mode = mode[j], max = max[j])
mvn[, j] <- quantile(prt, probs = mvn[, j])
}
return(mvn)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment