Skip to content

Commit

Permalink
fix: fix lpdf_vec to return 0 if data is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Bai-Li-NOAA committed Nov 13, 2024
1 parent f829e20 commit b2cd47f
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions inst/include/distributions/functors/multinomial_lpmf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace fims_distributions
{
Type lpdf = 0.0; /**< total negative log-likelihood contribution of the distribution */
fims::Vector<size_t> dims; /**< Dimensions of the number of rows and columns of the multivariate dataset */

// data_indicator<tmbutils::vector<Type> , Type> keep; /**< Indicator used in TMB one-step-ahead residual calculations */

/** @brief Constructor.
Expand All @@ -49,16 +49,16 @@ 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();
}


// 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()
Expand All @@ -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<Type> x_vector;
fims::Vector<Type> 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"){
Expand All @@ -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<Type>)x_vector, (vector<Type>) prob_vector, true);

if(!containsNA){
this->lpdf_vec[i] = dmultinom((vector<Type>)x_vector, (vector<Type>) prob_vector, true);
} else {
this->lpdf_vec[i] = 0;
}
lpdf += this->lpdf_vec[i];
/*
if (this->simulate_flag)
Expand Down

0 comments on commit b2cd47f

Please sign in to comment.