FIMS  v0.8.0
Loading...
Searching...
No Matches
rcpp_maturity.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MATURITY_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MATURITY_HPP
11
12#include "../../../population_dynamics/maturity/maturity.hpp"
14
20 public:
24 static uint32_t id_g;
34 static std::map<uint32_t, std::shared_ptr<MaturityInterfaceBase>>
36
41 this->id = MaturityInterfaceBase::id_g++;
42 /* Create instance of map: key is id and value is pointer to
43 MaturityInterfaceBase */
44 // MaturityInterfaceBase::live_objects[this->id] = this;
45 }
46
53
58
62 virtual uint32_t get_id() = 0;
63
68 virtual double evaluate(double x) = 0;
69};
70// static id of the MaturityInterfaceBase object
72// local id of the MaturityInterfaceBase object map relating the ID of the
73// MaturityInterfaceBase to the MaturityInterfaceBase objects
74std::map<uint32_t, std::shared_ptr<MaturityInterfaceBase>>
76
82 public:
91
97 std::make_shared<LogisticMaturityInterface>(*this);
100 }
101
111
116
121 virtual uint32_t get_id() { return this->id; }
122
128 virtual double evaluate(double x) {
131 LogisticMat.inflection_point[0] = this->inflection_point[0].initial_value_m;
132 LogisticMat.slope.resize(1);
133 LogisticMat.slope[0] = this->slope[0].initial_value_m;
134 return LogisticMat.evaluate(x);
135 }
136
141 virtual void finalize() {
142 if (this->finalized) {
143 // log warning that finalize has been called more than once.
144 FIMS_WARNING_LOG("Logistic Maturity " + fims::to_string(this->id) +
145 " has been finalized already.");
146 }
147
148 this->finalized = true; // indicate this has been called already
149
150 std::shared_ptr<fims_info::Information<double>> info =
152
154
155 // search for maturity in Information
156 it = info->maturity_models.find(this->id);
157 // if not found, just return
158 if (it == info->maturity_models.end()) {
159 FIMS_WARNING_LOG("Logistic Maturity " + fims::to_string(this->id) +
160 " not found in Information.");
161 return;
162 } else {
163 std::shared_ptr<fims_popdy::LogisticMaturity<double>> mat =
164 std::dynamic_pointer_cast<fims_popdy::LogisticMaturity<double>>(
165 it->second);
166
167 for (size_t i = 0; i < inflection_point.size(); i++) {
168 if (this->inflection_point[i].estimation_type_m.get() == "constant") {
169 this->inflection_point[i].final_value_m =
170 this->inflection_point[i].initial_value_m;
171 } else {
172 this->inflection_point[i].final_value_m = mat->inflection_point[i];
173 }
174 }
175
176 for (size_t i = 0; i < slope.size(); i++) {
177 if (this->slope[i].estimation_type_m.get() == "constant") {
178 this->slope[i].final_value_m = this->slope[i].initial_value_m;
179 } else {
180 this->slope[i].final_value_m = mat->slope[i];
181 }
182 }
183 }
184 }
185
194 virtual void set_uncertainty(
195 std::map<std::string, std::vector<double>>& se_values) {
197 this->inflection_point.size(), -999);
198 fims::Vector<double> slope_uncertainty(this->slope.size(), -999);
199 this->get_se_values("inflection_point", se_values,
201 this->get_se_values("slope", se_values, slope_uncertainty);
202
203 for (size_t i = 0; i < this->inflection_point.size(); i++) {
204 this->inflection_point[i].uncertainty_m = inflection_point_uncertainty[i];
205 }
206 for (size_t i = 0; i < this->slope.size(); i++) {
207 this->slope[i].uncertainty_m = slope_uncertainty[i];
208 }
209 }
210
217 virtual std::string to_json() {
218 std::stringstream ss;
219 ss << "{\n";
220 ss << " \"module_name\": \"Maturity\",\n";
221 ss << " \"module_type\": \"Logistic\",\n";
222 ss << " \"module_id\": " << this->id << ",\n";
223
224 ss << " \"parameters\": [\n{\n";
225 ss << " \"name\": \"inflection_point\",\n";
226 ss << " \"id\":" << this->inflection_point.id_m << ",\n";
227 ss << " \"type\": \"vector\",\n";
228 ss << " \"dimensionality\": {\n";
229 ss << " \"header\": [null],\n";
230 ss << " \"dimensions\": [1]\n},\n";
231 ss << " \"values\":" << this->inflection_point << "},\n ";
232
233 ss << "{\n";
234 ss << " \"name\": \"slope\",\n";
235 ss << " \"id\":" << this->slope.id_m << ",\n";
236 ss << " \"type\": \"vector\",\n";
237 ss << " \"dimensionality\": {\n";
238 ss << " \"header\": [null],\n";
239 ss << " \"dimensions\": [1]\n},\n";
240 ss << " \"values\":" << this->slope << "}]\n";
241
242 ss << "}";
243
244 return ss.str();
245 }
246
247#ifdef TMB_MODEL
248
249 template <typename Type>
251 std::shared_ptr<fims_info::Information<Type>> info =
253
254 std::shared_ptr<fims_popdy::LogisticMaturity<Type>> maturity =
255 std::make_shared<fims_popdy::LogisticMaturity<Type>>();
256
257 // set relative info
258 maturity->id = this->id;
259 std::stringstream ss;
260 maturity->inflection_point.resize(this->inflection_point.size());
261 for (size_t i = 0; i < this->inflection_point.size(); i++) {
262 maturity->inflection_point[i] = this->inflection_point[i].initial_value_m;
263 if (this->inflection_point[i].estimation_type_m.get() ==
264 "fixed_effects") {
265 ss.str("");
266 ss << "Maturity." << this->id << ".inflection_point."
267 << this->inflection_point[i].id_m;
268 info->RegisterParameterName(ss.str());
269 info->RegisterParameter(maturity->inflection_point[i]);
270 }
271 if (this->inflection_point[i].estimation_type_m.get() ==
272 "random_effects") {
273 ss.str("");
274 ss << "Maturity." << this->id << ".inflection_point."
275 << this->inflection_point[i].id_m;
276 info->RegisterRandomEffectName(ss.str());
277 info->RegisterRandomEffect(maturity->inflection_point[i]);
278 }
279 }
280
281 maturity->slope.resize(this->slope.size());
282 for (size_t i = 0; i < this->slope.size(); i++) {
283 maturity->slope[i] = this->slope[i].initial_value_m;
284 if (this->slope[i].estimation_type_m.get() == "fixed_effects") {
285 ss.str("");
286 ss << "Maturity." << this->id << ".slope." << this->slope[i].id_m;
287 info->RegisterParameterName(ss.str());
288 info->RegisterParameter(maturity->slope[i]);
289 }
290 if (this->slope[i].estimation_type_m.get() == "random_effects") {
291 ss.str("");
292 ss << "Maturity." << this->id << ".slope." << this->slope[i].id_m;
293 info->RegisterRandomEffect(maturity->slope[i]);
294 info->RegisterRandomEffectName(ss.str());
295 }
296 }
297
298 // add to Information
299 info->maturity_models[maturity->id] = maturity;
300
301 return true;
302 }
303
308 virtual bool add_to_fims_tmb() {
309#ifdef TMBAD_FRAMEWORK
312#else
317#endif
318
319 return true;
320 }
321
322#endif
323};
324
325#endif
Base class for all interface objects.
Definition rcpp_interface_base.hpp:634
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:639
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:644
void get_se_values(std::string name, std::map< std::string, std::vector< double > > &se_values, fims::Vector< double > &values)
Method to extract standard error values from the se_values working map.
Definition rcpp_interface_base.hpp:672
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:649
Rcpp interface for logistic maturity to instantiate the object from R: logistic_maturity <- methods::...
Definition rcpp_maturity.hpp:81
virtual ~LogisticMaturityInterface()
The destructor.
Definition rcpp_maturity.hpp:115
ParameterVector slope
The width of the curve at the inflection point.
Definition rcpp_maturity.hpp:90
ParameterVector inflection_point
The index value at which the response reaches 0.5.
Definition rcpp_maturity.hpp:86
LogisticMaturityInterface()
The constructor.
Definition rcpp_maturity.hpp:95
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_maturity.hpp:121
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_maturity.hpp:141
LogisticMaturityInterface(const LogisticMaturityInterface &other)
Construct a new Logistic Maturity Interface object.
Definition rcpp_maturity.hpp:107
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_maturity.hpp:217
virtual double evaluate(double x)
Evaluate maturity using the logistic function.
Definition rcpp_maturity.hpp:128
virtual void set_uncertainty(std::map< std::string, std::vector< double > > &se_values)
Set uncertainty values for logistic maturity parameters.
Definition rcpp_maturity.hpp:194
Rcpp interface that serves as the parent class for Rcpp maturity interfaces. This type should be inhe...
Definition rcpp_maturity.hpp:19
virtual double evaluate(double x)=0
A method for each child maturity interface object to inherit so each maturity option can have an eval...
virtual ~MaturityInterfaceBase()
The destructor.
Definition rcpp_maturity.hpp:57
virtual uint32_t get_id()=0
Get the ID for the child maturity interface objects to inherit.
static std::map< uint32_t, std::shared_ptr< MaturityInterfaceBase > > live_objects
The map associating the IDs of MaturityInterfaceBase to the objects. This is a live object,...
Definition rcpp_maturity.hpp:35
uint32_t id
The local id of the MaturityInterfaceBase object.
Definition rcpp_maturity.hpp:28
MaturityInterfaceBase(const MaturityInterfaceBase &other)
Construct a new Maturity Interface Base object.
Definition rcpp_maturity.hpp:52
MaturityInterfaceBase()
The constructor.
Definition rcpp_maturity.hpp:40
static uint32_t id_g
The static id of the MaturityInterfaceBase object.
Definition rcpp_maturity.hpp:24
An Rcpp interface class that defines the ParameterVector class.
Definition rcpp_interface_base.hpp:178
void resize(size_t size)
Resizes a ParameterVector to the desired length.
Definition rcpp_interface_base.hpp:323
size_t size()
Returns the size of a ParameterVector.
Definition rcpp_interface_base.hpp:316
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:191
Parameter & get(size_t pos)
An internal accessor for calling a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:295
Definition fims_vector.hpp:27
std::map< uint32_t, std::shared_ptr< fims_popdy::MaturityBase< Type > > > maturity_models
Definition information.hpp:97
std::map< uint32_t, std::shared_ptr< fims_popdy::MaturityBase< Type > > >::iterator maturity_models_iterator
Definition information.hpp:101
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:238
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:598
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:279
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...
LogisticMaturity class that returns the logistic function value from fims_math.
Definition logistic.hpp:24
fims::Vector< Type > inflection_point
Definition logistic.hpp:26