# IST5109 - Hafta 5 laboratuvari
# Muafiyet, limit, koasurans, toplam hasar ve prim prensipleri
# Bu betik internet baglantisi olmadan calisir; veri dosyasi okumaz.

for (paket in c("actuar", "ggplot2", "scales")) {
  if (!requireNamespace(paket, quietly = TRUE)) {
    stop("Eksik paket: ", paket, ". install.packages() ile kurunuz.", call. = FALSE)
  }
}

library(ggplot2)
library(scales)

# --- On hazirlik -------------------------------------------------------------
# Asagidaki tanimlar ders notunun GOVDESINDE kuruluyor; betigin tek basina
# calisabilmesi icin burada yeniden uretiliyorlar. Laboratuvarin kendisi
# "# Gorev A" satirindan sonra baslar ve oradan itibaren notun sayfasindaki
# chunk'larla birebir aynidir.

policy_payment <- function(loss, deductible = 0,
                           limit = Inf, coinsurance = 1) {
  stopifnot(
    is.numeric(loss), all(loss >= 0),
    deductible >= 0, limit > 0,
    coinsurance > 0, coinsurance <= 1
  )
  coinsurance * pmin(pmax(loss - deductible, 0), limit)
}

shape <- 2.8
scale <- 6000
d <- 1000
L <- 10000
c <- 0.80

beklenen_paket <- c * (
  actuar::levpareto(d + L, shape = shape, scale = scale) -
    actuar::levpareto(d, shape = shape, scale = scale)
)

ikinci_moment_kapali <- c^2 * (
  actuar::levpareto(d + L, shape = shape, scale = scale, order = 2) -
    actuar::levpareto(d, shape = shape, scale = scale, order = 2) -
    2 * d * (actuar::levpareto(d + L, shape = shape, scale = scale) -
               actuar::levpareto(d, shape = shape, scale = scale))
)

# Gorev A - Policenin parcalarini tek tek kapatin ----------------------------
kayiplar <- c(0, 500, 1500, 5000, 12000, 30000)
print(kayiplar)

gorev_a <- data.frame(
  kayip       = kayiplar,
  temel       = policy_payment(kayiplar, d, L, c),
  koasurans_1 = policy_payment(kayiplar, d, L, 1),
  limitsiz    = policy_payment(kayiplar, d, Inf, c),
  muafiyetsiz = policy_payment(kayiplar, 0, L, c)
)
print(gorev_a)

gorev_a_hata <- tryCatch(
  policy_payment(-1, d, L, c),
  error = function(e) conditionMessage(e)
)
print(gorev_a_hata)


# Gorev B - Muafiyet beklenen odemeyi nereye kadar ucuzlatir? ----------------
muafiyetler <- c(0, 1000, 5000)
print(muafiyetler)

gorev_b <- data.frame(
  muafiyet = muafiyetler,
  beklenen_odeme = c * (
    actuar::levpareto(muafiyetler + L, shape = shape, scale = scale) -
      actuar::levpareto(muafiyetler, shape = shape, scale = scale)
  )
)
print(gorev_b)

gorev_b_dusus <- round(
  100 * (gorev_b$beklenen_odeme / gorev_b$beklenen_odeme[1] - 1), 1
)
print(gorev_b_dusus)

gorev_b_egim <- c * (
  actuar::ppareto(muafiyetler, shape = shape, scale = scale,
                  lower.tail = FALSE) -
    actuar::ppareto(muafiyetler + L, shape = shape, scale = scale,
                    lower.tail = FALSE)
)
print(round(gorev_b_egim, 3))

muafiyet_izgara <- seq(0, 10000, by = 50)
print(head(muafiyet_izgara))

beklenen_odeme_egrisi <- data.frame(
  muafiyet = muafiyet_izgara,
  beklenen_odeme = c * (
    actuar::levpareto(muafiyet_izgara + L, shape = shape, scale = scale) -
      actuar::levpareto(muafiyet_izgara, shape = shape, scale = scale)
  )
)
print(head(beklenen_odeme_egrisi))

print(ggplot(beklenen_odeme_egrisi, aes(x = muafiyet, y = beklenen_odeme)) +
  geom_line(colour = "#1F4E79", linewidth = 1) +
  geom_point(
    data = data.frame(muafiyet = d, beklenen_odeme = beklenen_paket),
    colour = "#C8A45C", size = 3
  ) +
  annotate("text", x = d, y = beklenen_paket,
           label = "Derste kullanılan d = 1.000 TL",
           hjust = -0.08, vjust = -1.1, colour = "#C8A45C", size = 3.4) +
  scale_x_continuous(
    labels = label_number(big.mark = ".", decimal.mark = ",")
  ) +
  scale_y_continuous(
    limits = c(0, NA),
    labels = label_number(big.mark = ".", decimal.mark = ",")
  ) +
  labs(x = "Muafiyet d (TL)",
       y = "Beklenen sigortacı ödemesi E[Y] (TL)") +
  theme_minimal(base_size = 12))


# Gorev C - Hangi prim buyuk, ve bu siralama evrensel mi? --------------------
lambda <- 120
print(lambda)

beklenen_toplam <- lambda * beklenen_paket
print(beklenen_toplam)

toplam_sd <- sqrt(lambda * ikinci_moment_kapali)
print(toplam_sd)

gorev_c <- c(
  beklenen_deger_primi = (1 + 0.20) * beklenen_toplam,
  standart_sapma_primi = beklenen_toplam + 0.30 * toplam_sd
)
print(round(gorev_c, 2))

gorev_c_esitlik <- c(
  degisim_katsayisi = toplam_sd / beklenen_toplam,
  esitleyen_k       = 0.20 * beklenen_toplam / toplam_sd
)
print(round(gorev_c_esitlik, 4))

gorev_c_lambda_esigi <- (0.30 / 0.20)^2 * ikinci_moment_kapali / beklenen_paket^2
print(gorev_c_lambda_esigi)
