/
obs_lpmf.stan
47 lines (46 loc) · 1.52 KB
/
obs_lpmf.stan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Observation likelihood
*
* Computes the log probability mass function (LPMF) for observations. This
* function is overloaded to handle both vectorized and non-vectorized inputs.
*
* @param obs Observations, can be a single integer (non-vectorized) or an
* array of integers (vectorized).
*
* @param log_exp_obs Logarithm of expected observations, can be a single real
* (non-vectorized) or a vector (vectorized).
*
* @param phi Dispersion parameter for the negative binomial model, ignored for
* the Poisson model. Should be an array with the first element used.
*
* @param model_obs Indicator of the model used (0 for Poisson, 1 for negative
* binomial).
*
* @return The log probability mass of the observations under the specified
* model.
*
* @note The function selects between the negative binomial model and the
* Poisson model based on the `model_obs` flag. It uses
* `neg_binomial_2_log_lpmf` for the negative binomial model and
* `poisson_log_lpmf` for the Poisson model.
*/
real obs_lpmf(array[] int obs, vector log_exp_obs, array[] real phi,
int model_obs) {
real tar = 0;
if (model_obs) {
tar = neg_binomial_2_log_lpmf(obs | log_exp_obs, phi[1]);
}else{
tar = poisson_log_lpmf(obs | log_exp_obs);
}
return(tar);
}
real obs_lpmf(int obs, real log_exp_obs, array[] real phi,
int model_obs) {
real tar = 0;
if (model_obs) {
tar = neg_binomial_2_log_lpmf(obs | log_exp_obs, phi[1]);
}else{
tar = poisson_log_lpmf(obs | log_exp_obs);
}
return(tar);
}