Skip to content

Commit

Permalink
fix(proportion_female): Removes faulty est input
Browse files Browse the repository at this point in the history
proportion_female appeared to be estimable and/or specified by the user
but it was still hard coded to 0.5 for every age. This commit reverts
back part of PR #543 where the hard-coded value (not by age) was made
0.5 by age with a user interface.

Part of #638
  • Loading branch information
kellijohnson-NOAA committed Aug 31, 2024
1 parent 13307d1 commit 6eb54ec
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 54 deletions.
2 changes: 0 additions & 2 deletions inst/include/interface/rcpp/rcpp_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,7 @@ RCPP_MODULE(fims) {
.field("nyears", &PopulationInterface::nyears)
.field("log_M", &PopulationInterface::log_M)
.field("log_init_naa", &PopulationInterface::log_init_naa)
.field("proportion_female", &PopulationInterface::proportion_female)
.field("ages", &PopulationInterface::ages)
.field("estimate_prop_female", &PopulationInterface::estimate_prop_female)
.method("evaluate", &PopulationInterface::evaluate)
.method("SetMaturity", &PopulationInterface::SetMaturity)
.method("SetGrowth", &PopulationInterface::SetGrowth)
Expand Down
9 changes: 0 additions & 9 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_population.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ class PopulationInterface : public PopulationInterfaceBase {
ParameterVector log_init_naa; /**<log of the initial numbers at age*/
ParameterVector numbers_at_age; /**<log of the initial numbers at age*/
Rcpp::NumericVector ages; /**<vector of ages in the population; length nages*/
Rcpp::NumericVector proportion_female; /**<doule representing the proportion
of female individuals */
bool estimate_prop_female; /**<whether proportion female should be estimated*/

PopulationInterface() : PopulationInterfaceBase() {}

Expand Down Expand Up @@ -150,12 +147,6 @@ class PopulationInterface : public PopulationInterfaceBase {
for (int i = 0; i < ages.size(); i++) {
population->ages[i] = this->ages[i];
}
for (int i = 0; i < proportion_female.size(); i++) {
population->proportion_female[i] = this->proportion_female[i];
if (estimate_prop_female) {
info->RegisterParameter(population->proportion_female[i]);
}
}

population->numbers_at_age.resize((nyears + 1) * nages);
info->variable_map[this->numbers_at_age.id_m] = &(population)->numbers_at_age;
Expand Down
19 changes: 9 additions & 10 deletions inst/include/population_dynamics/population/population.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ struct Population : public fims_model_object::FIMSObject<Type> {
size_t nages; /*!< total number of ages in the population*/
size_t nfleets; /*!< total number of fleets in the fishery*/

// constants
const Type proportion_female = 0.5; /*!< Sex proportion fixed at 50/50*/

// parameters are estimated; after initialize in create_model, push_back to
// parameter list - in information.hpp (same for initial F in fleet)
fims::Vector<Type>
log_init_naa; /*!< estimated parameter: log numbers at age*/
fims::Vector<Type> log_M; /*!< estimated parameter: log Natural Mortality*/
fims::Vector<Type>
proportion_female; /*!< estimated parameter: proportion female by age */

// Transformed values
fims::Vector<Type> M; /*!< transformed parameter: Natural Mortality*/
Expand Down Expand Up @@ -131,7 +132,6 @@ struct Population : public fims_model_object::FIMSObject<Type> {
mortality_F.resize(nyears * nages);
mortality_Z.resize(nyears * nages);
proportion_mature_at_age.resize((nyears + 1) * nages);
proportion_female.resize(nages);
weight_at_age.resize(nages);
unfished_numbers_at_age.resize((nyears + 1) * nages);
biomass.resize((nyears + 1));
Expand Down Expand Up @@ -169,7 +169,6 @@ struct Population : public fims_model_object::FIMSObject<Type> {
std::fill(proportion_mature_at_age.begin(), proportion_mature_at_age.end(),
0.0);
std::fill(mortality_Z.begin(), mortality_Z.end(), 0.0);
std::fill(proportion_female.begin(), proportion_female.end(), 0.5);

// Transformation Section
for (size_t age = 0; age < this->nages; age++) {
Expand Down Expand Up @@ -315,9 +314,9 @@ struct Population : public fims_model_object::FIMSObject<Type> {
*/
void CalculateSpawningBiomass(size_t i_age_year, size_t year, size_t age) {
this->spawning_biomass[year] +=
this->proportion_female[age] * this->numbers_at_age[i_age_year] *
this->proportion_female * this->numbers_at_age[i_age_year] *
this->proportion_mature_at_age[i_age_year] * this->weight_at_age[age];
POPULATION_LOG << " proportion female " << this->proportion_female[age]
POPULATION_LOG << " proportion female " << this->proportion_female
<< " "
<< " mature age " << age << " is "
<< this->proportion_mature_at_age[i_age_year] << " "
Expand All @@ -340,7 +339,7 @@ struct Population : public fims_model_object::FIMSObject<Type> {
void CalculateUnfishedSpawningBiomass(size_t i_age_year, size_t year,
size_t age) {
this->unfished_spawning_biomass[year] +=
this->proportion_female[age] *
this->proportion_female *
this->unfished_numbers_at_age[i_age_year] *
this->proportion_mature_at_age[i_age_year] * this->weight_at_age[age];
}
Expand All @@ -353,12 +352,12 @@ struct Population : public fims_model_object::FIMSObject<Type> {
Type CalculateSBPR0() {
std::vector<Type> numbers_spr(this->nages, 1.0);
Type phi_0 = 0.0;
phi_0 += numbers_spr[0] * this->proportion_female[0] *
phi_0 += numbers_spr[0] * this->proportion_female *
this->proportion_mature_at_age[0] *
this->growth->evaluate(ages[0]);
for (size_t a = 1; a < (this->nages - 1); a++) {
numbers_spr[a] = numbers_spr[a - 1] * fims_math::exp(-this->M[a]);
phi_0 += numbers_spr[a] * this->proportion_female[a] *
phi_0 += numbers_spr[a] * this->proportion_female *
this->proportion_mature_at_age[a] *
this->growth->evaluate(ages[a]);
}
Expand All @@ -367,7 +366,7 @@ struct Population : public fims_model_object::FIMSObject<Type> {
(numbers_spr[nages - 2] * fims_math::exp(-this->M[nages - 2])) /
(1 - fims_math::exp(-this->M[this->nages - 1]));
phi_0 += numbers_spr[this->nages - 1] *
this->proportion_female[this->nages - 1] *
this->proportion_female *
this->proportion_mature_at_age[this->nages - 1] *
this->growth->evaluate(ages[this->nages - 1]);
return phi_0;
Expand Down
4 changes: 2 additions & 2 deletions tests/gtest/test_population_B_and_SB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace
std::vector<double> test_SB(nyears + 1, 0);
std::vector<double> test_B(nyears + 1, 0);

test_SB[year] += population.numbers_at_age[i_age_year] * population.proportion_female[age] * population.proportion_mature_at_age[i_age_year] *
test_SB[year] += population.numbers_at_age[i_age_year] * population.proportion_female * population.proportion_mature_at_age[i_age_year] *
population.growth->evaluate(population.ages[age]);
test_B[year] += population.numbers_at_age[i_age_year] *
population.growth->evaluate(population.ages[age]);
Expand All @@ -40,7 +40,7 @@ namespace

std::vector<double> test_SSB(nyears + 1, 0);

test_SSB[nyears] += population.numbers_at_age[i_age_year] * population.proportion_female[age] *
test_SSB[nyears] += population.numbers_at_age[i_age_year] * population.proportion_female *
population.proportion_mature_at_age[i_age_year] *
population.growth->evaluate(population.ages[age]);

Expand Down
2 changes: 1 addition & 1 deletion tests/gtest/test_population_Unfished_Initial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ namespace
population.CalculateUnfishedSpawningBiomass(i_age_year, year, age);

test_unfished_spawning_biomass[year] += population.proportion_mature_at_age[i_age_year] *
population.proportion_female[age] *
population.proportion_female *
test_unfished_numbers_at_age[i_age_year] *
population.growth->evaluate(population.ages[age]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ namespace
EXPECT_EQ(population.unfished_spawning_biomass.size(), (nyears + 1));
EXPECT_EQ(population.spawning_biomass.size(), nyears + 1);
EXPECT_EQ(population.log_init_naa.size(), nages);
EXPECT_EQ(population.proportion_female.size(), nages);
EXPECT_EQ(population.log_M.size(), nyears * nages);
EXPECT_EQ(population.M.size(), nyears * nages);
}
Expand Down Expand Up @@ -85,13 +84,6 @@ namespace
}
EXPECT_EQ(population.M.size(), nyears * nages);

// Test population.proportion_female
fims::Vector<double> p_female(nages, 0.5);
for(int i = 0; i < nages; i++)
{
EXPECT_EQ(population.proportion_female[i], p_female[i]);
}

// Test population.fleet->Fmort
// fmort and logfmort are vectors of length year
fims::Vector<double> Fmort(nfleets * nyears, 0);
Expand Down
18 changes: 0 additions & 18 deletions tests/gtest/test_population_test_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,6 @@ class PopulationEvaluateTestFixture : public testing::Test {
population.log_init_naa[i] = log_naa_distribution(generator);
}

// prop_female
double prop_female_min = 0.1;
double prop_female_max = 0.9;
std::uniform_real_distribution<double> prop_female_distribution(
prop_female_min, prop_female_max);
for (int i = 0; i < nages; i++) {
population.proportion_female[i] = prop_female_distribution(generator);
}

// log_M
double log_M_min = fims_math::log(0.1);
double log_M_max = fims_math::log(0.3);
Expand Down Expand Up @@ -277,15 +268,6 @@ class PopulationPrepareTestFixture : public testing::Test {
population.log_init_naa[i] = log_naa_distribution(generator);
}

// prop_female
double prop_female_min = 0.1;
double prop_female_max = 0.9;
std::uniform_real_distribution<double> prop_female_distribution(
prop_female_min, prop_female_max);
for (int i = 0; i < nages; i++) {
population.proportion_female[i] = prop_female_distribution(generator);
}

// log_M
double log_M_min = fims_math::log(0.1);
double log_M_max = fims_math::log(0.3);
Expand Down
6 changes: 2 additions & 4 deletions tests/testthat/test-rcpp-population-interface.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ test_that("Population input settings work as expected", {
population$nfleets <- 2
population$nseasons <- 1
population$nyears <- nyears
population$proportion_female <- rep(0.5, nages)
population$estimate_prop_female <- FALSE
population$proportion_female <- 0.5

expect_equal(population$get_id(), 1)
for(i in 1:(nyears * nages)){
Expand All @@ -23,8 +22,7 @@ test_that("Population input settings work as expected", {
expect_equal(population$log_init_naa[i]$value, 0)
expect_true(population$log_init_naa[i]$estimated)
}
expect_false(population$estimate_prop_female)
expect_equal(population$proportion_female, rep(0.5, nages))
expect_equal(population$proportion_female, 0.5)

clear()
})

0 comments on commit 6eb54ec

Please sign in to comment.