FIMS  v0.11.0
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 // nullptr outside of a TMB call because TMB owns the objective function.
47 ::objective_function<Type> *of = nullptr;
48#endif
49
55 static std::shared_ptr<Model<Type>> GetInstance() {
56 if (Model<Type>::fims_model == nullptr) {
57 Model<Type>::fims_model = std::make_shared<fims_model::Model<Type>>();
60 }
62 }
63
67 const Type Evaluate() {
68 // jnll = negative-log-likelihood (the objective function)
69 Type jnll = static_cast<Type>(0.0);
71 // Check if fims_information is set
72 if (this->fims_information == nullptr) {
74 "Model: fims_information is not set. Please set fims_information "
75 "before "
76 "calling Evaluate().");
77 return jnll;
78 }
79
80 // Create vector for reporting out nll components
81 fims::Vector<Type> nll_vec(
82 this->fims_information->density_components.size(), 0.0);
83
84 for (m_it = this->fims_information->models_map.begin();
85 m_it != this->fims_information->models_map.end(); ++m_it) {
86 //(*m_it).second points to the Model module
87 std::shared_ptr<fims_popdy::FisheryModelBase<Type>> m = (*m_it).second;
88 m->Prepare();
89 m->Evaluate();
90 }
91
92 // Loop over densities and evaluate joint negative log densities for priors
94 int nll_vec_idx = 0;
95 size_t n_priors = 0;
96 for (d_it = this->fims_information->density_components.begin();
97 d_it != this->fims_information->density_components.end(); ++d_it) {
98 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
99 (*d_it).second;
100#ifdef TMB_MODEL
101 d->of = this->of;
102#endif
103 if (d->input_type == "prior") {
104 nll_vec[nll_vec_idx] = -d->evaluate();
105 jnll += nll_vec[nll_vec_idx];
106 n_priors += 1;
107 nll_vec_idx += 1;
108 }
109 }
110
112 "Model: Finished evaluating prior distributions. The jnll after "
113 "evaluating " +
114 fims::to_string(n_priors) + " priors is: " + fims::to_string(jnll));
115
116 // Loop over densities and evaluate joint negative log-likelihoods for
117 // random effects
118 size_t n_random_effects = 0;
119 for (d_it = this->fims_information->density_components.begin();
120 d_it != this->fims_information->density_components.end(); ++d_it) {
121 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
122 (*d_it).second;
123#ifdef TMB_MODEL
124 d->of = this->of;
125#endif
126 if (d->input_type == "random_effects") {
127 nll_vec[nll_vec_idx] = -d->evaluate();
128 jnll += nll_vec[nll_vec_idx];
129 n_random_effects += 1;
130 nll_vec_idx += 1;
131 }
132 }
133
135 "Model: Finished evaluating random effect distributions. The jnll "
136 "after evaluating priors and " +
137 fims::to_string(n_random_effects) +
138 " random_effects is: " + fims::to_string(jnll));
139
140 // Loop over and evaluate data joint negative log-likelihoods
141 int n_data = 0;
142 for (d_it = this->fims_information->density_components.begin();
143 d_it != this->fims_information->density_components.end(); ++d_it) {
144 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
145 (*d_it).second;
146#ifdef TMB_MODEL
147 d->of = this->of;
148 // d->keep = this->keep;
149#endif
150 if (d->input_type == "data") {
151 nll_vec[nll_vec_idx] = -d->evaluate();
152 jnll += nll_vec[nll_vec_idx];
153 n_data += 1;
154 nll_vec_idx += 1;
155 }
156 }
157
159 "Model: Finished evaluating data likelihoods. The jnll after "
160 "evaluating priors, random effects, and " +
161 fims::to_string(n_data) +
162 " data likelihoods is: " + fims::to_string(jnll));
163
164 // report out nll components
165
166#ifdef TMB_MODEL
167
168 vector<Type> nll_components = nll_vec.to_tmb();
169 FIMS_REPORT_F(nll_components, this->of);
170 FIMS_REPORT_F(jnll, this->of);
171
172#endif
173
174 // report out model family objects
175 for (m_it = this->fims_information->models_map.begin();
176 m_it != this->fims_information->models_map.end(); ++m_it) {
177 //(*m_it).second points to the Model module
178 std::shared_ptr<fims_popdy::FisheryModelBase<Type>> m = (*m_it).second;
179 m->of = this->of; // link to TMB objective function
180 m->Report();
181 }
182
183 return jnll;
184 }
185};
186
187// Create singleton instance of Model class
188template <typename Type>
189std::shared_ptr<Model<Type>> Model<Type>::fims_model =
190 nullptr; // singleton instance
191} // namespace fims_model
192
193#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:494
std::unordered_map< uint32_t, std::shared_ptr< fims_popdy::FisheryModelBase< Type > > > models_map
Definition information.hpp:139
std::map< uint32_t, std::shared_ptr< fims_distributions::DensityComponentBase< Type > > > density_components
Definition information.hpp:129
std::unordered_map< uint32_t, std::shared_ptr< fims_popdy::FisheryModelBase< Type > > >::iterator model_map_iterator
Definition information.hpp:143
std::map< uint32_t, std::shared_ptr< fims_distributions::DensityComponentBase< Type > > >::iterator density_components_iterator
Definition information.hpp:134
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:262
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:55
const Type Evaluate()
Evaluate. Calculates the joint negative log-likelihood function.
Definition model.hpp:67
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:636
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
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:79