EpiNow2 Stan Functions
Loading...
Searching...
No Matches
params.stan
Go to the documentation of this file.
1/**
2 * Parameter Handlers
3 *
4 * This group of functions handles parameter access, retrieval, and prior
5 * specification in the model. Parameters can be either fixed (specified in advance)
6 * or variable (estimated during inference).
7 */
8
9/**
10 * Get a parameter value from either fixed or variable parameters
11 *
12 * This function retrieves a parameter value based on its ID, checking first if it's
13 * a fixed parameter and then if it's a variable parameter.
14 *
15 * @param id Parameter ID
16 * @param params_fixed_lookup Array of fixed parameter lookup indices
17 * @param params_variable_lookup Array of variable parameter lookup indices
18 * @param params_value Vector of fixed parameter values
19 * @param params Vector of variable parameter values
20 * @return The parameter value (scalar)
21 *
22 * @ingroup parameter_handlers
23 */
24real get_param(int id,
25 array[] int params_fixed_lookup,
26 array[] int params_variable_lookup,
27 vector params_value, vector params) {
28 if (id == 0) {
29 return 0; // parameter not used
30 } else if (params_fixed_lookup[id]) {
31 return params_value[params_fixed_lookup[id]];
32 } else {
33 return params[params_variable_lookup[id]];
34 }
35}
36
37/**
38 * Get a parameter value from either fixed or variable parameters (matrix version)
39 *
40 * This function is an overloaded version of get_param that works with a matrix of
41 * parameter values, returning a vector of parameter values for multiple samples.
42 *
43 * @param id Parameter ID
44 * @param params_fixed_lookup Array of fixed parameter lookup indices
45 * @param params_variable_lookup Array of variable parameter lookup indices
46 * @param params_value Vector of fixed parameter values
47 * @param params Matrix of variable parameter values (rows are samples)
48 * @return A vector of parameter values across samples
49 *
50 * @ingroup parameter_handlers
51 */
52vector get_param(int id,
53 array[] int params_fixed_lookup,
54 array[] int params_variable_lookup,
55 vector params_value, matrix params) {
56 int n_samples = rows(params);
57 if (id == 0) {
58 return rep_vector(0, n_samples) ; // parameter not used
59 } else if (params_fixed_lookup[id]) {
60 return rep_vector(params_value[params_fixed_lookup[id]], n_samples);
61 } else {
62 return params[, params_variable_lookup[id]];
63 }
64}
65
66/**
67 * Update log density for parameter priors
68 *
69 * This function adds the log density contributions from parameter priors
70 * to the target, supporting multiple prior distribution types.
71 *
72 * @param params Vector of parameter values
73 * @param prior_dist Array of prior distribution types (0: lognormal, 1: gamma, 2: normal)
74 * @param prior_dist_params Vector of prior distribution parameters
75 * @param params_lower Vector of lower bounds for parameters
76 * @param params_upper Vector of upper bounds for parameters
77 *
78 * @ingroup parameter_handlers
79 */
80void params_lp(vector params, array[] int prior_dist,
81 vector prior_dist_params, vector params_lower,
82 vector params_upper) {
83 int params_id = 1;
84 int num_params = num_elements(params);
85 for (id in 1:num_params) {
86 if (prior_dist[id] == 0) { // lognormal
87 params[id] ~
88 lognormal(
89 prior_dist_params[params_id], prior_dist_params[params_id + 1]
90 )
91 T[params_lower[id], params_upper[id]];
92 params_id += 2;
93 } else if (prior_dist[id] == 1) {
94 params[id] ~
95 gamma(prior_dist_params[params_id], prior_dist_params[params_id + 1])
96 T[params_lower[id], params_upper[id]];
97 params_id += 2;
98 } else if (prior_dist[id] == 2) {
99 params[id] ~
100 normal(prior_dist_params[params_id], prior_dist_params[params_id + 1])
101 T[params_lower[id], params_upper[id]];
102 params_id += 2;
103 } else {
104 reject("dist must be <= 2");
105 }
106 }
107}
108
void params_lp(vector params, array[] int prior_dist, vector prior_dist_params, vector params_lower, vector params_upper)
Definition params.stan:80
real get_param(int id, array[] int params_fixed_lookup, array[] int params_variable_lookup, vector params_value, vector params)
Definition params.stan:24