Skip to content

Commit

Permalink
Rename functions and lists
Browse files Browse the repository at this point in the history
  • Loading branch information
melantha-wang committed Sep 5, 2021
1 parent cb7e11c commit 54963a6
Show file tree
Hide file tree
Showing 13 changed files with 733 additions and 715 deletions.
338 changes: 169 additions & 169 deletions SPLICE/.Rhistory
Original file line number Diff line number Diff line change
@@ -1,172 +1,3 @@
View(indicator)
View(as.numeric(indicator))
View(as.factor(indicator))
View(output_actual[indicator])
View(output_actual *indicator)
View(upper.tri(output_actual[, 1:40], diag = TRUE))
output_actual[!indicator] <- NA
View(output_actual)
x <- output_noinflation
View(x[!indicator])
x[!indicator] <- NA
View(x)
View(output_noinflation)
document()
?output_incurred
vignette("SPLICE-demo", package = "SPLICE")
vignette("SynthETIC-demo", package = "SynthETIC")
?vignette
# incurred triangles
# Constant dollar value INCREMENTAL triangle
incurred_noInf <- incurred_output(test, incremental = TRUE)
# incurred triangles
# Constant dollar value INCREMENTAL triangle
incurred_noInf <- output_incurred(test, incremental = TRUE)
View(incurred_noInf)
incurred_noInf <- output_incurred(test, incremental = TRUE, aggregate_level = 2)
View(incurred_noInf)
incurred_noInf <- output_incurred(test, incremental = F)
incurred_noInf <- output_incurred(test, incremental = F, future = F)
View(incurred_noInf)
load_all()
incurred_noInf <- output_incurred(test, incremental = F, future = F)
View(incurred_noInf)
load_all()
incurred_noInf <- output_incurred(test, incremental = F, future = F)
View(incurred_noInf)
ceiling(1:10)
ceiling(1:10 / 4)
40 + 40 - 1
?output_incurred
i <- 1
j <- 1
incurred_history <- test
# convert to discrete time scale (t in terms of absolute time)
t <- ceiling(incurred_history[[i]][[j]]$txn_time / aggregate_level)
aggregate_level <- 4
# convert to discrete time scale (t in terms of absolute time)
t <- ceiling(incurred_history[[i]][[j]]$txn_time / aggregate_level)
t
incurred_right <- incurred_history[[i]][[j]]$incurred_right
i_rescaled <- 1
side <- 10
# Now get the latest incurred estimate in a period
incurred_latest <- rep(NA, i_rescaled + side - 1)
incurred_latest
# Fill the incurred estimates
incurred_latest[unique(t)] <- incurred_right[!rev(duplicated(rev(t)))]
incurred_latest
# Fill the NAs prior to the first non-NA with 0
# (i.e. assume no incurred until claim notified)
firstNonNA <- min(which(!is.na(incurred_latest)))
firstNonNA
i <- 40
i_rescaled <- 10
# convert to discrete time scale (t in terms of absolute time)
t <- ceiling(incurred_history[[i]][[j]]$txn_time / aggregate_level)
t
incurred_right <- incurred_history[[i]][[j]]$incurred_right
# Now get the latest incurred estimate in a period
incurred_latest <- rep(NA, i_rescaled + side - 1)
# Fill the incurred estimates
incurred_latest[unique(t)] <- incurred_right[!rev(duplicated(rev(t)))]
incurred_latest
unique(t)
i_rescaled <- ceiling(i / 4)
i_rescaled
load_all()
View(incurred_inflated)
x <- incurred_inflated
y <- output_incurred(test_inflated, incremental = TRUE)
load_all()
y <- output_incurred(test_inflated, incremental = TRUE)
sum(x == y)
sum(x != y)
View(x)
View(y)
View(test_inflated)
test_inflated[[1]][[1]]
set.seed(20201006)
test_claims <- SynthETIC::test_claims_object
major <- claim_maRev_no(test_claims)
major <- claim_maRev_time(test_claims, major)
major <- claim_maRev_size(major)
# minor revisions
minor <- claim_miRev_no(test_claims)
minor <- claim_miRev_time(test_claims, minor)
minor <- claim_miRev_size(test_claims, major, minor)
test <- claim_history(test_claims, major, minor)
test_inflated <- claim_history(test_claims, major, minor, inflated = TRUE,
base_inflation_vector = rep((1 + 0.02)^(1/4) - 1, times = 80))
test_inflated[[1]][[1]]$miRev
test_inflated[[1]][[1]]$incurred_right
major[[1]][[1]]
major[[1]][[2]]
major[[1]][[29]]
# Incurred triangles
incurred_output <- function(
incurred_history,
aggregate_level = 1,
incremental = TRUE) {
frequency_vector <- lengths(incurred_history)
I <- length(frequency_vector)
incurred_cumulative <- array(0, c(I, I))
adjustment <- 0 # track the number of corrections required for keeping all the
# transaction times within the bound
for (i in 1:I) {
for (j in 1:frequency_vector[i]) {
# convert to discrete time scale (t in terms of absolute time)
t <- ceiling(incurred_history[[i]][[j]]$txn_t)
incurred_right <- incurred_history[[i]][[j]]$incurred_right
# Firstly need to treat the out-of-bound transaction times
if (any(t - i + 1 > I)) {
t[which(t - i + 1 > I)] <- i + I - 1
adjustment <- adjustment + 1
}
# Now get the latest incurred estimate in a period
incurred_latest <- rep(NA, i + I - 1)
# Fill the incurred estimates
incurred_latest[unique(t)] <- incurred_right[!rev(duplicated(rev(t)))]
# Fill the NAs prior to the first non-NA with 0
# (i.e. assume no incurred until claim notified)
firstNonNA <- min(which(!is.na(incurred_latest)))
if (firstNonNA > i) {
incurred_latest[i:(firstNonNA - 1)] <- 0
}
# Fill the rest of NAs with the prior non-NAs
incurred_latest <- zoo::na.locf(incurred_latest, na.rm = TRUE)
for (k in 1:I) {
incurred_cumulative[i, k] <- incurred_cumulative[i, k] + incurred_latest[k]
}
}
}
no_txn <- lengths(lapply(unlist(incurred_history, recursive = F), `[[`, "txn_time"))
total_no_txn <- sum(no_txn)
if (adjustment / total_no_txn > 0.03) {
warning("More than 3% of the transactions were outside the bound.")
}
if (aggregate_level != 1) {
# if aggregate at a higher level (e.g. aggregate_level = 4 for yearly triangles)
new_side_length <- I / aggregate_level
incurred_cumulative_orig <- incurred_cumulative
incurred_cumulative <- array(0, c(new_side_length, new_side_length))
for (i in 1:new_side_length) {
side_occurrence <- (aggregate_level * (i-1) + 1): (aggregate_level * i)
for (j in 1:new_side_length) {
side_development <- aggregate_level * j
incurred_cumulative[i, j] <- sum(
incurred_cumulative_orig[side_occurrence, side_development])
}
}
}
if (incremental == TRUE) {
incurred_incremental <- incurred_cumulative
for (i in 1:dim(incurred_cumulative)[1]) {
incurred_incremental[i, 1] <- incurred_cumulative[i, 1]
for (j in 2:dim(incurred_cumulative)[2]) {
incurred_incremental[i, j] <- incurred_cumulative[i, j] - incurred_cumulative[i, j - 1]
}
}
incurred_incremental
} else {
incurred_cumulative
Expand Down Expand Up @@ -510,3 +341,172 @@ major <- claim_maRev_no(test_claims)
major <- claim_maRev_time(test_claims, major)
major <- claim_maRev_size(major)
major[[1]][[29]]
## generated with default assumptions
library(SynthETIC)
set.seed(20200131)
n_vector <- claim_frequency(I = 40, E = 12000, freq = 0.1)
occurrence_times <- claim_occurrence(n_vector)
claim_sizes <- claim_size(n_vector)
notidel <- claim_notification(n_vector, claim_sizes)
setldel <- claim_closure(n_vector, claim_sizes)
no_payments <- claim_payment_no(n_vector, claim_sizes)
payment_sizes <- claim_payment_size(n_vector, claim_sizes, no_payments)
payment_delays <- claim_payment_delay(n_vector, claim_sizes, no_payments, setldel)
payment_times <- claim_payment_time(n_vector, occurrence_times, notidel, payment_delays)
base_inflation_vector <- rep((1.02)^(1/4) - 1, times = 80)
payment_inflated <- claim_payment_inflation(
n_vector, payment_sizes, payment_times, occurrence_times,
claim_sizes, base_inflation_vector)
# generate datasets
# claim level data
claim_dataset <- generate_claim_dataset(
frequency_vector = n_vector,
occurrence_list = occurrence_times,
claim_size_list = claim_sizes,
notification_list = notidel,
settlement_list = setldel,
no_payments_list = no_payments
)
# payment level data
test_claims <- claims(
n_vector, occurrence_times, claim_sizes, notidel, setldel, no_payments,
payment_sizes, payment_delays, payment_times, payment_inflated)
transaction_dataset <- generate_transaction_dataset(
test_claims,
adjust = FALSE # to keep the original simulated payment times
)
write.csv(claim_dataset, "~/Downloads/claim_dataset_20210825.csv")
write.csv(transaction_dataset, "~/Downloads/payment_dataset_inflated_20210825.csv")
## With SPLICE loaded (locally)
major <- claim_maRev_no(test_claims)
library(devtools)
load_all()
## With SPLICE loaded (locally)
major <- claim_maRev_no(test_claims)
major <- claim_maRev_time(test_claims, major)
major <- claim_maRev_size(major)
# minor revisions
minor <- claim_miRev_no(test_claims)
minor <- claim_miRev_time(test_claims, minor)
minor <- claim_miRev_size(test_claims, major, minor)
test <- claim_history(test_claims, major, minor)
test_inflated <- claim_history(test_claims, major, minor, inflated = TRUE,
base_inflation_vector = rep((1 + 0.02)^(1/4) - 1, times = 80))
test_inflated <- claim_history(test_claims, major, minor,
base_inflation_vector = rep((1 + 0.02)^(1/4) - 1, times = 80))
test_incurred_dataset <- generate_incurred_dataset(test_claims, test)
test_incurred_dataset_inflated <- generate_incurred_dataset(test_claims, test_inflated)
View(test_incurred_dataset)
test[[1]][[1]]
## generated with default assumptions
library(SynthETIC)
set.seed(20200131)
n_vector <- claim_frequency(I = 40, E = 12000, freq = 0.1)
occurrence_times <- claim_occurrence(n_vector)
claim_sizes <- claim_size(n_vector)
notidel <- claim_notification(n_vector, claim_sizes)
setldel <- claim_closure(n_vector, claim_sizes)
no_payments <- claim_payment_no(n_vector, claim_sizes)
payment_sizes <- claim_payment_size(n_vector, claim_sizes, no_payments)
payment_delays <- claim_payment_delay(n_vector, claim_sizes, no_payments, setldel)
payment_times <- claim_payment_time(n_vector, occurrence_times, notidel, payment_delays)
base_inflation_vector <- rep((1.02)^(1/4) - 1, times = 80)
payment_inflated <- claim_payment_inflation(
n_vector, payment_sizes, payment_times, occurrence_times,
claim_sizes, base_inflation_vector)
# generate datasets
# claim level data
claim_dataset <- generate_claim_dataset(
frequency_vector = n_vector,
occurrence_list = occurrence_times,
claim_size_list = claim_sizes,
notification_list = notidel,
settlement_list = setldel,
no_payments_list = no_payments
)
# payment level data
test_claims <- claims(
n_vector, occurrence_times, claim_sizes, notidel, setldel, no_payments,
payment_sizes, payment_delays, payment_times, payment_inflated)
transaction_dataset <- generate_transaction_dataset(
test_claims,
adjust = FALSE # to keep the original simulated payment times
)
# write.csv(claim_dataset, "~/Downloads/claim_dataset_20210825.csv")
# write.csv(transaction_dataset, "~/Downloads/payment_dataset_inflated_20210825.csv")
# triangles
# Constant dollar value INCREMENTAL triangle
output_noinflation <- claim_output(n_vector, payment_times, payment_sizes,
incremental = TRUE)
# Inflated INCREMENTAL triangle
output_actual <- claim_output(n_vector, payment_times, payment_inflated,
incremental = TRUE)
# write.csv(output_noinflation, "~/Downloads/triangle_noInf_20210825.csv")
# write.csv(output_actual, "~/Downloads/triangle_inflated_20210825.csv")
## With SPLICE loaded (locally)
major <- claim_maRev_no(test_claims)
major <- claim_maRev_time(test_claims, major)
major <- claim_maRev_size(major)
# minor revisions
minor <- claim_miRev_no(test_claims)
minor <- claim_miRev_time(test_claims, minor)
minor <- claim_miRev_size(test_claims, major, minor)
test <- claim_history(test_claims, major, minor)
test_inflated <- claim_history(test_claims, major, minor,
base_inflation_vector = rep((1 + 0.02)^(1/4) - 1, times = 80))
test_incurred_dataset <- generate_incurred_dataset(test_claims, test)
test_incurred_dataset_inflated <- generate_incurred_dataset(test_claims, test_inflated)
major[[1]][[1]]
View(test_incurred_dataset_inflated)
View(test_incurred_dataset)
View(transaction_dataset)
write.csv(test_incurred_dataset, "~/Downloads/incurred_dataset_noInf_20210825.csv")
write.csv(test_incurred_dataset_inflated, "~/Downloads/incurred_dataset_inflated_20210825.csv")
write.csv(incurred_noInf, "~/Downloads/incurred_triangle_noInf_20210825.csv")
# incurred triangles
# Constant dollar value INCREMENTAL triangle
incurred_noInf <- output_incurred(test, incremental = TRUE)
# Inflated INCREMENTAL triangle
incurred_inflated <- output_incurred(test_inflated, incremental = TRUE)
write.csv(incurred_noInf, "~/Downloads/incurred_triangle_noInf_20210825.csv")
write.csv(incurred_inflated, "~/Downloads/incurred_triangle_inflated_20210825.csv")
x <- claim_dataset %>%
select(claim_no, occurrence_time) %>%
merge(test_incurred_dataset, by = "claim_no")
library(dplyr)
i
x <- claim_dataset %>%
select(claim_no, occurrence_time) %>%
merge(test_incurred_dataset, by = "claim_no")
View(x)
test_incurred_dataset <- generate_incurred_dataset(test_claims, test)
colnames(test_incurred_dataset)
occurrence <- data.frame(
claim_no = claim_dataset$claim_no,
claim_size = claim_dataset$claim_size,
txn_time = claim_dataset$occurrence_time,
txn_delay = -claim_dataset$notidel,
txn_type = "O",
incurred = 0,
OCL = 0,
cumpaid = 0,
multiplier = NA)
View(occurrence)
x <- rbind(test_incurred_dataset, occurrence) %>%
arrange(claim_no, txn_time)
View(x)
nrow(x)
nrow(test_incurred_dataset) + nrow(claim_dataset)
1.7948353 - 0.6238351
2.5837741 - 0.1206679
y <- rbind(test_incurred_dataset_inflated, occurrence) %>%
arrange(claim_no, txn_time)
write.csv(x, "~/Downloads/incurred_dataset_noInf_20210825.csv")
write.csv(y, "~/Downloads/incurred_dataset_inflated_20210825.csv")
# incurred triangles
# Constant dollar value INCREMENTAL triangle
incurred_noInf <- output_incurred(test, incremental = TRUE)
# Inflated INCREMENTAL triangle
incurred_inflated <- output_incurred(test_inflated, incremental = TRUE)
write.csv(incurred_noInf, "~/Downloads/incurred_triangle_noInf_20210825.csv")
write.csv(incurred_inflated, "~/Downloads/incurred_triangle_inflated_20210825.csv")
12 changes: 6 additions & 6 deletions SPLICE/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Generated by roxygen2: do not edit by hand

export(claim_history)
export(claim_maRev_no)
export(claim_maRev_size)
export(claim_maRev_time)
export(claim_miRev_no)
export(claim_miRev_size)
export(claim_miRev_time)
export(claim_majRev_freq)
export(claim_majRev_size)
export(claim_majRev_time)
export(claim_minRev_freq)
export(claim_minRev_size)
export(claim_minRev_time)
export(dtri)
export(generate_incurred_dataset)
export(output_incurred)
Expand Down
5 changes: 4 additions & 1 deletion SPLICE/R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
#' \item{cumpaid}{double; cumulative claim paid **after** the transaction.}
#' \item{multiplier}{revision multipliers (subject to further constraints
#' documented in \code{\link{claim_history}}), `NA` for
#' transactions that do not involve a revision.}
#' transactions that do not involve a revision. Note that
#' major revision multipliers apply to the incurred losses,
#' while minor revision multipliers apply to the outstanding
#' claim payments.}
#' }
#' @seealso \code{\link{generate_incurred_dataset}}
#' @name test_incurred_dataset
Expand Down
Loading

0 comments on commit 54963a6

Please sign in to comment.