# Gausovo jezgro
K = function(t) {
  return(exp(-t^2 / 2) / sqrt(2 * pi))
}

podaci = c(-0.77, -0.60, -0.25, 0.14, 0.45, 0.64, 0.65, 1.19, 1.71, 1.74)
n = length(podaci)
range(podaci)

for (h in c(0.25, 0.4, 0.6, 1)) {
  # tacke u kojima se ocenjuje gustina
  x = seq(-3, 3, length.out = 1000) 
  fn = rep(0, length(x))
  
  plot(x, fn, type = "n", xlab = "", ylab = "", 
       main = paste("h = ", h), xlim = c(-3, 3), ylim = c(0, 0.5))
  
  for (i in 1:n) {
    # ocena gustine na osnovu samo jednog podatka: podaci[i]
    lines(x, K((x - podaci[i]) / h) / (n * h), col = i + 1) 
    fn = fn + K((x - podaci[i]) / h) / (n * h)
  }
  # ocena gustine na osnovu svih podataka
  lines(x, fn, lwd = 1.5) 
}
