source("96.r")
# zbog funkcije ocena_gustine_jezgrom
source("81.r")
# zbog funkcije rN_mesavina

n = 100
par(mfrow = c(1, 2))

for (a in c(1/2, 2)) {
  podaci = rN_mesavina(n, c(a, -a), c(1, 1), 0.5)
  y = seq(-4, 4, 0.1)

  # Silvermanovo pravilo
  h = 0.9*min(sd(podaci), IQR(podaci)/1.34)*n^(-1/5)

  plot(y, ocena_gustine_jezgrom(y, podaci, h, "Gausovo"), 
       type = "n", xlab = "", ylab = "", xlim = c(-4, 4),
       ylim = c(0, 0.6), main = "Ocena gustine jezgrom")
  lines(y, ocena_gustine_jezgrom(y, podaci, h, "Gausovo"), type = "l")
  lines(y, ocena_gustine_jezgrom(y, podaci, h, "bikvadratno"), type = "l", col = 2)
  lines(y, ocena_gustine_jezgrom(y, podaci, h, "trougaono"), type = "l", col = 3)
  lines(y, ocena_gustine_jezgrom(y, podaci, h, "Epanehnikovo"), type = "l", col = 4)
  lines(y, ocena_gustine_jezgrom(y, podaci, h, "uniformno"), type = "l", col = 5)
  legend(1.2, 0.5, legend=c("Gausovo jezgro", "Bikvadratno jezgro", "Trougaono jezgro", "Epanehnikovo jezgro", "Uniformno jezgro"),  
         col = 1:5, lty = rep(1, 5), cex = 0.3, box.lty = 1)
}

