FIMS  v0.9.3
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
16namespace fims_distributions {
35template <typename Type>
42
46
49 virtual ~MultinomialLPMF() {}
50
62 virtual const Type evaluate() {
63 // set dims using data_observed_values if no user input
64 if (dims.size() != 2) {
65 dims.resize(2);
66 dims[0] = this->data_observed_values->get_imax();
67 dims[1] = this->data_observed_values->get_jmax();
68 }
69
70 // setup vector for recording the log probability density function values
71 this->lpdf = static_cast<Type>(0.0);
73 this->lpdf_vec.resize(dims[0] * dims[1]);
74 std::fill(this->lpdf_vec.begin(), this->lpdf_vec.end(), 0);
75 size_t lpdf_vec_idx = 0;
76 // Dimension checks
77 if (this->input_type == "data") {
78 if (this->data_expected_values) {
79 if (dims[0] * dims[1] != this->data_expected_values->size()) {
80 throw std::invalid_argument(
81 "MultinomialLPDF: Vector index out of bounds. The dimension of "
82 "the "
83 "number of rows times the number of columns is of size " +
84 std::to_string(dims[0] * dims[1]) +
85 " and the expected vector is of size " +
86 std::to_string(this->data_expected_values->size()));
87 }
88 }
89 } else {
90 if (dims[0] * dims[1] != this->observed_values.size()) {
91 throw std::invalid_argument(
92 "MultinomialLPDF: Vector index out of bounds. The dimension of the "
93 "number of rows times the number of columns is of size " +
94 std::to_string(dims[0] * dims[1]) +
95 " and the observed vector is of size " +
96 std::to_string(this->observed_values.size()));
97 }
98 if (this->observed_values.size() != this->expected_values.size()) {
99 throw std::invalid_argument(
100 "MultinomialLPDF: Vector index out of bounds. The dimension of the "
101 "observed vector of size " +
102 std::to_string(this->observed_values.size()) +
103 " and the expected vector is of size " +
104 std::to_string(this->expected_values.size()));
105 }
106 }
107
108 for (size_t i = 0; i < dims[0]; i++) {
109 // for each row, create new observed_values and prob vectors
110 fims::Vector<Type> observed_values_vector;
111 fims::Vector<Type> prob_vector;
112 observed_values_vector.resize(dims[1]);
113 prob_vector.resize(dims[1]);
114
115 // Skips the entire row if any values are NA
116 bool containsNA = false;
117
118#ifdef TMB_MODEL
119 for (size_t j = 0; j < dims[1]; j++) {
120 if (this->input_type == "data") {
121 // if data, check if there are any NA values and skip lpdf calculation
122 // for entire row if there are
123 if (this->get_observed(static_cast<size_t>(i),
124 static_cast<size_t>(j)) ==
125 this->data_observed_values->na_value) {
126 containsNA = true;
127 break;
128 }
129 if (!containsNA) {
130 size_t idx = (i * dims[1]) + j;
131 observed_values_vector[j] = this->get_observed(i, j);
132 prob_vector[j] = this->get_expected(idx);
133 }
134 } else {
135 // if not data (i.e. prior or process), use observed_values vector
136 // instead of data_observed_values
137 size_t idx = (i * dims[1]) + j;
138 observed_values_vector[j] = this->get_observed(idx);
139 prob_vector[j] = this->get_expected(idx);
140 }
141 }
142
143 if (!containsNA) {
144 std::fill(this->lpdf_vec.begin() + lpdf_vec_idx,
145 this->lpdf_vec.begin() + lpdf_vec_idx + dims[1],
146 dmultinom(observed_values_vector.to_tmb(),
147 prob_vector.to_tmb(), true));
148
149 this->lpdf += this->lpdf_vec[lpdf_vec_idx];
150 } else {
151 this->lpdf_vec[i] = 0;
152 }
153 lpdf_vec_idx += dims[1];
154/*
155if (this->simulate_flag)
156{
157 FIMS_SIMULATE_F(this->of)
158 {
159 fims::Vector<Type> sim_observed;
160 sim_observed.resize(dims[1]);
161 sim_observed = rmultinom(prob_vector);
162 sim_observed.resize(this->observed_values.size());
163 for (size_t j = 0; j < dims[1]; j++)
164 {
165 idx = (i * dims[1]) + j;
166 this->observed_values[idx] = sim_observed[j];
167 }
168 }
169}
170*/
171#endif
172 }
173
174#ifdef TMB_MODEL
175#endif
176 return (this->lpdf);
177 }
178};
179} // namespace fims_distributions
180#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:36
virtual const Type evaluate()
Evaluates the multinomial log probability mass function.
Definition multinomial_lpmf.hpp:62
virtual ~MultinomialLPMF()
Destructor.
Definition multinomial_lpmf.hpp:49
fims::Vector< size_t > dims
Dimensions of the number of rows and columns of the multivariate dataset.
Definition multinomial_lpmf.hpp:41
MultinomialLPMF()
Constructor.
Definition multinomial_lpmf.hpp:45