|
Rceattle C++ template
The CEATTLE likelihood: src/TMB/ceattle.cpp and its headers
|
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. | |
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.
| 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.
| mean_sq_resid | S, the mean of squared log(obs) - log(pred). |
| bias_adjust | b, the bias-adjustment flag applied by the density. |
Definition at line 90 of file helper_functions.hpp.
References square().
| 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.
| n_years | Number of years. |
| n_ages | Number of ages. |
| ay_Index | [total_n x 2] age/year index locating each node. |
| rho_y | Partial correlation to the node one back in ay_Index column 0. |
| rho_a | Partial correlation to the node one back in column 1. |
| rho_c | Partial correlation to the node one back in both – the diagonal, which is the cohort when the columns are filled (age, year). |
| log_sigma2 | Log variance parameter governing the GMRF. |
| Var_Param | 0 = conditional variance, 1 = marginal variance. |
Definition at line 175 of file helper_functions.hpp.
| 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().
| vector< Type > first_difference | ( | const vector< Type > & | x | ) |
Definition at line 98 of file helper_functions.hpp.
| Type imax | ( | const vector< Type > & | x | ) |
Definition at line 11 of file helper_functions.hpp.
Referenced by calculate_msvpa_suitability().
| bool isFinite | ( | Type | x | ) |
Definition at line 62 of file helper_functions.hpp.
| bool isNA | ( | Type | x | ) |
Definition at line 56 of file helper_functions.hpp.
Referenced by calculate_selectivity().
| 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.
| x | non-empty vector of log-scale values. |
Definition at line 140 of file helper_functions.hpp.
References max2().
Referenced by calculate_selectivity().
| Type max2 | ( | Type | x, |
| Type | y ) |
Definition at line 120 of file helper_functions.hpp.
Referenced by calculate_msvpa_suitability(), calculate_selectivity(), log_mean_exp(), and normalize_and_project_selectivity().
| Type posfun | ( | Type | x, |
| Type | eps, | ||
| Type & | penalty ) |
Definition at line 26 of file helper_functions.hpp.
| Type rho_trans | ( | Type | x | ) |
Definition at line 50 of file helper_functions.hpp.
| Type square | ( | Type | x | ) |
Definition at line 67 of file helper_functions.hpp.
Referenced by calculate_selectivity(), concentrated_lognormal_sd(), and linkage_log_prior().