GEV responses

GEV responses#

In this tutorial, we illustrate how to set up a distributional regression model with the generalized extreme value distribution as a response distribution. First, we simulate some data in R:

  • The location parameter (\(\mu\)) is a function of an intercept and a non-linear covariate effect.

  • The scale parameter (\(\sigma\)) is a function of an intercept and a linear effect and uses a log-link.

  • The shape or concentration parameter (\(\xi\)) is a function of an intercept and a linear effect.

After simulating the data, we can configure the model with a single call to the rliesel::liesel() function.

library(rliesel)
Please make sure you are using a virtual or conda environment with Liesel installed, e.g. using `reticulate::use_virtualenv()` or `reticulate::use_condaenv()`. See `vignette("versions", "reticulate")`.

After setting the environment, check if the installed versions of RLiesel and Liesel are compatible with `check_liesel_version()`.
library(VGAM)
Loading required package: stats4

Loading required package: splines
set.seed(13)

n <- 1000

x0 <- runif(n)
x1 <- runif(n)
x2 <- runif(n)

y <- rgev(
  n,
  location = 0 + sin(2 * pi * x0),
  scale = exp(-3 + x1),
  shape = 0.1 + x2
)

plot(y)

model <- liesel(
  response = y,
  distribution = "GeneralizedExtremeValue",
  predictors = list(
    loc = predictor(~ s(x0)),
    scale = predictor(~ x1, inverse_link = "Exp"),
    concentration = predictor(~ x2)
  )
)
Installed Liesel version 0.2.9-dev is compatible, continuing to set up model

Now, we can continue in Python and use the lsl.dist_reg_mcmc() function to set up a sampling algorithm with IWLS kernels for the regression coefficients (\(\boldsymbol{\beta}\)) and a Gibbs kernel for the smoothing parameter (\(\tau^2\)) of the spline.

The support of the GEV distribution changes with the parameter values (compare Wikipedia). To ensure that the initial parameters support the observed data we set \(xi = 0.1\) and disable jittering of the the variance and regression parameters. For the latter, we supply user-defined jitter functions to lsl.dist_reg_mcmc that are essentially the identity function w.r.t. the parameter value.

import liesel.model as lsl
import jax.numpy as jnp

model = r.model

# concentration == 0.0 seems to break the sampler
model.vars["concentration_p0_beta"].value = jnp.array([0.1, 0.0])

builder = lsl.dist_reg_mcmc(model, seed=42, num_chains=4, tau2_jitter_fn=lambda key, val: val, beta_jitter_fn=lambda key, val: val)
builder.set_duration(warmup_duration=1000, posterior_duration=1000)

engine = builder.build()
liesel.goose.engine - INFO - Initializing kernels...
liesel.goose.engine - INFO - Done
engine.sample_all_epochs()
liesel.goose.engine - INFO - Starting epoch: FAST_ADAPTATION, 75 transitions, 25 jitted together
liesel.goose.engine - WARNING - Errors per chain for kernel_00: 2, 3, 7, 9 / 75 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_01: 0, 1, 0, 0 / 75 transitions
liesel.goose.engine - INFO - Finished epoch
liesel.goose.engine - INFO - Starting epoch: SLOW_ADAPTATION, 25 transitions, 25 jitted together
liesel.goose.engine - WARNING - Errors per chain for kernel_00: 2, 1, 1, 1 / 25 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_01: 1, 1, 1, 1 / 25 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_03: 1, 1, 2, 1 / 25 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_04: 1, 2, 0, 0 / 25 transitions
liesel.goose.engine - INFO - Finished epoch
liesel.goose.engine - INFO - Starting epoch: SLOW_ADAPTATION, 50 transitions, 25 jitted together
liesel.goose.engine - WARNING - Errors per chain for kernel_00: 3, 2, 2, 2 / 50 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_01: 2, 2, 1, 2 / 50 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_03: 0, 1, 1, 2 / 50 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_04: 1, 2, 1, 1 / 50 transitions
liesel.goose.engine - INFO - Finished epoch
liesel.goose.engine - INFO - Starting epoch: SLOW_ADAPTATION, 100 transitions, 25 jitted together
liesel.goose.engine - WARNING - Errors per chain for kernel_00: 3, 3, 5, 4 / 100 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_01: 1, 4, 1, 2 / 100 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_03: 2, 1, 2, 1 / 100 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_04: 1, 4, 0, 1 / 100 transitions
liesel.goose.engine - INFO - Finished epoch
liesel.goose.engine - INFO - Starting epoch: SLOW_ADAPTATION, 200 transitions, 25 jitted together
liesel.goose.engine - WARNING - Errors per chain for kernel_00: 5, 2, 4, 3 / 200 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_01: 2, 0, 1, 1 / 200 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_03: 1, 1, 1, 2 / 200 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_04: 0, 1, 0, 1 / 200 transitions
liesel.goose.engine - INFO - Finished epoch
liesel.goose.engine - INFO - Starting epoch: SLOW_ADAPTATION, 500 transitions, 25 jitted together
liesel.goose.engine - WARNING - Errors per chain for kernel_00: 5, 1, 5, 3 / 500 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_01: 1, 1, 2, 1 / 500 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_03: 1, 0, 1, 1 / 500 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_04: 2, 0, 2, 0 / 500 transitions
liesel.goose.engine - INFO - Finished epoch
liesel.goose.engine - INFO - Starting epoch: FAST_ADAPTATION, 50 transitions, 25 jitted together
liesel.goose.engine - WARNING - Errors per chain for kernel_00: 2, 2, 3, 0 / 50 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_01: 1, 0, 1, 1 / 50 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_03: 1, 1, 0, 1 / 50 transitions
liesel.goose.engine - WARNING - Errors per chain for kernel_04: 1, 0, 1, 1 / 50 transitions
liesel.goose.engine - INFO - Finished epoch
liesel.goose.engine - INFO - Finished warmup
liesel.goose.engine - INFO - Starting epoch: POSTERIOR, 1000 transitions, 25 jitted together
liesel.goose.engine - WARNING - Errors per chain for kernel_00: 0, 0, 1, 2 / 1000 transitions
liesel.goose.engine - INFO - Finished epoch

Some tabular summary statistics of the posterior samples:

import liesel.goose as gs

results = engine.get_results()
gs.Summary(results)

Parameter summary:

kernel mean sd q_0.05 q_0.5 q_0.95 sample_size ess_bulk ess_tail rhat
parameter index
concentration_p0_beta (0,) kernel_00 0.097 0.052 0.014 0.095 0.186 4000 376.865 890.752 1.009
(1,) kernel_00 0.975 0.095 0.818 0.976 1.132 4000 178.318 430.090 1.031
loc_np0_beta (0,) kernel_03 0.523 0.213 0.182 0.515 0.880 4000 88.465 227.390 1.034
(1,) kernel_03 -0.161 0.107 -0.341 -0.162 0.012 4000 132.522 161.398 1.018
(2,) kernel_03 -0.506 0.132 -0.726 -0.503 -0.289 4000 93.919 202.170 1.031
(3,) kernel_03 -0.016 0.067 -0.129 -0.017 0.091 4000 102.661 234.676 1.042
(4,) kernel_03 -0.469 0.066 -0.582 -0.467 -0.361 4000 104.817 266.324 1.021
(5,) kernel_03 0.461 0.029 0.411 0.461 0.506 4000 126.258 301.886 1.012
(6,) kernel_03 -5.902 0.032 -5.954 -5.903 -5.849 4000 112.182 125.223 1.028
(7,) kernel_03 0.372 0.060 0.276 0.369 0.472 4000 146.533 263.829 1.007
(8,) kernel_03 -1.785 0.027 -1.829 -1.786 -1.741 4000 98.017 158.082 1.024
loc_np0_tau2 () kernel_02 6.050 4.342 2.356 4.932 13.059 4000 3976.814 3732.341 1.001
loc_p0_beta (0,) kernel_04 -0.027 0.003 -0.031 -0.027 -0.022 4000 29.925 305.644 1.113
scale_p0_beta (0,) kernel_01 -3.099 0.061 -3.196 -3.100 -2.995 4000 50.342 290.386 1.099
(1,) kernel_01 1.205 0.080 1.074 1.204 1.336 4000 165.052 503.756 1.045

Error summary:

count relative
kernel error_code error_msg phase
kernel_00 90 nan acceptance prob warmup 85 0.021
posterior 3 0.001
kernel_01 90 nan acceptance prob warmup 32 0.008
posterior 0 0.000
kernel_03 90 nan acceptance prob warmup 26 0.007
posterior 0 0.000
kernel_04 90 nan acceptance prob warmup 23 0.006
posterior 0 0.000

And the corresponding trace plots:

fig = gs.plot_trace(results, "loc_p0_beta")

fig = gs.plot_trace(results, "loc_np0_tau2")

fig = gs.plot_trace(results, "loc_np0_beta")

fig = gs.plot_trace(results, "scale_p0_beta")

fig = gs.plot_trace(results, "concentration_p0_beta")

We need to reset the index of the summary data frame before we can transfer it to R.

summary = gs.Summary(results).to_dataframe().reset_index()

After transferring the summary data frame to R, we can process it with packages like dplyr and ggplot2. Here is a visualization of the estimated spline vs. the true function:

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(ggplot2)
library(reticulate)

summary <- py$summary

beta <- summary %>%
  filter(variable == "loc_np0_beta") %>%
  group_by(var_index) %>%
  summarize(mean = mean(mean)) %>%
  ungroup()

beta <- beta$mean
X <- py_to_r(model$vars["loc_np0_X"]$value)
estimate <- X %*% beta

true <- sin(2 * pi * x0)

ggplot(data.frame(x0 = x0, estimate = estimate, true = true)) +
  geom_line(aes(x0, estimate), color = palette()[2]) +
  geom_line(aes(x0, true), color = palette()[4]) +
  ggtitle("Estimated spline (red) vs. true function (blue)") +
  ylab("f") +
  theme_minimal()