Learn R Programming

reservr

The goal of reservr is to provide a flexible interface for specifying distributions and fitting them to (randomly) truncated and possibly interval-censored data. It provides custom fitting algorithms to fit distributions to i.i.d. samples as well as dynnamic TensorFlow integration to allow training neural networks with arbitrary output distributions. The latter can be used to include explanatory variables in the distributional fits. Reservr also provides some tools relevant for working with its core functionality in an actuarial setting, namely the functions prob_report() and truncate_claims(), both of which make assumptions on the type of random truncation applied to the data.

Please refer to the vignettes distributions.Rmd and tensorflow.Rmd for detailed introductions.

Installation

reservr is not yet on CRAN. You can install the latest development version of reservr via

devtools::install_github("AshesITR/reservr")

You can install the released version of reservr from CRAN with:

install.packages("reservr")

If you want to use all of reservrs features, make sure to also install tensorflow.

Example

This is a basic example which shows how to fit a normal distribution to randomly truncated and censored data.

library(reservr)
set.seed(123)
mu <- 0
sigma <- 1
N <- 1000
p_cens <- 0.8

x <- rnorm(N, mean = mu, sd = sigma)
is_censored <- rbinom(N, size = 1L, prob = p_cens) == 1L
x_lower <- x
x_lower[is_censored] <- x[is_censored] - runif(sum(is_censored), min = 0, max = 0.5)
x_upper <- x
x_upper[is_censored] <- x[is_censored] + runif(sum(is_censored), min = 0, max = 0.5)

t_lower <- runif(N, min = -2, max = 0)
t_upper <- runif(N, min = 0, max = 2)

is_observed <- t_lower <= x & x <= t_upper

obs <- trunc_obs(
  xmin = pmax(x_lower, t_lower)[is_observed],
  xmax = pmin(x_upper, t_upper)[is_observed],
  tmin = t_lower[is_observed],
  tmax = t_upper[is_observed]
)

# Summary of the simulation
cat(sprintf(
  "simulated samples: %d\nobserved samples: %d\ncensored samples: %d\n", 
  N, nrow(obs), sum(is.na(obs$x))
))

# Define outcome distribution and perform fit to truncated and (partially) censored sample
dist <- dist_normal()
the_fit <- fit(dist, obs)

# Visualize resulting parameters and show a kernel density estimate of the samples.
# We replace interval-censored samples with their midpoint for the kernel density estimate.
plot_distributions(
  true = dist,
  fitted = dist, 
  empirical = dist_empirical(0.5 * (obs$xmin + obs$xmax)), 
  .x = seq(-5, 5, length.out = 201), 
  plots = "density", 
  with_params = list(
    true = list(mean = mu, sd = sigma), 
    fitted = the_fit$params
  )
)

Code of Conduct

Please note that the reservr project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('reservr')

Monthly Downloads

584

Version

0.0.2

License

GPL

Issues

Pull Requests

Stars

Forks

Maintainer

Alexander Rosenstock

Last Published

October 18th, 2023

Functions in reservr (0.0.2)

dist_erlangmix

Erlang Mixture distribution
dist_lognormal

Log Normal distribution
dist_dirac

Dirac (degenerate point) Distribution
dist_exponential

Exponential distribution
dist_empirical

Empirical distribution
dist_discrete

Discrete Distribution
dist_gamma

Gamma distribution
dist_genpareto

Generalized Pareto Distribution
dist_poisson

Poisson Distribution
dist_pareto

Pareto Distribution
fit_blended

Fit a Blended mixture using an ECME-Algorithm
dist_mixture

Mixture distribution
fit.reservr_keras_model

Fit a neural network based distribution model to data
dist_uniform

Uniform distribution
dist_blended

Blended distribution
dist_normal

Normal distribution
is.Distribution

Test if object is a Distribution
fit_erlang_mixture

Fit an Erlang mixture using an ECME-Algorithm
dist_weibull

Weibull Distribution
dist_negbinomial

Negative binomial Distribution
dist_translate

Tranlsated distribution
fit_dist

Fit a general distribution to observations
k_matrix

Cast to a TensorFlow matrix
reexports

Objects exported from other packages
interval-operations

Convex union and intersection of intervals
fit_mixture

Fit a generic mixture using an ECME-Algorithm
prob_report

Determine probability of reporting under a Poisson arrival Process
quantile.Distribution

Quantiles of Distributions
interval

Intervals
plot_distributions

Plot several distributions
softmax

Soft-Max function
predict.reservr_keras_model

Predict individual distribution parameters
tf_compile_model

Compile a Keras model for truncated data under dist
integrate_gk

Adaptive Gauss-Kronrod Quadrature for multiple limits
flatten_params

Flatten / Inflate parameter lists / vectors
dist_trunc

Truncated distribution
weighted_tabulate

Compute weighted tabulations
tf_initialise_model

Initialise model weights to a global parameter fit
fit_dist_start.MixtureDistribution

Find starting values for distribution parameters
truncate_claims

Truncate claims data subject to reporting delay
trunc_obs

Define a set of truncated observations
weighted_moments

Compute weighted moments
weighted_quantile

Compute weighted quantiles
dist_bdegp

Construct a BDEGP-Family
callback_debug_dist_gradients

Callback to monitor likelihood gradient components
Distribution

Base class for Distributions
dist_binomial

Binomial Distribution
Pareto

The Pareto Distribution
GenPareto

The Generalized Pareto Distribution (GPD)
dist_beta

Beta Distribution
as_params

Convert TensorFlow tensors to distribution parameters recursively
blended_transition

Transition functions for blended distributions
callback_adaptive_lr

Keras Callback for adaptive learning rate with weight restoration