#ogranicen nosac - refleksija
Kn<-function(t){
  return(exp(-t^2/2)/sqrt(2*pi))
}
x<-rexp(100,1)
n<-length(x)
y<-seq(-4, 4, .01) #tacke u kojima se ocenjuje gustina
fh<-rep(0, length(x)) #vektor ocena
par(mfrow=c(1,2))
h<-0.25
#bez refleksije
f<-c()
for(j in 1:length(y)){
  suma<-0
  for(i in 1:n){
    suma<-suma+Kn((y[j]-x[i])/h)
  }
  f[j]<-suma
}
fh<-f/(h*n) #ocena gustine jezgrom
plot(y, fh, type = 'l') #grafik gustine u svim tackama
abline(v = 0, col='red')
#sa refleksijom
f1<-c()
for(j in 1:length(y)){
  suma<-0
  for(i in 1:n){
    suma<-suma+Kn((y[j]-x[i])/h)+Kn((-x[i]-y[j])/h)
  }
  f1[j]<-suma

}
fh1<-f1/(h*n) #ocena gustine jezgrom
fh2<-c()
for(i in 1:length(y)){
  if(y[i]<0)
    fh2[i]<-0
  else
    fh2[i]<-fh1[i]
}
plot(y, fh2, type = 'l') #grafik gustine u svim tackama
abline(v = 0, col='red')
     