################################################################################ ## Function: x0 <- rand.x(p, x) ## Parameters: k [number of goods] ## n [number of observations] ## Inputs: p [(n by t) matrix of prices] ## x [(n by t) matrix of consumption amounts] ## Outputs: x0 [(n by t) matrix of consumption amounts, with bundles at ## each observation chosen uniformly randomly] ## Libraries: gtools ## Calls: none ## Description: returns a matrix of consumption amounts, with bundles at each ## observation chosen uniformly randomly from budgets given by ## the data input of prices and consumption amounts ################################################################################ rand.x <- function(p, x) { # number of goods k <- dim(p)[1] # number of observations n <- dim(p)[2] # generate uniformly random budget shares w0 <- t(rdirichlet(n, rep(1, k))) # generate uniformly random consumption bundles p.x <- apply(p * x, 2, sum) p.x <- matrix(p.x, k, t, byrow = TRUE) x0 <- w0 * p.x / p # clean and return result result <- x0 remove(k, n, p, p.x, x, x0, w0) return(result) }