Last active
August 1, 2019 22:29
-
-
Save TonyLadson/12798e70a594298ccab509d5e0ece662 to your computer and use it in GitHub Desktop.
Calculating an empirical probability density plot for Initial and Continuing Loss. See https://tonyladson.wordpress.com/2019/07/30/the-distribution-of-losses-ii/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(pracma) | |
library(here) | |
loss_std <- structure(list(Percentile = c(100, 90, 80, 70, 60, 50, 40, 30, | |
20, 10, 0), IL = c(0.14, 0.39, 0.53, 0.68, 0.85, 1, 1.2, 1.4, | |
1.71, 2.26, 3.19), CL = c(0.15, 0.35, 0.48, 0.61, 0.79, 1, 1.24, | |
1.5, 1.88, 2.48, 3.85), prob = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, | |
0.6, 0.7, 0.8, 0.9, 1)), class = c("tbl_df", "tbl", "data.frame" | |
), row.names = c(NA, -11L)) | |
loss_std <- loss_std %>% | |
mutate(prob = (100-Percentile)/100) %>% | |
arrange(prob) | |
loss_std | |
# IL | |
p <- loss_std %>% | |
ggplot(aes(x = IL, y = prob)) + | |
geom_point() + | |
geom_line() + | |
scale_x_continuous(name = 'Standardised Initial Loss', minor_breaks = seq(0,3.2,0.2)) + | |
scale_y_continuous(name = 'Percentile') + | |
geom_hline(yintercept = 1.00, linetype = 'dashed') + | |
theme_grey(base_size = 7) | |
p | |
ggsave(here('figures', 'IL_percentile.png'), p, width = 4, height = 3) | |
# CL | |
p <- loss_std %>% | |
ggplot(aes(x = CL, y = prob)) + | |
geom_point() + | |
geom_line() + | |
scale_x_continuous(name = 'Standardised Continuing Loss', minor_breaks = seq(0,3.2,0.2)) + | |
scale_y_continuous(name = 'Percentile') + | |
geom_hline(yintercept = 1.00, linetype = 'dashed') + | |
theme_grey(base_size = 7) | |
p | |
# Differentiate the CDF ------------------------------------------------------------- | |
# https://stackoverflow.com/questions/29247676/derivative-of-a-set-of-points/57084226#57084226 | |
# https://stackoverflow.com/questions/11081069/calculate-the-derivative-of-a-data-function-in-r/57212359#57212359 | |
dP_IL <- pracma::gradient(loss_std$prob, loss_std$IL) | |
dP_CL <- pracma::gradient(loss_std$prob, loss_std$CL) | |
plot(x =loss_std$IL, y = dP_IL, type = 'b') | |
plot(x =loss_std$CL, y = dP_CL, type = 'b') | |
loss_std <- loss_std %>% | |
add_column(dP_IL = dP_IL) %>% | |
add_column(dP_CL = dp_CL) | |
p <- loss_std %>% | |
ggplot(aes(x = IL, y = dP_IL)) + | |
geom_point() + | |
geom_line() + | |
scale_x_continuous(name = 'Standardised Initial Loss') + | |
scale_y_continuous(name = 'Density') + | |
theme_grey(base_size = 7) | |
ggsave(here('figures', 'IL_pdf.png'), p, width = 4, height = 3) | |
p <- loss_std %>% | |
ggplot(aes(x = CL, y = dP_CL)) + | |
geom_point() + | |
geom_line() + | |
scale_x_continuous(name = 'Standardised Continuing Loss') + | |
scale_y_continuous(name = 'Density') + | |
theme_grey(base_size = 7) | |
ggsave(here('figures', 'CL_pdf.png'), p, width = 4, height = 3) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment