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

Likelihood math for section 13 of ceattle.cpp: the composition row density and linkage prior families that several terms share, and the composition plus-group fold. More...

Go to the source code of this file.

Functions

template<class Type>
void fold_comp_tails (vector< Type > &obs, vector< Type > &hat, int &n_comp, int nbins_blk, int nblk, int yng, int old)
 Composition tail accumulation (AFSC ac_yng / ac_old).
template<class Type>
void comp_row_nll (int ll_type, int osa_mode, vector< Type > obs_prop, vector< Type > hat_prop, Type offset, Type n_samp, Type weight, Type dm_theta, vector< Type > &obsvec, data_indicator< vector< Type >, Type > &keep, int osa_start, const char *type_name, Type &nll, Type &unweighted_nll)
 Negative log-likelihood of one composition row: an age or length composition, or one length bin of conditional age-at-length (CAAL).
template<class Type>
Type linkage_log_prior (int family, Type value, bool value_is_log, Type p1, Type p2, Type bias_adjust_proc)
 Log density of a linkage prior on the natural scale.

Detailed Description

Likelihood math for section 13 of ceattle.cpp: the composition row density and linkage prior families that several terms share, and the composition plus-group fold.

The loops over observations, and the SIMULATE draws beside each density, stay in ceattle.cpp.

Definition in file likelihood.hpp.

Function Documentation

◆ comp_row_nll()

template<class Type>
void comp_row_nll ( int ll_type,
int osa_mode,
vector< Type > obs_prop,
vector< Type > hat_prop,
Type offset,
Type n_samp,
Type weight,
Type dm_theta,
vector< Type > & obsvec,
data_indicator< vector< Type >, Type > & keep,
int osa_start,
const char * type_name,
Type & nll,
Type & unweighted_nll )

Negative log-likelihood of one composition row: an age or length composition, or one length bin of conditional age-at-length (CAAL).

Likelihood types:

  • -1 AFSC/AMAK multinomial pseudo-likelihood, log L = sum N (o + c) log((p + c) / (o + c)). Not a density, so OSA residualizes it as a multinomial.
  • 0 Multinomial, via the conditional-binomial form in comp_osa.hpp.
  • 1 Dirichlet-multinomial with concentration N * p * theta (Thorson et al. 2017).

The data weight multiplies the multinomial log-likelihood (types -1 and 0); the Dirichlet-multinomial carries its weight as theta instead. unweighted_nll gets the row without either, in ordinary fitting only.

Parameters
ll_typeLikelihood type, -1, 0 or 1.
osa_mode0 = ordinary fitting; 1 or 2 = OSA build (2 adds the CDF terms).
obs_prop,hat_propObserved and expected proportions, after any tail fold.
offsetConstant c added to both, to keep log(0) out (comp_offset).
n_sampInput sample size N.
weightData weight on the multinomial log-likelihood.
dm_thetaDirichlet-multinomial theta, exp() of the log weight.
obsvec,keep,osa_startOSA observation vector, its keep indicator, and this row's first position in them (< 0 = row not in obsvec).
type_nameData column named in the error for an unknown type.
nll,unweighted_nllCells of jnll_comp and unweighted_jnll_comp to add to.

Definition at line 80 of file likelihood.hpp.

References ddirmultinom(), ddirmultinom_osa(), and dmultinom_osa().

◆ fold_comp_tails()

template<class Type>
void fold_comp_tails ( vector< Type > & obs,
vector< Type > & hat,
int & n_comp,
int nbins_blk,
int nblk,
int yng,
int old )

Composition tail accumulation (AFSC ac_yng / ac_old).

Bins below yng are added into yng and bins above old into old, and only bins yng..old are kept. Joint-sex rows are folded per sex block, so a fold never crosses from females into males. yng = 1 with old >= nbins is a no-op.

Parameters
obs,hatObserved and expected proportions; replaced by the folded vectors.
n_compNumber of bins in the row; replaced by the folded count.
nbins_blkBins per sex block (ages or lengths).
nblkSex blocks in the row: 2 for joint-sex comps, else 1.
yng,oldFirst and last bin kept, 1-based ordinals on the fleet's comp dimension.

Definition at line 27 of file likelihood.hpp.

◆ linkage_log_prior()

template<class Type>
Type linkage_log_prior ( int family,
Type value,
bool value_is_log,
Type p1,
Type p2,
Type bias_adjust_proc )

Log density of a linkage prior on the natural scale.

Families: 1 = normal(p1, p2); 2 = lognormal, log x ~ normal(p1 - b * p2^2 / 2, p2), so exp(p1) is the mean when bias_adjust_proc b = 1 and the median when 0; 3 = gamma(shape p1, rate p2); 4 = beta(p1, p2). Any other code returns 0.

Parameters
familyPrior family code, from linkage_spec(priors = ).
valueThe parameter, on the scale it is stored.
value_is_logTrue if value is a log, so the prior reads exp(value).
p1,p2The family's two parameters, as listed above.
bias_adjust_proc1 to centre the lognormal on its mean, 0 on its median.
Returns
The log prior density (0 for an unknown family).

Definition at line 156 of file likelihood.hpp.

References square().