39 GrowthModel(std::size_t n_years, std::size_t n_ages, std::size_t n_sexes = 1)
43 products_(n_years, n_ages, n_sexes) {}
48 Type growth_coefficient,
49 Type reference_age_for_length_young,
50 Type reference_age_for_length_old) {
51 vb_.mean_length_young = mean_length_young;
52 vb_.mean_length_old = mean_length_old;
53 vb_.growth_coefficient = growth_coefficient;
54 vb_.reference_age_for_length_young = reference_age_for_length_young;
55 vb_.reference_age_for_length_old = reference_age_for_length_old;
61 vb_.length_weight_a = length_weight_a;
62 vb_.length_weight_b = length_weight_b;
69 Type length_at_age_sd_at_reference_age_old) {
70 length_at_age_sd_at_reference_age_young_ =
71 length_at_age_sd_at_reference_age_young;
72 length_at_age_sd_at_reference_age_old_ =
73 length_at_age_sd_at_reference_age_old;
88 Type mean_length_young_variance,
89 Type mean_length_young_mean_length_old_covariance,
90 Type mean_length_young_growth_coefficient_covariance,
91 Type mean_length_old_variance,
92 Type mean_length_old_growth_coefficient_covariance,
93 Type growth_coefficient_variance) {
94 ValidateGrowthParameterCovariance(
95 mean_length_young_variance,
96 mean_length_young_mean_length_old_covariance,
97 mean_length_young_growth_coefficient_covariance,
98 mean_length_old_variance, mean_length_old_growth_coefficient_covariance,
99 growth_coefficient_variance);
101 mean_length_young_variance_ = mean_length_young_variance;
102 mean_length_young_mean_length_old_covariance_ =
103 mean_length_young_mean_length_old_covariance;
104 mean_length_young_growth_coefficient_covariance_ =
105 mean_length_young_growth_coefficient_covariance;
106 mean_length_old_variance_ = mean_length_old_variance;
107 mean_length_old_growth_coefficient_covariance_ =
108 mean_length_old_growth_coefficient_covariance;
109 growth_coefficient_variance_ = growth_coefficient_variance;
110 use_delta_method_variability_ =
true;
111 needs_update_ =
true;
117 mean_length_young_variance_ =
static_cast<Type
>(0.0);
118 mean_length_young_mean_length_old_covariance_ =
static_cast<Type
>(0.0);
119 mean_length_young_growth_coefficient_covariance_ =
static_cast<Type
>(0.0);
120 mean_length_old_variance_ =
static_cast<Type
>(0.0);
121 mean_length_old_growth_coefficient_covariance_ =
static_cast<Type
>(0.0);
122 growth_coefficient_variance_ =
static_cast<Type
>(0.0);
123 use_delta_method_variability_ =
false;
124 needs_update_ =
true;
129 if (!needs_update_)
return;
132 throw std::runtime_error(
133 "VonBertalanffySchnuteGrowth requires n_ages > 0");
135 if (vb_.reference_age_for_length_old <=
136 vb_.reference_age_for_length_young) {
137 throw std::runtime_error(
138 "VonBertalanffySchnuteGrowth reference_age_for_length_old must be > "
139 "reference_age_for_length_young");
143 for (std::size_t y = 0; y < n_years_; ++y) {
144 for (std::size_t a = 0; a < n_ages_; ++a) {
145 for (std::size_t s = 0; s < n_sexes_; ++s) {
146 const Type age =
static_cast<Type
>(a) + age_offset_;
149 const Type laa = vb_.length_at_age(age);
150 const Type sd_laa = ComputeLengthSdAtAge(age, laa);
151 const Type waa = vb_.weight_at_age(age);
153 products_.MeanLAA(y, a, s) = laa;
154 products_.SdLAA(y, a, s) = sd_laa;
155 products_.MeanWAA(y, a, s) = waa;
160 needs_update_ =
false;
169 void ValidateGrowthParameterCovariance(
170 Type mean_length_young_variance,
171 Type mean_length_young_mean_length_old_covariance,
172 Type mean_length_young_growth_coefficient_covariance,
173 Type mean_length_old_variance,
174 Type mean_length_old_growth_coefficient_covariance,
175 Type growth_coefficient_variance)
const {
176 if (mean_length_young_variance < Type(0.0) ||
177 mean_length_old_variance < Type(0.0) ||
178 growth_coefficient_variance < Type(0.0)) {
179 throw std::runtime_error(
"Growth parameter variances must be >= 0");
182 if (mean_length_young_mean_length_old_covariance *
183 mean_length_young_mean_length_old_covariance >
184 mean_length_young_variance * mean_length_old_variance) {
185 throw std::runtime_error(
186 "Growth covariance between mean_length_young and "
187 "mean_length_old is inconsistent with the supplied variances");
190 if (mean_length_young_growth_coefficient_covariance *
191 mean_length_young_growth_coefficient_covariance >
192 mean_length_young_variance * growth_coefficient_variance) {
193 throw std::runtime_error(
194 "Growth covariance between mean_length_young and "
195 "growth_coefficient is inconsistent with the supplied variances");
198 if (mean_length_old_growth_coefficient_covariance *
199 mean_length_old_growth_coefficient_covariance >
200 mean_length_old_variance * growth_coefficient_variance) {
201 throw std::runtime_error(
202 "Growth covariance between mean_length_old and "
203 "growth_coefficient is inconsistent with the supplied variances");
206 const Type determinant =
207 mean_length_young_variance *
208 (mean_length_old_variance * growth_coefficient_variance -
209 mean_length_old_growth_coefficient_covariance *
210 mean_length_old_growth_coefficient_covariance) -
211 mean_length_young_mean_length_old_covariance *
212 (mean_length_young_mean_length_old_covariance *
213 growth_coefficient_variance -
214 mean_length_young_growth_coefficient_covariance *
215 mean_length_old_growth_coefficient_covariance) +
216 mean_length_young_growth_coefficient_covariance *
217 (mean_length_young_mean_length_old_covariance *
218 mean_length_old_growth_coefficient_covariance -
219 mean_length_young_growth_coefficient_covariance *
220 mean_length_old_variance);
222 if (determinant < Type(0.0)) {
223 throw std::runtime_error(
224 "Growth parameter covariance matrix must be positive "
231 Type ComputeLengthSdAtAge(
const Type& age,
const Type& laa)
const {
232 if (use_delta_method_variability_) {
233 const Type log_var = ComputeLogLengthVarianceAtAge(age);
234 const Type log_var_safe =
236 const Type sd_laa = laa * fims_math::sqrt(log_var_safe);
240 Type sd_laa = length_at_age_sd_at_reference_age_young_;
243 const Type age_above_young =
244 fims_math::cond_exp_lt(age, vb_.reference_age_for_length_young,
245 vb_.reference_age_for_length_young, age);
246 const Type interpolation_age = fims_math::cond_exp_lt(
247 vb_.reference_age_for_length_old, age_above_young,
248 vb_.reference_age_for_length_old, age_above_young);
250 sd_laa += vb_.growth_fraction(interpolation_age) *
251 (length_at_age_sd_at_reference_age_old_ -
252 length_at_age_sd_at_reference_age_young_);
260 Type ComputeLogLengthVarianceAtAge(
const Type& age)
const {
261 Type d_log_laa_d_log_length_young = Type(0.0);
262 Type d_log_laa_d_log_length_old = Type(0.0);
263 Type d_log_laa_d_log_k = Type(0.0);
265 vb_.log_length_at_age_logscale_gradient(age, d_log_laa_d_log_length_young,
266 d_log_laa_d_log_length_old,
269 return d_log_laa_d_log_length_young * d_log_laa_d_log_length_young *
270 mean_length_young_variance_ +
271 Type(2.0) * d_log_laa_d_log_length_young *
272 d_log_laa_d_log_length_old *
273 mean_length_young_mean_length_old_covariance_ +
274 Type(2.0) * d_log_laa_d_log_length_young * d_log_laa_d_log_k *
275 mean_length_young_growth_coefficient_covariance_ +
276 d_log_laa_d_log_length_old * d_log_laa_d_log_length_old *
277 mean_length_old_variance_ +
278 Type(2.0) * d_log_laa_d_log_length_old * d_log_laa_d_log_k *
279 mean_length_old_growth_coefficient_covariance_ +
280 d_log_laa_d_log_k * d_log_laa_d_log_k * growth_coefficient_variance_;
283 std::size_t n_years_;
285 std::size_t n_sexes_;
287 GrowthProducts<Type> products_;
290 VonBertalanffySchnuteGrowth<Type> vb_;
293 bool needs_update_ =
true;
294 Type length_at_age_sd_at_reference_age_young_ =
static_cast<Type
>(3.0);
295 Type length_at_age_sd_at_reference_age_old_ =
static_cast<Type
>(7.0);
296 bool use_delta_method_variability_ =
false;
297 Type mean_length_young_variance_ =
static_cast<Type
>(0.0);
298 Type mean_length_young_mean_length_old_covariance_ =
static_cast<Type
>(0.0);
299 Type mean_length_young_growth_coefficient_covariance_ =
300 static_cast<Type
>(0.0);
301 Type mean_length_old_variance_ =
static_cast<Type
>(0.0);
302 Type mean_length_old_growth_coefficient_covariance_ =
static_cast<Type
>(0.0);
303 Type growth_coefficient_variance_ =
static_cast<Type
>(0.0);
304 Type age_offset_ =
static_cast<Type
>(0.0);
309 age_offset_ = offset;
310 needs_update_ =
true;