cdfInverse <- function(loopOut, plot = F)
{
	#function takes the loopOut info and returns the 
	#vectors of cdfInverse
	#if plot, then produces the rank histogram and qq plot
	
	numReps <- dim(loopOut$zNotMtx)[2]
	cdfInvVal <- numeric(numReps)
	
	for(i in seq(1, numReps))
	{
		numPts <- sum(!is.na(loopOut$zNotMtx[,i]))
		index <- seq(1, numPts)
		k <- loopOut$zNotMtx[1:numPts,i] < 
					loopOut$zActual[loopOut$predLoc,i]
		greatLowBd <- max(index[k])
		#linearly interpolate 
		y1 <- loopOut$cumTraps[greatLowBd,i]
		y2 <- loopOut$cumTraps[(greatLowBd+1), i]
		x1 <- loopOut$zNot[greatLowBd,i]
		x2 <- loopOut$zNot[(greatLowBd+1), i]
		m <- (y2 - y1)/(x2 - x1)
		x <- loopOut$zActual[loopOut$predLoc, i]
		cdfInvVal[i] <- m*x + y1 - m*x1
	}			
	
	cdfInvVal[cdfInvVal > 1] <- 1 - 1e-10

	if(plot)
	{
		par(mfrow = c(1,2))
	
		#code for rank histogram
		hist(cdfInvVal, freq = F, breaks = seq(0, 1, .1),
			xlab = "quantile", ylab = "emp. probability",
			main = "")
		abline(h = 1, lty = 3)
		
		#code of qq plot
		model <- seq(1, numReps)/(numReps+1)
		plot(model, sort(cdfInvVal), ylab = "empirical", 
			xlim = c(0,1), ylim = c(0,1))
		abline(0,1, lty = 3)
	}

	return(cdfInvVal)
}