#@SLLN #approx. calculate area of intersection of two circles of radius one, one with center (0,0), one with center (1,0) (simplest case of niche project with biology) n <- 10000000 x <- runif(n,-1,2) y <- runif(n,-1,1) A <- data.frame(x=x,y=y) A$K1 <- ifelse(A$x^2 + A$y^2 <=1,1,0) A$K2 <- ifelse((A$x-1)^2 + A$y^2 <=1,1,0) A$in.both <- A$K1*A$K2 mean(A$K1)*6 mean(A$in.both)*6 #---------------------------------------------------------------------------------- #@Glivenko Cantelli #Block GC1: #Exercise 4 on first exercise sheet (compare with Glivenko-Cantelli theorem) #the distribution of D_n does not dependent on F (as long as it is continuous) #a) X ~ U(-1,1) #Step I: one run n<-500 x <- runif(n,-1,1) plot(ecdf(x),cex=0.5) xg<-seq(-1.1,1.1,length=1001) lines(xg,punif(xg,min=-1,max=1),col="red",type="l") y <- sort(x) right <- abs(punif(y,min=-1,max=1)-1:n/n) left <- abs(punif(y,min=-1,max=1)-0:(n-1)/n) Dn <- max(max(right),max(left)) Dn #Step2: several runs, generate samples of Dn n <- 5 R <- 10000 Dn <- rep(0,R) for (i in 1:R){ x <- runif(n,-1,1) y <- sort(x) right <- abs(punif(y,min=-1,max=1)-1:n/n) left <- abs(punif(y,min=-1,max=1)-0:(n-1)/n) Dn[i] <- max(max(right),max(left)) } hist(Dn) Dn.unif <- Dn #b) Exponential distribution lambda <- 2 x <- rexp(n,rate=lambda) plot(ecdf(x),cex=0.5) xg<-seq(min(x),max(x),length=101) lines(xg,pexp(xg,rate=lambda),col="red",type="l") y <- sort(x) right <- abs(pexp(y,rate=lambda)-1:n/n) left <- abs(pexp(y,rate=lambda)-0:(n-1)/n) Dn <- max(max(right),max(left)) Dn R <- 10000 Dn <- rep(0,R) for (i in 1:R){ lambda <- 20 x <- rexp(n,rate=lambda) y <- sort(x) right <- abs(pexp(y,rate=lambda)-1:n/n) left <- abs(pexp(y,rate=lambda)-0:(n-1)/n) Dn[i] <- max(max(right),max(left)) } hist(Dn) Dn.exp <- Dn par(mfrow=c(1,3)) hist(Dn.unif,col="lightblue") hist(Dn.exp,col="lightblue") plot(ecdf(Dn.unif),cex=0.5) lines(ecdf(Dn.exp),cex=0.5,col="magenta") par(mfrow=c(1,1)) #c) any other continuous distribution, try yourself