diff --git a/inst/extdata/TMB_tests/distributions/test_fleet_acomp_nll.cpp b/inst/extdata/TMB_tests/distributions/test_fleet_acomp_nll.cpp deleted file mode 100644 index 69045c891..000000000 --- a/inst/extdata/TMB_tests/distributions/test_fleet_acomp_nll.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/*! \file test_fleet_acomp_nll.cpp - * links TMB to test_fleet_acomp_nll.hpp - */ - -#include "test_fleet_acomp_nll.hpp" - -template -Type objective_function::operator()(){ - /* - * create pointer, inst, that points to singleton class of Model in test_fleet_acomp_nll.hpp - * getinstance is defined in test_fleet_acomp_nll.hpp - */ - fims::Model* inst = fims::Model::getInstance(); - - DATA_VECTOR(x); - PARAMETER_VECTOR(p); - /* - * access and assign members of Model class using inst pointer - */ - inst -> of = this; - inst -> x = x; - inst -> p = p; - /* - * create Type nll and assign value to the return of the - * evaluate() function defined in test_fleet_acomp_nll.hpp - */ - Type nll = inst -> evaluate(); - - return nll; -} diff --git a/inst/extdata/TMB_tests/distributions/test_fleet_acomp_nll.hpp b/inst/extdata/TMB_tests/distributions/test_fleet_acomp_nll.hpp deleted file mode 100644 index fc3ed997a..000000000 --- a/inst/extdata/TMB_tests/distributions/test_fleet_acomp_nll.hpp +++ /dev/null @@ -1,98 +0,0 @@ - /*! \file test_dmultinom_distribution.hpp - * Specifies the negative log-likelihood of - * the dmultinom distribution given data and parameters. - * Creates singleton class that links this .hpp with - * the test_dmultinom_distribution.cpp TMB verison of the model - */ - #ifndef TEST_FLEET_ACOMP_NLL_HPP - #define TEST_FLEET_ACOMP_NLL_HPP - - #include "../../../include/population_dynamics/fleet/fleet_nll.hpp" - #include "../../../include/interface/interface.hpp" - - namespace fims { - - /** - * @brief Model class defines and returns negative log-likelihood (nll) - * - * @tparam Type - */ - template - class Model { - - using Vector = typename ModelTraits::EigenVector; - public: - Vector x; /*!< Vector of length K of integers */ - Vector p; /*!< Vector of length K, specifying the probability for the K classes (note, unlike in R these must sum to 1). */ - - // Initiate pointer to link .cpp to .hpp - static Model* instance; - - - ::objective_function *of; - - /** @brief Constructor. - */ - Model(){} - - /** - * @brief Create new singleton class - * - * @return Model* - */ - static Model* getInstance(){ - return Model::instance; - } - - /** - * @brief Function that calculates the negative log-likelihood given the data and parameters - * - * @return negative log-likelihood (nll) - */ - Type evaluate(){ - - Type nll = 0.0; - int n = x.size(); - - fims::FleetAgeCompNLL nll_fac; - nll_fac.catch_numbers_at_age.resize(n); - nll_fac.nyears = 1; - nll_fac.nages = 10; - std::shared_ptr> age_comp_data = - std::make_shared>(10, 1); - - nll_fac.observed_agecomp_data = age_comp_data; - Vector obs; - Vector p_set; - obs.resize(n); - p_set.resize(n); - for(int i =0; i < n; i++){ - nll_fac.observed_agecomp_data->at(0, i); - nll_fac.catch_numbers_at_age[i] = p[i]; - - obs[i] = nll_fac.observed_agecomp_data->at(0,i); - p_set[i] = nll_fac.catch_numbers_at_age[i]; - } - - - REPORT_F(obs, of); - REPORT_F(p_set, of); - REPORT_F(n, of); - nll = nll_fac.evaluate(); - return nll; - } - }; - - /** - * @brief Create new instance of Model - * - * @tparam Type - */ - template - Model* Model::instance = new Model(); - } - - - #endif /* TEST_DMULTINOM_DISTRIBUTION_HPP */ - - diff --git a/inst/extdata/TMB_tests/distributions/test_fleet_index_nll.cpp b/inst/extdata/TMB_tests/distributions/test_fleet_index_nll.cpp deleted file mode 100644 index c6953abc7..000000000 --- a/inst/extdata/TMB_tests/distributions/test_fleet_index_nll.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/*! \file test_fleet_nll.cpp - * links TMB to test_fleet_nll.hpp - */ - -#include "test_fleet_index_nll.hpp" - -template -Type objective_function::operator()(){ - /* - * create pointer, inst, that points to singleton class of Model in test_dlnorm_distribution.hpp - * getinstance is defined in test_dlnorm_distribution.hpp - */ - fims::Model* inst = fims::Model::getInstance(); - - DATA_VECTOR(y); - PARAMETER_VECTOR(p); - PARAMETER(log_sd) - - /* - * access and assign members of Model class using inst pointer - */ - inst->mean = p; - inst -> y = y; - inst -> logsd = log_sd; - inst -> of = this; - /* - * create Type nll and assign value to the return of the - * evaluate() function defined in test_dlnorm_distribution.hpp - */ - Type nll = inst -> evaluate(); - return nll; -} diff --git a/inst/extdata/TMB_tests/distributions/test_fleet_index_nll.hpp b/inst/extdata/TMB_tests/distributions/test_fleet_index_nll.hpp deleted file mode 100644 index 8554d9a56..000000000 --- a/inst/extdata/TMB_tests/distributions/test_fleet_index_nll.hpp +++ /dev/null @@ -1,92 +0,0 @@ - /*! \file test_dlnorm_distribution.hpp - * Specifies the negative log-likelihood of - * the dlnorm distribution given data and parameters. - * Creates singleton class that links this .hpp with - * the test_dlnorm_distribution.cpp TMB verison of the model - */ - #ifndef TEST_FLEET_INDEX_HPP - #define TEST_FLEET_INDEX_HPP - - #include "../../../include/population_dynamics/fleet/fleet_nll.hpp" - #include "../../../include/interface/interface.hpp" - - namespace fims { - - /** - * @brief Model class defines and returns negative log-likelihood (nll) - * - * @tparam Type - */ - template - class Model { - using DataVector = typename ModelTraits::DataVector; - using Vector = typename ModelTraits::EigenVector; - public: - DataVector y; /*!< observation */ - Vector mean; /*!< expected fleet index */ - Type logsd; /*!< standard deviation of the fleet observation error, must be strictly positive.*/ - - // Initiate pointer to link .cpp to .hpp - static Model* instance; - - ::objective_function *of; - - /** @brief Constructor. - */ - Model(){} - - /** - * @brief Create new singleton class - * - * @return Model* - */ - static Model* getInstance(){ - return Model::instance; - } - - /** - * @brief Function that calculates the negative log-likelihood given the data and parameters - * - * @return negative log-likelihood (nll) - */ - Type evaluate(){ - - Type nll = 0; - int n = y.size(); - - fims::FleetIndexNLL nll_fleet_index; - nll_fleet_index.log_obs_error = logsd; - - std::shared_ptr> index_data = - std::make_shared>(n); - nll_fleet_index.observed_index_data = index_data; - - nll_fleet_index.expected_index.resize(n); - nll_fleet_index.expected_index = mean; - Vector temp; - temp.resize(n); - for(int i =0; i < n; i++){ - nll_fleet_index.observed_index_data->at(i); - temp[i] = nll_fleet_index.observed_index_data->at(i); - nll = nll_fleet_index.evaluate(); - } - REPORT_F(mean, of); - REPORT_F(temp, of); - - return nll; - } - }; - - /** - * @brief Create new instance of Model - * - * @tparam Type - */ - template - Model* Model::instance = new Model(); - } - - - #endif /* TEST_FLEET_NLL_HPP */ - - diff --git a/inst/extdata/TMB_tests/distributions/test_fleet_nll.R b/inst/extdata/TMB_tests/distributions/test_fleet_nll.R deleted file mode 100644 index 9574925f5..000000000 --- a/inst/extdata/TMB_tests/distributions/test_fleet_nll.R +++ /dev/null @@ -1,80 +0,0 @@ -# open FIMS.proj and run locally from console using: -# source('inst/extdata/TMB_tests/distributions/test_distributions.R') - -library(testthat) -library(TMB) - -# project_path <- find.package("FIMS") -# old_wd <- getwd() -# setwd(project_path) - -project_path <- getwd() - -# if (!file.exists(file.path(project_path, "inst"))) { -# path <- file.path("extdata", "TMB_tests", "distributions") -# } else { - path <- file.path("inst", "extdata", "TMB_tests", "distributions") -# } - - -# compile test .cpp files from inst/extdata/TMB_tests/distributions -TMB::compile(paste0(path, "/test_fleet_index_nll.cpp"), flags = "-O1 -g -DTMB_MODEL", DLLFLAGS="") -TMB::compile(paste0(path, "/test_fleet_acomp_nll.cpp"), flags = "-O1 -g -DTMB_MODEL", DLLFLAGS="") - -test_that("fleet index nll unit test", { - - # setwd(project_path) - # on.exit(setwd(old_wd), add = TRUE) - - # # dmultinom unit test - # # load test - dyn.load(dynlib(paste0(path, "/test_fleet_index_nll"))) - - set.seed(123) - #Simulate new data with R - - #Simulate new data with R - y = stats::rlnorm(10, 2, 1) - #Calculate negative log-likelihood with R dlnorm - nll = -sum(stats::dnorm(log(y), log(2), 1, TRUE)) - - #Initialize TMB model object with true values - mod = MakeADFun(data = list(y = y), - parameters = list(p = rep(2,10), log_sd = log(1)), - DLL = "test_fleet_index_nll") - #Compare R nll to TMB nll - expect_equal(nll, mod$fn()) - dyn.unload(dynlib(paste0(path, "/test_fleet_index_nll"))) - file.remove(paste0(path, "/", dynlib("test_fleet_index_nll"))) - file.remove(paste0(path, "/test_fleet_index_nll.o")) - -}) - -test_that("fleet acomp nll unit test", { - - # setwd(project_path) - # on.exit(setwd(old_wd), add = TRUE) - - # # dmultinom unit test - # # load test - dyn.load(dynlib(paste0(path, "/test_fleet_acomp_nll"))) - - set.seed(123) - p = (1:10)/sum(1:10) - x = stats::rmultinom(1, 100, p) - - #Calculate negative log-likelihood with R dnmultinom - nll = -stats::dmultinom(x, 100,p, TRUE) - - #Initialize TMB model object with true values - mod = MakeADFun(data = list(x = x), - parameters = list(p = p), - DLL = "test_fleet_acomp_nll") - #Compare R nll to TMB nll - expect_equal(nll, mod$fn()) - - dyn.unload(dynlib(file.path(path, "test_fleet_acomp_nll"))) - file.remove(file.path(path, dynlib("test_fleet_acomp_nll"))) - file.remove(file.path(path, "test_fleet_acomp_nll.o")) - -})