Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea-Havron-NOAA committed Aug 1, 2024
1 parent 887f83b commit b04f6db
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 48 deletions.
1 change: 1 addition & 0 deletions inst/include/common/data_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <vector>

#include "model_object.hpp"
#include "fims_vector.hpp"

namespace fims_data_object {

Expand Down
4 changes: 2 additions & 2 deletions inst/include/common/information.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class Information {
}
void setup_random_effects(){
for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > n = (*it).second;
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > d = (*it).second;
if(d->input_type == "re"){
variable_map_iterator vmit;
vmit = this->variable_map.find(d->key[0]);
Expand All @@ -188,7 +188,7 @@ class Information {
}
void setup_data(){
for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > n = (*it).second;
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > d = (*it).second;
if(d->input_type == "data"){
variable_map_iterator vmit;
vmit = this->variable_map.find(d->key[0]);
Expand Down
14 changes: 7 additions & 7 deletions inst/include/distributions/functors/lognormal_lpdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ namespace fims_distributions
*/
virtual const Type evaluate()
{
this->logmu.resize(this->x.size());
this->logsd.resize(this->x.size());
this->lpdf_vec.resize(this->x.size());
for (size_t i = 0; i < this->x.size(); i++)
this->logmu.resize(this->x->size());
this->logsd.resize(this->x->size());
this->lpdf_vec.resize(this->x->size());
for (size_t i = 0; i < this->x->size(); i++)
{
if (this->expected_values.size() == 1)
{
this->logmu[i] = this->expected_values[0];
} else {
if(this->x.size() != this->expected_values.size()){
if(this->x->size() != this->expected_values.size()){
/* move error handling to CreateModel in information so not to crash R
Rcpp::stop("the dimensions of the observed and expected values from lognormal negative log likelihood do not match");
*/
Expand All @@ -67,7 +67,7 @@ namespace fims_distributions
{
logsd[i] = fims_math::exp(log_logsd[0]);
} else {
if(this->x.size() != this->log_logsd.size()){
if(this->x->size() != this->log_logsd.size()){
/* move error handling to CreateModel in information so not to crash R
Rcpp::stop("the dimensions of the observed and log logsd values from lognormal negative log likelihood do not match");
*/
Expand All @@ -94,7 +94,7 @@ namespace fims_distributions
FIMS_SIMULATE_F(this->of)
{ // preprocessor definition in interface.hpp
// this simulates data that is mean biased
this->x[i] = fims_math::exp(rnorm(logmu[i], logsd[i]));
this->x->at(i) = fims_math::exp(rnorm(logmu[i], logsd[i]));
}
}
#endif
Expand Down
14 changes: 7 additions & 7 deletions inst/include/distributions/functors/normal_lpdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ struct NormalLPDF : public DensityComponentBase<Type> {
* @brief Evaluates the normal probability density function
*/
virtual const Type evaluate(){
this->mu.resize(this->x.size());
this->sd.resize(this->x.size());
this->lpdf_vec.resize(this->x.size());
for(size_t i=0; i<this->x.size(); i++){
this->mu.resize(this->x->size());
this->sd.resize(this->x->size());
this->lpdf_vec.resize(this->x->size());
for(size_t i=0; i<this->x->size(); i++){
if(this->expected_values.size() == 1){
this->mu[i] = this->expected_values[0];
} else {
if(this->x.size() != this->expected_values.size()){
if(this->x->size() != this->expected_values.size()){
/* move error handling to CreateModel in information so not to crash R
Rcpp::stop("the dimensions of the observed and expected values from normal negative log likelihood do not match");
*/
Expand All @@ -63,7 +63,7 @@ struct NormalLPDF : public DensityComponentBase<Type> {
if(log_sd.size() == 1){
sd[i] = fims_math::exp(log_sd[0]);
} else {
if(this->x.size() != this->log_sd.size()){
if(this->x->size() != this->log_sd.size()){
/* move error handling to CreateModel in information so not to crash R
Rcpp::stop("the dimensions of the observed and log sd values from normal negative log likelihood do not match");
*/
Expand All @@ -86,7 +86,7 @@ struct NormalLPDF : public DensityComponentBase<Type> {
lpdf += this->lpdf_vec[i];
if(this->simulate_flag){
FIMS_SIMULATE_F(this->of){
this->x[i] = rnorm(mu[i], sd[i]);
this->x->at(i) = rnorm(mu[i], sd[i]);
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase {

virtual double evaluate() {
fims_distributions::NormalLPDF<double> dnorm;
dnorm.x.resize(this->x.size());
dnorm.x->resize(this->x.size());
dnorm.expected_values.resize(this->expected_values.size());
dnorm.log_sd.resize(this->log_sd.size());
for(int i=0; i<x.size(); i++){
Expand All @@ -124,7 +124,7 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase {

// interface to data/parameter value
distribution->id = this->id_m;
distribution->x.resize(this->x.size());
distribution->x->resize(this->x.size());
for(int i=0; i<this->x.size(); i++){
distribution->x->at(i) = this->x[i].value_m;
}
Expand All @@ -138,7 +138,7 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase {
distribution->log_sd[i] = this->log_sd[i].value_m;
}

info->distribution_models[distribution->id] = distribution;
info->density_components[distribution->id] = distribution;

return true;
}
Expand Down Expand Up @@ -190,7 +190,7 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase {
virtual double evaluate() {
fims_distributions::LogNormalLPDF<double> dlnorm;
dlnorm.input_type = this->input_type;
dlnorm.x.resize(this->x.size());
dlnorm.x->resize(this->x.size());
dlnorm.expected_values.resize(this->expected_values.size());
dlnorm.log_logsd.resize(this->log_logsd.size());
for(int i=0; i<x.size(); i++){
Expand Down Expand Up @@ -218,7 +218,7 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase {
// set relative info
distribution->id = this->id_m;
distribution->input_type = this->input_type;
distribution->x.resize(this->x.size());
distribution->x->resize(this->x.size());
for(int i=0; i<this->x.size(); i++){
distribution->x->at(i) = this->x[i].value_m;
}
Expand All @@ -232,7 +232,7 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase {
distribution->log_logsd[i] = this->log_logsd[i].value_m;
}

info->distribution_models[distribution->id] = distribution;
info->density_components[distribution->id] = distribution;

return true;
}
Expand Down Expand Up @@ -283,7 +283,7 @@ class DmultinomDistributionsInterface : public DistributionsInterfaceBase {
virtual double evaluate() {
fims_distributions::MultinomialLPMF<double> dmultinom;
// Declare TMBVector in this scope
dmultinom.x.resize(this->x.size());
dmultinom.x->resize(this->x.size());
dmultinom.expected_values.resize(this->expected_values.size());
for(int i=0; i<x.size(); i++){
dmultinom.x->at(i) = this->x[i].value_m;
Expand All @@ -308,7 +308,7 @@ class DmultinomDistributionsInterface : public DistributionsInterfaceBase {
std::make_shared<fims_distributions::MultinomialLPMF<Type>>();

distribution->id = this->id_m;
distribution->x.resize(this->x.size());
distribution->x->resize(this->x.size());
for(int i=0; i<this->x.size(); i++){
distribution->x->at(i) = this->x[i].value_m;
}
Expand All @@ -321,7 +321,7 @@ class DmultinomDistributionsInterface : public DistributionsInterfaceBase {
distribution->dims[0] = this->dims[0];
distribution->dims[1] = this->dims[1];

info->distribution_models[distribution->id] = distribution;
info->density_components[distribution->id] = distribution;

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,6 @@ struct RecruitmentBase : public fims_model_object::FIMSObject<Type> {
const Type &spawners,
const Type &ssbzero) = 0; // need to add input parameter values

/** @brief Calculates the negative log likelihood of recruitment deviations.
*
*/
virtual const Type evaluate_lpdf() {
Type lpdf = 0.0; /**< The log probability density function value */

if (!this->estimate_log_recruit_devs) {
return lpdf;
} else {
#ifdef TMB_MODEL
fims_distributions::NormalLPDF<Type> dnorm;
dnorm.x = this->log_recruit_devs;
dnorm.expected_values.resize(this->log_recruit_devs.size());
dnorm.log_sd.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.log_sd[i] = this->log_sigma_recruit[0];
}
lpdf += dnorm.evaluate();
#endif
return lpdf;
}
}

/** @brief Prepare constrained recruitment deviations.
* Based on ADMB sum-to-zero constraint implementation. We still
Expand Down

0 comments on commit b04f6db

Please sign in to comment.