From b2cd47fdd88e6c89824cc1f375fcd867c3bf8afe Mon Sep 17 00:00:00 2001 From: Bai-Li-NOAA Date: Wed, 13 Nov 2024 12:00:38 -0500 Subject: [PATCH] fix: fix lpdf_vec to return 0 if data is missing --- .../functors/multinomial_lpmf.hpp | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/inst/include/distributions/functors/multinomial_lpmf.hpp b/inst/include/distributions/functors/multinomial_lpmf.hpp index 5d237b68..4265d799 100644 --- a/inst/include/distributions/functors/multinomial_lpmf.hpp +++ b/inst/include/distributions/functors/multinomial_lpmf.hpp @@ -28,7 +28,7 @@ namespace fims_distributions { Type lpdf = 0.0; /**< total negative log-likelihood contribution of the distribution */ fims::Vector dims; /**< Dimensions of the number of rows and columns of the multivariate dataset */ - + // data_indicator , Type> keep; /**< Indicator used in TMB one-step-ahead residual calculations */ /** @brief Constructor. @@ -49,7 +49,7 @@ namespace fims_distributions // set dims using observed_values if no user input if(dims.size() != 2){ dims.resize(2); - dims[0] = this->observed_values->get_imax(); + dims[0] = this->observed_values->get_imax(); dims[1] = this->observed_values->get_jmax(); } @@ -57,8 +57,8 @@ namespace fims_distributions // setup vector for recording the log probability density function values Type lpdf = 0.0; /**< total log probability mass contribution of the distribution */ this->lpdf_vec.resize(dims[0]); - std::fill(this->lpdf_vec.begin(), this->lpdf_vec.end(), 0); - + std::fill(this->lpdf_vec.begin(), this->lpdf_vec.end(), 0); + if (dims[0]*dims[1] != this->expected_values.size()) { ERROR_LOG << "Error: observed age comp is of size " << dims[0]*dims[1] << " and expected is of size " << this->expected_values.size() @@ -67,15 +67,15 @@ namespace fims_distributions } else { for (size_t i = 0; i < dims[0]; i++) { - // for each row, create new x and prob vectors + // for each row, create new x and prob vectors fims::Vector x_vector; fims::Vector prob_vector; x_vector.resize(dims[1]); prob_vector.resize(dims[1]); - + bool containsNA = false; /**< skips the entire row if any values are NA */ - + #ifdef TMB_MODEL for (size_t j = 0; j < dims[1]; j++){ if(this->input_type == "data"){ @@ -91,14 +91,18 @@ namespace fims_distributions prob_vector[j] = this->expected_values[idx]; } } else { - // if not data (i.e. prior or process), use x vector instead of observed_values + // if not data (i.e. prior or process), use x vector instead of observed_values size_t idx = (i * dims[1]) + j; x_vector[j] = this->x[idx]; prob_vector[j] = this->expected_values[idx]; } } - - this->lpdf_vec[i] = dmultinom((vector)x_vector, (vector) prob_vector, true); + + if(!containsNA){ + this->lpdf_vec[i] = dmultinom((vector)x_vector, (vector) prob_vector, true); + } else { + this->lpdf_vec[i] = 0; + } lpdf += this->lpdf_vec[i]; /* if (this->simulate_flag)