# Load required libraries
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(fitdistrplus)
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
## Loading required package: survival
library(ggplot2)
# Step 1: Load and clean monthly claims data
claims <- read.csv("C:/Users/accou/Downloads/ZP Risk Assessment/Claims dataset 2A.csv")
# Convert TotalClaims to numeric if needed
claims$TotalClaims <- as.numeric(gsub("[^0-9.]", "", claims$TotalClaims))
claims <- claims %>% filter(!is.na(TotalClaims))
Step 2: Compute fixed exposure and normalize losses
exposure_years <- as.numeric(as.Date("2025-07-31") - as.Date("2018-03-31")) / 365.25
claims <- claims %>%
mutate(loss_per_year = TotalClaims / exposure_years)
Step 3: Fit parametric distribution (log-normal)
fit <- fitdist(claims$TotalClaims, "lnorm")
mu <- fit$estimate["meanlog"]
sigma <- fit$estimate["sdlog"]
Step 4: Compute VaR and CVaR
confidence_level <- 0.99
VaR_param <- qlnorm(confidence_level, meanlog = mu, sdlog = sigma)
VaR_hist <- quantile(claims$TotalClaims, probs = confidence_level)
set.seed(123)
sim_losses <- rlnorm(10000, meanlog = mu, sdlog = sigma)
VaR_sim <- quantile(sim_losses, probs = confidence_level)
CVaR_sim <- mean(sim_losses[sim_losses > VaR_sim])
Step 5: Visualize simulated distribution
ggplot(data.frame(loss = sim_losses), aes(x = loss)) +
geom_histogram(binwidth = 5000, fill = "steelblue", color = "blue") +
geom_vline(xintercept = VaR_sim, color = "red", linetype = "dashed", size = 1.2) +
geom_vline(xintercept = CVaR_sim, color = "darkred", linetype = "dotted", size = 1.2) +
scale_x_continuous(breaks = seq(0, max(sim_losses), by = 500000), labels = scales::comma) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Simulated Monthly Loss Distribution with VaR and CVaR",
x = "Monthly Loss", y = "Frequency")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Step 6: Output summary
cat("Parametric VaR (99%):", round(VaR_param, 2), "\n")
## Parametric VaR (99%): 2484980
cat("Historical VaR (99%):", round(VaR_hist, 2), "\n")
## Historical VaR (99%): 2191536
cat("Simulated VaR (99%):", round(VaR_sim, 2), "\n")
## Simulated VaR (99%): 2494759
cat("Simulated CVaR (99%):", round(CVaR_sim, 2), "\n")
## Simulated CVaR (99%): 2803611