##################################################### # Solutions to Worksheet from Chap. 12 # # Simulations # ##################################################### # A- Study of the distribution f(x)=(3/2)\sqrt(x) on [0,1] # 12.1- f <- function(x) (3/2)*sqrt(x) integrate(f,lower=0,upper=1) # 12.2- Method of generic inversion: Finv <- function(x) x^(2/3) u <- runif(1000) x <- Finv(u) # 12.3- mean(x) var(x) # 12.4- Theoretical values are E(X)= 3/5 and Var(X) = 12/175. # 12.5- The cumulative distribution function is F(x)= x ^(2/3). The theoretical probabilities of the various classes are: 0.3^(3/2) = 0.1643168, 0.5^(3/2) - 0.3^(3/2) = 0.1892366, 0.7^(3/2) - 0.5^(3/2) = 0.2321086, 0.85^(3/2) - 0.7^(3/2) = 0.1979993 and 1 - 0.85^(3/2) = 0.2163387. We can evaluate them numerically: class <- c(0.3,0.5,0.7,0.85) prob <- function(x) {integrate(f,lower=0,upper=x)$value} dist <- c(0,apply(as.matrix(class),MARGIN=1,FUN=prob),1) (theoretical.prob <- diff(dist)) # The empirical probabilities can be computed using the following code: count <- function(x,vect.y) sum(vect.y