## ESTIMADOR NAIVE

uni<-function(x){
    apply(x,2,function(x){ifelse(abs(x)<1,1/2,0)})
}

nucleo<-function(x,muestra,h,nucleo){

    apply(nucleo(outer(x,muestra,"-")/h),1,"mean")/h
}


n=100
M=100
muestra<-rnorm(n)
bandwith<-seq(0.2,1,by=0.05)
x<-seq(-3,3,by=0.01)
MISE<-matrix(0,nrow=n,ncol=length(bandwith))
for(m in 1:M){
MISE[m,]<-apply(as.matrix(bandwith),1,function(h){sum((nucleo(x,muestra,h,uni)-dnorm(x))^2)})
}

Sol<-apply(MISE,2,"mean")
plot(Sol~bandwith)
h_opt<-bandwith[which(Sol==min(Sol))]
