source("73.r")
# zbog funkcije koef_asimetrije
source("78.r")
# zbog funkcije butstrep

nerv = read.table("baze//nerv.txt")
podaci = nerv$V1
n = length(podaci)

B = 1000
T_b = butstrep(podaci, koef_asimetrije, B)
# ocena standardne devijacije
(se_boot = sd(T_b)*sqrt((B-1)/B))

interval_poverenja_boot = function(x, metod = "normalni", alpha, ocena) {
  hn = ocena(x)
  T_b = butstrep(x, koef_asimetrije, B)
  se_boot = sd(T_b)*sqrt((B-1)/B)
  
  if (metod == "normalni") {
    return(c(hn - qnorm(1-alpha/2)*se_boot,
             hn + qnorm(1-alpha/2)*se_boot))
  }
  else if (metod == "stozerni") {
    return(c(2*hn - sort(T_b)[(1 - alpha/2)*B],
             2*hn - sort(T_b)[(alpha/2)*B]))
  }
  else if (metod == "percentilni") {
    return(c(sort(T_b)[(alpha/2)*B],
             sort(T_b)[(1-alpha/2)*B]))
  }
  else if (metod == "studentizovani") {
    xb = sample(x, n*B, replace = T) 
    # redovi butstrep uzorci obima n
    xb = matrix(xb, nrow = B, ncol = n) 
    h_b = apply(xb, 1, koef_asimetrije)
  
    # racunamo niz se_b butstrep ocena standardnih gresaka za niz ocena h_b
    uz_b = apply(xb, 1, butstrep, koef_asimetrije, B)
    se_b = apply(uz_b, 1, sd)*sqrt((B-1)/B)
    z = (h_b - hn)/se_b
    
    return(c(hn - sort(z)[(1-alpha/2)*B]*se_boot,
             hn - sort(z)[(alpha/2)*B]*se_boot))
  }
}

alpha = 0.05
interval_poverenja_boot(podaci, metod = "normalni", alpha, koef_asimetrije)
interval_poverenja_boot(podaci, metod = "stozerni", alpha, koef_asimetrije)
interval_poverenja_boot(podaci, metod = "percentilni", alpha, koef_asimetrije)
interval_poverenja_boot(podaci, metod = "studentizovani", alpha, koef_asimetrije)

