Skip to content

Instantly share code, notes, and snippets.

@jayjacobs
Created July 29, 2025 22:14
Show Gist options
  • Save jayjacobs/ece0d0ce0e2946ccfb9f8e3d46a43894 to your computer and use it in GitHub Desktop.
Save jayjacobs/ece0d0ce0e2946ccfb9f8e3d46a43894 to your computer and use it in GitHub Desktop.
# Some quick R code to answer https://www.linkedin.com/posts/alexsidorenko_you-have-three-risks-a-30-probability-activity-7356011763118546944-F2ed
library(tidyverse)
stats <- tibble(cat = c("A", "B", "C"),
prob = c(0.3, 0.2, 0.4),
loss = c(100, 200, 50))
all_scenarios <- expand_grid(A = c(TRUE, FALSE),
B = c(TRUE, FALSE),
C=c(TRUE, FALSE))
prob_scenarios <- all_scenarios %>%
mutate(scenario = row_number()) %>%
gather(cat, event, -scenario) %>%
left_join(stats, by="cat") %>%
mutate(true_prob = ifelse(event, prob, 1-prob),
loss = ifelse(event, loss, 0)) %>%
summarize(.by=scenario, probability = prod(true_prob), loss = sum(loss)) %>%
mutate(txt = paste0(percent(probability), " of ", loss, "k"))
to_cat <- prob_scenarios %>%
arrange(loss) %>%
summarize(txt = paste0(txt, collapse=", ")) %>%
pull(txt)
cat(to_cat)
# 33.6% of 0k, 22.4% of 50k, 14.4% of 100k, 9.6% of 150k, 8.4% of 200k, 5.6% of 250k, 3.6% of 300k, 2.4% of 350k
exceed <- prob_scenarios %>%
arrange(-loss) %>%
mutate(cumprob = cumsum(probability)) %>%
mutate(txt = paste0(percent(cumprob), " of ", loss, "k or more loss"))
ggplot(exceed, aes(loss, cumprob, label=txt)) +
geom_step(direction = "vh") +
geom_point(size=1.5) +
geom_text(vjust=-0.4, hjust=c(seq(1,0,length.out=nrow(exceed)))) +
scale_x_continuous("Loss (in thousands)", expand=expansion(mult=c(0,0.06))) +
scale_y_continuous("Probability of >= Loss", expand=expansion(mult=c(0,0.06)), limits=c(0,1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment