FIMS  v0.11.0
Loading...
Searching...
No Matches
growth_model.hpp
Go to the documentation of this file.
1
8#ifndef POPULATION_DYNAMICS_GROWTH_MODEL_HPP
9#define POPULATION_DYNAMICS_GROWTH_MODEL_HPP
10
11#include <cstddef>
12#include <stdexcept>
13
14#include "growth_model_base.hpp"
15#include "growth_products.hpp"
17#include "../../common/def.hpp"
18
19namespace fims_popdy {
20
30template <typename Type>
31class GrowthModel : public GrowthModelBase<Type> {
32 public:
39 GrowthModel(std::size_t n_years, std::size_t n_ages, std::size_t n_sexes = 1)
40 : n_years_(n_years),
41 n_ages_(n_ages),
42 n_sexes_(n_sexes),
43 products_(n_years, n_ages, n_sexes) {}
44
46 void SetVonBertalanffySchnuteParameters(Type mean_length_young,
47 Type mean_length_old,
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;
56 needs_update_ = true;
57 }
58
60 void SetLengthWeightParameters(Type length_weight_a, Type length_weight_b) {
61 vb_.length_weight_a = length_weight_a;
62 vb_.length_weight_b = length_weight_b;
63 needs_update_ = true;
64 }
65
68 void SetLengthSdParams(Type length_at_age_sd_at_reference_age_young,
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;
74 needs_update_ = true;
75 }
76
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);
100
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;
112 }
113
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;
125 }
126
128 void Prepare() override {
129 if (!needs_update_) return;
130
131 if (n_ages_ == 0) {
132 throw std::runtime_error(
133 "VonBertalanffySchnuteGrowth requires n_ages > 0");
134 }
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");
140 }
141
142 // Fill mean length-at-age, sd, and mean weight-at-age.
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_;
147
148 // log-scale params live upstream; laa here is natural scale
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);
152
153 products_.MeanLAA(y, a, s) = laa;
154 products_.SdLAA(y, a, s) = sd_laa;
155 products_.MeanWAA(y, a, s) = waa;
156 }
157 }
158 }
159
160 needs_update_ = false;
161 }
162
163 const GrowthProducts<Type>& GetProducts() const override { return products_; }
164
165 private:
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");
180 }
181
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");
188 }
189
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");
196 }
197
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");
204 }
205
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);
221
222 if (determinant < Type(0.0)) {
223 throw std::runtime_error(
224 "Growth parameter covariance matrix must be positive "
225 "semi-definite");
226 }
227 }
228
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 =
235 fims_math::ad_max(log_var, static_cast<Type>(0.0));
236 const Type sd_laa = laa * fims_math::sqrt(log_var_safe);
237 return fims_math::ad_max(sd_laa, static_cast<Type>(1e-8));
238 }
239
240 Type sd_laa = length_at_age_sd_at_reference_age_young_;
241 if (n_ages_ > 1) {
242 // Hold SD at its endpoint values outside the reference-age interval.
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);
249
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_);
253 }
254
255 return fims_math::ad_max(sd_laa, static_cast<Type>(1e-8));
256 }
257
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);
264
265 vb_.log_length_at_age_logscale_gradient(age, d_log_laa_d_log_length_young,
266 d_log_laa_d_log_length_old,
267 d_log_laa_d_log_k);
268
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_;
281 }
282
283 std::size_t n_years_;
284 std::size_t n_ages_;
285 std::size_t n_sexes_;
286
287 GrowthProducts<Type> products_;
288
289 // Phase 1 functor
290 VonBertalanffySchnuteGrowth<Type> vb_;
291
292 // Caching state
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);
305
306 public:
308 void SetAgeOffset(Type offset) {
309 age_offset_ = offset;
310 needs_update_ = true;
311 }
312};
313
314} // namespace fims_popdy
315
316#endif /* POPULATION_DYNAMICS_GROWTH_MODEL_HPP */
Growth module boundary (product-oriented).
Definition growth_model_base.hpp:24
void SetLengthSdParams(Type length_at_age_sd_at_reference_age_young, Type length_at_age_sd_at_reference_age_old)
Definition growth_model.hpp:68
GrowthModel(std::size_t n_years, std::size_t n_ages, std::size_t n_sexes=1)
Construct a growth model with fixed dimensions.
Definition growth_model.hpp:39
void Prepare() override
Compute and cache growth products.
Definition growth_model.hpp:128
void ClearGrowthParameterCovariance()
Definition growth_model.hpp:116
void SetLengthWeightParameters(Type length_weight_a, Type length_weight_b)
fixed length-weight params (phase 1)
Definition growth_model.hpp:60
const GrowthProducts< Type > & GetProducts() const override
Read-only access to cached growth products.
Definition growth_model.hpp:163
void SetGrowthParameterCovariance(Type mean_length_young_variance, Type mean_length_young_mean_length_old_covariance, Type mean_length_young_growth_coefficient_covariance, Type mean_length_old_variance, Type mean_length_old_growth_coefficient_covariance, Type growth_coefficient_variance)
Definition growth_model.hpp:87
void SetVonBertalanffySchnuteParameters(Type mean_length_young, Type mean_length_old, Type growth_coefficient, Type reference_age_for_length_young, Type reference_age_for_length_old)
Set fixed VonBertalanffySchnute parameters (explicit reference ages).
Definition growth_model.hpp:46
void SetAgeOffset(Type offset)
Set an age offset if population ages do not start at zero.
Definition growth_model.hpp:308
Platform macros and the core FIMS logging system.
const Type ad_max(const Type &a, const Type &b, Type C=1e-5)
Definition fims_math.hpp:410
Declares the GrowthModelBase interface. This is the growth module boundary that downstream code consu...
Declares the GrowthProducts struct holding growth "products" that downstream population dynamics code...
The population dynamics of FIMS.
Definition catch_at_age.hpp:45
Growth "products" in a consistent (year, age, sex) space.
Definition growth_products.hpp:26
Defines the VonBertalanffySchnuteGrowth class, which inherits from the GrowthBase class.