FIMS  v0.11.0
Loading...
Searching...
No Matches
fims_math.hpp
Go to the documentation of this file.
1
8#ifndef FIMS_MATH_HPP
9#define FIMS_MATH_HPP
10
11// note: this is modeling platform specific, must be controlled by
12// preprocessing macros
13#include <cmath>
14#include <random>
15#include <sstream>
16
18#include "fims_vector.hpp"
19#include "def.hpp"
20
21namespace fims_math {
22#ifdef STD_LIB
23
31template <class Type>
32inline const Type exp(const Type &x) {
33 return std::exp(x);
34}
35
39template <class Type>
40inline Type cond_exp_lt(const Type &left, const Type &right,
41 const Type &if_true, const Type &if_false) {
42 return left < right ? if_true : if_false;
43}
44
51template <class Type>
52inline const Type log(const Type &x) {
53 return std::log(x);
54}
55
56template <class Type>
57inline const Type cos(const Type &x) {
58 return std::cos(x);
59}
60
61template <class Type>
62inline const Type sqrt(const Type &x) {
63 return std::sqrt(x);
64}
65
66template <class Type>
67inline const Type pow(const Type &x, const Type &y) {
68 return std::pow(x, y);
69}
70
71template <class Type>
72inline const Type lgamma(const Type &x) {
73 return std::lgamma(x);
74}
75
76inline double Value(const double &x) { return x; }
77
87template <class Type>
88inline const Type pnorm(const Type &x, const Type &mean, const Type &sd) {
89 const double z = static_cast<double>(x - mean) /
90 (static_cast<double>(sd) * std::sqrt(2.0));
91 return static_cast<Type>(0.5 * (1.0 + std::erf(z)));
92}
93
94#endif
95
96#ifdef TMB_MODEL
97
98inline double Value(const TMBAD_FIMS_TYPE &x) { return x.Value(); }
99
100inline double Value(const double &x) { return x; }
101
102// Add the following line to CMakeLists.txt to enable documentation of TMB_MODEL
103// in doxygen or none of the following is rendered.
104// set(DOXYGEN_PREDEFINED "TMB_MODEL=1" "ENABLE_TMB_CODE")
105
117template <class Type>
118inline const Type exp(const Type &x) {
119 // use std::exp for double type, look for TMB version of exp if AD type
120 using std::exp;
121 return exp(x);
122}
123
127template <class Type>
128inline Type cond_exp_lt(const Type &left, const Type &right,
129 const Type &if_true, const Type &if_false) {
130 return TMBad::CondExpLt(left, right, if_true, if_false);
131}
132
145template <class Type>
146inline const Type log(const Type &x) {
147 // use std::log for double type, look for TMB version of log if AD type
148 using std::log;
149 return log(x);
150}
151
164template <class Type>
165inline const Type cos(const Type &x) {
166 // use std::cos for double type, look for TMB version of cos if AD type
167 using std::cos;
168 return cos(x);
169}
170
182template <class Type>
183inline const Type sqrt(const Type &x) {
184 // use std::std for double type, look for TMB version of std if AD type
185 using std::sqrt;
186 return sqrt(x);
187}
188
201template <class Type>
202inline const Type pow(const Type &x, const Type &y) {
203 // use std::pow for double type, look for TMB version of pow if AD type
204 using std::pow;
205 return pow(x, y);
206}
207
223template <class Type>
224inline const Type lgamma(const Type &x) {
225 // use std::lgamma for double type, look for TMB version of lgamma if AD type
226 using std::lgamma;
227 return lgamma(x);
228}
229
233template <class Type>
234inline const Type pnorm(const Type &x, const Type &mean, const Type &sd) {
235 // Use :: to call TMB's global pnorm; an unqualified call recurses into this
236 // wrapper and can hang CreateTMBModel() when growth uses normal
237 // probabilities.
238 return ::pnorm(x, mean, sd);
239}
240
241#endif
242
260template <class Type>
261inline const Type logistic(const Type &inflection_point, const Type &slope,
262 const Type &x) {
263 return static_cast<Type>(1.0) /
264 (static_cast<Type>(1.0) +
265 exp(Type(-1.0) * slope * (x - inflection_point)));
266}
267
278template <class Type>
279inline const Type logit(const Type &a, const Type &b, const Type &x) {
280 return -fims_math::log(b - x) + fims_math::log(x - a);
281}
282
293template <class Type>
294inline const Type inv_logit(const Type &a, const Type &b, const Type &logit_x) {
295 return a + (b - a) / (static_cast<Type>(1.0) + fims_math::exp(-logit_x));
296}
297
320
321template <class Type>
322inline const Type double_logistic(const Type &inflection_point_asc,
323 const Type &slope_asc,
324 const Type &inflection_point_desc,
325 const Type &slope_desc, const Type &x) {
326 return (static_cast<Type>(1.0)) /
327 (static_cast<Type>(1.0) +
328 exp(Type(-1.0) * slope_asc * (x - inflection_point_asc))) *
329 (static_cast<Type>(1.0) /
330 (static_cast<Type>(1.0) +
331 exp(slope_desc * (x - inflection_point_desc))));
332}
333
352template <class Type>
353const Type ad_fabs(const Type &x, Type C = 1e-5) {
354 // C = 0 gives the exact |x|, which has a NaN derivative at x = 0. C < 0
355 // gives NaN when |x| is small. C is a fixed setting, not an estimated
356 // value, so checking it once when TMB records the tape is enough.
357 if (fims_math::Value(C) <= 0.0) {
358 throw std::invalid_argument("fims_math::ad_fabs: C is " +
359 fims::to_string(fims_math::Value(C)) +
360 ", which is not greater than 0.");
361 }
362 return sqrt((x * x) + C);
363}
364
384
385template <typename Type>
386inline const Type ad_min(const Type &a, const Type &b, Type C = 1e-5) {
387 return (a + b - fims_math::ad_fabs(a - b, C)) * static_cast<Type>(0.5);
388}
389
409template <typename Type>
410inline const Type ad_max(const Type &a, const Type &b, Type C = 1e-5) {
411 return (a + b + fims_math::ad_fabs(a - b, C)) * static_cast<Type>(.5);
412}
413
435template <typename Type>
437 const fims::Vector<Type> &proportion_at_true_age,
438 const fims::Vector<Type> &ageing_error, size_t year = 0) {
439 const size_t n_ages = proportion_at_true_age.size();
440 const size_t matrix_size = n_ages * n_ages;
441 const size_t matrix_offset =
442 (ageing_error.size() == matrix_size) ? 0 : year * matrix_size;
443
444 fims::Vector<Type> proportion_at_observed_age(n_ages, static_cast<Type>(0.0));
445 for (size_t true_age = 0; true_age < n_ages; true_age++) {
446 for (size_t observed_age = 0; observed_age < n_ages; observed_age++) {
447 proportion_at_observed_age[observed_age] +=
448 proportion_at_true_age[true_age] *
449 ageing_error[matrix_offset + true_age * n_ages + observed_age];
450 }
451 }
452 return proportion_at_observed_age;
453}
454
463template <class T>
464T sum(const std::vector<T> &v) {
465 T ret = 0.0;
466 for (size_t i = 0; i < v.size(); i++) {
467 ret += v[i];
468 }
469 return ret;
470}
471
480template <class T>
481T sum(const fims::Vector<T> &v) {
482 T ret = 0.0;
483 for (size_t i = 0; i < v.size(); i++) {
484 ret += v[i];
485 }
486 return ret;
487}
488
489} // namespace fims_math
490
491#endif /* FIMS_MATH_HPP */
Definition fims_vector.hpp:27
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:299
Platform macros and the core FIMS logging system.
std::string to_string(T v)
Definition def.hpp:735
const Type logit(const Type &a, const Type &b, const Type &x)
A logit function for bounding of parameters.
Definition fims_math.hpp:279
fims::Vector< Type > apply_ageing_error(const fims::Vector< Type > &proportion_at_true_age, const fims::Vector< Type > &ageing_error, size_t year=0)
Converts values at true age, proportions or expected numbers, to values at observed age using an agei...
Definition fims_math.hpp:436
T sum(const std::vector< T > &v)
Definition fims_math.hpp:464
const Type ad_min(const Type &a, const Type &b, Type C=1e-5)
Definition fims_math.hpp:386
const Type double_logistic(const Type &inflection_point_asc, const Type &slope_asc, const Type &inflection_point_desc, const Type &slope_desc, const Type &x)
The general double logistic function.
Definition fims_math.hpp:322
const Type ad_max(const Type &a, const Type &b, Type C=1e-5)
Definition fims_math.hpp:410
const Type inv_logit(const Type &a, const Type &b, const Type &logit_x)
An inverse logit function for bounding of parameters.
Definition fims_math.hpp:294
const Type ad_fabs(const Type &x, Type C=1e-5)
Definition fims_math.hpp:353
const Type logistic(const Type &inflection_point, const Type &slope, const Type &x)
The general logistic function.
Definition fims_math.hpp:261
Establishes the FIMS Vector class.
An interface to the modeling platforms, e.g., TMB.