################################################################################ ## Function: pass <- garp(p, x, e) ## Parameters: k [number of goods] ## n [number of observations] ## Inputs: p [(k by n) matrix of prices] ## x [(k by n) matrix of consumption amounts] ## e [(1 by n) vector of efficiency levels] ## Outputs: pass [pass/fail indicator] ## Libraries: none ## Calls: warshall ## Description: returns a pass/fail indicator representing the outcome of a ## GARP test at some efficiency vector ################################################################################ garp <- function(p, x, e) { # number of observations n <- length(e) # construct direct revealed preference relations e.p.x.i <- matrix(e * apply(p * x, 2, sum), n, n) p.x.j <- crossprod(p, x) R0 <- 1 * (p.x.j <= e.p.x.i) P0 <- 1 * (p.x.j <= e.p.x.i - 1e-12) # compute transitive closure R <- warshall(R0) # check for strict cycles cycles <- 1 * any(R + t(P0) == 2) # clean and return result result <- 1 - cycles remove(cycles, e, e.p.x.i, n, p, p.x.j, P0, R, R0, x) return(result) }