# Table 6.14 werner (loss table)
# exposure: earned exposure
# count: closed claim count
# losses: paid  losses
# pp: pure premium
# lt <- data.frame(
#   quarter=seq(as.Date("2009-03-31"),as.Date("2013-12-31"),by="quarter"),
#   exposure =  c(131911,132700,133602,
#                 135079,137384,138983,
#                 140396,140997,140378,
#                 139682,138982,138984,
#                 139155,139618,139996,
#                 140141,140754,141534,
#                 141800,1482986),
#   count = c(7745,7785,7917,7928,7997,
#            8037,7939,7831,7748,7719,
#            7730,7790,7782,7741,7720,
#            7691,7735,7769,7755,7778),
#   losses = c(8220899,8381016,
#              8594389,8705108,
#              8816379,8901163,
#              8873491,8799730,
#              8736859,8676220,
#              8629925,8642835,
#              8602105,8535327,
#              8466272,8412159,
#              8513679,8614224,
#              8702135,8761588),
#   frequency = c(0.0587,0.0587,0.0593,
#                 0.0587,0.0582,0.0578,
#                 0.0565,0.0555,0.0552,
#                 0.0553,0.0556,0.0560,
#                 0.0559,0.0554,0.0551,
#                 0.0549,0.0550,0.0549,
#                 0.0547,0.0544),
#   severity=c(1061.45,1076.56,1085.56,
#              1098.02,1102.46,1107.52,
#              1117.71,1123.70,1127.63,
#              1124.01,1116.42,1109.48,
#              1105.38,1102.61,1096.67,
#              1093.77,1100.67,1108.79,
#              1122.13,1126.46),
#   pp=c(62.32,63.16,64.33,64.44,
#        64.17,64.04,63.20,62.41,
#        62.24,62.11,62.09,62.19,
#        61.82,61.13,60.48,60.03,
#        60.49,60.86,61.37,61.28)
# )
lt <- read.csv("~/Dropbox/Round1/Excel/loss_trend.csv")
names(lt) <- c("Year", "quarter", "exposure", "count", "losses", "frequency", "severity", "pp")

# frequency <- c(0.0375, 0.0339, 0.0337, 0.0555,
#                0.0360, 0.0345, 0.0342, 0.0436,
#                0.0331, 0.0333, 0.0345, 0.0399,
#                0.0323, 0.0344, 0.0322, 0.0428,
#                0.0330, 0.0350, 0.0286, 0.0441)

expfit <- function(y, point=20) {
  x <- 1:point
  y <- tail(log(y),point)
  exp(coef(lm(y~x))[2])^4 - 1
}

expfit_r2 <- function(y, point=20) {
  x <- 1:point
  y <- tail(log(y),point)
  summary(lm(y~x))$r.squared
}

print_coef <- function(coef_table) {
  #coef_table$Freq_Severity <- (1+coef_table$Frequency) * (1+coef_table$Severity) - 1
  coef_table[,2:(ncol(coef_table))] <- sapply(2:(ncol(coef_table)),function(x)paste0(round(coef_table[,x]*100,1), "%"))
  print(coef_table)
}

# match parameters table shown in werner
points <- c(20,16, 12, 8, 6, 4)
fcoef <- sapply(points, function(m) expfit(y=lt$frequency,point=m))
fr2 <- sapply(points, function(m) expfit_r2(y=lt$frequency,point=m))
scoef <- sapply(points, function(m) expfit(y=lt$severity,point=m))
sr2 <- sapply(points, function(m) expfit_r2(y=lt$severity,point=m))
pcoef <- sapply(points, function(m) expfit(y=lt$pp,point=m))
ppr2 <- sapply(points, function(m) expfit_r2(y=lt$pp,point=m))
coef_table <- data.frame(Points=points,Frequency=fcoef,Frequency_R2=fr2,
                         Severity=scoef,Severity_R2=sr2,PP=pcoef,
                         PP_R2=ppr2)
print_coef(coef_table)
##   Points Frequency Frequency_R2 Severity Severity_R2    PP PP_R2
## 1     20     -1.7%         2.8%     4.7%       15.8%    3%    5%
## 2     16     -0.2%           0%     2.9%        3.9%  2.7%    4%
## 3     12      2.4%         2.8%     5.9%        7.5%  8.4% 20.7%
## 4      8      6.1%           6%     1.3%        0.2%  7.5%  7.3%
## 5      6      4.9%         1.7%    -4.9%        1.1% -0.3%    0%
## 6      4     30.3%        22.5%    17.5%          4% 53.1% 41.6%