RLearnerVJ / Clash

0 stars 0 forks source link

Test #1

Open RLearnerVJ opened 2 weeks ago

RLearnerVJ commented 2 weeks ago

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

set.seed(554) # any number
claimcount <- rpois(n=10000,lambda = lambda)

YearCount<-sum(claimcount)

Vector for Year, Day and Sev will need to be as long as the sum of all the modeled claim counts

Year<-c(1:YearCount)
Day<-c(1:YearCount)
Loss<-c(1:YearCount)

#Starting point for index
k<- 1
for (i in 1:10000) {
  freq <- claimcount[i]

    for (j in 1:freq) {
      Year[k]<- i
      Day[k]<- 365*runif(1)
      Loss[k]<- rgamma(n=1,Alpha,Beta)
      k<-k+1
    }
  ##Using the sum of the simulated claim frequencies
  k<- sum(claimcount[1:i])+1
}

Combine Year, Day and Loss data frames and export output

Loss_Set<- data.frame(Year,Day,Loss)

write_xlsx(

Loss_Set,"My_path\Loss_Set.xlsx",

#  col_names = TRUE, 
 # format_headers = TRUE)
RLearnerVJ commented 2 weeks ago

library("writexl") library("readxl") library("openxlsx")

Define file paths for input and output

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"

Load input data

input_data <- read_excel(input_file, sheet = 1) transition_matrix <- read_excel(input_file, sheet = 2)

Initialize storage for results

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

Initialize a counter for unique Clash IDs

clash_counter <- 1

Set the number of years for the simulation

years <- 100 # Set the number of years to 100

Track used days for each year

used_days <- vector("list", years) # List of used days for each year

Simulation loop

set.seed(554) # For reproducibility

for (i in 1:years) {

Initialize available days for the year (1 to 365) if not already set

used_days[[i]] <- integer() # Initialize the list to track used days in this year

Loop through each segment for each year

for (s in 1:nrow(input_data)) {

Get parameters for the current segment

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)
  }
}

} }

Combine Year, Day_2, Loss, Segment, Clash, Clash_ID, and Clash_Segment into a data frame

Loss_Set <- data.frame(Year, Day_2, Loss, Segment, Clash, Clash_ID, Clash_Segment)

Create a workbook and add only the main simulation results sheet

wb <- createWorkbook()

Add the main simulation results sheet

addWorksheet(wb, "Simulation_Results") writeData(wb, "Simulation_Results", Loss_Set)

Save the workbook with only the Simulation_Results sheet

saveWorkbook(wb, output_file, overwrite = TRUE)

RLearnerVJ commented 2 weeks ago

input.xlsx