78class VonBertalanffySchnuteGrowthModelAdapter
79 :
public GrowthDerivedObservationBase<Type> {
81 VonBertalanffySchnuteGrowthModelAdapter()
82 : GrowthDerivedObservationBase<Type>() {}
88 kEstimatedReferenceLengths = 0,
89 kConstantLengthYoungEstimatedLengthOld,
90 kEstimatedLengthYoungBelowConstantLengthOld,
98 length_reference_parameterization_ =
99 LengthReferenceParameterization::kEstimatedReferenceLengths;
100 growth_products_prepared_ =
false;
110 Type constant_mean_length_young) {
111 length_reference_parameterization_ =
112 LengthReferenceParameterization::kConstantLengthYoungEstimatedLengthOld;
113 constant_mean_length_young_ = constant_mean_length_young;
114 growth_products_prepared_ =
false;
124 Type constant_mean_length_old) {
125 length_reference_parameterization_ = LengthReferenceParameterization::
126 kEstimatedLengthYoungBelowConstantLengthOld;
127 constant_mean_length_old_ = constant_mean_length_old;
128 growth_products_prepared_ =
false;
140 Type constant_mean_length_old) {
141 length_reference_parameterization_ =
142 LengthReferenceParameterization::kBothConstant;
143 constant_mean_length_young_ = constant_mean_length_young;
144 constant_mean_length_old_ = constant_mean_length_old;
145 growth_products_prepared_ =
false;
154 use_param_vectors_ =
true;
155 vb_params_set_ =
true;
156 growth_products_prepared_ =
false;
157 return mean_length_young_vector_;
167 use_param_vectors_ =
true;
168 vb_params_set_ =
true;
169 growth_products_prepared_ =
false;
170 return mean_length_old_vector_;
179 use_param_vectors_ =
true;
180 vb_params_set_ =
true;
181 growth_products_prepared_ =
false;
182 return growth_coefficient_vector_;
190 use_param_vectors_ =
true;
191 vb_params_set_ =
true;
192 growth_products_prepared_ =
false;
193 return reference_age_for_length_young_vector_;
201 use_param_vectors_ =
true;
202 vb_params_set_ =
true;
203 growth_products_prepared_ =
false;
204 return reference_age_for_length_old_vector_;
212 use_param_vectors_ =
true;
213 lw_params_set_ =
true;
214 growth_products_prepared_ =
false;
215 return length_weight_a_vector_;
223 use_param_vectors_ =
true;
224 lw_params_set_ =
true;
225 growth_products_prepared_ =
false;
226 return length_weight_b_vector_;
234 use_param_vectors_ =
true;
235 growth_products_prepared_ =
false;
236 return length_at_age_sd_young_vector_;
244 use_param_vectors_ =
true;
245 growth_products_prepared_ =
false;
246 return length_at_age_sd_old_vector_;
254 use_param_vectors_ =
true;
255 growth_products_prepared_ =
false;
256 return log_sd_mean_length_young_vector_;
264 use_param_vectors_ =
true;
265 growth_products_prepared_ =
false;
266 return log_sd_mean_length_old_vector_;
274 use_param_vectors_ =
true;
275 growth_products_prepared_ =
false;
276 return log_sd_growth_coefficient_vector_;
285 use_param_vectors_ =
true;
286 growth_products_prepared_ =
false;
287 return mean_length_young_mean_length_old_logit_corr_vector_;
296 use_param_vectors_ =
true;
297 growth_products_prepared_ =
false;
298 return mean_length_young_growth_coefficient_logit_corr_vector_;
307 use_param_vectors_ =
true;
308 growth_products_prepared_ =
false;
309 return mean_length_old_growth_coefficient_logit_corr_vector_;
317 age_offset_ = min_age;
318 age_offset_set_ =
true;
319 growth_products_prepared_ =
false;
321 model_->SetAgeOffset(
static_cast<Type
>(age_offset_));
332 std::size_t n_sexes = 1)
override {
335 throw std::runtime_error(
336 "VonBertalanffySchnuteGrowthModelAdapter currently supports n_sexes "
342 growth_products_prepared_ =
false;
343 model_ = std::make_shared<GrowthModel<Type>>(n_years, n_ages, n_sexes);
345 if (age_offset_set_) {
346 model_->SetAgeOffset(
static_cast<Type
>(age_offset_));
357 virtual const Type
evaluate(
int year,
const double& a)
override {
359 throw std::runtime_error(
"Negative age not supported");
361 const double a_round = std::round(a);
362 const double tol = 1e-8;
363 if (std::fabs(a - a_round) > tol) {
364 throw std::runtime_error(
"Non-integer age not supported yet");
367 const Type ref_age_young = CurrentReferenceAgeForLengthYoung();
368 const Type ref_age_old = CurrentReferenceAgeForLengthOld();
369 if (ref_age_old <= ref_age_young) {
370 throw std::runtime_error(
371 "VonBertalanffySchnuteGrowth reference_age_for_length_old must be > "
372 "reference_age_for_length_young");
376 return EvaluateWithFunctor(year, a);
381 const auto& p = model_->GetProducts();
382 const double offset = age_offset_set_ ? age_offset_ : 0.0;
383 const double age_index_raw = a_round - offset;
384 const double age_index_round = std::round(age_index_raw);
385 if (std::fabs(age_index_raw - age_index_round) <= tol &&
386 age_index_round >= 0.0) {
387 const std::size_t age_index =
static_cast<std::size_t
>(age_index_round);
388 if (age_index < p.n_ages) {
389 const std::size_t year_index =
390 (year >= 0 &&
static_cast<std::size_t
>(year) < p.n_years)
391 ?
static_cast<std::size_t
>(year)
392 :
static_cast<std::size_t
>(0);
393 return p.MeanWAA(year_index, age_index, 0);
398 return EvaluateWithFunctor(year, a);
405 growth_products_prepared_ =
false;
408 throw std::runtime_error(
"Growth model not initialized; n_ages is 0");
410 Initialize(n_years_ == 0 ? 1 : n_years_, n_ages_,
411 n_sexes_ == 0 ? 1 : n_sexes_);
415 growth_products_prepared_ =
true;
423 if (!model_ || !growth_products_prepared_) {
426 return &(model_->GetProducts());
442 return CurrentLengthWeightA() *
443 fims_math::pow(length_safe, CurrentLengthWeightB());
468 bool use_param_vectors_ =
false;
470 LengthReferenceParameterization::kEstimatedReferenceLengths;
471 Type constant_mean_length_young_ = Type(0.0);
472 Type constant_mean_length_old_ = Type(0.0);
473 std::size_t n_years_ = 0;
474 std::size_t n_ages_ = 0;
475 std::size_t n_sexes_ = 1;
476 double age_offset_ = 0.0;
477 bool age_offset_set_ =
false;
478 bool vb_params_set_ =
false;
479 bool lw_params_set_ =
false;
480 bool growth_products_prepared_ =
false;
482 mutable std::shared_ptr<GrowthModel<Type>> model_;
484 Type EvaluateWithFunctor(
int year,
const double& a)
const {
497 void SyncParamsToModel()
const {
501 model_->SetVonBertalanffySchnuteParameters(
502 CurrentMeanLengthYoung(), CurrentMeanLengthOld(),
503 CurrentGrowthCoefficient(), CurrentReferenceAgeForLengthYoung(),
504 CurrentReferenceAgeForLengthOld());
505 model_->SetLengthWeightParameters(CurrentLengthWeightA(),
506 CurrentLengthWeightB());
508 if (HasInterpolationSdInputs()) {
509 model_->SetLengthSdParams(CurrentLengthAtAgeSdAtReferenceAge1(),
510 CurrentLengthAtAgeSdAtReferenceAge2());
513 if (HasStructuredDeltaMethodInputs()) {
514 model_->SetGrowthParameterCovariance(
515 CurrentMeanLengthYoungVariance(),
516 CurrentMeanLengthYoungLengthAtRefAgeOldCovariance(),
517 CurrentMeanLengthYoungKCovariance(), CurrentMeanLengthOldVariance(),
518 CurrentMeanLengthOldKCovariance(),
519 CurrentGrowthCoefficientVariance());
521 model_->ClearGrowthParameterCovariance();
525 Type CurrentMeanLengthYoungStorage()
const {
526 return fims_math::exp(mean_length_young_vector_[0]);
529 Type CurrentMeanLengthOldStorage()
const {
530 return fims_math::exp(mean_length_old_vector_[0]);
533 Type CurrentMeanLengthYoung()
const {
534 switch (length_reference_parameterization_) {
535 case LengthReferenceParameterization::kEstimatedReferenceLengths:
536 return CurrentMeanLengthYoungStorage();
538 case LengthReferenceParameterization::
539 kConstantLengthYoungEstimatedLengthOld:
540 return constant_mean_length_young_;
542 case LengthReferenceParameterization::
543 kEstimatedLengthYoungBelowConstantLengthOld:
545 constant_mean_length_old_,
546 mean_length_young_vector_[0]);
548 case LengthReferenceParameterization::kBothConstant:
549 return constant_mean_length_young_;
552 return CurrentMeanLengthYoungStorage();
555 Type CurrentMeanLengthOld()
const {
556 switch (length_reference_parameterization_) {
557 case LengthReferenceParameterization::kEstimatedReferenceLengths:
558 case LengthReferenceParameterization::
559 kConstantLengthYoungEstimatedLengthOld:
562 return CurrentMeanLengthYoung() + CurrentMeanLengthOldStorage();
564 case LengthReferenceParameterization::
565 kEstimatedLengthYoungBelowConstantLengthOld:
566 return constant_mean_length_old_;
568 case LengthReferenceParameterization::kBothConstant:
569 return constant_mean_length_old_;
572 return CurrentMeanLengthOldStorage();
575 Type CurrentGrowthCoefficient()
const {
576 return fims_math::exp(growth_coefficient_vector_[0]);
578 Type CurrentReferenceAgeForLengthYoung()
const {
579 return reference_age_for_length_young_vector_[0];
581 Type CurrentReferenceAgeForLengthOld()
const {
582 return reference_age_for_length_old_vector_[0];
584 Type CurrentLengthWeightA()
const {
585 return fims_math::exp(length_weight_a_vector_[0]);
587 Type CurrentLengthWeightB()
const {
588 return fims_math::exp(length_weight_b_vector_[0]);
590 Type CurrentLengthAtAgeSdAtReferenceAge1()
const {
591 return fims_math::exp(length_at_age_sd_young_vector_[0]);
593 Type CurrentLengthAtAgeSdAtReferenceAge2()
const {
594 return fims_math::exp(length_at_age_sd_old_vector_[0]);
596 Type CurrentSdLengthAtRefAgeYoung()
const {
597 return fims_math::exp(log_sd_mean_length_young_vector_[0]);
600 Type CurrentSdLengthAtRefAgeOld()
const {
601 return fims_math::exp(log_sd_mean_length_old_vector_[0]);
604 Type CurrentSdGrowthCoefficient()
const {
605 return fims_math::exp(log_sd_growth_coefficient_vector_[0]);
608 Type CurrentCorrLengthAtRefAgeYoungLengthAtRefAgeOld()
const {
610 static_cast<Type
>(-1.0),
static_cast<Type
>(1.0),
611 mean_length_young_mean_length_old_logit_corr_vector_[0]);
614 Type CurrentCorrLengthAtRefAgeYoungK()
const {
616 static_cast<Type
>(-1.0),
static_cast<Type
>(1.0),
617 mean_length_young_growth_coefficient_logit_corr_vector_[0]);
620 Type CurrentCorrLengthAtRefAgeOldK()
const {
622 static_cast<Type
>(-1.0),
static_cast<Type
>(1.0),
623 mean_length_old_growth_coefficient_logit_corr_vector_[0]);
626 Type CurrentMeanLengthYoungVariance()
const {
627 const Type sd = CurrentSdLengthAtRefAgeYoung();
631 Type CurrentMeanLengthOldVariance()
const {
632 const Type sd = CurrentSdLengthAtRefAgeOld();
636 Type CurrentGrowthCoefficientVariance()
const {
637 const Type sd = CurrentSdGrowthCoefficient();
641 Type CurrentMeanLengthYoungLengthAtRefAgeOldCovariance()
const {
642 return CurrentCorrLengthAtRefAgeYoungLengthAtRefAgeOld() *
643 CurrentSdLengthAtRefAgeYoung() * CurrentSdLengthAtRefAgeOld();
646 Type CurrentMeanLengthYoungKCovariance()
const {
647 return CurrentCorrLengthAtRefAgeYoungK() * CurrentSdLengthAtRefAgeYoung() *
648 CurrentSdGrowthCoefficient();
651 Type CurrentMeanLengthOldKCovariance()
const {
652 return CurrentCorrLengthAtRefAgeOldK() * CurrentSdLengthAtRefAgeOld() *
653 CurrentSdGrowthCoefficient();
656 bool HasInterpolationSdInputs()
const {
657 return length_at_age_sd_young_vector_.size() > 0 ||
658 length_at_age_sd_old_vector_.size() > 0;
661 bool HasAnyStructuredDeltaMethodInput()
const {
662 return log_sd_mean_length_young_vector_.size() > 0 ||
663 log_sd_mean_length_old_vector_.size() > 0 ||
664 log_sd_growth_coefficient_vector_.size() > 0 ||
665 mean_length_young_mean_length_old_logit_corr_vector_.size() > 0 ||
666 mean_length_young_growth_coefficient_logit_corr_vector_.size() > 0 ||
667 mean_length_old_growth_coefficient_logit_corr_vector_.size() > 0;
670 bool HasStructuredDeltaMethodInputs()
const {
671 return log_sd_mean_length_young_vector_.size() > 0 &&
672 log_sd_mean_length_old_vector_.size() > 0 &&
673 log_sd_growth_coefficient_vector_.size() > 0 &&
674 mean_length_young_mean_length_old_logit_corr_vector_.size() > 0 &&
675 mean_length_young_growth_coefficient_logit_corr_vector_.size() > 0 &&
676 mean_length_old_growth_coefficient_logit_corr_vector_.size() > 0;
679 void EnsureParamsSet()
const {
680 if (!use_param_vectors_ || mean_length_young_vector_.size() < 1 ||
681 mean_length_old_vector_.size() < 1 ||
682 growth_coefficient_vector_.size() < 1 ||
683 reference_age_for_length_young_vector_.size() < 1 ||
684 reference_age_for_length_old_vector_.size() < 1 ||
685 length_weight_a_vector_.size() < 1 ||
686 length_weight_b_vector_.size() < 1) {
687 throw std::runtime_error(
688 "VonBertalanffySchnuteGrowth parameters not set");
691 if (mean_length_young_vector_.size() != 1 ||
692 mean_length_old_vector_.size() != 1 ||
693 growth_coefficient_vector_.size() != 1 ||
694 reference_age_for_length_young_vector_.size() != 1 ||
695 reference_age_for_length_old_vector_.size() != 1 ||
696 length_weight_a_vector_.size() != 1 ||
697 length_weight_b_vector_.size() != 1) {
698 throw std::runtime_error(
699 "VonBertalanffySchnuteGrowthModelAdapter currently supports a single "
700 "growth pattern; expected size 1 for von Bertalanffy--Schnute and "
701 "length-weight parameter vectors");
704 const bool has_sd = HasInterpolationSdInputs();
705 const bool has_any_structured_delta = HasAnyStructuredDeltaMethodInput();
706 const bool has_structured_delta = HasStructuredDeltaMethodInputs();
708 if (!has_sd && !has_any_structured_delta) {
709 throw std::runtime_error(
710 "VonBertalanffySchnuteGrowthModelAdapter requires either "
711 "both length_at_age_sd_young and length_at_age_sd_old, "
712 "or the structured delta-method growth variability inputs");
715 if (has_any_structured_delta && !has_structured_delta) {
716 throw std::runtime_error(
717 "VonBertalanffySchnuteGrowthModelAdapter requires all six structured "
718 "delta-method variability inputs when using that path");
721 if (has_sd && has_structured_delta) {
722 throw std::runtime_error(
723 "von Bertalanffy--Schnute growth adapter requires variability inputs "
724 "for exactly one supported path. Supply either the interpolation "
725 "inputs length_at_age_sd_young and length_at_age_sd_old or the full "
726 "delta-method variability inputs, but not both");
729 if (has_sd && (length_at_age_sd_young_vector_.size() != 1 ||
730 length_at_age_sd_old_vector_.size() != 1)) {
731 throw std::runtime_error(
732 "VonBertalanffySchnuteGrowthModelAdapter requires exactly one "
733 "length_at_age_sd_young value and one length_at_age_sd_old value");
736 if (has_structured_delta &&
737 (log_sd_mean_length_young_vector_.size() != 1 ||
738 log_sd_mean_length_old_vector_.size() != 1 ||
739 log_sd_growth_coefficient_vector_.size() != 1 ||
740 mean_length_young_mean_length_old_logit_corr_vector_.size() != 1 ||
741 mean_length_young_growth_coefficient_logit_corr_vector_.size() != 1 ||
742 mean_length_old_growth_coefficient_logit_corr_vector_.size() != 1)) {
743 throw std::runtime_error(
744 "VonBertalanffySchnuteGrowthModelAdapter currently supports a single "
745 "structured delta-method variability parameter set; expected "
746 "size 1 for each structured uncertainty input");