######################################################################### ## This function returns the value of the probability density function ## ## of a mixed Poisson distributed random variable with the parameters ## ## given in exercise 1, sheet #5 ## ######################################################################### pdf_N <- function(k) { lambda <- c(60,35,16,12,6) p <- c(0.05,0.2,0.4,0.25,0.1) sum <- NULL for (i in 1:length(k)) { temp <- exp(-lambda)*lambda^k[i]/factorial(k[i]) sum <- c(sum,t(p)%*%temp) } return(sum) } ## Example 1: value of the pdf at k=1 pdf_N(1) ## Example 2: value of the pdf at k=1,3,5 k <- c(1,3,5) pdf_N(k) ## (a) lambda <- c(60,35,16,12,6) p <- c(0.05,0.2,0.4,0.25,0.1) mixed_mean <- t(lambda) %*% p mixed_mean ## (b) ## This function returns the value of the cdf of the mixed Poisson distributed rv cdf_N <- function(k) { if (!is.vector(k)) { cat("The argument for cdf_N has to be a vector.") } else { values <- NULL for (i in 1:length(k)) { x <- 0:k[i] temp <- sum(pdf_N(x)) values <- c(values, temp) } return(values) } } k <- c(10,14,20,30,40,60) cdf_N(k) ppois(k, 20)