Rceattle C++ template
The CEATTLE likelihood: src/TMB/ceattle.cpp and its headers
Loading...
Searching...
No Matches
helper_functions.hpp File Reference

Small differentiable (AD-safe) utilities used across the CEATTLE template: numeric guards and transforms, the Dirichlet-multinomial density, and the separable year x age x cohort GMRF precision matrix (construct_Q). More...

Go to the source code of this file.

Functions

template<class Type>
Type imax (const vector< Type > &x)
template<class Type>
Type posfun (Type x, Type eps, Type &penalty)
template<class Type>
Type ddirmultinom (vector< Type > obs, vector< Type > alpha, int do_log)
template<class Type>
Type rho_trans (Type x)
template<class Type>
bool isNA (Type x)
template<class Type>
bool isFinite (Type x)
template<class Type>
Type square (Type x)
template<class Type>
Type concentrated_lognormal_sd (Type mean_sq_resid, Type bias_adjust)
 Concentrated (analytical) sd of a lognormal observation likelihood.
template<class Type>
vector< Type > first_difference (const vector< Type > &x)
template<class Type>
Type max2 (Type x, Type y)
template<class Type>
Type log_mean_exp (const vector< Type > &x)
 log(mean(exp(x))) over a non-empty vector, computed so it cannot overflow.
template<class Type>
Eigen::SparseMatrix< Type > construct_Q (int n_years, int n_ages, matrix< Type > ay_Index, Type rho_y, Type rho_a, Type rho_c, Type log_sigma2, int Var_Param)
 Construct a sparse GMRF precision matrix separable along year, age, and cohort.

Detailed Description

Small differentiable (AD-safe) utilities used across the CEATTLE template: numeric guards and transforms, the Dirichlet-multinomial density, and the separable year x age x cohort GMRF precision matrix (construct_Q).

Definition in file helper_functions.hpp.

Function Documentation

◆ concentrated_lognormal_sd()

template<class Type>
Type concentrated_lognormal_sd ( Type mean_sq_resid,
Type bias_adjust )

Concentrated (analytical) sd of a lognormal observation likelihood.

Catch and index observations are fit as log(obs) ~ N(log(pred) - b * sigma^2 / 2, sigma), with b the bias-adjustment flag bias_adjust_obs (1 = mean-unbiased predictions, Rceattle's default; 0 = median-unbiased). Minimising that density over sigma with S = mean squared log residual gives

sigma^2 = 2 * S / (sqrt(1 + b^2 * S) + 1)

the Walters and Ludwig (1994) estimator sigma = sqrt(S) at b = 0, shrunk as the bias term takes up part of the residual. It is the rationalised form of 2 (sqrt(1 + b^2 S) - 1) / b^2, which needs no branch at b = 0 and stays accurate for small b, where that expression cancels to zero.

Parameters
mean_sq_residS, the mean of squared log(obs) - log(pred).
bias_adjustb, the bias-adjustment flag applied by the density.
Returns
The sd that minimises the lognormal negative log-likelihood.

Definition at line 90 of file helper_functions.hpp.

References square().

◆ construct_Q()

template<class Type>
Eigen::SparseMatrix< Type > construct_Q ( int n_years,
int n_ages,
matrix< Type > ay_Index,
Type rho_y,
Type rho_a,
Type rho_c,
Type log_sigma2,
int Var_Param )

Construct a sparse GMRF precision matrix separable along year, age, and cohort.

Builds Q = (I - B^T) * Omega * (I - B), where B carries the partial correlation to each neighbouring node and Omega is the diagonal precision (1 / variance). Var_Param selects the conditional (0) or marginal (1) variance parameterization of the GMRF.

The sole caller reads these names the other way round: ceattle.cpp's 3D-AR1 selectivity passes its (bin, year, cohort) correlations in and fills ay_Index as (bin, year), so the two inversions cancel. Read the note at that call before renaming anything here.

Parameters
n_yearsNumber of years.
n_agesNumber of ages.
ay_Index[total_n x 2] age/year index locating each node.
rho_yPartial correlation to the node one back in ay_Index column 0.
rho_aPartial correlation to the node one back in column 1.
rho_cPartial correlation to the node one back in both – the diagonal, which is the cohort when the columns are filled (age, year).
log_sigma2Log variance parameter governing the GMRF.
Var_Param0 = conditional variance, 1 = marginal variance.
Returns
Sparse precision matrix [total_n x total_n], total_n = n_years * n_ages.

Definition at line 175 of file helper_functions.hpp.

◆ ddirmultinom()

template<class Type>
Type ddirmultinom ( vector< Type > obs,
vector< Type > alpha,
int do_log )

Definition at line 37 of file helper_functions.hpp.

Referenced by comp_row_nll(), and ddirmultinom_osa().

◆ first_difference()

template<class Type>
vector< Type > first_difference ( const vector< Type > & x)

Definition at line 98 of file helper_functions.hpp.

◆ imax()

template<class Type>
Type imax ( const vector< Type > & x)

Definition at line 11 of file helper_functions.hpp.

Referenced by calculate_msvpa_suitability().

◆ isFinite()

template<class Type>
bool isFinite ( Type x)

Definition at line 62 of file helper_functions.hpp.

◆ isNA()

template<class Type>
bool isNA ( Type x)

Definition at line 56 of file helper_functions.hpp.

Referenced by calculate_selectivity().

◆ log_mean_exp()

template<class Type>
Type log_mean_exp ( const vector< Type > & x)

log(mean(exp(x))) over a non-empty vector, computed so it cannot overflow.

The non-parametric selectivity forms rescale their log-scale coefficients against log(mean(exp(coefficients))) once a year, holding a curve whose level is otherwise unidentified at mean selectivity 1. Summed directly, exp() overflows to infinity once a coefficient passes about 709, the largest argument it takes in double precision, and the objective is NaN thereafter. Subtracting the largest coefficient first holds every exponent at or below 0; the shift cancels out of both the result and its derivative.

Parameters
xnon-empty vector of log-scale values.
Returns
log of the mean of exp(x).

Definition at line 140 of file helper_functions.hpp.

References max2().

Referenced by calculate_selectivity().

◆ max2()

template<class Type>
Type max2 ( Type x,
Type y )

◆ posfun()

template<class Type>
Type posfun ( Type x,
Type eps,
Type & penalty )

Definition at line 26 of file helper_functions.hpp.

◆ rho_trans()

template<class Type>
Type rho_trans ( Type x)

Definition at line 50 of file helper_functions.hpp.

◆ square()

template<class Type>
Type square ( Type x)