FIMS  v0.8.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>>
31#ifdef TMB_MODEL
32 bool do_tmb_reporting = true;
33 ::objective_function<Type> *of;
34#endif
35
36 // constructor
37
38 virtual ~Model() {}
39
45 static std::shared_ptr<Model<Type>> GetInstance() {
46 if (Model<Type>::fims_model == nullptr) {
47 Model<Type>::fims_model = std::make_shared<fims_model::Model<Type>>();
50 }
52 }
53
57 const Type Evaluate() {
58 // jnll = negative-log-likelihood (the objective function)
59 Type jnll = static_cast<Type>(0.0);
61 // Check if fims_information is set
62 if (this->fims_information == nullptr) {
64 "fims_information is not set. Please set fims_information before "
65 "calling Evaluate().");
66 return jnll;
67 }
68
69 // Create vector for reporting out nll components
70 fims::Vector<Type> nll_vec(
71 this->fims_information->density_components.size(), 0.0);
72
73 for (m_it = this->fims_information->models_map.begin();
74 m_it != this->fims_information->models_map.end(); ++m_it) {
75 //(*m_it).second points to the Model module
76 std::shared_ptr<fims_popdy::FisheryModelBase<Type>> m = (*m_it).second;
77 m->of = this->of; // link to TMB objective function
78 m->Prepare();
79 m->Evaluate();
80 }
81
82 // Loop over densities and evaluate joint negative log densities for priors
84 int nll_vec_idx = 0;
85 size_t n_priors = 0;
86 FIMS_INFO_LOG("Begin evaluating prior densities.")
87 for (d_it = this->fims_information->density_components.begin();
88 d_it != this->fims_information->density_components.end(); ++d_it) {
89 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
90 (*d_it).second;
91#ifdef TMB_MODEL
92 d->of = this->of;
93#endif
94 if (d->input_type == "prior") {
95 nll_vec[nll_vec_idx] = -d->evaluate();
96 jnll += nll_vec[nll_vec_idx];
97 n_priors += 1;
98 nll_vec_idx += 1;
99 }
100 }
102 "Model: Finished evaluating prior distributions. The jnll after "
103 "evaluating " +
104 fims::to_string(n_priors) + " priors is: " + fims::to_string(jnll));
105
106 // Loop over densities and evaluate joint negative log-likelihoods for
107 // random effects
108 size_t n_random_effects = 0;
109 for (d_it = this->fims_information->density_components.begin();
110 d_it != this->fims_information->density_components.end(); ++d_it) {
111 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
112 (*d_it).second;
113#ifdef TMB_MODEL
114 d->of = this->of;
115#endif
116 if (d->input_type == "random_effects") {
117 nll_vec[nll_vec_idx] = -d->evaluate();
118 jnll += nll_vec[nll_vec_idx];
119 n_random_effects += 1;
120 nll_vec_idx += 1;
121 }
122 }
124 "Model: Finished evaluating random effect distributions. The jnll "
125 "after evaluating priors and " +
126 fims::to_string(n_random_effects) +
127 " random_effects is: " + fims::to_string(jnll));
128
129 // Loop over and evaluate data joint negative log-likelihoods
130 int n_data = 0;
131 for (d_it = this->fims_information->density_components.begin();
132 d_it != this->fims_information->density_components.end(); ++d_it) {
133 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
134 (*d_it).second;
135#ifdef TMB_MODEL
136 d->of = this->of;
137 // d->keep = this->keep;
138#endif
139 if (d->input_type == "data") {
140 nll_vec[nll_vec_idx] = -d->evaluate();
141 jnll += nll_vec[nll_vec_idx];
142 n_data += 1;
143 nll_vec_idx += 1;
144 }
145 }
146
147// report out nll components
148#ifdef TMB_MODEL
149 vector<Type> nll_components = nll_vec.to_tmb();
150 FIMS_REPORT_F(nll_components, this->of);
151 FIMS_REPORT_F(jnll, this->of);
152#endif
153
154 // report out model family objects
155 for (m_it = this->fims_information->models_map.begin();
156 m_it != this->fims_information->models_map.end(); ++m_it) {
157 //(*m_it).second points to the Model module
158 std::shared_ptr<fims_popdy::FisheryModelBase<Type>> m = (*m_it).second;
159 m->Report();
160 }
161
162 return jnll;
163 }
164};
165
166// Create singleton instance of Model class
167template <typename Type>
168std::shared_ptr<Model<Type>> Model<Type>::fims_model =
169 nullptr; // singleton instance
170} // namespace fims_model
171
172#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
static std::shared_ptr< Model< Type > > fims_model
Definition model.hpp:26
static std::shared_ptr< Model< Type > > GetInstance()
Definition model.hpp:45
const Type Evaluate()
Evaluate. Calculates the joint negative log-likelihood function.
Definition model.hpp:57
std::shared_ptr< fims_info::Information< Type > > fims_information
Definition model.hpp:28
#define FIMS_INFO_LOG(MESSAGE)
Definition def.hpp:590
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:606
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:74