FIMS  v0.8.1
Loading...
Searching...
No Matches
density_components_base.hpp
Go to the documentation of this file.
1
15#ifndef DENSITY_COMPONENT_BASE_HPP
16#define DENSITY_COMPONENT_BASE_HPP
17
18#include "../../common/data_object.hpp"
19#include "../../common/model_object.hpp"
20#include "../../interface/interface.hpp"
21#include "../../common/fims_vector.hpp"
22#include "../../common/fims_math.hpp"
23
24namespace fims_distributions {
25
31template <typename Type>
38 std::string input_type;
39
41 std::shared_ptr<fims_data_object::DataObject<Type>> data_observed_values;
42
45
48
51
54
56 std::vector<fims::Vector<Type>*> priors;
57
62
67
72 std::string use_mean = fims::to_string("no");
73
74 // std::shared_ptr<DistributionElementObject<Type>> expected;
75 // // Expected value of distribution function.
76
83 inline Type& get_observed(size_t i) {
84 if (this->input_type == "data") {
85 return data_observed_values->at(i);
86 }
87 if (this->input_type == "random_effects") {
88 return (*re)[i];
89 }
90 if (this->input_type == "prior") {
91 if (priors.size() == 0) {
92 throw std::runtime_error("No priors defined for this distribution.");
93 } else if (priors.size() == 1) {
94 return (*(priors[0]))[i];
95 } else if (priors.size() > 1) {
96 return (*(priors[i]))[0];
97 }
98 }
99 return observed_values[i];
100 }
101
108 inline Type& get_observed(size_t i, size_t j) {
109 if (this->input_type == "data") {
110 return data_observed_values->at(i, j);
111 }
112 return observed_values[i * (j - 1) + j];
113 }
114
122 inline Type& get_expected(size_t i) {
123 if (this->input_type == "data") {
124 return (*data_expected_values)[i];
125 } else if (this->use_mean == "yes") {
126 return this->expected_mean.get_force_scalar(i);
127 } else if (this->input_type == "random_effects") {
128 return (*re_expected_values)[i];
129 } else {
130 return this->expected_values.get_force_scalar(i);
131 }
132 }
133
138 inline size_t get_n_x() {
139 if (this->input_type == "data") {
140 return this->data_observed_values->data.size();
141 }
142 if (this->input_type == "random_effects") {
143 return (*re).size();
144 }
145 if (this->input_type == "prior") {
146 return this->expected_values.size();
147 }
148 return observed_values.size();
149 }
150
155 inline size_t get_n_expected() {
156 if (this->input_type == "data") {
157 return (*data_expected_values).size();
158 }
159 if (this->input_type == "random_effects") {
160 return (*re_expected_values).size();
161 }
162 if (this->input_type == "prior") {
163 return this->expected_values.size();
164 }
165 return observed_values.size();
166 }
167 // id_g is the ID of the instance of the DensityComponentBase class.
168 // this is like a memory tracker.
169 // Assigning each one its own ID is a way to keep track of
170 // all the instances of the DensityComponentBase class.
174 static uint32_t id_g;
175
180 Type lpdf;
181
186
191
195 bool osa_flag = false;
196
200 bool simulate_flag = false;
201
205 std::vector<uint32_t> key;
206
207#ifdef TMB_MODEL
211 ::objective_function<Type>* of;
212#endif
213
218 // initialize the priors vector with a size of 1 and set the first element
219 // to NULL
220 this->priors.resize(1);
221 this->priors[0] = NULL;
222 this->id = DensityComponentBase::id_g++;
223 }
224
225 virtual ~DensityComponentBase() {}
230 virtual const Type evaluate() = 0;
231};
232
235template <typename Type>
237
238} // namespace fims_distributions
239
240#endif /* DENSITY_COMPONENT_BASE_HPP */
Definition fims_vector.hpp:27
Type & get_force_scalar(size_t pos)
If this vector is size 1 and pos is greater than zero, the first index is returned....
Definition fims_vector.hpp:182
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:273
Base class for all module_name functors.
Definition density_components_base.hpp:32
std::vector< uint32_t > key
Unique ID for variable map that points to a fims::Vector.
Definition density_components_base.hpp:205
std::string use_mean
If "yes", expected_mean is used instead of expected_values. The default is "no" leading to the use of...
Definition density_components_base.hpp:72
fims::Vector< Type > lpdf_vec
Vector storing observation-level log-likelihood contributions.
Definition density_components_base.hpp:190
fims::Vector< Type > observed_values
Input value of distribution function for priors or random effects.
Definition density_components_base.hpp:61
bool simulate_flag
Boolean; if true, data are simulated from the distribution.
Definition density_components_base.hpp:200
Type & get_expected(size_t i)
Retrieve one expected value based on input_type and use_mean.
Definition density_components_base.hpp:122
bool osa_flag
Boolean; if true, one-step-ahead (OSA) residuals are calculated.
Definition density_components_base.hpp:195
Type lpdf
Total log probability density contribution of the distribution.
Definition density_components_base.hpp:180
fims::Vector< Type > * data_expected_values
Expected value vector for data pathways.
Definition density_components_base.hpp:53
DensityComponentBase()
Constructor, which initializes default prior pointer state and ID.
Definition density_components_base.hpp:217
virtual const Type evaluate()=0
Evaluate the distribution-specific log-likelihood contribution.
size_t get_n_expected()
Get length of the active expected input vector.
Definition density_components_base.hpp:155
size_t get_n_x()
Get length of the active observed input vector.
Definition density_components_base.hpp:138
Type & get_observed(size_t i)
Retrieve one observed value based on input_type.
Definition density_components_base.hpp:83
fims::Vector< Type > * re
Pointer to random effects vector.
Definition density_components_base.hpp:47
std::string input_type
Classification of the input pathway for this distribution object. Options used by accessor methods ar...
Definition density_components_base.hpp:38
fims::Vector< Type > expected_values
Expected value vector for prior-based pathways.
Definition density_components_base.hpp:44
fims::Vector< Type > * re_expected_values
Expected value vector for random-effects pathways.
Definition density_components_base.hpp:50
std::shared_ptr< fims_data_object::DataObject< Type > > data_observed_values
Observed data.
Definition density_components_base.hpp:41
static uint32_t id_g
Global unique identifier for distribution modules.
Definition density_components_base.hpp:174
int observed_data_id_m
ID of observed data component.
Definition density_components_base.hpp:185
std::vector< fims::Vector< Type > * > priors
Vector of pointers where each entry points to a prior parameter.
Definition density_components_base.hpp:56
Type & get_observed(size_t i, size_t j)
Retrieve one observed matrix-like value based on input_type.
Definition density_components_base.hpp:108
fims::Vector< Type > expected_mean
The expected mean of the distribution; overrides expected values.
Definition density_components_base.hpp:66
FIMSObject struct that defines member types and returns the unique id.
Definition model_object.hpp:25