### Non-Life Insurance: Mathematics and Statistics ### Exercise Sheet 4 ### Exercise 2 ### a) ### Define the function that generates the data and applies the chi-squared goodness-of-fit test ### in order to test the Poisson assumption chi.squared.test.1 <- function(seed1, n, t, lambda, v, alpha){ ### Generate the claim counts set.seed(seed1) claim.counts <- array(rpois(n*t,lambda*v), dim=c(t,n)) ### Distribution of the MLEs of lambda lambda_MLE <- colSums(claim.counts)/(t*v) plot(density(lambda_MLE), main="Distribution of the MLEs", xlab="Values of the MLEs", cex.lab=1.25, cex.main=1.25, cex.axis=1.25) abline(v=mean(lambda_MLE), col="red") legend("topleft", lty=1, col="red", legend="mean") print("1: See plot for the distribution of the MLEs") ### Distribution of the test statistic lambda_MLE_array <- array(rep(lambda_MLE,each=t), dim=c(t,n)) test.statistic <- colSums(v*(claim.counts/v-lambda_MLE_array)^2/lambda_MLE_array) theoretical.quantiles <- qchisq(p=(1:n)/(n+1), df=t-1) empirical.quantiles <- test.statistic[order(test.statistic)] lim <- c(min(theoretical.quantiles,empirical.quantiles), max(theoretical.quantiles,empirical.quantiles)) plot(theoretical.quantiles, empirical.quantiles, xlim=lim, ylim=lim, xlab="Theoretical Quantiles", ylab="Empirical Quantiles", main="QQ plot", cex.lab=1.25, cex.main=1.25, cex.axis=1.25) abline(a=0, b=1, col="red") print("2: See the QQ plot for a comparison between the empirical quantiles of the test statistic and the theoretical quantiles of a chi-squared distribution with t-1 degrees of freedom") ### Result of the hypothesis test print(paste("3: How often we wrongly reject the null hypothesis: ",sum(test.statistic > qchisq(p=1-alpha, df=t-1))/n,sep="")) } ### Apply the function with the desired parameters chi.squared.test.1(seed1=100, n=10000, t=10, lambda=0.1, v=10000, alpha=0.05) ### b) ### Define the function that generates the data and applies the chi-squared goodness-of-fit test ### in order to test the Poisson assumption chi.squared.test.2 <- function(seed1, n, t, lambda, v, alpha, gamma){ ### Generate the claim counts set.seed(seed1) claim.counts <- array(rnbinom(n*t, size = gamma, mu=lambda*v), dim=c(t,n)) ### Calculate the MLEs lambda_MLE <- colSums(claim.counts)/(t*v) ### Calculate the test statistic lambda_MLE_array <- array(rep(lambda_MLE,each=t), dim=c(t,n)) test.statistic <- colSums(v*(claim.counts/v-lambda_MLE_array)^2/lambda_MLE_array) ### Result of the hypothesis test print(paste("How often we correctly reject the null hypothesis: ",sum(test.statistic > qchisq(p=1-alpha, df=t-1))/n,sep="")) } ### Apply the function with the desired parameters chi.squared.test.2(seed1=100, n=10000, t=10, lambda=0.1, v=10000, alpha=0.05, gamma=100) chi.squared.test.2(seed1=100, n=10000, t=10, lambda=0.1, v=10000, alpha=0.05, gamma=1000) chi.squared.test.2(seed1=100, n=10000, t=10, lambda=0.1, v=10000, alpha=0.05, gamma=10000)