---
title: "Simulation Report"
output:
  html_document:
    toc: true
    toc_depth: 2
    theme: readable
    df_print: paged
params:
  numOfSimulations: NULL
  freq_params: NULL
  sev_params: NULL
  seedSetBinary: NULL
  seedValue: NULL
  freqDistr: NULL
  sevDistr: NULL
  paretoSlice: NULL
  pareto_slice_times: NULL
  slice_pareto_alphas: NULL
  slice_pareto_x_ms: NULL
  sevCapBinary: NULL
  sev_cap_amount: NULL
  reinsuranceStructureEEL: NULL
  reinsurance_structure_eel_dedctible_amount: NULL
  reinsurance_structure_eel_limit_amount: NULL
  reinsuranceStructureAL: NULL
  reinsurance_structure_al_dedctible_amount: NULL
  reinsurance_structure_al_limit_amount: NULL
  multiprocessing: NULL
  reinsuranceStructureLimitedReinstatements: NULL
  reinsuranceStructureReinstatementLimit: NULL
  total_claims_data: NULL
---

# Simulation Report

This report summarizes the simulation settings and the resulting distribution of total claims.

## Overview

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)

fmt_num <- function(x) formatC(x, format = "f", digits = 2, big.mark = ",")
fmt_int <- function(x) formatC(round(x, 0), format = "d", big.mark = ",")
fmt_pct <- function(x) paste0(fmt_num(100 * x), "%")
claims <- params$total_claims_data
```

```{r overview-table}
overview <- data.frame(
  Metric = c(
    "Number of simulations",
    "Multiprocessing used",
    "Fixed seed",
    "Seed value"
  ),
  Value = c(
    fmt_int(params$numOfSimulations),
    ifelse(isTRUE(params$multiprocessing), "Yes", "No"),
    ifelse(isTRUE(params$seedSetBinary), "Yes", "No"),
    ifelse(is.null(params$seedValue), "—", as.character(params$seedValue))
  )
)

knitr::kable(overview, align = c("l", "l"))
```

## Model settings

```{r settings-table}
settings <- data.frame(
  Section = c(
    rep("Frequency", 2),
    rep("Severity", 2),
    rep("Pareto slicing", 3),
    rep("Severity cap", 2),
    rep("EEL reinsurance", 3),
    rep("AL reinsurance", 3)
  ),
  Metric = c(
    "Distribution", "Parameters",
    "Distribution", "Parameters",
    "Applied", "Slice count", "Slice parameters",
    "Applied", "Cap amount",
    "Structure", "Deductible", "Limit",
    "Structure", "Deductible", "Limit"
  ),
  Value = c(
    params$freqDistr,
    paste(params$freq_params, collapse = ", "),
    params$sevDistr,
    paste(params$sev_params, collapse = ", "),
    ifelse(isTRUE(params$paretoSlice), "Yes", "No"),
    ifelse(is.null(params$pareto_slice_times), "—", as.character(params$pareto_slice_times)),
    ifelse(isTRUE(params$paretoSlice),
           paste(
             "alpha:", paste(params$slice_pareto_alphas, collapse = ", "),
             "; x_m:", paste(params$slice_pareto_x_ms, collapse = ", ")
           ),
           "—"),
    ifelse(isTRUE(params$sevCapBinary), "Yes", "No"),
    ifelse(is.null(params$sev_cap_amount), "—", fmt_num(params$sev_cap_amount)),
    params$reinsuranceStructureEEL,
    ifelse(is.null(params$reinsurance_structure_eel_dedctible_amount), "—", fmt_num(params$reinsurance_structure_eel_dedctible_amount)),
    ifelse(is.null(params$reinsurance_structure_eel_limit_amount), "—", fmt_num(params$reinsurance_structure_eel_limit_amount)),
    params$reinsuranceStructureAL,
    ifelse(is.null(params$reinsurance_structure_al_dedctible_amount), "—", fmt_num(params$reinsurance_structure_al_dedctible_amount)),
    ifelse(is.null(params$reinsurance_structure_al_limit_amount), "—", fmt_num(params$reinsurance_structure_al_limit_amount))
  )
)

knitr::kable(settings, align = c("l", "l", "l"))
```

## Summary statistics

```{r summary-stats}
summary_stats <- data.frame(
  Metric = c(
    "Count",
    "Mean",
    "Median",
    "Standard deviation",
    "Minimum",
    "Maximum",
    "Coefficient of variation"
  ),
  Value = c(
    fmt_int(length(claims)),
    fmt_num(mean(claims, na.rm = TRUE)),
    fmt_num(median(claims, na.rm = TRUE)),
    fmt_num(sd(claims, na.rm = TRUE)),
    fmt_num(min(claims, na.rm = TRUE)),
    fmt_num(max(claims, na.rm = TRUE)),
    fmt_num(sd(claims, na.rm = TRUE) / mean(claims, na.rm = TRUE))
  )
)

knitr::kable(summary_stats, align = c("l", "r"))
```

## Percentiles and tail risk

```{r tail-table}
probs <- c(0.5, 0.75, 0.9, 0.95, 0.975, 0.99, 0.995)
qv <- quantile(claims, probs = probs, na.rm = TRUE)

percentile_df <- data.frame(
  Percentile = fmt_pct(probs),
  Claims = fmt_num(as.numeric(qv))
)

knitr::kable(percentile_df, align = c("l", "r"))
```

```{r tail-risk-table}
var95 <- unname(quantile(claims, 0.95, na.rm = TRUE))
var99 <- unname(quantile(claims, 0.99, na.rm = TRUE))
var995 <- unname(quantile(claims, 0.995, na.rm = TRUE))

tail_df <- data.frame(
  Metric = c(
    paste0("VaR ", fmt_pct(0.95)),
    paste0("TVaR ", fmt_pct(0.95)),
    paste0("VaR ", fmt_pct(0.99)),
    paste0("TVaR ", fmt_pct(0.99)),
    paste0("VaR ", fmt_pct(0.995)),
    paste0("TVaR ", fmt_pct(0.995))
  ),
  Value = c(
    fmt_num(var95),
    fmt_num(mean(claims[claims >= var95], na.rm = TRUE)),
    fmt_num(var99),
    fmt_num(mean(claims[claims >= var99], na.rm = TRUE)),
    fmt_num(var995),
    fmt_num(mean(claims[claims >= var995], na.rm = TRUE))
  )
)

knitr::kable(tail_df, align = c("l", "r"))
```

## Visual diagnostics

### Histogram

```{r hist-plot}
hist(
  claims,
  breaks = 200,
  main = "Histogram of Total Claims",
  xlab = "Total Claims",
  ylab = "Frequency",
  col = "steelblue",
  border = "white"
)
```

### Box plot

```{r boxplot-plot}
boxplot(
  claims,
  main = "Box Plot of Total Claims",
  ylab = "Total Claims",
  col = "lightsteelblue",
  border = "gray40"
)
```

### Empirical CDF

```{r ecdf-plot}
plot(
  ecdf(claims),
  xlab = "Total Claims",
  ylab = "Cumulative Probability",
  main = "Empirical CDF of Total Claims",
  verticals = TRUE,
  do.points = FALSE
)
```

## Notes

- High skewness and a large gap between the median and tail percentiles usually indicate heavy-tail behavior.
- VaR and TVaR are often the most useful measures for insurance and reinsurance interpretation.
- If the histogram is very concentrated near zero with a long right tail, consider adding a log-scale histogram as an optional view.
