Skip to content

Instantly share code, notes, and snippets.

@j-greer
Created September 30, 2015 15:33
Show Gist options
  • Save j-greer/8826353a41c0fd792383 to your computer and use it in GitHub Desktop.
Save j-greer/8826353a41c0fd792383 to your computer and use it in GitHub Desktop.
Simple MonteCarlo simulation
####### load required packages and ste seed ########
library(mc2d) #for PERT distribution
library(ggplot2) #for plotting
set.seed(1501)
DDoS <- 10 #Number of DDoS attacks per year
software_cost <- 100000 #Cost of new technology
DDoS_new <- runif(n=1000,min=3,max=6) #Num of attacks after investment
downtime_min <- 2
downtime_max <- 24
downtime_avg <- 6
downtime_cost <- 1000
brand_damage_min <- 50000
brand_damage_max <- 150000
####### without investment ########
no_invest <- rpois(n=1000,DDoS) * (runif(n=1000,min=brand_damage_min,
max=brand_damage_max) +
rpert(1000,downtime_min,downtime_avg,downtime_max,
4)*downtime_cost)
print(paste("Without investment Annualised Loss Expectancy at the 10th Percentile is",
round(quantile(no_invest,.1),2),", ",round(median(no_invest,.1),2),
" at the Median", "& ",round(quantile(no_invest,.9),2),
" at the 90th Percentile"))
######## with investment ########
invest <- rpois(n=1000,DDoS_new) * (runif(n=1000,min=brand_damage_min,
max=brand_damage_max) +
rpert(1000,downtime_min,downtime_avg,downtime_max,
4)*downtime_cost) + software_cost
print(paste("With investment Annualised Loss Expectancy at the 10th Percentile is",
round(quantile(invest,.1),2),", ",round(median(invest,.1),2),
" at the Median", "& ",round(quantile(invest,.9),2),
" at the 90th Percentile"))
####### plot the distributions of the monte carlo simulations #######
ALE <- data.frame(investment = c(rep("Investment",1000),rep("No Investment",1000)),
Cost=c(invest/100000,no_invest/100000))
ggplot(ALE,aes(x=Cost))+geom_density(aes(fill=factor(investment),
colour=factor(investment)),
alpha=0.4)+
scale_colour_manual("",values=c("Investment"="black","No Investment"="red"))+
scale_fill_manual("",values=c("Investment"="black","No Investment"="red"))+
xlab("Cost (£100,000)")+
ylab("Density")+
ggtitle("The Effect of an £100k Investment on Annualised Loss Expectancy")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment