plot10 <- ggplot(data=AEP_C_net_df_plot, aes(x = RP, y = (Loss/1000000000))) +
# AEP gross - correlated
# Stderr
geom_ribbon (aes(ymin=MinE/1000000000, ymax=MaxE/1000000000, alpha=0.3), fill = "blue") +
# Mean
geom_line(data=AEP_C_net_df_plot,
col="blue", size=2, linetype="dashed",
aes(x = RP, y = (Loss/1000000000))) +
# AEP gross - uncorrelated
# Stderr
geom_ribbon (data=AEP_Default_net_df_plot,
aes(y = (Loss/1000000000), ymin=MinE/1000000000, ymax=MaxE/1000000000, alpha=0.3, fill = "red")) +
# Mean
geom_line(data=AEP_Default_net_df_plot, col="red", size=2, linetype="dashed",
aes(x = RP, y = (Loss/1000000000))) +
# General
theme(legend.position = "none") +
labs(title=sprintf("AEP net - %s(%.2f,blue) and %s(red)","correlated",rho,"independent"), x="Return Period (yrs)", y="Loss (? billion)")
plot10
ggsave(
outfile18,    # easiest to use file name to set device
plot = plot10,  # default of last_plot()
)
##########################
# 3 - Output Analytics   #
##########################
# Results are in the following files
# Loop through to extract at selected RPs
# And, tabulated comparison
#AEP_C_gross_df
# Select certain RPs from a curve
AEPg_D_summary_df <- F_EP_summary(AEP_Default_gross_df,RPs_select_df)
AEPg_C1_summary_df <- F_EP_summary(AEP_C1_gross_df,RPs_select_df)
AEPg_C2_summary_df <- F_EP_summary(AEP_C2_gross_df,RPs_select_df)
AEPg_C3_summary_df <- F_EP_summary(AEP_C3_gross_df,RPs_select_df)
AEPg_C4_summary_df <- F_EP_summary(AEP_C4_gross_df,RPs_select_df)
AEPg_C5_summary_df <- F_EP_summary(AEP_C5_gross_df,RPs_select_df)
AEPg_summary_df <- as.data.frame(AEPg_D_summary_df[,"RP"])
colnames(AEPg_summary_df)[1] <- "RP"
AEPg_summary_df$D <- AEPg_D_summary_df$Loss
AEPg_summary_df$C1 <- AEPg_C1_summary_df$Loss
AEPg_summary_df$C2 <- AEPg_C2_summary_df$Loss
AEPg_summary_df$C3 <- AEPg_C3_summary_df$Loss
AEPg_summary_df$C4 <- AEPg_C4_summary_df$Loss
AEPg_summary_df$C5 <- AEPg_C5_summary_df$Loss
# Work out percentage losses
AEPg_summary_df$C1_per <- 100*((AEPg_summary_df$C1/AEPg_summary_df$D)-1)
AEPg_summary_df$C2_per <- 100*((AEPg_summary_df$C2/AEPg_summary_df$D)-1)
AEPg_summary_df$C3_per <- 100*((AEPg_summary_df$C3/AEPg_summary_df$D)-1)
AEPg_summary_df$C4_per <- 100*((AEPg_summary_df$C4/AEPg_summary_df$D)-1)
AEPg_summary_df$C5_per <- 100*((AEPg_summary_df$C5/AEPg_summary_df$D)-1)
#AEP_C_net_df
AEPn_D_summary_df <- F_EP_summary(AEP_Default_net_df,RPs_select_df)
AEPn_C1_summary_df <- F_EP_summary(AEP_C1_net_df,RPs_select_df)
AEPn_C2_summary_df <- F_EP_summary(AEP_C2_net_df,RPs_select_df)
AEPn_C3_summary_df <- F_EP_summary(AEP_C3_net_df,RPs_select_df)
AEPn_C4_summary_df <- F_EP_summary(AEP_C4_net_df,RPs_select_df)
AEPn_C5_summary_df <- F_EP_summary(AEP_C5_net_df,RPs_select_df)
AEPn_summary_df <- as.data.frame(AEPn_D_summary_df[,"RP"])
colnames(AEPn_summary_df)[1] <- "RP"
AEPn_summary_df$D <- AEPn_D_summary_df$Loss
AEPn_summary_df$C1 <- AEPn_C1_summary_df$Loss
AEPn_summary_df$C2 <- AEPn_C2_summary_df$Loss
AEPn_summary_df$C3 <- AEPn_C3_summary_df$Loss
AEPn_summary_df$C4 <- AEPn_C4_summary_df$Loss
AEPn_summary_df$C5 <- AEPn_C5_summary_df$Loss
# Work out percentage losses
AEPn_summary_df$C1_per <- 100*((AEPn_summary_df$C1/AEPn_summary_df$D)-1)
AEPn_summary_df$C2_per <- 100*((AEPn_summary_df$C2/AEPn_summary_df$D)-1)
AEPn_summary_df$C3_per <- 100*((AEPn_summary_df$C3/AEPn_summary_df$D)-1)
AEPn_summary_df$C4_per <- 100*((AEPn_summary_df$C4/AEPn_summary_df$D)-1)
AEPn_summary_df$C5_per <- 100*((AEPn_summary_df$C5/AEPn_summary_df$D)-1)
# 15,16 - Output files of percentage difference
write.table(AEPg_summary_df, file=outfile15, append=F)
write.table(AEPn_summary_df, file=outfile16, append=F)
###############################
# 4 - Company-level analysis  #
###############################
# Scale UK industry losses by appropriate amounts to make them match
# real/simulated curves (e.g. for a company), generated independently
Data_H1_orig_df <- Data_H1_df
Data_H2_orig_df <- Data_H2_df
######## FOR EACH COMPANY, can just RUN FROM HERE !!
# Repeats for a company, need only from 4.6 the copula modelling only
Data_H1_df <- Data_H1_orig_df
Data_H2_df <- Data_H2_orig_df
### 4.1. Read in Event data & Key RPs, and scale
Company_H1H2_df <- as.data.frame(read.table(Company_H1H2_f,header=F,sep = "\t",na.strings=NaN, stringsAsFactors=F))
#Company_H1H2_df[,2] <- as.numeric(Company_H1H2_df[,2])
# Determine gross the AEP at selected RPs
# This is after the scaling is forced to a set ratio, but this is not relevant as it'll get scaled again.
#AEP_H1_df - gross loss
#AEP_H2_df - gross loss
# Get the numbered RPs
EP_num_df <- as.data.frame(RPs_select_df[3:(length(RPs_select_df$V1)),1])
colnames(EP_num_df)[1] <- "RP"
# Losses from the catastrophe model
EP_num_df$Loss_H1_m <- data.frame(approx(x= AEP_H1_df$RP, y = AEP_H1_df$Insurable_Loss, xout = EP_num_df$RP))[,2]
EP_num_df$Loss_H2_m <- data.frame(approx(x= AEP_H2_df$RP, y = AEP_H2_df$Insurable_Loss, xout = EP_num_df$RP))[,2]
# Add losses from the company
EP_num_df$Loss_H1_c <- Company_H1H2_df$V2[3:length(Company_H1H2_df$V2)]
EP_num_df$Loss_H2_c <- Company_H1H2_df$V3[3:length(Company_H1H2_df$V3)]
# Work out a multiplier per hazard
EP_num_df$multH1 <- EP_num_df$Loss_H1_c/EP_num_df$Loss_H1_m
EP_num_df$multH2 <- EP_num_df$Loss_H2_c/EP_num_df$Loss_H2_m
# Take my 'expert judgement' as mean of key RPs 50-500 yrs
Company_mult_H1 <- mean(EP_num_df$multH1[3:7])
Company_mult_H2 <- mean(EP_num_df$multH2[3:7])
Company_mult_H1[]
Company_mult_H2[]
# Apply these to the original data [overwrites that loaded in]
Data_H1_df$Insurable_Loss <- Data_H1_df$Insurable_Loss*Company_mult_H1
Data_H2_df$Insurable_Loss <- Data_H2_df$Insurable_Loss*Company_mult_H2
plot1 <- ggplot(data=Data_H1_df, aes(log(Insurable_Loss))) +
#geom_histogram(aes(y =..density..,
#alpha=0.2)) +
geom_density(col="blue", size=2) +
#geom_histogram(data=Data_H2_df,
#aes(log(Insurable_Loss), y =..density.., alpha=0.2)) +
geom_density(data=Data_H2_df,
col=2, size=2,
aes(log(Insurable_Loss), y =..density..)) +
labs(title=sprintf("%s(blue) and %s(red)",Haz1_name,Haz2_name), x="log(Loss)", y="prob. density")
plot1
### 4.2 OEP curves - Gross
# Hazard 1
# Create OEP curve - in this case gross input
OEP_df <- F_OEP(Data_H1_df)
OEP_H1c_df <- OEP_df
# Hazard 2
OEP_df <- F_OEP(Data_H2_df)
OEP_H2c_df <- OEP_df
# Plot the first 500 years RP
OEP_H1_df_plot <- subset(OEP_H1c_df, OEP_H1c_df$RP < 500)
OEP_H2_df_plot <- subset(OEP_H2c_df, OEP_H2c_df$RP < 500)
plot2 <- ggplot(data=OEP_H1_df_plot, aes(x = RP, y = (Insurable_Loss/1000000000))) +
geom_line(col="blue", size=2) +
geom_line(data=OEP_H2_df_plot,
col=2, size=2,
aes(x = RP, y = (Insurable_Loss/1000000000))) +
labs(title=sprintf("OEP - %s(blue) and %s(red)",Haz1_name,Haz2_name), x="Return Period (yrs)", y="Loss (? billion)")
plot2
#ggsave(
#  outfile3,    # easiest to use file name to set device
#  plot = plot2,  # default of last_plot()
#)
# 4.2 AEP curves - Gross
# Hazard 1
# Create AEP curve
AEP_df <- F_AEP(Data_H1_df)
AEP_H1c_df <- AEP_df
# Hazard 2
# Create AEP curve - in this case gross input
AEP_df <- F_AEP(Data_H2_df)
AEP_H2c_df <- AEP_df
# Plot the first 500 years RP
AEP_H1c_df_plot <- subset(AEP_H1c_df, AEP_H1c_df$RP < 500)
AEP_H2c_df_plot <- subset(AEP_H2c_df, AEP_H2c_df$RP < 500)
plot3 <- ggplot(data=AEP_H1_df_plot, aes(x = RP, y = (Insurable_Loss/1000000000))) +
geom_line(col="blue", size=2) +
geom_line(data=AEP_H2_df_plot,
col=2, size=2,
aes(x = RP, y = (Insurable_Loss/1000000000))) +
labs(title=sprintf("AEP - %s(blue) and %s(red)",Haz1_name,Haz2_name), x="Return Period (yrs)", y="Loss (? billion)")
plot3
#ggsave(
#  outfile4,    # easiest to use file name to set device
#  plot = plot3,  # default of last_plot()
#)
# 4.3 Apply illustrative reinsurance at Event Level - input case (i.e. whatever dependency is in the input data). Will need to re-applied for different levels of correlation.
# Group the two hazards
Reins_df <- F_Haz_comb(Data_H1_df,Data_H2_df)
# Create AEP curve - in this case gross input
AEP_df <- F_AEP(Reins_df)
# Create OEP curve - in this case gross input
OEP_df <- F_OEP(Reins_df)
# Apply reinsurance
Reins_Default_df <- F_ReIns_Apply(Reins_df, retention_mult, limit_RP,OEP_df)
#OEP and AEP, have a look at the combined gross and net of reinsurance
# OEP first
# Gross
OEP_df <- F_OEP(Reins_Default_df)
OEP_Default_gross_c_df <- OEP_df
# net - just if due to simple reinsurance structure
OEP_df <- F_OEP_net(Reins_Default_df)
OEP_Default_net_c_df <- OEP_df
# AEP Second
# Gross
AEP_df <- F_AEP(Reins_Default_df)
AEP_Default_gross_c_df <- AEP_df
# net
AEP_df <- F_AEP_net(Reins_Default_df)
AEP_Default_net_c_df <- AEP_df
# Plot the first 500 years RP -OEP
OEP_Default_gross_df_plot <- subset(OEP_Default_gross_c_df, OEP_Default_gross_c_df$RP < 500)
OEP_Default_net_df_plot <- subset(OEP_Default_net_c_df, OEP_Default_net_c_df$RP < 500)
plot4 <- ggplot(data=OEP_Default_gross_df_plot, aes(x = RP, y = (Insurable_Loss/1000000000))) +
geom_line(col="blue", size=2) +
geom_line(data=OEP_Default_net_df_plot,
col=2, size=2,
aes(x = RP, y = (Retained/1000000000))) +
labs(title=sprintf("OEP - %s(blue) and %s(red)","gross","net"), x="Return Period (yrs)", y="Loss (? billion)")
plot4
# Plot the first 500 years RP - AEP
AEP_Default_gross_df_plot <- subset(AEP_Default_gross_c_df, AEP_Default_gross_c_df$RP < 500)
AEP_Default_net_df_plot <- subset(AEP_Default_net_c_df, AEP_Default_net_c_df$RP < 500)
plot5 <- ggplot(data=AEP_Default_gross_df_plot, aes(x = RP, y = (Insurable_Loss/1000000000))) +
geom_line(col="blue", size=2) +
geom_line(data=AEP_Default_net_df_plot,
col=2, size=2,
aes(x = RP, y = (Retained/1000000000))) +
labs(title=sprintf("AEP - %s(blue) and %s(red)","gross","net"), x="Return Period (yrs)", y="Loss (? billion)")
plot5
# 4.4 - With resampling of the original data to reduce simulation uncertainty (see Section 1.8)
## Simulation parameters
Itt <- 1
OEPg_df <- OEP_Default_gross_c_df
OEPn_df <- OEP_Default_net_c_df
AEPg_df <- AEP_Default_gross_c_df
AEPn_df <- AEP_Default_net_c_df
while (Itt <= Sim_Itt) {
col_name <- sprintf("Sim%s",Itt)
# Resample
OEPg_df[,col_name] <- sample(OEPg_df$Insurable_Loss, nrow(OEPg_df),replace = TRUE)
OEPn_df[,col_name] <- sample(OEPn_df$Retained, nrow(OEPn_df),replace = TRUE)
AEPg_df[,col_name] <- sample(AEPg_df$Insurable_Loss, nrow(AEPg_df),replace = TRUE)
AEPn_df[,col_name] <- sample(AEPn_df$Retained, nrow(AEPn_df),replace = TRUE)
# Sort in descending order
col_sort_num <- which( colnames(OEPg_df)==col_name)    # This is a hack to get around handling of a large df I think
temp <- as.data.frame(OEPg_df[,..col_sort_num])        # No idea what the addition of '..' means, but suggested by error message. Works.
OEPg_df[,col_name] <- temp[order(-temp[,1]),]
temp <- as.data.frame(OEPn_df[,..col_sort_num])
OEPn_df[,col_name] <- temp[order(-temp[,1]),]
temp <- as.data.frame(AEPg_df[,col_name])
AEPg_df[,col_name] <- temp[order(-temp[,1]),]
temp <- as.data.frame(AEPn_df[,col_name])
AEPn_df[,col_name] <- temp[order(-temp[,1]),]
Itt = Itt +1
}
#Means and stddev of rows - self-defined function - F7
OEPg_df <- F_EP_mean(OEPg_df)
OEPn_df <- F_EP_mean(OEPn_df)
AEPg_df <- F_EP_mean(AEPg_df)
AEPn_df <- F_EP_mean(AEPn_df)
# Rename dfs for storage
OEP_Default_gross_c_df <- OEPg_df
OEP_Default_net_c_df <- OEPn_df
AEP_Default_gross_c_df <- AEPg_df
AEP_Default_net_c_df <- AEPn_df
#### 4.5 - Inducing a stated correlation #####
# i.e. as section 2 above
# using _c for 'company', but on the OEP / AEP curves only
# There is some repetition here, but I'm keeping this identical to Section 2 above, excpet the _c
# First task is to ensure that all rows have a value (even if zero)
# Create a list of all relevant years (or other time-windows), and join to it to the data
# Inputs both need to have
# "Year", "Severity" and "Insurable_Loss"
# Get Years to patch holes with
Yrs <- max(Data_H1_df$Year)
Yrs_df <- as.data.frame(seq(1,Yrs))
colnames(Yrs_df)[1] <- "Year"
N_data <- length(Yrs_df[,1]) # Find number of observations
# Aggregate, rename columns, fill any holes, rank by severity
# Hazard 1
# Inputs
H_num <- "H1"
Data_df <- Data_H1_df
# Function
Agg_H_df <- F_Join_Prep(Data_df, H_num)
# And, save
Agg_H1_df <- Agg_H_df
# Hazard 2
# Inputs
H_num <- "H2"
Data_df <- Data_H2_df
# Function
Agg_H_df <- F_Join_Prep(Data_df, H_num)
# And, save
Agg_H2_df <- Agg_H_df
# Quick check to see how correlated the inputs are
# In case the 'independent' isn't that
cor_in <- cor(x = Agg_H1_df$Loss_H1, y =  Agg_H2_df$Loss_H2)
write(sprintf("\nCorrelation - rp: %s",cor_in), file=outfile1, append=T)
# Merge these two together
# Columns for each - linking function works on the basis of two columns
# for severity, each with a rank
Agg_H1H2_df <- Agg_H1_df
c_start <- ncol(Agg_H1_df)+1
c_end <- ncol(Agg_H1_df)+ncol(Agg_H2_df)
Agg_H1H2_df[,c_start:c_end] <- Agg_H2_df
# Set up an AEP/OEP file to record the results in
l_df1 <- length(Data_H1_df[,1])
l_df2 <- length(Data_H2_df[,1])
## Set up the AEP_df for multiple columns
y_max <- max(Data_H1_df[,"Year"])
AEP_df <- as.data.frame(seq.int(1:y_max))
colnames(AEP_df)[1] <- "Year"
# Add yearly exceedence probability (i.e. rate)
AEP_df$rate <- seq.int(nrow(AEP_df))/max(AEP_df$Year)
# Add return period
AEP_df$RP <- 1/AEP_df$rate
AEPg_df <- AEP_df
AEPn_df <- AEP_df
## Set up the OEP_df for multiple columns
Reins_df <- F_Haz_comb(Data_H1_df,Data_H2_df)
# Create OEP curve - in this case gross input
OEP_df <- F_OEP(Reins_df)
OEPg_df <- OEP_df[,c("Year","rate", "RP")]
OEPn_df <- OEP_df[,c("Year","rate", "RP")]
##########################
#  4.6 Gaussian Copula   #
##########################
# "C6" is for Correlation 6
# Parameters
# Estimate could be taken from a fit, or from the academic literature
#rho <- 0.4
rho <- r_val
Itt <- 1
# Explicitly reset the data frames
AEPg_df <- AEP_df
AEPn_df <- AEP_df
OEPg_df <- OEP_df[,c("Year","rate", "RP")]
OEPn_df <- OEP_df[,c("Year","rate", "RP")]
## Need to loop start here .........
## Uses
# Data_H1_df - Event by event
# Data_H2_df - Event by event
# Agg_H1H2_df - Annual aggregates
# AEP/OEP files of the same length to add to - prepared just above
# e.g. OEPg_df
# Add a first line to an output file
write(sprintf("%s:","Correlation 6"), file=outfile19, append=T)
write(sprintf("%s","Spearman: Severity, Loss, Rank"), file=outfile19, append=T)
write(sprintf("%s","Fits: rho, n/a = 0.00, tau"), file=outfile19, append=T)
cop_num <- 1
while (Itt <= Sim_Itt) {
col_name <- sprintf("Sim%s",Itt)
# A) - Simulation using the copula
# Two dimensions, rho as correlation coefficient, df is degrees of freedom
#u <- rCopula(N_data,tCopula(dim=2,rho,df=100))
u <- rCopula(N_data,normalCopula(dim=2,rho))
# To match previous scripts, rename to x2
x2 <- u
x2 <- as.data.frame(x2)
# x2 is a data frame that has two columns of simulated, correlated random
# values between 0 and 1
# Join the simulated years by severity of the hazards
# x2 contains the ranks to join by (i.e. with the correlation in)
# Agg_H1H2_df is the hazard/loss data, with ranked severity
x3 <- F_Join(x2,Agg_H1H2_df)
# B) - Apply the link to the event data in order to apply reinsurance at an event level
# Retain the years in Data_H1_df, reordering Data_H2_df
# Create link file i.e. yearH1, yearH2 to be joined .... get this from x3
link_yrs <- x3[,c("Yr_H1","Yr_H2")]
colnames(link_yrs)[match("Yr_H2",names(link_yrs))] <- c(sprintf("Year"))
# Join this with Data for Hazard 2
Reorder_H2_df <- merge(x = Data_H2_df, y = link_yrs, by = "Year", all.x = TRUE)
# Change the "Year" to be that from H1 defined by the link
Reorder_H2_df$Year <- Reorder_H2_df$Yr_H1
Reorder_H2_df$Yr_H1 <- NULL
# And, now treat these re-numbered years as above ...
# Group the two hazards
Reins_df <- F_Haz_comb(Data_H1_df,Reorder_H2_df)
# Create AEP curve - in this case gross input
AEPx_df <- F_AEP(Reins_df)
# Create OEP curve - in this case gross input
OEPx_df <- F_OEP(Reins_df)
# Apply reinsurance
Reins_C_df <- F_ReIns_Apply(Reins_df, retention_mult, limit_RP,OEPx_df)
#Fit copula to input and output, and dump to file
F_Equiv_fit(Data_H1_df, Reorder_H2_df, outfile19,cop_num)
#OEP and AEP, have a look at the combined gross and net of reinsurance
# OEP first, gross then net
temp <- F_OEP(Reins_C_df)
OEPg_df[1:nrow(temp),col_name] <- temp$Insurable_Loss
temp <- F_OEP_net(Reins_C_df)
OEPn_df[1:nrow(temp),col_name] <- temp$Retained
# AEP Second, gross then net
temp <- F_AEP(Reins_C_df)
AEPg_df[1:length(temp[,1]),col_name] <- temp$Insurable_Loss
temp <- F_AEP_net(Reins_C_df)
AEPn_df[1:length(temp[,1]),col_name] <- temp$Retained
Itt = Itt +1
}   # End of the while loop
# Replace all na with zeros
# d[is.na(d)] <- 0
OEPg_df[is.na(OEPg_df)] <- 0
OEPn_df[is.na(OEPn_df)] <- 0
AEPg_df[is.na(AEPg_df)] <- 0
AEPn_df[is.na(AEPn_df)] <- 0
#Means and stddev of rows - self-defined function - F7
OEPg_df <- F_EP_mean(OEPg_df)
OEPn_df <- F_EP_mean(OEPn_df)
AEPg_df <- F_EP_mean(AEPg_df)
AEPn_df <- F_EP_mean(AEPn_df)
# And, file the specific ones
OEP_C6_gross_df <- OEPg_df
OEP_C6_net_df <- OEPn_df
AEP_C6_gross_df <- AEPg_df
AEP_C6_net_df <- AEPn_df
# Make the plotting generic by transfering to the 'C' files
# Will also work for summary stats if this is needed.
OEP_C_gross_df <- OEP_C6_gross_df
OEP_C_net_df <- OEP_C6_net_df
AEP_C_gross_df <- AEP_C6_gross_df
AEP_C_net_df <- AEP_C6_net_df
# Plot OEP for the first 500 years RP gross vs net (means) - cross-check
OEP_C_gross_df_plot <- subset(OEP_C_gross_df, OEP_C_gross_df$RP < 500)
OEP_C_net_df_plot <- subset(OEP_C_net_df, OEP_C_net_df$RP < 500)
plot7 <- ggplot(data=OEP_C_gross_df_plot, aes(x = RP, y = (Loss/1000000000))) +
geom_line(col="blue", size=2) +
geom_line(data=OEP_C_net_df_plot,
col=2, size=2,
aes(x = RP, y = (Loss/1000000000))) +
labs(title=sprintf("OEP for C - means - %s(blue) and %s(red)","gross","net"), x="Return Period (yrs)", y="Loss (? billion)")
plot7
# Plot AEP for the first 500 years RP - AEP gross vs net - cross-check
AEP_C_gross_df_plot <- subset(AEP_C_gross_df, AEP_C_gross_df$RP < 500)
AEP_C_net_df_plot <- subset(AEP_C_net_df, AEP_C_net_df$RP < 500)
plot8 <- ggplot(data=AEP_C_gross_df_plot, aes(x = RP, y = (Loss/1000000000))) +
geom_line(col="blue", size=2) +
geom_line(data=AEP_C_net_df_plot,
col=2, size=2,
aes(x = RP, y = (Loss/1000000000))) +
labs(title=sprintf("AEP for C - means - %s(blue) and %s(red)","gross","net"), x="Return Period (yrs)", y="Loss (? billion)")
plot8
# Plot the first 500 years RP of the gross AEP - effect of correlation - KEY FIGURE
AEP_C_gross_df_plot <- subset(AEP_C_gross_df, AEP_C_gross_df$RP < 500)
# Unhash this line and change to 5,000 years to show all RPs
AEP_Default_gross_c_df_plot <- subset(AEP_Default_gross_c_df, AEP_Default_gross_c_df$RP < 500)
plot9 <- ggplot(data=AEP_C_gross_df_plot, aes(x = RP, y = (Loss/1000000000))) +
# AEP gross - correlated
# Stderr
geom_ribbon (aes(ymin=MinE/1000000000, ymax=MaxE/1000000000, alpha=0.3), fill = "blue") +
# Mean
geom_line(data=AEP_C_gross_df_plot,
col="blue", size=2, linetype="dashed",
aes(x = RP, y = (Loss/1000000000))) +
# AEP gross - uncorrelated
# Stderr
geom_ribbon (data=AEP_Default_gross_c_df_plot,
aes(y = (Loss/1000000000), ymin=MinE/1000000000, ymax=MaxE/1000000000, alpha=0.3, fill = "red")) +
# Mean
geom_line(data=AEP_Default_gross_c_df_plot, col="red", size=2, linetype="dashed",
aes(x = RP, y = (Loss/1000000000))) +
# General
theme(legend.position = "none") +
labs(title=sprintf("AEP gross - %s(%.2f,blue) and %s(red)","correlated",rho,"independent"), x="Return Period (yrs)", y="Loss (? billion)")
plot9
ggsave(
outfile20,    # easiest to use file name to set device
plot = plot9,  # default of last_plot()
)
# Plot the first 500 years RP of the gross AEP - effect of correlation - KEY FIGURE
AEP_C_net_df_plot <- subset(AEP_C_net_df, AEP_C_net_df$RP < 500)
# Unhash this line and change to 5,000 years to show all RPs
AEP_Default_net_c_df_plot <- subset(AEP_Default_net_c_df, AEP_Default_net_c_df$RP < 500)
plot10 <- ggplot(data=AEP_C_net_df_plot, aes(x = RP, y = (Loss/1000000000))) +
# AEP gross - correlated
# Stderr
geom_ribbon (aes(ymin=MinE/1000000000, ymax=MaxE/1000000000, alpha=0.3), fill = "blue") +
# Mean
geom_line(data=AEP_C_net_df_plot,
col="blue", size=2, linetype="dashed",
aes(x = RP, y = (Loss/1000000000))) +
# AEP gross - uncorrelated
# Stderr
geom_ribbon (data=AEP_Default_net_c_df_plot,
aes(y = (Loss/1000000000), ymin=MinE/1000000000, ymax=MaxE/1000000000, alpha=0.3, fill = "red")) +
# Mean
geom_line(data=AEP_Default_net_c_df_plot, col="red", size=2, linetype="dashed",
aes(x = RP, y = (Loss/1000000000))) +
# General
theme(legend.position = "none") +
labs(title=sprintf("AEP net - %s(%.2f,blue) and %s(red)","correlated",rho,"independent"), x="Return Period (yrs)", y="Loss (? billion)")
plot10
ggsave(
outfile21,    # easiest to use file name to set device
plot = plot10,  # default of last_plot()
)
### 4.7 - Output Analytics
# Results are in the following files
# Loop through to extract at selected RPs
# And, tabulated comparison
# AEP_C_gross_df
# Select certain RPs from a curve
# _c = "company"
AEPg_D_summary_df <- F_EP_summary(AEP_Default_gross_c_df,RPs_select_df)
AEPg_C6_summary_df <- F_EP_summary(AEP_C6_gross_df,RPs_select_df)
AEPg_summary_df <- as.data.frame(AEPg_D_summary_df[,"RP"])
colnames(AEPg_summary_df)[1] <- "RP"
AEPg_summary_df$D <- AEPg_D_summary_df$Loss
AEPg_summary_df$C6 <- AEPg_C6_summary_df$Loss
# Work out percentage losses
AEPg_summary_df$C6_per <- 100*((AEPg_summary_df$C6/AEPg_summary_df$D)-1)
#AEP_C_net_df
AEPn_D_summary_df <- F_EP_summary(AEP_Default_net_c_df,RPs_select_df)
AEPn_C6_summary_df <- F_EP_summary(AEP_C6_net_df,RPs_select_df)
AEPn_summary_df <- as.data.frame(AEPn_D_summary_df[,"RP"])
colnames(AEPn_summary_df)[1] <- "RP"
AEPn_summary_df$D <- AEPn_D_summary_df$Loss
AEPn_summary_df$C6 <- AEPn_C6_summary_df$Loss
# Work out percentage losses
AEPn_summary_df$C6_per <- 100*((AEPn_summary_df$C6/AEPn_summary_df$D)-1)
###############################
# 5- Input into SCR modelling #
###############################
# Run3_AEP_net_impact_Company.txt
# Run3_AEP_gross_impact_Company.txt
# 22,23 - Output files of percentage difference
write.table(AEPg_summary_df, file=outfile22, append=F)
write.table(AEPn_summary_df, file=outfile23, append=F)
