# MSE of Kernel Density Estimator

by Angel   Last Updated September 18, 2018 06:19 AM

Erlang Kernel is used for density estimation. By using this estimates are pretty close to the real density on graph on the other side MSE is very large. But Author of Erlang Kernel stated that it will close to 0.000266937. Following code is used for this purpose. Please provide help how to calculate "Mean Square Error".

``````n<-200
k=400
y<-rexp(n,1)

h<-0.79 * IQR(y) * length(y) ^ (-1/5)
x <- seq(min(y) + 0.05, max(y), length =k)
KErlang <- matrix(rep(0, k * n), ncol = k)
fhat <- rep(0, k)
###########erlang###########
for(j in 1:k) {
for(i in 1:n) {
KErlang[i, j] <-(1/gamma(1+(1/h)))*((1/x[j])*(1+(1/h)))^((h+1)/h)*y[i]^(1/h)*exp(-y[i]/x[j]*(1+(1/h)))
#KErlang[i, j] <-((y[i]^(x[j]/h))*exp(-(y[i]/h))/(h^((x[j]/h)+1))*factorial(x[j]/h))
}
fhat[j] <- 1/n * (sum( KErlang[, j]))
}
d1<-density(y,bw=h)
plot(x,fhat, type = "s", ylab = "Density Function", lty = 1, xlab = "Time")
lines(d1,type="p",col="red")
legend("topright", c("Real Density", "Density by Erlang Kernel"),
col=c("red", "black"), lty=c(1,2))
A<-matrix(nrow=n, ncol=3)
for(i in 1:n){
A[i,1]<-y[i]
A[i,2]<-fhat[i]
A[i,3]<-(fhat[i]-y[i])^2
}
(sum(A[,3]))/n
``````
Tags :