FIMS  v0.9.2
Loading...
Searching...
No Matches
model.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_COMMON_MODEL_HPP
10#define FIMS_COMMON_MODEL_HPP
11
12#include <future>
13#include <memory>
14
15#include "information.hpp"
16
17namespace fims_model {
18
22template <typename Type>
23class Model { // may need singleton
24 public:
25 static std::shared_ptr<Model<Type>>
27 std::shared_ptr<fims_info::Information<Type>>
35 Model() {}
36
41 ~Model() {}
45#ifdef TMB_MODEL
46 ::objective_function<Type> *of;
47#endif
48
54 static std::shared_ptr<Model<Type>> GetInstance() {
55 if (Model<Type>::fims_model == nullptr) {
56 Model<Type>::fims_model = std::make_shared<fims_model::Model<Type>>();
59 }
61 }
62
66 const Type Evaluate() {
67 // jnll = negative-log-likelihood (the objective function)
68 Type jnll = static_cast<Type>(0.0);
70 // Check if fims_information is set
71 if (this->fims_information == nullptr) {
73 "Model: fims_information is not set. Please set fims_information "
74 "before "
75 "calling Evaluate().");
76 return jnll;
77 }
78
79 // Create vector for reporting out nll components
80 fims::Vector<Type> nll_vec(
81 this->fims_information->density_components.size(), 0.0);
82
83 for (m_it = this->fims_information->models_map.begin();
84 m_it != this->fims_information->models_map.end(); ++m_it) {
85 //(*m_it).second points to the Model module
86 std::shared_ptr<fims_popdy::FisheryModelBase<Type>> m = (*m_it).second;
87 m->Prepare();
88 m->Evaluate();
89 }
90
91 // Loop over densities and evaluate joint negative log densities for priors
93 int nll_vec_idx = 0;
94 size_t n_priors = 0;
95 for (d_it = this->fims_information->density_components.begin();
96 d_it != this->fims_information->density_components.end(); ++d_it) {
97 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
98 (*d_it).second;
99#ifdef TMB_MODEL
100 d->of = this->of;
101#endif
102 if (d->input_type == "prior") {
103 nll_vec[nll_vec_idx] = -d->evaluate();
104 jnll += nll_vec[nll_vec_idx];
105 n_priors += 1;
106 nll_vec_idx += 1;
107 }
108 }
109
111 "Model: Finished evaluating prior distributions. The jnll after "
112 "evaluating " +
113 fims::to_string(n_priors) + " priors is: " + fims::to_string(jnll));
114
115 // Loop over densities and evaluate joint negative log-likelihoods for
116 // random effects
117 size_t n_random_effects = 0;
118 for (d_it = this->fims_information->density_components.begin();
119 d_it != this->fims_information->density_components.end(); ++d_it) {
120 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
121 (*d_it).second;
122#ifdef TMB_MODEL
123 d->of = this->of;
124#endif
125 if (d->input_type == "random_effects") {
126 nll_vec[nll_vec_idx] = -d->evaluate();
127 jnll += nll_vec[nll_vec_idx];
128 n_random_effects += 1;
129 nll_vec_idx += 1;
130 }
131 }
132
134 "Model: Finished evaluating random effect distributions. The jnll "
135 "after evaluating priors and " +
136 fims::to_string(n_random_effects) +
137 " random_effects is: " + fims::to_string(jnll));
138
139 // Loop over and evaluate data joint negative log-likelihoods
140 int n_data = 0;
141 for (d_it = this->fims_information->density_components.begin();
142 d_it != this->fims_information->density_components.end(); ++d_it) {
143 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
144 (*d_it).second;
145#ifdef TMB_MODEL
146 d->of = this->of;
147 // d->keep = this->keep;
148#endif
149 if (d->input_type == "data") {
150 nll_vec[nll_vec_idx] = -d->evaluate();
151 jnll += nll_vec[nll_vec_idx];
152 n_data += 1;
153 nll_vec_idx += 1;
154 }
155 }
156
158 "Model: Finished evaluating data likelihoods. The jnll after "
159 "evaluating priors, random effects, and " +
160 fims::to_string(n_data) +
161 " data likelihoods is: " + fims::to_string(jnll));
162
163 // report out nll components
164
165#ifdef TMB_MODEL
166
167 vector<Type> nll_components = nll_vec.to_tmb();
168 FIMS_REPORT_F(nll_components, this->of);
169 FIMS_REPORT_F(jnll, this->of);
170
171#endif
172
173 // report out model family objects
174 for (m_it = this->fims_information->models_map.begin();
175 m_it != this->fims_information->models_map.end(); ++m_it) {
176 //(*m_it).second points to the Model module
177 std::shared_ptr<fims_popdy::FisheryModelBase<Type>> m = (*m_it).second;
178 m->of = this->of; // link to TMB objective function
179 m->Report();
180 }
181
182 return jnll;
183 }
184};
185
186// Create singleton instance of Model class
187template <typename Type>
188std::shared_ptr<Model<Type>> Model<Type>::fims_model =
189 nullptr; // singleton instance
190} // namespace fims_model
191
192#endif /* FIMS_COMMON_MODEL_HPP */
Definition fims_vector.hpp:27
std::vector< Type > to_tmb() const
Convert fims::Vector to TMB vector type.
Definition fims_vector.hpp:468
std::unordered_map< uint32_t, std::shared_ptr< fims_popdy::FisheryModelBase< Type > > > models_map
Definition information.hpp:136
std::map< uint32_t, std::shared_ptr< fims_distributions::DensityComponentBase< Type > > > density_components
Definition information.hpp:126
std::unordered_map< uint32_t, std::shared_ptr< fims_popdy::FisheryModelBase< Type > > >::iterator model_map_iterator
Definition information.hpp:140
std::map< uint32_t, std::shared_ptr< fims_distributions::DensityComponentBase< Type > > >::iterator density_components_iterator
Definition information.hpp:131
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:238
Model class. FIMS objective function.
Definition model.hpp:23
Model()
Construct a new Model object.
Definition model.hpp:35
~Model()
Destroy the Model object.
Definition model.hpp:41
static std::shared_ptr< Model< Type > > fims_model
Definition model.hpp:26
static std::shared_ptr< Model< Type > > GetInstance()
Evaluate. Calculates the joint negative log-likelihood function.
Definition model.hpp:54
const Type Evaluate()
Evaluate. Calculates the joint negative log-likelihood function.
Definition model.hpp:66
std::shared_ptr< fims_info::Information< Type > > fims_information
Definition model.hpp:28
#define FIMS_INFO_LOG(MESSAGE)
Record an info-log entry with metadata.
Definition def.hpp:627
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:655
Code to store all objects that are created in FIMS because FIMS uses integer representation....
#define FIMS_REPORT_F(name, F)
TMB macro that reports variables.
Definition interface.hpp:75