FIMS  v0.8.1
Loading...
Searching...
No Matches
multinomial_lpmf.hpp
Go to the documentation of this file.
1
10#ifndef MULTINOMIAL_LPMF
11#define MULTINOMIAL_LPMF
12
14#include "../../common/fims_vector.hpp"
15#include "../../common/def.hpp"
16
17namespace fims_distributions {
36template <typename Type>
43
47
50 virtual ~MultinomialLPMF() {}
51
63 virtual const Type evaluate() {
64 // set dims using data_observed_values if no user input
65 if (dims.size() != 2) {
66 dims.resize(2);
67 dims[0] = this->data_observed_values->get_imax();
68 dims[1] = this->data_observed_values->get_jmax();
69 }
70
71 // setup vector for recording the log probability density function values
72 this->lpdf = static_cast<Type>(0.0);
74 this->lpdf_vec.resize(dims[0] * dims[1]);
75 std::fill(this->lpdf_vec.begin(), this->lpdf_vec.end(), 0);
76 size_t lpdf_vec_idx = 0;
77 // Dimension checks
78 if (this->input_type == "data") {
79 if (this->data_expected_values) {
80 if (dims[0] * dims[1] != this->data_expected_values->size()) {
81 throw std::invalid_argument(
82 "MultinomialLPDF: Vector index out of bounds. The dimension of "
83 "the "
84 "number of rows times the number of columns is of size " +
85 fims::to_string(dims[0] * dims[1]) +
86 " and the expected vector is of size " +
87 fims::to_string(this->data_expected_values->size()));
88 }
89 }
90 } else {
91 if (dims[0] * dims[1] != this->observed_values.size()) {
92 throw std::invalid_argument(
93 "MultinomialLPDF: Vector index out of bounds. The dimension of the "
94 "number of rows times the number of columns is of size " +
95 fims::to_string(dims[0] * dims[1]) +
96 " and the observed vector is of size " +
97 fims::to_string(this->observed_values.size()));
98 }
99 if (this->observed_values.size() != this->expected_values.size()) {
100 throw std::invalid_argument(
101 "MultinomialLPDF: Vector index out of bounds. The dimension of the "
102 "observed vector of size " +
103 fims::to_string(this->observed_values.size()) +
104 " and the expected vector is of size " +
105 fims::to_string(this->expected_values.size()));
106 }
107 }
108
109 for (size_t i = 0; i < dims[0]; i++) {
110 // for each row, create new observed_values and prob vectors
111 fims::Vector<Type> observed_values_vector;
112 fims::Vector<Type> prob_vector;
113 observed_values_vector.resize(dims[1]);
114 prob_vector.resize(dims[1]);
115
116 // Skips the entire row if any values are NA
117 bool containsNA = false;
118
119#ifdef TMB_MODEL
120 for (size_t j = 0; j < dims[1]; j++) {
121 if (this->input_type == "data") {
122 // if data, check if there are any NA values and skip lpdf calculation
123 // for entire row if there are
124 if (this->get_observed(static_cast<size_t>(i),
125 static_cast<size_t>(j)) ==
126 this->data_observed_values->na_value) {
127 containsNA = true;
128 break;
129 }
130 if (!containsNA) {
131 size_t idx = (i * dims[1]) + j;
132 observed_values_vector[j] = this->get_observed(i, j);
133 prob_vector[j] = this->get_expected(idx);
134 }
135 } else {
136 // if not data (i.e. prior or process), use observed_values vector
137 // instead of data_observed_values
138 size_t idx = (i * dims[1]) + j;
139 observed_values_vector[j] = this->get_observed(idx);
140 prob_vector[j] = this->get_expected(idx);
141 }
142 }
143
144 if (!containsNA) {
145 std::fill(this->lpdf_vec.begin() + lpdf_vec_idx,
146 this->lpdf_vec.begin() + lpdf_vec_idx + dims[1],
147 dmultinom(observed_values_vector.to_tmb(),
148 prob_vector.to_tmb(), true));
149
150 this->lpdf += this->lpdf_vec[lpdf_vec_idx];
151 } else {
152 this->lpdf_vec[i] = 0;
153 }
154 lpdf_vec_idx += dims[1];
155/*
156if (this->simulate_flag)
157{
158 FIMS_SIMULATE_F(this->of)
159 {
160 fims::Vector<Type> sim_observed;
161 sim_observed.resize(dims[1]);
162 sim_observed = rmultinom(prob_vector);
163 sim_observed.resize(this->observed_values.size());
164 for (size_t j = 0; j < dims[1]; j++)
165 {
166 idx = (i * dims[1]) + j;
167 this->observed_values[idx] = sim_observed[j];
168 }
169 }
170}
171*/
172#endif
173 }
174
175#ifdef TMB_MODEL
176#endif
177 return (this->lpdf);
178 }
179};
180} // namespace fims_distributions
181#endif
Definition fims_vector.hpp:27
void resize(size_t s)
Changes the number of elements stored.
Definition fims_vector.hpp:375
std::vector< Type > to_tmb() const
Convert fims::Vector to TMB vector type.
Definition fims_vector.hpp:468
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.
Base class for all module_name functors.
Definition density_components_base.hpp:32
fims::Vector< Type > lpdf_vec
Vector storing observation-level log-likelihood contributions.
Definition density_components_base.hpp:190
fims::Vector< Type > observed_values
Input value of distribution function for priors or random effects.
Definition density_components_base.hpp:61
Type & get_expected(size_t i)
Retrieve one expected value based on input_type and use_mean.
Definition density_components_base.hpp:122
Type lpdf
Total log probability density contribution of the distribution.
Definition density_components_base.hpp:180
fims::Vector< Type > * data_expected_values
Expected value vector for data pathways.
Definition density_components_base.hpp:53
Type & get_observed(size_t i)
Retrieve one observed value based on input_type.
Definition density_components_base.hpp:83
std::string input_type
Classification of the input pathway for this distribution object. Options used by accessor methods ar...
Definition density_components_base.hpp:38
std::shared_ptr< fims_data_object::DataObject< Type > > data_observed_values
Observed data.
Definition density_components_base.hpp:41
Implements the MultinomialLPMF distribution functor used by FIMS to evaluate the observation-level an...
Definition multinomial_lpmf.hpp:37
virtual const Type evaluate()
Evaluates the multinomial log probability mass function.
Definition multinomial_lpmf.hpp:63
virtual ~MultinomialLPMF()
Destructor.
Definition multinomial_lpmf.hpp:50
fims::Vector< size_t > dims
Dimensions of the number of rows and columns of the multivariate dataset.
Definition multinomial_lpmf.hpp:42
MultinomialLPMF()
Constructor.
Definition multinomial_lpmf.hpp:46