Open RLearnerVJ opened 2 weeks ago
library("writexl") library("readxl") library("openxlsx")
input_file <- "C:/Users/vpers/OneDrive/Documents/R Practice/input.xlsx" output_file <- "C:/Users/vpers/OneDrive/Documents/R Practice/output/All_Simulations_Loss_Set_with_Tables.xlsx"
input_data <- read_excel(input_file, sheet = 1) transition_matrix <- read_excel(input_file, sheet = 2)
Year <- integer() Day_2 <- integer() # Day used for clash claims Loss <- numeric() Segment <- character() Clash <- character() # Indicates if there's a clash Clash_ID <- integer() # Unique ID for each clash Clash_Segment <- character() # Segment that the claim clashed with
clash_counter <- 1
years <- 100 # Set the number of years to 100
used_days <- vector("list", years) # List of used days for each year
set.seed(554) # For reproducibility
for (i in 1:years) {
used_days[[i]] <- integer() # Initialize the list to track used days in this year
for (s in 1:nrow(input_data)) {
segment_row <- input_data[s, ]
segment_name <- segment_row$Segment
lambda <- segment_row$lambda
sev <- segment_row$sev
sev_var <- segment_row$sev_var
Beta <- sev / sev_var
Alpha <- sev * Beta
# Simulate claim count for this segment in the current year
claimcount <- rpois(1, lambda)
# Generate losses for each claim
for (j in 1:claimcount) {
# Assign a unique day within the year (1 to 365)
available_days <- setdiff(1:365, used_days[[i]]) # Get days that haven't been used
day <- sample(available_days, 1) # Pick one available day
used_days[[i]] <- c(used_days[[i]], day) # Mark this day as used
loss <- rgamma(1, shape = Alpha, rate = Beta)
clash_flag <- "No"
clash_id <- NA
clash_segment <- NA
shared_day <- day # Default shared day is the original day
# Determine clash probabilities for the current segment
clash_probs <- transition_matrix[transition_matrix$Segment == segment_name, -1]
segments <- colnames(clash_probs)
# Check if a clash occurs based on probabilities
random_vals <- runif(length(segments))
clash_occurred <- random_vals < as.numeric(clash_probs)
if (any(clash_occurred)) {
# Assign clash details to this claim
clash_flag <- "Yes"
clash_segment <- segments[clash_occurred][1] # Select the first clashing segment
clash_id <- clash_counter
# Use the original day's value as the shared Day_2 for the clash
shared_day <- day
# Record the original claim with clash information and shared Day_2
Year <- c(Year, i)
Day_2 <- c(Day_2, shared_day)
Loss <- c(Loss, loss)
Segment <- c(Segment, segment_name)
Clash <- c(Clash, clash_flag)
Clash_ID <- c(Clash_ID, clash_id)
Clash_Segment <- c(Clash_Segment, clash_segment)
# Create a matching clash claim in the clashing segment with the same shared Day_2
clash_loss <- rgamma(1, shape = Alpha, rate = Beta) # Generate a random loss for the clash claim
# Record the clash claim with the same Clash_ID and shared Day_2
Year <- c(Year, i)
Day_2 <- c(Day_2, shared_day) # Use the shared day from the original claim
Loss <- c(Loss, clash_loss)
Segment <- c(Segment, clash_segment)
Clash <- c(Clash, "Yes")
Clash_ID <- c(Clash_ID, clash_id)
Clash_Segment <- c(Clash_Segment, segment_name)
# Increment clash counter for the next unique clash
clash_counter <- clash_counter + 1
} else {
# Record non-clash claim with Day_2 as the original day
Year <- c(Year, i)
Day_2 <- c(Day_2, day)
Loss <- c(Loss, loss)
Segment <- c(Segment, segment_name)
Clash <- c(Clash, clash_flag)
Clash_ID <- c(Clash_ID, clash_id)
Clash_Segment <- c(Clash_Segment, NA)
}
}
} }
Loss_Set <- data.frame(Year, Day_2, Loss, Segment, Clash, Clash_ID, Clash_Segment)
wb <- createWorkbook()
addWorksheet(wb, "Simulation_Results") writeData(wb, "Simulation_Results", Loss_Set)
saveWorkbook(wb, output_file, overwrite = TRUE)
library("writexl")
Input variables for frequency and severity distributions
lambda<- 5 ##frequency being modeled by poisson
sev<- 10000 sev_var <- 5000
Compute alpha and Beta for gamma severity distribution
Beta <- sev/sev_var Alpha <-sev*Beta
10,000 simulations -> 10,000 years
simulation_number <- c(1:10000)
simulated claim counts
Vector for Year, Day and Sev will need to be as long as the sum of all the modeled claim counts
Combine Year, Day and Loss data frames and export output
write_xlsx(
Loss_Set,"My_path\Loss_Set.xlsx",