setwd("D:/Export/WiSe 2016/Angewandte Stochastik 2/Übungsblätter/Blatt 5/Lösung/") ################Aufgabe 1####################### ################################################ #################(a)############################ ################################################ epanechnikov=function(x){ if (x > 1 | x < -1){ return (0) } return (0.75*(1-x^2)) } estDensity=function(t, x, h){ sum = 0 for (i in 1:length(x)){ sum = sum + epanechnikov((t-x[i])/h) } return (sum/(length(x)*h)) } #################(b)############################ ################################################ x=rnorm(200,0,1) col=1:8 count=1 values=c(0.1,0.2,0.5,1,2,4,5) int=seq(-4,4,0.1) for (h in values){ res=c() for (i in 1:length(int)){ res=c(res, estDensity(int[i], x, h)) } if (h==0.1){ plot(int, res, type="l", col=col[count], lwd=2) } else { lines(int, res, col=col[count], lwd=2) } count=count+1 } lines(int, dnorm(int), col=8, lwd=2) legend("topleft", legend=c(values, "N(0,1)"), fill=col) #################(c)############################ ################################################ h_opt=function(x, values){ n=length(x) hopt=0 prodopt=0 for (h in values){ prod=1 for (i in 1:n){ y=c(x[1:(i-1)], x[(i+1):n]) if (i==1){ y=x[2:n] } if (i==n){ y=x[1:(n-1)] } prod=prod*estDensity(x[i], y, h) } if (prod>prodopt){ hopt=h prodopt=prod } } return (hopt) } #################(d)############################ ################################################ values=seq(0.7,2,0.05) hopt=h_opt(x, values) print(hopt) int=seq(-4,4,0.1) res=c() for (i in 1:length(int)){ res=c(res, estDensity(int[i], x, hopt)) } plot(int, res, type="l", lwd=2) lines(int, dnorm(int), col="red", lwd=2) ################Aufgabe 2####################### ################################################ #################(a)############################ ################################################ s1=0 s2=0 s3=0 for (i in 1:100){ ######(i)####### data=rexp(20, 5) #####(ii)####### s1[i]=(mean(data) - 0.2)^2 s2[i]=(sqrt(var(data)) - 0.2)^2 s3[i]=(20*min(data) - 0.2)^2 } #####(iii)###### #####alle drei boxplot(s1, s2, s3) #####1 und 2 zur besseren Unterscheidung boxplot(s1, s2) #################(b)############################ ################################################ lebensdauer<-read.table("lebensdauer.txt", header=T) means=sapply(lebensdauer, mean) means[means==max(means)]