Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name refactoring for tests, growth, and median #503

Merged
merged 5 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/create_rcpp_interface_object.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' model = "LogisticSelectivity",
#' base_class = "SelectivityInterfaceBase",
#' container = "selectivity_models",
#' parameters = c("slope", "median"),
#' parameters = c("slope", "inflection_point"),
#' evaluate_parameter = "x",
#' evaluate_parameter_type = "double"
#' )
Expand Down
29 changes: 14 additions & 15 deletions inst/include/common/fims_math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,16 @@ inline const double log(const double &x) {
/**
* @brief The general logistic function
*
* \f$ \frac{1.0}{ 1.0 + exp(-1.0 * slope (x - median))} \f$
* \f$ \frac{1.0}{ 1.0 + exp(-1.0 * slope (x - inflection_point))} \f$
*
* @param median the median (inflection point) of the logistic function
* @param inflection_point the inflection point of the logistic function
* @param slope the slope of the logistic function
* @param x the index the logistic function should be evaluated at
* @return
*/
template <class Type>
inline const Type logistic(const Type &median, const Type &slope,
const Type &x) {
return (1.0) / (1.0 + exp(-1.0 * slope * (x - median)));
inline const Type logistic(const Type &inflection_point, const Type &slope, const Type &x) {
return (1.0) / (1.0 + exp(-1.0 * slope * (x - inflection_point)));
}

/**
Expand Down Expand Up @@ -138,28 +137,28 @@ inline const Type inv_logit(const Type &a, const Type &b, const Type &logit_x) {
/**
* @brief The general double logistic function
*
* \f$ \frac{1.0}{ 1.0 + exp(-1.0 * slope_{asc} (x - median_{asc}))}
* \left(1-\frac{1.0}{ 1.0 + exp(-1.0 * slope_{desc} (x - median_{desc}))}
* \f$ \frac{1.0}{ 1.0 + exp(-1.0 * slope_{asc} (x - inflection_point_{asc}))}
* \left(1-\frac{1.0}{ 1.0 + exp(-1.0 * slope_{desc} (x - inflection_point_{desc}))}
* \right)\f$
*
* @param median_asc the median (inflection point) of the ascending limb of the
* @param inflection_point_asc the inflection point of the ascending limb of the
* double logistic function
* @param slope_asc the slope of the ascending limb of the double logistic
* function
* @param median_desc the median (inflection point) of the descending limb of
* the double logistic function, where median_desc > median_asc
* @param inflection_point_desc the inflection point of the descending limb of
* the double logistic function, where inflection_point_desc > inflection_point_asc
* @param slope_desc the slope of the descending limb of the double logistic
* function
* @param x the index the logistic function should be evaluated at
* @return
*/

template <class Type>
inline const Type double_logistic(const Type &median_asc, const Type &slope_asc,
const Type &median_desc,
const Type &slope_desc, const Type &x) {
return (1.0) / (1.0 + exp(-1.0 * slope_asc * (x - median_asc))) *
(1.0 - (1.0) / (1.0 + exp(-1.0 * slope_desc * (x - median_desc))));
inline const Type double_logistic(const Type &inflection_point_asc, const Type &slope_asc,
const Type &inflection_point_desc, const Type &slope_desc,
const Type &x) {
return (1.0) / (1.0 + exp(-1.0 * slope_asc * (x - inflection_point_asc))) *
(1.0 - (1.0) / (1.0 + exp(-1.0 * slope_desc * (x - inflection_point_desc))));
}

/**
Expand Down
8 changes: 4 additions & 4 deletions inst/include/interface/rcpp/rcpp_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,23 +418,23 @@ RCPP_MODULE(fims) {

Rcpp::class_<LogisticMaturityInterface>("LogisticMaturity")
.constructor()
.field("median", &LogisticMaturityInterface::median)
.field("inflection_point", &LogisticMaturityInterface::inflection_point)
.field("slope", &LogisticMaturityInterface::slope)
.method("get_id", &LogisticMaturityInterface::get_id)
.method("evaluate", &LogisticMaturityInterface::evaluate);

Rcpp::class_<LogisticSelectivityInterface>("LogisticSelectivity")
.constructor()
.field("median", &LogisticSelectivityInterface::median)
.field("inflection_point", &LogisticSelectivityInterface::inflection_point)
.field("slope", &LogisticSelectivityInterface::slope)
.method("get_id", &LogisticSelectivityInterface::get_id)
.method("evaluate", &LogisticSelectivityInterface::evaluate);

Rcpp::class_<DoubleLogisticSelectivityInterface>("DoubleLogisticSelectivity")
.constructor()
.field("median_asc", &DoubleLogisticSelectivityInterface::median_asc)
.field("inflection_point_asc", &DoubleLogisticSelectivityInterface::inflection_point_asc)
.field("slope_asc", &DoubleLogisticSelectivityInterface::slope_asc)
.field("median_desc", &DoubleLogisticSelectivityInterface::median_desc)
.field("inflection_point_desc", &DoubleLogisticSelectivityInterface::inflection_point_desc)
.field("slope_desc", &DoubleLogisticSelectivityInterface::slope_desc)
.method("get_id", &DoubleLogisticSelectivityInterface::get_id)
.method("evaluate", &DoubleLogisticSelectivityInterface::evaluate);
Expand Down
16 changes: 8 additions & 8 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ std::map<uint32_t, MaturityInterfaceBase*> MaturityInterfaceBase::live_objects;
*/
class LogisticMaturityInterface : public MaturityInterfaceBase {
public:
Parameter median; /**< the index value at which the response reaches .5 */
Parameter slope; /**< the width of the curve at the median */
Parameter inflection_point; /**< the index value at which the response reaches .5 */
Parameter slope; /**< the width of the curve at the inflection_point */

LogisticMaturityInterface() : MaturityInterfaceBase() {}

Expand All @@ -76,7 +76,7 @@ class LogisticMaturityInterface : public MaturityInterfaceBase {
*/
virtual double evaluate(double x) {
fims_popdy::LogisticMaturity<double> LogisticMat;
LogisticMat.median = this->median.value_m;
LogisticMat.inflection_point = this->inflection_point.value_m;
LogisticMat.slope = this->slope.value_m;
return LogisticMat.evaluate(x);
}
Expand All @@ -93,12 +93,12 @@ class LogisticMaturityInterface : public MaturityInterfaceBase {

// set relative info
maturity->id = this->id;
maturity->median = this->median.value_m;
if (this->median.estimated_m) {
if (this->median.is_random_effect_m) {
info->RegisterRandomEffect(maturity->median);
maturity->inflection_point = this->inflection_point.value_m;
if (this->inflection_point.estimated_m) {
if (this->inflection_point.is_random_effect_m) {
info->RegisterRandomEffect(maturity->inflection_point);
} else {
info->RegisterParameter(maturity->median);
info->RegisterParameter(maturity->inflection_point);
}
}
maturity->slope = this->slope.value_m;
Expand Down
48 changes: 24 additions & 24 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ std::map<uint32_t, SelectivityInterfaceBase*>
*/
class LogisticSelectivityInterface : public SelectivityInterfaceBase {
public:
Parameter median; /**< the index value at which the response reaches .5 */
Parameter slope; /**< the width of the curve at the median */
Parameter inflection_point; /**< the index value at which the response reaches .5 */
Parameter slope; /**< the width of the curve at the inflection_point */

LogisticSelectivityInterface() : SelectivityInterfaceBase() {}

Expand All @@ -76,7 +76,7 @@ class LogisticSelectivityInterface : public SelectivityInterfaceBase {
*/
virtual double evaluate(double x) {
fims_popdy::LogisticSelectivity<double> LogisticSel;
LogisticSel.median = this->median.value_m;
LogisticSel.inflection_point = this->inflection_point.value_m;
LogisticSel.slope = this->slope.value_m;
return LogisticSel.evaluate(x);
}
Expand All @@ -93,12 +93,12 @@ class LogisticSelectivityInterface : public SelectivityInterfaceBase {

// set relative info
selectivity->id = this->id;
selectivity->median = this->median.value_m;
if (this->median.estimated_m) {
if (this->median.is_random_effect_m) {
info->RegisterRandomEffect(selectivity->median);
selectivity->inflection_point = this->inflection_point.value_m;
if (this->inflection_point.estimated_m) {
if (this->inflection_point.is_random_effect_m) {
info->RegisterRandomEffect(selectivity->inflection_point);
} else {
info->RegisterParameter(selectivity->median);
info->RegisterParameter(selectivity->inflection_point);
}
}
selectivity->slope = this->slope.value_m;
Expand Down Expand Up @@ -136,11 +136,11 @@ class LogisticSelectivityInterface : public SelectivityInterfaceBase {
*/
class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase {
public:
Parameter median_asc; /**< the index value at which the response reaches .5 */
Parameter slope_asc; /**< the width of the curve at the median */
Parameter inflection_point_asc; /**< the index value at which the response reaches .5 */
Parameter slope_asc; /**< the width of the curve at the inflection_point */
Parameter
median_desc; /**< the index value at which the response reaches .5 */
Parameter slope_desc; /**< the width of the curve at the median */
inflection_point_desc; /**< the index value at which the response reaches .5 */
Parameter slope_desc; /**< the width of the curve at the inflection_point */

DoubleLogisticSelectivityInterface() : SelectivityInterfaceBase() {}

Expand All @@ -155,9 +155,9 @@ class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase {
*/
virtual double evaluate(double x) {
fims_popdy::DoubleLogisticSelectivity<double> DoubleLogisticSel;
DoubleLogisticSel.median_asc = this->median_asc.value_m;
DoubleLogisticSel.inflection_point_asc = this->inflection_point_asc.value_m;
DoubleLogisticSel.slope_asc = this->slope_asc.value_m;
DoubleLogisticSel.median_desc = this->median_desc.value_m;
DoubleLogisticSel.inflection_point_desc = this->inflection_point_desc.value_m;
DoubleLogisticSel.slope_desc = this->slope_desc.value_m;
return DoubleLogisticSel.evaluate(x);
}
Expand All @@ -174,12 +174,12 @@ class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase {

// set relative info
selectivity->id = this->id;
selectivity->median_asc = this->median_asc.value_m;
if (this->median_asc.estimated_m) {
if (this->median_asc.is_random_effect_m) {
info->RegisterRandomEffect(selectivity->median_asc);
selectivity->inflection_point_asc = this->inflection_point_asc.value_m;
if (this->inflection_point_asc.estimated_m) {
if (this->inflection_point_asc.is_random_effect_m) {
info->RegisterRandomEffect(selectivity->inflection_point_asc);
} else {
info->RegisterParameter(selectivity->median_asc);
info->RegisterParameter(selectivity->inflection_point_asc);
}
}
selectivity->slope_asc = this->slope_asc.value_m;
Expand All @@ -190,12 +190,12 @@ class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase {
info->RegisterParameter(selectivity->slope_asc);
}
}
selectivity->median_desc = this->median_desc.value_m;
if (this->median_desc.estimated_m) {
if (this->median_desc.is_random_effect_m) {
info->RegisterRandomEffect(selectivity->median_desc);
selectivity->inflection_point_desc = this->inflection_point_desc.value_m;
if (this->inflection_point_desc.estimated_m) {
if (this->inflection_point_desc.is_random_effect_m) {
info->RegisterRandomEffect(selectivity->inflection_point_desc);
} else {
info->RegisterParameter(selectivity->median_desc);
info->RegisterParameter(selectivity->inflection_point_desc);
}
}
selectivity->slope_desc = this->slope_desc.value_m;
Expand Down
11 changes: 6 additions & 5 deletions inst/include/population_dynamics/maturity/functors/logistic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ namespace fims_popdy {
*/
template <typename Type>
struct LogisticMaturity : public MaturityBase<Type> {
Type median; /*!< 50% quantile of the value of the quantity of interest (x);
Type inflection_point; /*!< 50% quantile of the value of the quantity of interest (x);
e.g. age at which 50% of the fish are mature */
Type slope; /*!<scalar multiplier of difference between quantity of interest
value (x) and median */
value (x) and inflection_point */

LogisticMaturity() : MaturityBase<Type>() {}

/**
* @brief Method of the logistic maturity class that implements the
* logistic function from FIMS math.
*
* \f[ \frac{1.0}{ 1.0 + exp(-1.0 * slope (x - median))} \f]
* \f[ \frac{1.0}{ 1.0 + exp(-1.0 * slope (x - inflection_point))} \f]
*
* @param x The independent variable in the logistic function (e.g., age or
* size at maturity).
*/
virtual const Type evaluate(const Type& x) {
return fims_math::logistic<Type>(median, slope, x);

virtual const Type evaluate(const Type & x) {
return fims_math::logistic<Type>(inflection_point, slope, x);
}
};

Expand Down
21 changes: 11 additions & 10 deletions inst/include/population_dynamics/population/population.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ struct Population : public fims_model_object::FIMSObject<Type> {

// Transformation Section
for (size_t age = 0; age < this->nages; age++) {
this->weight_at_age[age] = growth->evaluate(ages[age]);
Bai-Li-NOAA marked this conversation as resolved.
Show resolved Hide resolved
for (size_t year = 0; year < this->nyears; year++) {
size_t i_age_year = age * this->nyears + year;
this->M[i_age_year] = fims_math::exp(this->log_M[i_age_year]);
Expand Down Expand Up @@ -286,9 +287,9 @@ struct Population : public fims_model_object::FIMSObject<Type> {
*/
void CalculateBiomass(size_t i_age_year, size_t year, size_t age) {
this->biomass[year] +=
this->numbers_at_age[i_age_year] * growth->evaluate(ages[age]);
this->numbers_at_age[i_age_year] * this->weight_at_age[age];
POPULATION_LOG << " age " << ages[age] << std::endl;
POPULATION_LOG << "growth evaluate: " << growth->evaluate(ages[age])
POPULATION_LOG << "growth evaluate: " << this->weight_at_age[age]
<< " biomass inputs----- +++\n";
}

Expand All @@ -302,7 +303,7 @@ struct Population : public fims_model_object::FIMSObject<Type> {
*/
void CalculateUnfishedBiomass(size_t i_age_year, size_t year, size_t age) {
this->unfished_biomass[year] += this->unfished_numbers_at_age[i_age_year] *
this->growth->evaluate(ages[age]);
this->weight_at_age[age];
}

/**
Expand All @@ -316,12 +317,12 @@ struct Population : public fims_model_object::FIMSObject<Type> {
this->spawning_biomass[year] += this->proportion_female *
this->numbers_at_age[i_age_year] *
this->proportion_mature_at_age[i_age_year] *
growth->evaluate(ages[age]);
this->weight_at_age[age];
POPULATION_LOG << " proportion female " << this->proportion_female << " "
<< " mature age " << age << " is "
<< this->proportion_mature_at_age[i_age_year] << " "
<< " numbers at age " << this->numbers_at_age[i_age_year] << " "
<< " growth " << growth->evaluate(ages[age]) << " "
<< " growth " << this->weight_at_age[age] << " "
<< " spawning biomass " << this->spawning_biomass[year] << " "
<< " spawning biomass inputs----- +++\n";
}
Expand All @@ -339,7 +340,7 @@ struct Population : public fims_model_object::FIMSObject<Type> {
this->unfished_spawning_biomass[year] +=
this->proportion_female * this->unfished_numbers_at_age[i_age_year] *
this->proportion_mature_at_age[i_age_year] *
this->growth->evaluate(ages[age]);
this->weight_at_age[age];
}

/**
Expand Down Expand Up @@ -438,13 +439,13 @@ struct Population : public fims_model_object::FIMSObject<Type> {
// I = qN (N is total numbers), I is an index in numbers
if (this->fleets[fleet_]->is_survey == false) {
index_ = this->fleets[fleet_]->catch_numbers_at_age[i_age_year] *
growth->evaluate(ages[age]);
this->weight_at_age[age];
} else {
POPULATION_LOG << "fleet " << fleet_ << " is a survey" << std::endl;
index_ = this->fleets[fleet_]->q *
this->fleets[fleet_]->selectivity->evaluate(ages[age]) *
this->numbers_at_age[i_age_year] *
growth->evaluate(ages[age]); // this->weight_at_age[age];
this->weight_at_age[age]; // this->weight_at_age[age];
}
fleets[fleet_]->expected_index[year] += index_;
POPULATION_LOG << " expected index in year " << year << " is "
Expand Down Expand Up @@ -503,10 +504,10 @@ struct Population : public fims_model_object::FIMSObject<Type> {
POPULATION_LOG << " fleet " << fleet_ << std::endl;
POPULATION_LOG << " catchnaa "
<< this->fleets[fleet_]->catch_numbers_at_age[year] << std::endl;
POPULATION_LOG << " weight " << this->growth->evaluate(ages[age]) << std::endl;
POPULATION_LOG << " weight " << this->weight_at_age[age] << std::endl;
this->fleets[fleet_]->catch_weight_at_age[i_age_year] =
this->fleets[fleet_]->catch_numbers_at_age[i_age_year] *
this->growth->evaluate(ages[age]); // this->weight_at_age[age];
this->weight_at_age[age];
POPULATION_LOG << " catch_waa "
<< this->fleets[fleet_]->catch_weight_at_age[i_age_year]
<< std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ namespace fims_popdy {
*/
template <typename Type>
struct DoubleLogisticSelectivity : public SelectivityBase<Type> {
Type median_asc; /*!< 50% quantile of the value of the quantity of interest
Type inflection_point_asc; /*!< 50% quantile of the value of the quantity of interest
(x) on the ascending limb of the double logistic curve; e.g. age
at which 50% of the fish are selected */
Type slope_asc; /*!<scalar multiplier of difference between quantity of
interest value (x) and median on the ascending limb of the
interest value (x) and inflection_point on the ascending limb of the
double logistic curve*/
Type median_desc; /*!< 50% quantile of the value of the quantity of interest
Type inflection_point_desc; /*!< 50% quantile of the value of the quantity of interest
(x) on the descending limb of the double logistic curve; e.g.
age at which 50% of the fish are selected */
Type slope_desc; /*!<scalar multiplier of difference between quantity of
interest value (x) and median on the descending limb of the
interest value (x) and inflection_point on the descending limb of the
double logistic curve */

DoubleLogisticSelectivity() : SelectivityBase<Type>() {}
Expand All @@ -39,16 +39,16 @@ struct DoubleLogisticSelectivity : public SelectivityBase<Type> {
* @brief Method of the double logistic selectivity class that implements the
* double logistic function from FIMS math.
*
* \f$ \frac{1.0}{ 1.0 + exp(-1.0 * slope_{asc} (x - median_{asc}))}
* \left(1.0-\frac{1.0}{ 1.0 + exp(-1.0 * slope_{desc} (x - median_{desc}))}
* \f$ \frac{1.0}{ 1.0 + exp(-1.0 * slope_{asc} (x - inflection_point_{asc}))}
* \left(1.0-\frac{1.0}{ 1.0 + exp(-1.0 * slope_{desc} (x - inflection_point_{desc}))}
* \right)\f$
*
* @param x The independent variable in the double logistic function (e.g.,
* age or size in selectivity).
*/
virtual const Type evaluate(const Type &x) {
return fims_math::double_logistic<Type>(median_asc, slope_asc, median_desc,
slope_desc, x);
return fims_math::double_logistic<Type>(inflection_point_asc, slope_asc, inflection_point_desc,
slope_desc, x);
}
};

Expand Down
Loading
Loading