FIMS  v0.8.0
Loading...
Searching...
No Matches
normal_lpdf.hpp
Go to the documentation of this file.
1
11#ifndef NORMAL_LPDF
12#define NORMAL_LPDF
13
15#include "../../common/fims_vector.hpp"
16#include "../../common/def.hpp"
17
18namespace fims_distributions {
22template <typename Type>
23struct NormalLPDF : public DensityComponentBase<Type> {
26 Type lpdf = static_cast<Type>(0.0);
32
35 virtual ~NormalLPDF() {}
36
40 virtual const Type evaluate() {
41 // set vector size based on input type (prior, process, or data)
42 size_t n_x = this->get_n_x();
43 // get expected value vector size
44 size_t n_expected = this->get_n_expected();
45 // setup vector for recording the log probability density function values
46 this->lpdf_vec.resize(n_x);
47 this->report_lpdf_vec.resize(n_x);
48 std::fill(this->lpdf_vec.begin(), this->lpdf_vec.end(),
49 static_cast<Type>(0));
50 std::fill(this->report_lpdf_vec.begin(), this->report_lpdf_vec.end(),
51 static_cast<Type>(0));
52 lpdf = static_cast<Type>(0);
53
54 // Dimension checks
55 if (n_x != n_expected) {
56 if (n_expected == 1) {
57 n_expected = n_x;
58 } else if (n_x > n_expected) {
59 n_x = n_expected;
60 }
61 }
62
63 if (this->log_sd.size() > 1 && n_x != this->log_sd.size()) {
64 throw std::invalid_argument(
65 "NormalLPDF::Vector index out of bounds. The size of observed data "
66 "does not equal the size of the log_sd vector. The observed data "
67 "vector is of size " +
68 fims::to_string(n_x) + " and the log_sd vector is of size " +
69 fims::to_string(this->log_sd.size()));
70 }
71
72 for (size_t i = 0; i < n_x; i++) {
73#ifdef TMB_MODEL
74 if (this->input_type == "data") {
75 // if data, check if there are any NA values and skip lpdf calculation
76 // if there are
77 if (this->get_observed(i) != this->observed_values->na_value) {
78 this->lpdf_vec[i] =
79 dnorm(this->get_observed(i), this->get_expected(i),
80 fims_math::exp(log_sd.get_force_scalar(i)), true);
81 } else {
82 this->lpdf_vec[i] = 0;
83 }
84 // if not data (i.e. prior or process), use x vector instead of
85 // observed_values
86 } else {
87 this->lpdf_vec[i] =
88 dnorm(this->get_observed(i), this->get_expected(i),
89 fims_math::exp(log_sd.get_force_scalar(i)), true);
90 }
91 this->report_lpdf_vec[i] = this->lpdf_vec[i];
92 lpdf += this->lpdf_vec[i];
93 if (this->simulate_flag) {
94 FIMS_SIMULATE_F(this->of) {
95 if (this->input_type == "data") {
96 this->observed_values->at(i) =
97 rnorm(this->get_expected(i),
98 fims_math::exp(log_sd.get_force_scalar(i)));
99 }
100 if (this->input_type == "random_effects") {
101 (*this->re)[i] = rnorm(this->get_expected(i),
102 fims_math::exp(log_sd.get_force_scalar(i)));
103 }
104 if (this->input_type == "prior") {
105 (*(this->priors[i]))[0] =
106 rnorm(this->get_expected(i),
107 fims_math::exp(log_sd.get_force_scalar(i)));
108 }
109 }
110 }
111#endif
112 /* osa not working yet
113 if(osa_flag){//data observation type implements osa residuals
114 //code for osa cdf method
115 this->lpdf_vec[i] = this->keep.cdf_lower[i] * log( pnorm(this->x[i],
116 this->get_expected(i), sd[i]) ); this->lpdf_vec[i] =
117 this->keep.cdf_upper[i] * log( 1.0 - pnorm(this->x[i],
118 this->get_expected(i), sd[i]) );
119 } */
120 }
121#ifdef TMB_MODEL
122 vector<Type> normal_x = this->x.to_tmb();
123#endif
124 return (lpdf);
125 }
126};
127
128} // namespace fims_distributions
129#endif
Definition fims_vector.hpp:27
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:273
Declares the DensityComponentBase class, which is the base class for all distribution functors.
#define FIMS_SIMULATE_F(F)
TMB macro that simulates data.
Definition interface.hpp:70
Base class for all module_name functors.
Definition density_components_base.hpp:152
fims::Vector< Type > lpdf_vec
Definition density_components_base.hpp:160
bool simulate_flag
Definition density_components_base.hpp:165
fims::Vector< Type > report_lpdf_vec
Definition density_components_base.hpp:162
size_t get_n_x()
Definition density_components_base.hpp:114
Type & get_observed(size_t i)
Definition density_components_base.hpp:55
std::vector< fims::Vector< Type > * > priors
Definition density_components_base.hpp:40
size_t get_n_expected()
Definition density_components_base.hpp:131
std::shared_ptr< fims_data_object::DataObject< Type > > observed_values
Definition density_components_base.hpp:32
fims::Vector< Type > * re
Definition density_components_base.hpp:35
std::string input_type
Definition density_components_base.hpp:28
fims::Vector< Type > x
Definition density_components_base.hpp:41
Type & get_expected(size_t i)
Definition density_components_base.hpp:98
Definition normal_lpdf.hpp:23
NormalLPDF()
Constructor.
Definition normal_lpdf.hpp:31
Type lpdf
Definition normal_lpdf.hpp:26
fims::Vector< Type > log_sd
Definition normal_lpdf.hpp:24
virtual ~NormalLPDF()
Destructor.
Definition normal_lpdf.hpp:35
virtual const Type evaluate()
Evaluates the normal probability density function.
Definition normal_lpdf.hpp:40