diff --git a/inst/include/common/data_object.hpp b/inst/include/common/data_object.hpp index b78a67bce..19eb4a774 100644 --- a/inst/include/common/data_object.hpp +++ b/inst/include/common/data_object.hpp @@ -45,7 +45,7 @@ namespace fims_data_object { template struct DataObject : public fims_model_object::FIMSObject { static uint32_t id_g; /**< id of the Data Object >*/ - std::vector data; /**< vector of the data >*/ + std::shared_ptr> data; /**< vector of the data >*/ size_t dimensions; /**< dimension of the Data object >*/ size_t imax; /**<1st dimension of data object >*/ size_t jmax; /**< 2nd dimension of data object>*/ @@ -57,7 +57,8 @@ struct DataObject : public fims_model_object::FIMSObject { * Constructs a one-dimensional data object. */ DataObject(size_t imax) : dimensions(1), imax(imax) { - data.resize(imax); + this->data = std::make_shared > (imax); + //data.resize(imax); this->id = DataObject::id_g++; } @@ -66,7 +67,7 @@ struct DataObject : public fims_model_object::FIMSObject { * Constructs a two-dimensional data object. */ DataObject(size_t imax, size_t jmax) : dimensions(2), imax(imax), jmax(jmax) { - data.resize(imax * jmax); + this->data = std::make_shared > (imax * jmax); this->id = DataObject::id_g++; } @@ -75,7 +76,7 @@ struct DataObject : public fims_model_object::FIMSObject { */ DataObject(size_t imax, size_t jmax, size_t kmax) : dimensions(3), imax(imax), jmax(jmax), kmax(kmax) { - data.resize(imax * jmax * kmax); + this->data = std::make_shared > (imax * jmax * kmax); this->id = DataObject::id_g++; } @@ -84,7 +85,7 @@ struct DataObject : public fims_model_object::FIMSObject { */ DataObject(size_t imax, size_t jmax, size_t kmax, size_t lmax) : dimensions(4), imax(imax), jmax(jmax), kmax(kmax), lmax(lmax) { - data.resize(imax * jmax * kmax * lmax); + this->data = std::make_shared >(imax * jmax * kmax * lmax); this->id = DataObject::id_g++; } diff --git a/inst/include/common/information.hpp b/inst/include/common/information.hpp index 8ad4ad8e9..1b976ab4b 100644 --- a/inst/include/common/information.hpp +++ b/inst/include/common/information.hpp @@ -158,14 +158,14 @@ class Information { void setup_priors(){ for(density_components_iterator it = density_components.begin(); it!= density_components.end(); ++it){ - std::shared_ptr > n = (*it).second; - if(n->input_type == "prior"){ + std::shared_ptr > d = (*it).second; + if(d->input_type == "prior"){ variable_map_iterator vmit; - vmit = this->variable_map.find(n->key[0]); - n->x = *(*vmit).second; - for(size_t i=1; ikey.size(); i++){ - vmit = this->variable_map.find(n->key[i]); - n->x.insert(std::end(n->x), + vmit = this->variable_map.find(d->key[0]); + d->x = *(*vmit).second; + for(size_t i=1; ikey.size(); i++){ + vmit = this->variable_map.find(d->key[i]); + d->x.insert(std::end(d->x), std::begin(*(*vmit).second), std::end(*(*vmit).second)); } } @@ -174,13 +174,13 @@ class Information { void setup_random_effects(){ for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){ std::shared_ptr > n = (*it).second; - if(n->input_type == "re"){ + if(d->input_type == "re"){ variable_map_iterator vmit; - vmit = this->variable_map.find(n->key[0]); - n->x = *(*vmit).second; - for(size_t i=1; ikey.size(); i++){ - vmit = this->variable_map.find(n->key[i]); - n->x.insert(std::end(n->x), + vmit = this->variable_map.find(d->key[0]); + d->x = *(*vmit).second; + for(size_t i=1; ikey.size(); i++){ + vmit = this->variable_map.find(d->key[i]); + d->x.insert(std::end(d->x), std::begin(*(*vmit).second), std::end(*(*vmit).second)); } } @@ -189,14 +189,14 @@ class Information { void setup_data(){ for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){ std::shared_ptr > n = (*it).second; - if(n->input_type == "data"){ + if(d->input_type == "data"){ variable_map_iterator vmit; - vmit = this->variable_map.find(n->key[0]); - n->expected_value = *(*vmit).second; + vmit = this->variable_map.find(d->key[0]); + d->expected_values = *(*vmit).second; - for(size_t i=1; ikey.size(); i++){ - vmit = this->variable_map.find(n->key[i]); - n->expected_value.insert(std::end(n->expected_value), + for(size_t i=1; ikey.size(); i++){ + vmit = this->variable_map.find(d->key[i]); + d->expected_values.insert(std::end(d->expected_values), std::begin(*(*vmit).second), std::end(*(*vmit).second)); } } diff --git a/inst/include/common/model.hpp b/inst/include/common/model.hpp index a132b4e4b..16b86c575 100644 --- a/inst/include/common/model.hpp +++ b/inst/include/common/model.hpp @@ -92,7 +92,7 @@ class Model { // may need singleton #endif // Loop over densities and evaluate joint negative log densities for priors size_t n_priors = 0; - typename density_components_iterator d_it; + density_components_iterator d_it; for(d_it = this->density_components.begin(); d_it!= this->density_components.end(); ++d_it){ std::shared_ptr > d = (*d_it).second; #ifdef TMB_MODEL @@ -148,7 +148,7 @@ class Model { // may need singleton << this->fims_information->populations.size() << " populations." << std::endl; for (p_it = this->fims_information->populations.begin(); - p_it != this->fims_information->populations.end(); ++it) { + p_it != this->fims_information->populations.end(); ++p_it) { //(*p_it).second points to the Population module std::shared_ptr > p = (*p_it).second; // link to TMB objective function @@ -159,7 +159,7 @@ class Model { // may need singleton p->Evaluate(); } - typename fims_info::Information::population_iterator f_it; + typename fims_info::Information::fleet_iterator f_it; // Loop over fleets/surveys, and evaluate age comp and index expected values for (f_it = this->fims_information->fleets.begin(); f_it != this->fims_information->fleets.end(); ++f_it) { @@ -171,7 +171,7 @@ class Model { // may need singleton MODEL_LOG << "Setting up pointer to fleet " << f->id << "." << std::endl; f->evaluate_age_comp(); - f->evaluate_index() + f->evaluate_index(); } // Loop over and evaluate data joint negative log-likelihoods @@ -179,7 +179,7 @@ class Model { // may need singleton std::shared_ptr > d = (*d_it).second; #ifdef TMB_MODEL d->of = this->of; - d->keep = this->keep; + //d->keep = this->keep; #endif if(d->input_type == "data"){ jnll -= d->evaluate(); diff --git a/inst/include/distributions/functors/density_components_base.hpp b/inst/include/distributions/functors/density_components_base.hpp index 852017faf..af8464a09 100644 --- a/inst/include/distributions/functors/density_components_base.hpp +++ b/inst/include/distributions/functors/density_components_base.hpp @@ -34,13 +34,17 @@ struct DensityComponentBase : public fims_model_object::FIMSObject { // Assigning each one its own ID is a way to keep track of // all the instances of the DensityComponentBase class. static uint32_t id_g; /**< global unique identifier for distribution modules */ - fims::Vector x; /**< input value of distribution function */ + std::shared_ptr > x; /**< input value of distribution function */ fims::Vector expected_values; /**< expected value of distribution function */ fims::Vector lpdf_vec; /**< vector to record observation level negative log-likelihood values */ std::string input_type; /**< string classifies the type of the negative log-likelihood; options are: prior, re, data */ bool osa_flag = false; /**< Boolean; if true, osa residuals are calculated */ bool simulate_flag = false; /**< Boolean; if true, data are simulated from the distribution */ std::vector key; /**< unique id for variable map that points to a fims::Vector */ + Type na_value = -999; /**< specifying the NA value >*/ + #ifdef TMB_MODEL + ::objective_function *of; /**< Pointer to the TMB objective function */ + #endif /** @brief Constructor. */ diff --git a/inst/include/distributions/functors/lognormal_lpdf.hpp b/inst/include/distributions/functors/lognormal_lpdf.hpp index ce6ab6e01..1822599c4 100644 --- a/inst/include/distributions/functors/lognormal_lpdf.hpp +++ b/inst/include/distributions/functors/lognormal_lpdf.hpp @@ -28,10 +28,6 @@ namespace fims_distributions fims::Vector log_logsd; /**< log of the standard deviation of the distribution on the log scale; can be a vector or scalar */ fims::Vector logmu; /**< mean of the distribution on the log scale; can be a vector or scalar */ fims::Vector logsd; /**< standard deviation of the distribution on the log scale; can be a vector or scalar */ - std::vector is_na; /**< Boolean; if true, data observation is NA and the likelihood contribution is skipped */ - #ifdef TMB_MODEL - ::objective_function *of; /**< Pointer to the TMB objective function */ - #endif Type lpdf = 0.0; /**< total log probability density contribution of the distribution */ // data_indicator , Type> keep; /**< Indicator used in TMB one-step-ahead residual calculations */ @@ -52,7 +48,6 @@ namespace fims_distributions { this->logmu.resize(this->x.size()); this->logsd.resize(this->x.size()); - is_na.resize(this->x.size()); this->lpdf_vec.resize(this->x.size()); for (size_t i = 0; i < this->x.size(); i++) { @@ -83,14 +78,14 @@ namespace fims_distributions #ifdef TMB_MODEL if(this->input_type == "data"){ - if(this->x->at(i) != this->x->na_value){ + if(this->x->at(i) != this->na_value){ + // this->lpdf_vec[i] = this->keep[i] * -dnorm(this->x->at(i), logmu[i], logsd[i], true) - this->x->at(i); this->lpdf_vec[i] = dnorm(this->x->at(i), logmu[i], logsd[i], true) - this->x->at(i); } else { this->lpdf_vec[i] = 0; } } else { - // this->lpdf_vec[i] = this->keep[i] * -dnorm(this->x[i], logmu[i], logsd[i], true); - this->lpdf_vec[i] = dnorm(this->x[i], logmu[i], logsd[i], true); + this->lpdf_vec[i] = dnorm(this->x->at(i), logmu[i], logsd[i], true); } lpdf += this->lpdf_vec[i]; diff --git a/inst/include/distributions/functors/multinomial_lpmf.hpp b/inst/include/distributions/functors/multinomial_lpmf.hpp index 914e14c55..8bc3337dd 100644 --- a/inst/include/distributions/functors/multinomial_lpmf.hpp +++ b/inst/include/distributions/functors/multinomial_lpmf.hpp @@ -28,10 +28,7 @@ namespace fims_distributions { Type lpmf = 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 */ - std::vector is_na; /**< Boolean; if true, data observation is NA and the likelihood contribution for the entire row is skipped */ - #ifdef TMB_MODEL - ::objective_function *of; /**< Pointer to the TMB objective function */ - #endif + // data_indicator , Type> keep; /**< Indicator used in TMB one-step-ahead residual calculations */ /** @brief Constructor. @@ -75,7 +72,7 @@ namespace fims_distributions #ifdef TMB_MODEL for (size_t j = 0; j < dims[1]; j++){ - if (this->x->at(i,j) != this->x->na_value) { + if (this->x->at(i)->at(j) != this->na_value) { size_t idx = (i * dims[1]) + j; } else { containsNA - true; @@ -85,8 +82,8 @@ namespace fims_distributions if(!containsNA){ for (size_t j = 0; j < dims[1]; j++){ size_t idx = (i * dims[1]) + j; - x_vector[j] = this->x->at(i, j); - prob_vector[j] = this->expected_value[idx]; + x_vector[j] = this->x->at(i)->at(j); + prob_vector[j] = this->expected_values[idx]; } } diff --git a/inst/include/distributions/functors/normal_lpdf.hpp b/inst/include/distributions/functors/normal_lpdf.hpp index c671e350f..615441eef 100644 --- a/inst/include/distributions/functors/normal_lpdf.hpp +++ b/inst/include/distributions/functors/normal_lpdf.hpp @@ -28,10 +28,7 @@ struct NormalLPDF : public DensityComponentBase { fims::Vector mu; /**< mean of the distribution; can be a vector or scalar */ fims::Vector sd; /**< standard deviation of the distribution; can be a vector or scalar */ Type lpdf = 0.0; /**< total log probability density contribution of the distribution */ - std::vector is_na; /**< Boolean; if true, data observation is NA and the likelihood contribution is skipped */ - #ifdef TMB_MODEL - ::objective_function *of; /**< Pointer to the TMB objective function */ - #endif + //data_indicator , Type> keep; /**< Indicator used in TMB one-step-ahead residual calculations */ /** @brief Constructor. @@ -76,15 +73,15 @@ struct NormalLPDF : public DensityComponentBase { } #ifdef TMB_MODEL if(this->input_type == "data"){ - if(this->x->at(i) != this->x->na_value){ + if(this->x->at(i) != this->na_value){ + // this->lpdf_vec[i] = this->keep[i] * -dnorm(this->x->at(i), mu[i], sd[i], true); this->lpdf_vec[i] = dnorm(this->x->at(i), mu[i], sd[i], true); } else { this->lpdf_vec[i] = 0; } } else { - // this->lpdf_vec[i] = this->keep[i] * -dnorm(this->x[i], mu[i], sd[i], true); - this->lpdf_vec[i] = dnorm(this->x[i], mu[i], sd[i], true); + this->lpdf_vec[i] = dnorm(this->x->at(i), mu[i], sd[i], true); } lpdf += this->lpdf_vec[i]; if(this->simulate_flag){ diff --git a/inst/include/interface/rcpp/rcpp_interface.hpp b/inst/include/interface/rcpp/rcpp_interface.hpp index 1cc08530a..25114eb63 100644 --- a/inst/include/interface/rcpp/rcpp_interface.hpp +++ b/inst/include/interface/rcpp/rcpp_interface.hpp @@ -432,8 +432,7 @@ RCPP_MODULE(fims) { .method("evaluate", &DnormDistributionsInterface::evaluate) .field("x", &DnormDistributionsInterface::x) .field("expected_values", &DnormDistributionsInterface::expected_values) - .field("log_sd", &DnormDistributionsInterface::log_sd) - .field("is_na", &DnormDistributionsInterface::is_na); + .field("log_sd", &DnormDistributionsInterface::log_sd); Rcpp::class_("LogisticMaturity") .constructor() @@ -475,8 +474,7 @@ RCPP_MODULE(fims) { .field("input_type", &DlnormDistributionsInterface::input_type) .field("x", &DlnormDistributionsInterface::x) .field("expected_values", &DlnormDistributionsInterface::expected_values) - .field("log_logsd", &DlnormDistributionsInterface::log_logsd) - .field("is_na", &DlnormDistributionsInterface::is_na); + .field("log_logsd", &DlnormDistributionsInterface::log_logsd); Rcpp::class_("TMBDmultinomDistribution") .constructor() @@ -484,7 +482,6 @@ RCPP_MODULE(fims) { .method("get_id", &DmultinomDistributionsInterface::get_id) .field("x", &DmultinomDistributionsInterface::x) .field("expected_values", &DmultinomDistributionsInterface::expected_values) - .field("is_na", &DmultinomDistributionsInterface::is_na) .field("dims", &DmultinomDistributionsInterface::dims); } diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp index fff73e914..3badbd178 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp @@ -79,8 +79,7 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase { ParameterVector x; /**< observed data */ ParameterVector expected_values; /**< mean of x for the normal distribution */ ParameterVector log_sd; /**< sd of x for the normal distribution */ - Rcpp::LogicalVector is_na; /**id_m; } @@ -101,9 +100,8 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase { dnorm.x.resize(this->x.size()); dnorm.expected_values.resize(this->expected_values.size()); dnorm.log_sd.resize(this->log_sd.size()); - dnorm.is_na.resize(this->is_na.size()); for(int i=0; ix[i].value_m; + dnorm.x->at(i) = this->x[i].value_m; } for(int i=0; iexpected_values[i].value_m; @@ -111,9 +109,6 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase { for(int i=0; ilog_sd[i].value_m; } - for(int i=0; iis_na[i]; - } return dnorm.evaluate(); } @@ -131,7 +126,7 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase { distribution->id = this->id_m; distribution->x.resize(this->x.size()); for(int i=0; ix.size(); i++){ - distribution->x[i] = this->x[i].value_m; + distribution->x->at(i) = this->x[i].value_m; } // set relative info distribution->expected_values.resize(this->expected_values.size()); @@ -142,10 +137,6 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase { for(int i=0; ilog_sd.size(); i++){ distribution->log_sd[i] = this->log_sd[i].value_m; } - distribution->is_na.resize(this->is_na.size()); - for(int i=0; iis_na[i] = this->is_na[i]; - } info->distribution_models[distribution->id] = distribution; @@ -179,8 +170,7 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase { ParameterVector expected_values; /**< mean of the distribution of log(x) */ ParameterVector log_logsd; /**< log standard deviation of the distribution of log(x) */ Rcpp::String input_type; /**< character string indicating type of input: data, re, prior */ - Rcpp::LogicalVector is_na; /**x.size()); dlnorm.expected_values.resize(this->expected_values.size()); dlnorm.log_logsd.resize(this->log_logsd.size()); - dlnorm.is_na.resize(this->is_na.size()); for(int i=0; ix[i].value_m; + dlnorm.x->at(i) = this->x[i].value_m; } for(int i=0; iexpected_values[i].value_m; @@ -213,9 +202,6 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase { for(int i=0; ilog_logsd[i].value_m; } - for(int i=0; iis_na[i]; - } return dlnorm.evaluate(); } @@ -234,7 +220,7 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase { distribution->input_type = this->input_type; distribution->x.resize(this->x.size()); for(int i=0; ix.size(); i++){ - distribution->x[i] = this->x[i].value_m; + distribution->x->at(i) = this->x[i].value_m; } // set relative info distribution->expected_values.resize(this->expected_values.size()); @@ -245,10 +231,6 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase { for(int i=0; ilog_logsd.size(); i++){ distribution->log_logsd[i] = this->log_logsd[i].value_m; } - distribution->is_na.resize(this->is_na.size()); - for(int i=0; iis_na[i] = this->is_na[i]; - } info->distribution_models[distribution->id] = distribution; @@ -283,7 +265,6 @@ class DmultinomDistributionsInterface : public DistributionsInterfaceBase { ParameterVector x; /**< Vector of length K of integers */ ParameterVector expected_values; /**< Vector of length K, specifying the probability for the K classes (note, unlike in R these must sum to 1). */ - Rcpp::LogicalVector is_na; /**x.size()); dmultinom.expected_values.resize(this->expected_values.size()); - dmultinom.is_na.resize(this->is_na.size()); for(int i=0; ix[i].value_m; + dmultinom.x->at(i) = this->x[i].value_m; } for(int i=0; iexpected_values[i].value_m; } - for(int i=0; iis_na[i]; - } dmultinom.dims.resize(2); dmultinom.dims[0] = this->dims[0]; dmultinom.dims[1] = this->dims[1]; @@ -333,17 +310,13 @@ class DmultinomDistributionsInterface : public DistributionsInterfaceBase { distribution->id = this->id_m; distribution->x.resize(this->x.size()); for(int i=0; ix.size(); i++){ - distribution->x[i] = this->x[i].value_m; + distribution->x->at(i) = this->x[i].value_m; } // set relative info distribution->expected_values.resize(this->expected_values.size()); for(int i=0; iexpected_values.size(); i++){ distribution->expected_values[i] = this->expected_values[i].value_m; } - distribution->is_na.resize(this->is_na.size()); - for(int i=0; iis_na[i] = this->is_na[i]; - } distribution->dims.resize(2); distribution->dims[0] = this->dims[0]; distribution->dims[1] = this->dims[1]; diff --git a/inst/include/population_dynamics/recruitment/functors/recruitment_base.hpp b/inst/include/population_dynamics/recruitment/functors/recruitment_base.hpp index 1c9063dd3..28345cb76 100644 --- a/inst/include/population_dynamics/recruitment/functors/recruitment_base.hpp +++ b/inst/include/population_dynamics/recruitment/functors/recruitment_base.hpp @@ -80,10 +80,8 @@ struct RecruitmentBase : public fims_model_object::FIMSObject { dnorm.x = this->log_recruit_devs; dnorm.expected_values.resize(this->log_recruit_devs.size()); dnorm.log_sd.resize(this->log_recruit_devs.size()); - dnorm.is_na.resize(this->log_recruit_devs.size()); for (size_t i = 0; i < this->log_recruit_devs.size(); i++) { dnorm.expected_values[i] = 0.0; - dnorm.is_na[i] = false; dnorm.log_sd[i] = this->log_sigma_recruit[0]; } lpdf += dnorm.evaluate();