Skip to contents

Wüthrich and Merz's Combined Actuarial Neural Network: the offset (a GLM's linear predictor, with offset(...) terms or offset) plus a feed-forward network of the formula's design columns, eta = offset + NN(x). The network's output layer starts at zero, so training starts exactly at the GLM and learns only what it misses. Training minimizes the family's weighted deviance by Adam on mini-batches, on the CPU in double precision, and is reproducible from seed. Columns are standardized internally.

Usage

cann_fit(
  formula,
  data,
  family = "poisson",
  link = NULL,
  hidden = c(16, 8),
  epochs = 200,
  batch_size = 64,
  learning_rate = 0.001,
  seed = 0,
  validation_share = NULL,
  patience = 10,
  offset = NULL,
  weights = NULL,
  power = NULL
)

Arguments

formula

A model formula; offset(...) terms are honoured.

data

A data frame.

family

"poisson", "gamma", "tweedie" (needs power) or "gaussian".

NULL for log (identity for the Gaussian), the only links the networks support.

hidden

Widths of the hidden ReLU layers.

epochs, batch_size, learning_rate

Adam's settings.

seed

Seeds the initial weights and the mini-batch order.

validation_share

NULL, or a share of rows held out for early stopping: the mean deviance on them is scored after each epoch and the best epoch kept.

patience

With early stopping, epochs without improvement before stopping.

offset

Optional offset added to any offset() terms.

weights

Optional prior weights.

power

Tweedie power in (1, 2).

Value

A cann_model with properties fitted, dispersion, best_epoch (NA without early stopping) and validation_history. Use stats::predict(), predict_distribution() and network_correction(). A fitted network has no artifact yet, so save_model() does not take it.

Examples

set.seed(1)
d <- data.frame(x = seq(-2, 2, length.out = 200))
d$y <- rpois(200, exp(1 - d$x^2))
m <- cann_fit(y ~ x, d, offset = rep(log(mean(d$y)), 200), epochs = 20,
              learning_rate = 0.01)
head(predict(m, d))
#> [1] 0.06589099 0.06858894 0.07140864 0.07459875 0.07793137 0.08141288