2# pragma clang diagnostic push
3# pragma clang diagnostic ignored "-Wunknown-warning-option"
7# pragma clang diagnostic pop
15# pragma GCC diagnostic ignored "-Warray-bounds"
44struct LOM_t : vector<matrix<Type> > {
46 (*this).resize(LENGTH(x));
47 for(
int i = 0; i < LENGTH(x); i++){
48 SEXP m = VECTOR_ELT(x, i);
49 (*this)(i) = asMatrix<Type>(m);
90Type objective_function<Type>::operator() () {
91 using namespace density;
97 DATA_INTEGER(estimateMode);
98 DATA_INTEGER(msmMode);
104 DATA_IVECTOR(suitMode);
107 DATA_INTEGER(initMode);
108 DATA_IVECTOR(forecast);
112 DATA_INTEGER( styr );
113 DATA_INTEGER( endyr );
114 DATA_INTEGER( projyr );
116 DATA_INTEGER( srr_mse_switchyr );
117 DATA_IVECTOR( suit_styr );
118 DATA_IVECTOR( suit_endyr );
119 DATA_INTEGER( srr_hat_styr );
120 DATA_INTEGER( srr_hat_endyr );
122 int nyrs = projyr - styr + 1;
123 int nyrs_hind = endyr - styr + 1;
127 int nyrs_srrmean = srr_mse_switchyr - styr + 1;
129 srr_hat_styr = srr_hat_styr - styr;
130 srr_hat_endyr = srr_hat_endyr - styr;
131 if(nyrs_srrmean > nyrs_hind){nyrs_srrmean = nyrs_hind;}
132 if(srr_hat_styr == 0){srr_hat_styr = 1;}
135 DATA_INTEGER( nspp );
139 vector<int> nyrs_suit(nspp);
140 for(
int sp = 0; sp < nspp; sp++){
141 suit_endyr(sp) = suit_endyr(sp) - styr;
142 suit_styr(sp) = suit_styr(sp) - styr;
143 nyrs_suit(sp) = suit_endyr(sp) - suit_styr(sp) + 1;
146 DATA_IVECTOR( pop_wt_index );
147 DATA_IVECTOR( ssb_wt_index );
148 DATA_IVECTOR( pop_age_transition_index );
149 DATA_IVECTOR( estDynamics );
152 pop_age_transition_index -= 1;
156 DATA_INTEGER(srr_fun);
157 DATA_INTEGER(srr_pred_fun);
158 DATA_INTEGER(proj_mean_rec);
161 DATA_INTEGER(srr_est_mode);
162 DATA_VECTOR(srr_prior);
163 DATA_VECTOR(srr_prior_sd);
164 DATA_INTEGER( niter );
165 DATA_VECTOR( Bmsy_lim );
170 DATA_INTEGER(DynamicHCR);
171 DATA_VECTOR(Ptarget);
173 DATA_VECTOR(Ftarget_percent);
174 DATA_VECTOR(Flimit_percent);
177 DATA_VECTOR(QnormHCR);
183 int sp, sex, age, ln, yr;
185 int flt_yr, flt_sex, comp_type;
186 int fsh_ind, index_ind, comp_ind, yr_ind;
187 int wt_idx_pop, wt_idx_ssb, wt_idx_flt;
189 if(msmMode == 0) { niter = 1; }
197 DATA_IVECTOR( nsex );
198 int max_sex =
imax(nsex);
199 DATA_VECTOR( spawn_month );
200 DATA_IVECTOR( nages );
201 DATA_IVECTOR( minage );
202 DATA_IVECTOR( nlengths );
203 DATA_MATRIX(lengths);
204 DATA_IVECTOR( nlengths_pop );
205 DATA_MATRIX( lengths_pop );
206 DATA_IMATRIX( pop_to_data_bin );
207 DATA_ARRAY( NByageFixed );
208 DATA_VECTOR( MSSB0 );
211 int max_nlengths =
imax(nlengths);
214 int max_nlengths_pop =
imax(nlengths_pop);
221 matrix<int> pop_bin_lo(nspp, max_nlengths); pop_bin_lo.setZero();
222 matrix<int> pop_bin_hi(nspp, max_nlengths); pop_bin_hi.setZero();
223 for(
int sp_i = 0; sp_i < nspp; sp_i++){
224 for(
int d = 0; d < nlengths(sp_i); d++){ pop_bin_lo(sp_i, d) = -1; pop_bin_hi(sp_i, d) = -2; }
225 for(
int lp = 0; lp < nlengths_pop(sp_i); lp++){
226 int d = pop_to_data_bin(sp_i, lp);
227 if(pop_bin_lo(sp_i, d) < 0) pop_bin_lo(sp_i, d) = lp;
228 pop_bin_hi(sp_i, d) = lp;
231 int max_age =
imax(nages);
234 int max_bin = (max_age > max_nlengths_pop) ? max_age : max_nlengths_pop;
237 DATA_IVECTOR(M1_model);
239 DATA_IVECTOR(M1_use_prior);
240 DATA_IVECTOR(M2_use_prior);
241 DATA_VECTOR(M_prior);
242 DATA_VECTOR(M_prior_sd);
245 DATA_IVECTOR(growth_model);
246 DATA_IVECTOR(growth_sd_style);
247 DATA_IVECTOR(growth_sd_form);
248 DATA_IVECTOR(growth_plus_length);
249 DATA_VECTOR(plus_group_decay);
250 DATA_IMATRIX(sel_dn6_ends);
251 DATA_IVECTOR(mat_len_use);
252 DATA_MATRIX(mat_len_pars);
253 DATA_VECTOR(growth_age_L1);
281 DATA_IVECTOR(linkage_process);
282 DATA_IVECTOR(linkage_param);
283 DATA_IVECTOR(linkage_species);
284 DATA_IVECTOR(linkage_sex);
285 DATA_IVECTOR(linkage_age_bin);
286 DATA_IVECTOR(linkage_fleet);
287 DATA_IVECTOR(linkage_X_col);
288 DATA_IVECTOR(linkage_link);
292 int rec_floor_on = 0;
293 for (
int i = 0; i < linkage_link.size(); ++i) {
296 DATA_IVECTOR(linkage_re_index);
299 DATA_IVECTOR(linkage_re_sigma);
300 DATA_IVECTOR(linkage_re_integrate);
301 DATA_IVECTOR(linkage_re_slot);
302 DATA_IVECTOR(linkage_re_struct);
303 DATA_IVECTOR(linkage_re_rho);
304 DATA_IVECTOR(linkage_re_sigma_prior_family);
305 DATA_IVECTOR(linkage_re_rho_prior_family);
306 DATA_VECTOR(linkage_re_rho_prior_p1);
307 DATA_VECTOR(linkage_re_rho_prior_p2);
308 DATA_IVECTOR(linkage_re_obs);
310 DATA_VECTOR(linkage_re_obs_sd);
311 DATA_VECTOR(linkage_re_obs_value);
312 DATA_IVECTOR(linkage_re_obs_mask);
313 DATA_IVECTOR(linkage_re_obsvec_idx);
314 DATA_VECTOR(linkage_re_sigma_prior_p1);
315 DATA_VECTOR(linkage_re_sigma_prior_p2);
316 DATA_IVECTOR(linkage_is_intercept);
317 DATA_IVECTOR(linkage_prior_family);
318 DATA_VECTOR(linkage_prior_p1);
319 DATA_VECTOR(linkage_prior_p2);
320 DATA_MATRIX(linkage_X);
323 DATA_IVECTOR(flt_type);
324 DATA_VECTOR(flt_month);
325 DATA_IVECTOR(flt_sel_type);
326 DATA_IVECTOR(flt_sel_dim);
327 DATA_IVECTOR(flt_n_sel_bins);
328 DATA_IVECTOR(flt_sel_cap_bin);
329 DATA_IVECTOR(flt_varying_sel);
330 DATA_IVECTOR(flt_spp);
331 DATA_IVECTOR(bin_first_selected);
332 DATA_IVECTOR(age_first_selected);
333 DATA_IVECTOR(sel_norm_bin1);
334 DATA_IVECTOR(sel_norm_bin2);
335 DATA_IVECTOR(sel_norm_scope);
336 DATA_IVECTOR(flt_sel_start_yr);
337 DATA_IVECTOR(flt_sel_pen_first_bin);
338 DATA_IVECTOR(flt_sel_lead);
339 DATA_IVECTOR(flt_q_lead);
340 DATA_IVECTOR(flt_sel_pen_last_bin);
341 DATA_IVECTOR(flt_sel_shape_mode);
342 DATA_VECTOR(flt_sel_avgsel_pen);
343 DATA_IVECTOR(comp_ll_type);
344 DATA_IVECTOR(comp_accum_young);
345 DATA_IVECTOR(comp_accum_old);
346 DATA_IVECTOR(caal_ll_type);
347 DATA_IVECTOR(flt_units);
348 DATA_IVECTOR(flt_wt_index);
349 DATA_IVECTOR(flt_age_transition_index);
350 DATA_IVECTOR(flt_ageing_error_index);
351 DATA_IVECTOR(est_index_q);
352 DATA_IVECTOR(index_varying_q);
353 DATA_IVECTOR(est_sigma_index);
354 DATA_IVECTOR(est_sigma_fsh);
357 DATA_IMATRIX( catch_ctl );
358 DATA_IMATRIX( catch_n );
359 DATA_MATRIX( catch_obs );
362 DATA_IMATRIX( index_ctl );
363 DATA_MATRIX( index_n );
364 DATA_MATRIX( index_obs );
365 DATA_VECTOR( index_log_q_prior );
366 DATA_IVECTOR( index_ll_type );
367 DATA_STRUCT( index_cov_mat,
LOM_t );
368 DATA_VECTOR( index_cov_const );
382 DATA_VECTOR( obsvec );
383 DATA_VECTOR_INDICATOR( keep, obsvec );
384 DATA_IVECTOR( catch_obsvec_idx );
385 DATA_IVECTOR( index_obsvec_idx );
386 DATA_IVECTOR( comp_obsvec_idx );
387 DATA_IVECTOR( caal_obsvec_idx );
388 DATA_IVECTOR( diet_obsvec_idx );
389 DATA_INTEGER( osa_mode );
390 DATA_INTEGER( adreport_sel );
391 DATA_INTEGER( adreport_catch );
392 DATA_SCALAR( comp_offset );
409 DATA_IVECTOR( simulate_state );
410 DATA_IVECTOR( simulate_period );
413 DATA_IMATRIX( comp_ctl );
414 DATA_MATRIX( comp_n );
415 DATA_MATRIX( comp_obs );
416 DATA_IMATRIX( caal_ctl );
417 DATA_MATRIX( caal_n );
421 DATA_IMATRIX( equil_catch_ctl );
422 DATA_MATRIX( equil_catch_obs );
423 DATA_MATRIX( caal_obs );
426 DATA_IMATRIX( emp_sel_ctl );
427 DATA_MATRIX( emp_sel_obs );
428 DATA_ARRAY( age_trans_matrix);
429 DATA_ARRAY( age_error );
432 DATA_ARRAY( weight_obs );
435 DATA_IVECTOR(diet_ll_type);
437 DATA_ARRAY( ration_data );
438 DATA_MATRIX( diet_obs );
439 DATA_IMATRIX( diet_ctl );
440 DATA_INTEGER(n_stomach_obs);
441 DATA_IVECTOR(stomach_id);
444 DATA_MATRIX( env_index );
448 DATA_VECTOR( other_food );
449 DATA_VECTOR( Pvalue );
451 DATA_IVECTOR( Cindex );
462 DATA_MATRIX( sex_ratio );
463 DATA_MATRIX( maturity );
464 DATA_SCALAR(bias_adjust_obs);
465 DATA_SCALAR(bias_adjust_proc);
473 PARAMETER_VECTOR( log_pop_scalar );
476 PARAMETER_MATRIX( rec_pars );
477 PARAMETER_VECTOR( R_log_sd );
478 PARAMETER_MATRIX( rec_dev );
479 PARAMETER_MATRIX( init_dev );
482 PARAMETER_ARRAY( log_M1 );
483 PARAMETER_ARRAY( log_M1_dev );
484 PARAMETER_ARRAY( M1_beta );
485 PARAMETER_ARRAY( M1_rho );
486 PARAMETER_ARRAY( M1_dev_log_sd );
489 PARAMETER_ARRAY(log_growth_pars);
491 PARAMETER_ARRAY(growth_log_sd);
492 PARAMETER_MATRIX(weight_length_pars);
496 PARAMETER_VECTOR(beta_linkage);
502 PARAMETER_VECTOR(beta_linkage_re);
505 PARAMETER_VECTOR(beta_linkage_re_pen);
506 PARAMETER_VECTOR(log_sigma_linkage);
507 PARAMETER_VECTOR(trans_rho_linkage);
508 PARAMETER_VECTOR(beta_linkage_obs);
509 PARAMETER_VECTOR(log_obs_sd_linkage);
512 PARAMETER_VECTOR( log_Flimit );
513 PARAMETER_VECTOR( log_Ftarget );
514 PARAMETER_VECTOR( log_Finit );
515 PARAMETER_VECTOR( proj_F_prop );
516 PARAMETER_MATRIX( log_F );
519 PARAMETER_VECTOR( index_log_q );
520 PARAMETER_MATRIX( index_q_beta );
521 PARAMETER_MATRIX( index_q_dev );
522 PARAMETER_VECTOR( index_q_log_sd );
523 PARAMETER_VECTOR( index_q_dev_log_sd );
526 PARAMETER_ARRAY( sel_coff );
527 PARAMETER_ARRAY( sel_coff_dev );
528 PARAMETER_ARRAY( log_sel_slp );
529 PARAMETER_ARRAY( sel_inf );
530 PARAMETER_ARRAY( log_sel_slp_dev );
531 PARAMETER_ARRAY( sel_inf_dev );
532 PARAMETER_ARRAY( log_sel_apical );
533 PARAMETER_ARRAY( sel_dn6 );
534 PARAMETER_VECTOR( sel_dev_log_sd );
535 PARAMETER_MATRIX( sel_curve_pen );
539 PARAMETER_VECTOR( index_log_sd );
540 PARAMETER_VECTOR( catch_log_sd );
541 PARAMETER_VECTOR( comp_weights );
542 PARAMETER_VECTOR( caal_weights );
543 vector<Type> DM_pars_comp = exp(comp_weights);
544 vector<Type> DM_pars_caal = exp(caal_weights);
546 PARAMETER_VECTOR( diet_comp_weights );
547 vector<Type> DM_diet_pars = exp(diet_comp_weights);
550 PARAMETER_VECTOR( log_gam_a );
551 PARAMETER_VECTOR( log_gam_b );
554 PARAMETER_MATRIX( log_phi );
562 int n_flt = flt_type.size();
563 vector<int> joint_adjust(comp_obs.rows()); joint_adjust.setZero();
565 Type ricker_intercept = 0.0;
568 array<Type> growth_matrix(nspp * 2 + n_flt, max_sex, max_age, max_nlengths_pop, nyrs); growth_matrix.setZero();
569 array<Type> weight_hat(nspp * 2 + n_flt, max_sex, max_age, nyrs); weight_hat.setZero();
570 array<Type> mat_weight_hat(nspp * 2 + n_flt, max_sex, max_age, nyrs); mat_weight_hat.setZero();
571 array<Type> length_hat(nspp * 2 + n_flt, max_sex, max_age, nyrs); length_hat.setZero();
574 vector<Type> pop_scalar = exp(log_pop_scalar);
575 vector<Type> avg_R(nspp); avg_R.setZero();
576 matrix<Type> R_hat(nspp, nyrs); R_hat.setZero();
577 matrix<Type> mort_sum(nspp, max_age); mort_sum.setZero();
580 array<Type> sel_init(nspp, max_sex, max_age); sel_init.setZero();
586 array<Type> N_eq(nspp, max_sex, max_age); N_eq.setZero();
587 matrix<Type> R0(nspp, nyrs); R0.setZero();
588 matrix<Type> alpha(nspp, nyrs); alpha.setZero();
589 matrix<Type> Beta(nspp, nyrs); Beta.setZero();
590 matrix<Type> steepness(nspp, nyrs); steepness.setZero();
591 vector<Type> R_init(nspp); R_init.setZero();
592 matrix<Type> R(nspp, nyrs); R.setZero();
593 array<Type> biomass_at_age(nspp, max_sex, max_age, nyrs); biomass_at_age.setZero();
594 matrix<Type> biomass(nspp, nyrs); biomass.setZero();
595 matrix<Type> exploitable_biomass(nspp, nyrs); exploitable_biomass.setZero();
596 matrix<Type> ssb(nspp, nyrs); ssb.setZero();
597 matrix<Type> biomass_depletion(nspp, nyrs); biomass_depletion.setZero();
598 matrix<Type> ssb_depletion(nspp, nyrs); ssb_depletion.setZero();
599 array<Type> M_at_age(nspp, max_sex, max_age, nyrs); M_at_age.setZero();
600 array<Type> M_at_age_dB0(nspp, max_sex, max_age, nyrs); M_at_age_dB0.setZero();
601 array<Type> M_at_age_dBF(nspp, max_sex, max_age, nyrs); M_at_age_dBF.setZero();
602 array<Type> M1_at_age(nspp, max_sex, max_age, nyrs); M1_at_age.setZero();
603 array<Type> N_at_age(nspp, max_sex, max_age, nyrs); N_at_age.setZero();
604 array<Type> avgN_at_age(nspp, max_sex, max_age, nyrs); avgN_at_age.setZero();
605 array<Type> avgN_at_age_dB0(nspp, max_sex, max_age, nyrs); avgN_at_age_dB0.setZero();
606 array<Type> avgN_at_age_dBF(nspp, max_sex, max_age, nyrs); avgN_at_age_dBF.setZero();
607 array<Type> Z_at_age(nspp, max_sex, max_age, nyrs); Z_at_age.setZero();
608 vector<Type> R_sd(nspp); R_sd.setZero();
609 vector<Type> zero_N_pen(nspp); zero_N_pen.setZero();
612 array<Type> sel_at_age(n_flt, max_sex, max_age, nyrs); sel_at_age.setZero();
613 array<Type> sel_at_length(n_flt, max_sex, max_nlengths_pop, nyrs); sel_at_length.setZero();
614 array<Type> avg_sel(n_flt, max_sex, nyrs_hind); avg_sel.setZero();
615 array<Type> non_par_sel(n_flt, max_sex, max_bin, nyrs); non_par_sel.setZero();
616 array<Type> log_non_par_sel(n_flt, max_sex, max_bin, nyrs); log_non_par_sel.setZero();
617 vector<Type> sel_dev_sd(n_flt); sel_dev_sd.setZero();
620 matrix<Type> F_spp(nspp, nyrs); F_spp.setZero();
621 matrix<Type> F_flt(n_flt, nyrs); F_flt.setZero();
622 array<Type> F_flt_age(n_flt, max_sex, max_age, nyrs); F_flt_age.setZero();
623 array<Type> Flimit_at_age(nspp, max_sex, max_age, nyrs); Flimit_at_age.setZero();
624 array<Type> Ftarget_at_age(nspp, max_sex, max_age, nyrs); Ftarget_at_age.setZero();
625 array<Type> F_at_age(nspp, max_sex, max_age, nyrs); F_at_age.setZero();
626 vector<Type> catch_hat(catch_obs.rows()); catch_hat.setZero();
627 vector<Type> max_catch_hat(catch_obs.rows()); max_catch_hat.setZero();
631 vector<Type> catch_sd(catch_obs.rows()); catch_sd.setZero();
634 vector<Type> catch_analytical_sd(n_flt); catch_analytical_sd.setZero();
635 vector<Type> catch_n_obs(n_flt); catch_n_obs.setZero();
638 array<Type> NByage0(nspp, max_sex, max_age, nyrs); NByage0.setZero();
639 array<Type> NByageF(nspp, max_sex, max_age, nyrs); NByageF.setZero();
640 array<Type> N_at_age_dB0(nspp, max_sex, max_age, nyrs); N_at_age_dB0.setZero();
641 array<Type> N_at_age_dBF(nspp, max_sex, max_age, nyrs); N_at_age_dBF.setZero();
642 matrix<Type> DynamicSB0(nspp, nyrs); DynamicSB0.setZero();
643 matrix<Type> DynamicB0(nspp, nyrs); DynamicB0.setZero();
644 matrix<Type> DynamicSBF(nspp, nyrs); DynamicSBF.setZero();
648 array<Type> NbyageSPR(4, nspp, max_age); NbyageSPR.setZero();
649 vector<Type> SPRlimit(nspp); SPRlimit.setZero();
650 vector<Type> SPRtarget(nspp); SPRtarget.setZero();
651 vector<Type> SPR0(nspp); SPR0.setZero();
652 vector<Type> SPRFinit(nspp); SPRFinit.setZero();
653 matrix<Type> SB0(nspp, nyrs); SB0.setZero();
654 matrix<Type> SBF(nspp, nyrs); SBF.setZero();
655 matrix<Type> B0(nspp, nyrs); B0.setZero();
656 vector<Type> Flimit = exp(log_Flimit);
657 vector<Type> Ftarget = exp(log_Ftarget);
658 vector<Type> Finit = exp(log_Finit);
659 vector<Type> index_q_mult(index_q_beta.cols()); index_q_mult.setZero();
660 vector<Type> M1_mult(M1_beta.dim(2)); M1_mult.setZero();
661 vector<Type> beta_q_tmp(index_q_beta.cols()); beta_q_tmp.setZero();
662 vector<Type> env_q_tmp(index_q_beta.cols()); env_q_tmp.setZero();
663 vector<Type> beta_M1_tmp(M1_beta.dim(2)); beta_M1_tmp.setZero();
664 vector<Type> env_M1_tmp(M1_beta.dim(2)); env_M1_tmp.setZero();
665 matrix<Type> proj_F(nspp, nyrs); proj_F.setZero();
668 vector<Type> index_q_sd(n_flt); index_q_sd.setZero();
669 vector<Type> index_q_dev_sd(n_flt); index_q_dev_sd.setZero();
670 vector<Type> index_hat(index_obs.rows()); index_hat.setZero();
676 vector<Type> index_trunc_tries_sim(index_obs.rows()); index_trunc_tries_sim.setZero();
677 vector<Type> index_trunc_rejects_sim(index_obs.rows()); index_trunc_rejects_sim.setZero();
681 vector<Type> index_trunc_sd_sim(index_obs.rows()); index_trunc_sd_sim.setZero();
686 vector<Type> index_trunc_budget_sim(index_obs.rows()); index_trunc_budget_sim.setZero();
687 const int index_trunc_max_tries = 100;
693 vector<Type> index_sd(index_obs.rows()); index_sd.setZero();
694 vector<Type> index_analytical_sd(n_flt); index_analytical_sd.setZero();
695 vector<Type> index_q_analytical(n_flt); index_q_analytical.setZero();
696 matrix<Type> index_q(n_flt, nyrs_hind); index_q.setZero();
697 vector<Type> index_n_obs(n_flt); index_n_obs.setZero();
709 int max_age_cols =
static_cast<int>(comp_obs.cols());
710 for(
int comp_row = 0; comp_row < comp_ctl.rows(); comp_row++){
711 int comp_row_sp = comp_ctl(comp_row, 1) - 1;
712 if((comp_row_sp < 0) | (comp_row_sp >= nspp)){
713 error(
"comp_data Species on row %d is not in 1..nspp", comp_row + 1);
715 int comp_row_cols = (comp_ctl(comp_row, 2) == 3) ? nages(comp_row_sp) * 2 : nages(comp_row_sp);
716 if(comp_row_cols > max_age_cols){ max_age_cols = comp_row_cols; }
718 vector<Type> n_hat(comp_obs.rows()) ; n_hat.setZero() ;
719 matrix<Type> age_hat(comp_obs.rows(), max_age_cols); age_hat.setZero();
720 matrix<Type> age_obs_hat(comp_obs.rows(), max_age_cols); age_obs_hat.setZero();
721 matrix<Type> comp_hat = comp_obs; comp_hat.setZero();
722 matrix<Type> caal_hat = caal_obs; caal_hat.setZero();
723 array<Type> pred_CAAL(n_flt, max_sex, max_age, max_nlengths, nyrs); pred_CAAL.setZero();
724 array<Type> comp_pred(max_sex, max_age, max_nlengths); comp_pred.setZero();
727 array<Type> consumption_at_age( nspp, max_sex, max_age, nyrs ); consumption_at_age.setZero();
728 matrix<Type> fT( nspp, nyrs ); fT.setZero();
731 array<Type> diet_prop(nspp * max_sex, nspp * max_sex, max_age, max_age, nyrs); diet_prop.setZero();
732 array<Type> diet_prop_hat(nspp * max_sex, nspp * max_sex, max_age, max_age, nyrs); diet_prop_hat.setZero();
733 array<Type> other_food_diet_prop(nspp, max_sex, max_age, nyrs); other_food_diet_prop.setZero();
734 matrix<Type> diet_hat = diet_obs; diet_hat.setZero();
737 array<Type> avail_food(nspp, max_sex, max_age, nyrs); avail_food.setZero();
738 array<Type> avail_food_dB0(nspp, max_sex, max_age, nyrs); avail_food_dB0.setZero();
739 array<Type> avail_food_dBF(nspp, max_sex, max_age, nyrs); avail_food_dBF.setZero();
740 array<Type> stom_div_bio(nspp * max_sex, nspp * max_sex, max_age, max_age, nyrs); stom_div_bio.setZero();
741 array<Type> suitability(nspp * max_sex, nspp * max_sex, max_age, max_age, nyrs); suitability.setZero();
742 array<Type> suit_other(nspp, max_sex, max_age, nyrs); suit_other.setZero();
743 array<Type> other_diet_prop_hat(nspp, max_sex, max_age, nyrs); other_diet_prop_hat.setZero();
746 vector<Type> gam_a = exp(log_gam_a);
747 vector<Type> gam_b = exp(log_gam_b);
748 matrix<Type> vulnerability(nspp, nspp); vulnerability.setZero();
749 vector<Type> vulnerability_other(nspp); vulnerability_other.setZero();
752 array<Type> M2_at_age(nspp, max_sex, max_age, nyrs); M2_at_age.setZero();
753 array<Type> M2_at_age_dB0(nspp, max_sex, max_age, nyrs); M2_at_age_dB0.setZero();
754 array<Type> M2_at_age_dBF(nspp, max_sex, max_age, nyrs); M2_at_age_dBF.setZero();
755 array<Type> M2_prop(nspp * max_sex, nspp * max_sex, max_age, max_age, nyrs); M2_prop.setZero();
756 array<Type> B_eaten(nspp * max_sex, nspp * max_sex, max_age, max_age, nyrs); B_eaten.setZero();
757 array<Type> B_eaten_as_prey(nspp, max_sex, max_age, nyrs); B_eaten_as_prey.setZero();
764 R_sd = exp(R_log_sd);
765 sel_dev_sd = exp(sel_dev_log_sd) ;
766 index_q_sd = exp(index_q_log_sd) ;
767 index_q_dev_sd = exp(index_q_dev_log_sd) ;
781 vector<Type> beta_linkage_re_drawn_sim(linkage_re_sigma.size());
782 beta_linkage_re_drawn_sim.setZero();
783 if (log_sigma_linkage.size() > 0 && simulate_period(0) == 1) {
784 int n_grp = log_sigma_linkage.size();
789 vector<int> grp_used(n_grp), grp_draw(n_grp);
792 for (
int i = 0; i < linkage_re_index.size(); ++i) {
793 if (linkage_re_index(i) < 0)
continue;
794 int grp = linkage_re_sigma(linkage_re_index(i));
799 int proc = linkage_process(i);
800 if (proc < 0 || proc >= simulate_state.size() || simulate_state(proc) != 1) {
805 for (
int grp = 0; grp < n_grp; ++grp) {
806 if (grp_used(grp) == 0 || grp_draw(grp) == 0)
continue;
811 if (linkage_re_obs(grp) >= 0)
continue;
814 int len = slots.size();
815 if (len == 0)
continue;
817 Type sigma = exp(log_sigma_linkage(grp));
818 vector<Type> re(len);
819 int st = linkage_re_struct(grp);
824 re(0) = linkage_re_integrate(slots(0))
825 ? beta_linkage_re(linkage_re_slot(slots(0)))
826 : beta_linkage_re_pen(linkage_re_slot(slots(0)));
827 for (
int t = 1; t < len; ++t) re(t) = re(t - 1) + rnorm(Type(0), sigma);
829 Type rho =
rho_trans(trans_rho_linkage(linkage_re_rho(grp)));
830 AR1(rho).simulate(re);
833 for (
int t = 0; t < len; ++t) re(t) = rnorm(Type(0), sigma);
836 for (
int t = 0; t < len; ++t) {
837 if (linkage_re_integrate(slots(t))) {
838 beta_linkage_re(linkage_re_slot(slots(t))) = re(t);
840 beta_linkage_re_pen(linkage_re_slot(slots(t))) = re(t);
844 if (!(st ==
RCEATTLE_RE_RW && t == 0)) beta_linkage_re_drawn_sim(slots(t)) = 1;
848 REPORT(beta_linkage_re_drawn_sim);
856 vector<Type> beta_linkage_re_all(linkage_re_sigma.size());
857 for (
int s = 0; s < beta_linkage_re_all.size(); ++s) {
858 beta_linkage_re_all(s) = linkage_re_integrate(s)
859 ? beta_linkage_re(linkage_re_slot(s))
860 : beta_linkage_re_pen(linkage_re_slot(s));
870 vector<Type> beta_linkage_re_sim = beta_linkage_re_all;
871 REPORT(beta_linkage_re_sim);
878 vector<Type> beta_linkage_eff = beta_linkage;
879 for (
int i = 0; i < beta_linkage_eff.size(); ++i) {
880 if (linkage_re_index(i) >= 0) {
881 Type z = beta_linkage_re_all(linkage_re_index(i));
884 int grp = linkage_re_sigma(linkage_re_index(i));
885 if (linkage_re_obs(grp) >= 0) z *= beta_linkage_obs(linkage_re_obs(grp));
886 beta_linkage_eff(i) = z;
893 linkage_process, linkage_param,
894 linkage_species, linkage_sex, linkage_age_bin, linkage_fleet,
895 linkage_X_col, linkage_link, linkage_X, beta_linkage_eff};
901 matrix<Type> q_linkage_offset(n_flt, nyrs_hind); q_linkage_offset.setZero();
902 matrix<Type> q_linkage_offset_nat(n_flt, nyrs_hind); q_linkage_offset_nat.setZero();
903 matrix<Type> q_linkage_log_mult(n_flt, nyrs_hind); q_linkage_log_mult.setZero();
911 REPORT(q_linkage_offset);
912 REPORT(q_linkage_offset_nat);
913 REPORT(q_linkage_log_mult);
915 for(flt = 0; flt < n_flt; flt++){
916 for(yr = 0; yr < nyrs_hind; yr++){
920 index_q(flt, yr) = exp((index_log_q(flt) + q_linkage_offset(flt, yr))
921 * exp(q_linkage_log_mult(flt, yr))
922 + index_q_dev(flt, yr))
923 + q_linkage_offset_nat(flt, yr);
927 if(est_index_q(flt) == 5){
928 beta_q_tmp = index_q_beta.row(flt);
929 env_q_tmp = env_index.row(yr) ;
930 index_q_mult = env_q_tmp * beta_q_tmp;
931 index_q(flt, yr) = exp(index_log_q(flt) + (index_q_mult).sum());
936 matrix<int> r_sexes(diet_obs.rows(), 2); r_sexes.setZero();
937 matrix<int> k_sexes(diet_obs.rows(), 2); k_sexes.setZero();
942 matrix<Type> mature_females = maturity;
943 for( sp = 0; sp < nspp ; sp++) {
945 if(initMode < 3 || initMode == 5){
950 for( age = 0 ; age < nages(sp); age++ ) {
952 mature_females( sp, age ) = maturity( sp, age ) * sex_ratio(sp, age);
963 array<Type> recruitment_linkage_offset(nspp,
966 array<Type> recruitment_linkage_offset_nat(nspp,
969 recruitment_linkage_offset.setZero();
970 recruitment_linkage_offset_nat.setZero();
973 REPORT(recruitment_linkage_offset);
974 REPORT(recruitment_linkage_offset_nat);
977 array<Type> M_linkage_offset(nspp, max_sex, max_age, nyrs);
978 array<Type> M_linkage_offset_nat(nspp, max_sex, max_age, nyrs);
979 M_linkage_offset.setZero();
980 M_linkage_offset_nat.setZero();
983 REPORT(M_linkage_offset);
984 REPORT(M_linkage_offset_nat);
990 growth_linkage_offset.setZero();
991 growth_linkage_offset_nat.setZero();
994 REPORT(growth_linkage_offset);
995 REPORT(growth_linkage_offset_nat);
1001 for(sp = 0; sp < nspp; sp++){
1002 for(yr = 0; yr < nyrs; yr++){
1018 for(sp = 0; sp < nspp; sp++){
1019 for(sex = 0; sex < nsex(sp); sex ++){
1020 for(yr = 0; yr < nyrs; yr++){
1022 growth_parameters(sp, sex, yr, par) = exp(
1023 log_growth_pars(sp, sex, par)
1024 + growth_linkage_offset(sp, sex, yr, par)
1025 ) + growth_linkage_offset_nat(sp, sex, yr, par);
1030 REPORT(growth_parameters);
1074 array<Type> spawn_output(nspp, max_age, nyrs); spawn_output.setZero();
1075 for(sp = 0; sp < nspp; sp++){
1076 wt_idx_ssb = 2 * sp + 1;
1077 for(age = 0; age < nages(sp); age++){
1078 for(yr = 0; yr < nyrs; yr++){
1079 if(mat_len_use(sp) == 1){
1080 spawn_output(sp, age, yr) = mat_weight_hat(wt_idx_ssb, 0, age, yr) * ((nsex(sp) == 1) ? sex_ratio(sp, age) : Type(1.0));
1082 spawn_output(sp, age, yr) = weight_hat(wt_idx_ssb, 0, age, yr) * mature_females(sp, age);
1087 REPORT(spawn_output);
1093 array<Type> sel_slp_off (2, n_flt, max_sex, nyrs); sel_slp_off.setZero();
1094 array<Type> sel_slp_off_nat (2, n_flt, max_sex, nyrs); sel_slp_off_nat.setZero();
1095 array<Type> sel_inf_off (2, n_flt, max_sex, nyrs); sel_inf_off.setZero();
1096 array<Type> sel_inf_off_nat (2, n_flt, max_sex, nyrs); sel_inf_off_nat.setZero();
1097 array<Type> sel_coff_off (n_flt, max_sex, max_bin, nyrs); sel_coff_off.setZero();
1098 array<Type> sel_coff_off_nat (n_flt, max_sex, max_bin, nyrs); sel_coff_off_nat.setZero();
1099 array<Type> sel_apical_off (n_flt, max_sex, nyrs); sel_apical_off.setZero();
1100 array<Type> sel_apical_off_nat (n_flt, max_sex, nyrs); sel_apical_off_nat.setZero();
1101 array<Type> sel_dn6_off (6, n_flt, max_sex, nyrs); sel_dn6_off.setZero();
1102 array<Type> sel_dn6_off_nat (6, n_flt, max_sex, nyrs); sel_dn6_off_nat.setZero();
1107 int sel_apical_on = 0;
1108 for (
int i = 0; i < linkage_process.size(); i++) {
1113 sel_slp_off, sel_inf_off, sel_coff_off, sel_apical_off, sel_dn6_off,
1116 sel_slp_off_nat, sel_inf_off_nat, sel_coff_off_nat, sel_apical_off_nat, sel_dn6_off_nat,
1121 for(flt = 0; flt < n_flt; flt++){
1122 if(flt_sel_dim(flt) != 1 || flt_sel_type(flt) == 0)
continue;
1124 int wt_idx_mask = nspp * 2 + flt;
1125 for(sex = 0; sex < nsex(sp); sex++){
1126 for(age = 0; age < age_first_selected(flt) && age < nages(sp); age++){
1127 for(
int lp = 0; lp < nlengths_pop(sp); lp++){
1128 for(yr = 0; yr < nyrs; yr++) growth_matrix(wt_idx_mask, sex, age, lp, yr) = 0;
1173 sel_slp_off, sel_slp_off_nat,
1174 sel_inf_off, sel_inf_off_nat,
1175 sel_coff_off, sel_coff_off_nat,
1176 sel_apical_on, log_sel_apical,
1177 sel_apical_off, sel_apical_off_nat,
1178 sel_dn6, sel_dn6_off, sel_dn6_off_nat, sel_dn6_ends
1185 for(flt = 0; flt < n_flt; flt++){
1187 if(growth_model(sp) == 0 || flt_sel_dim(flt) != 1 || flt_sel_type(flt) == 0)
continue;
1188 int wt_idx_sel = nspp * 2 + flt;
1189 for(sex = 0; sex < nsex(sp); sex++){
1190 for(age = 0; age < nages(sp); age++){
1191 for(yr = 0; yr < nyrs; yr++){
1192 Type num = 0, den = 0;
1196 for(
int ln = 0; ln < nlengths_pop(sp); ln++){
1199 Type lenmid = (ln < nlengths_pop(sp) - 1)
1200 ? (lengths_pop(sp, ln) + lengths_pop(sp, ln + 1)) / Type(2.0)
1201 : (nlengths_pop(sp) < 2
1202 ? lengths_pop(sp, ln)
1203 : lengths_pop(sp, ln) + (lengths_pop(sp, ln) - lengths_pop(sp, ln - 1)) / Type(2.0));
1204 Type p_sel = growth_matrix(wt_idx_sel, sex, age, ln, yr) * sel_at_length(flt, sex, ln, yr);
1205 num += p_sel * weight_length_pars(sp, 0) * pow(lenmid, weight_length_pars(sp, 1));
1209 weight_hat(wt_idx_sel, sex, age, yr) = num / (den + Type(1e-30));
1218 calculate_temperature_function(fT, nspp, nyrs, Ceq, Cindex, Qc, Tcm, Tco, Tcl, CK1, CK4, env_index);
1221 calculate_ration(consumption_at_age, nspp, nyrs, nyrs_hind, nsex, nages, Ceq, CA, CB, fday, Pvalue, fT, ration_data, weight_hat);
1230 length_hat, weight_hat, vulnerability, vulnerability_other, gam_a, gam_b);
1236 diet_obs, diet_ctl, diet_prop);
1240 diet_prop, other_food_diet_prop);
1247 F_flt_age.setZero();
1249 Ftarget_at_age.setZero();
1250 Flimit_at_age.setZero();
1251 for(flt = 0; flt < n_flt; flt++) {
1255 if(flt_type(flt) == 1){
1256 for(age = 0; age < nages(sp); age++) {
1257 for(sex = 0; sex < nsex(sp); sex ++){
1258 for(yr = 0; yr < nyrs; yr++) {
1261 if( yr < nyrs_hind){
1262 F_flt_age(flt, sex, age, yr) = sel_at_age(flt, sex, age, yr) * exp(log_F(flt, yr));
1267 if( yr >= nyrs_hind){
1275 proj_F(sp, yr) = Ftarget(sp);
1279 proj_F(sp, yr) = Ftarget(sp);
1283 proj_F(sp, yr) = Ftarget(sp);
1287 proj_F(sp, yr) = Ftarget(sp) * Fmult(sp);
1291 proj_F(sp, yr) = Ftarget(sp);
1295 proj_F(sp, yr) = Ftarget(sp) = Flimit(sp) + QnormHCR(sp);
1299 proj_F(sp, yr) = Ftarget(sp);
1304 if((forecast(sp) == 0) | (estDynamics(sp) > 0)){
1307 F_flt_age(flt, sex, age, yr) = sel_at_age(flt, sex, age, yr) * proj_F_prop(flt) * proj_F(sp, yr);
1311 F_at_age(sp, sex, age, yr) += F_flt_age(flt, sex, age, yr);
1314 Flimit_at_age(sp, sex, age, yr) += sel_at_age(flt, sex, age, yr) * proj_F_prop(flt) * Flimit(sp);
1315 Ftarget_at_age(sp, sex, age, yr) += sel_at_age(flt, sex, age, yr) * proj_F_prop(flt) * Ftarget(sp);
1321 for(yr = 0; yr < nyrs; yr++) {
1323 if( yr < nyrs_hind){
1324 F_flt(flt, yr) = exp(log_F(flt, yr));
1325 F_spp(sp, yr) += exp(log_F(flt, yr));
1329 if( yr >= nyrs_hind){
1330 F_flt(flt, yr) = proj_F_prop(flt) * proj_F(sp, yr);
1331 F_spp(sp, yr) += proj_F_prop(flt) * proj_F(sp, yr);
1343 for(sp = 0; sp < nspp; sp++){
1344 for(sex = 0; sex < nsex(sp); sex++){
1345 for(age = 0; age < nages(sp); age++){
1346 sel_init(sp, sex, age) = 1.0;
1351 for(sp = 0; sp < nspp; sp++){
1353 for(flt = 0; flt < n_flt; flt++){
1354 if((flt_type(flt) == 1) && (flt_spp(flt) == sp)) n_fsh++;
1357 for(sex = 0; sex < nsex(sp); sex++){
1358 for(age = 0; age < nages(sp); age++){
1360 for(flt = 0; flt < n_flt; flt++){
1361 if((flt_type(flt) == 1) && (flt_spp(flt) == sp)){
1362 acc += sel_at_age(flt, sex, age, 0);
1365 sel_init(sp, sex, age) = acc / Type(n_fsh);
1385 int rec_srr_single_density = !((srr_fun == 0) && (srr_pred_fun > 0));
1386 REPORT(rec_srr_single_density);
1393 matrix<Type> rec_dev_drawn_sim(rec_dev.rows(), rec_dev.cols());
1394 matrix<Type> init_dev_drawn_sim(init_dev.rows(), init_dev.cols());
1395 array<Type> log_M1_dev_drawn_sim(log_M1_dev.dim);
1396 rec_dev_drawn_sim.setZero();
1397 init_dev_drawn_sim.setZero();
1398 log_M1_dev_drawn_sim.setZero();
1400 for(sp = 0; sp < nspp; sp++){
1410 if(simulate_state(0) == 1 && simulate_period(0) == 1 && rec_srr_single_density){
1411 for(yr = 0; yr < nyrs_hind; yr++){
1412 rec_dev(sp, yr) = rnorm(-bias_adjust_proc*
square(R_sd(sp))/2.0, R_sd(sp));
1413 rec_dev_drawn_sim(sp, yr) = 1;
1422 if(simulate_state(0) == 1 && simulate_period(0) == 1 && rec_srr_single_density &&
1423 (initMode > 1) && (initMode != 5)){
1424 for(age = 1; age < nages(sp); age++){
1425 init_dev(sp, age - 1) = rnorm(-bias_adjust_proc*
square(R_sd(sp))/2.0, R_sd(sp));
1426 init_dev_drawn_sim(sp, age - 1) = 1;
1441 if(simulate_state(1) == 1 && simulate_period(0) == 1){
1442 for(sp = 0; sp < nspp; sp++){
1443 int num_re_sexes = (M1_model(sp) == 2 && nsex(sp) > 1) ? 2 : 1;
1445 if((M1_re(sp) == 1) || (M1_re(sp) == 4)){
1446 Type sigma_M = exp(M1_dev_log_sd(sp, 0));
1447 Type rho_M_a =
rho_trans(M1_rho(sp, 0, 0));
1448 Type Sigma_M = pow(pow(sigma_M, 2) / (1.0 - pow(rho_M_a, 2)), 0.5);
1449 for(
int s = 0; s < num_re_sexes; s++){
1450 vector<Type> M_re_age(nages(sp));
1451 AR1(rho_M_a).simulate(M_re_age);
1452 M_re_age *= Sigma_M;
1453 int sex_end = (num_re_sexes == 1) ? nsex(sp) : s + 1;
1454 for(
int sex = s; sex < sex_end; sex++){
1455 for(age = 0; age < nages(sp); age++){
1456 for(yr = 0; yr < nyrs; yr++){
1457 log_M1_dev(sp, sex, age, yr) = M_re_age(age);
1458 log_M1_dev_drawn_sim(sp, sex, age, yr) = 1;
1465 if((M1_re(sp) == 2) || (M1_re(sp) == 5)){
1466 Type sigma_M = exp(M1_dev_log_sd(sp, 0));
1467 Type rho_M_y =
rho_trans(M1_rho(sp, 0, 1));
1468 Type Sigma_M = pow(pow(sigma_M, 2) / (1.0 - pow(rho_M_y, 2)), 0.5);
1469 for(
int s = 0; s < num_re_sexes; s++){
1470 vector<Type> M_re_yr(nyrs_hind);
1471 AR1(rho_M_y).simulate(M_re_yr);
1473 int sex_end = (num_re_sexes == 1) ? nsex(sp) : s + 1;
1474 for(
int sex = s; sex < sex_end; sex++){
1475 for(yr = 0; yr < nyrs_hind; yr++){
1476 for(age = 0; age < nages(sp); age++){
1477 log_M1_dev(sp, sex, age, yr) = M_re_yr(yr);
1478 log_M1_dev_drawn_sim(sp, sex, age, yr) = 1;
1485 if((M1_re(sp) == 3) || (M1_re(sp) == 6)){
1486 Type sigma_M = exp(M1_dev_log_sd(sp, 0));
1487 Type rho_M_a =
rho_trans(M1_rho(sp, 0, 0));
1488 Type rho_M_y =
rho_trans(M1_rho(sp, 0, 1));
1489 Type Sigma_M = pow(pow(sigma_M, 2) / ((1.0 - pow(rho_M_y, 2)) * (1.0 - pow(rho_M_a, 2))), 0.5);
1490 for(
int s = 0; s < num_re_sexes; s++){
1491 array<Type> M_re_a_yr(nages(sp), nyrs_hind);
1494 SEPARABLE(AR1(rho_M_y), AR1(rho_M_a)).simulate(M_re_a_yr);
1495 int sex_end = (num_re_sexes == 1) ? nsex(sp) : s + 1;
1496 for(
int sex = s; sex < sex_end; sex++){
1497 for(age = 0; age < nages(sp); age++){
1498 for(yr = 0; yr < nyrs_hind; yr++){
1499 log_M1_dev(sp, sex, age, yr) = Sigma_M * M_re_a_yr(age, yr);
1500 log_M1_dev_drawn_sim(sp, sex, age, yr) = 1;
1512 matrix<Type> rec_dev_sim = rec_dev;
1513 matrix<Type> init_dev_sim = init_dev;
1514 array<Type> log_M1_dev_sim = log_M1_dev;
1515 REPORT(rec_dev_sim);
1516 REPORT(init_dev_sim);
1517 REPORT(log_M1_dev_sim);
1518 REPORT(rec_dev_drawn_sim);
1519 REPORT(init_dev_drawn_sim);
1520 REPORT(log_M1_dev_drawn_sim);
1528 const bool reporting = isDouble<Type>::value;
1531 const bool diet_fitted = (msmMode > 2) || (
imax(suitMode) > 0);
1532 const bool diet_used = reporting || diet_fitted;
1534 const bool dynamic_B0_used = (DynamicHCR == 1) || reporting;
1537 for(
int iter = 0; iter < niter; iter++) {
1540 for(sp = 0; sp < nspp; sp++) {
1541 for(sex = 0; sex < nsex(sp); sex ++){
1543 for(
int i = 0; i < M1_beta.dim(2); i++){
1544 beta_M1_tmp(i) = M1_beta(sp, sex, i);
1546 for(age = 0; age < nages(sp); age++) {
1547 for(yr = 0; yr < nyrs; yr++) {
1549 env_M1_tmp = env_index.row(yr);
1550 M1_mult = env_M1_tmp * beta_M1_tmp;
1556 M1_at_age(sp, sex, age, yr) = exp(
1557 log_M1(sp, sex, age)
1558 + log_M1_dev(sp, sex, age, yr)
1560 + M_linkage_offset(sp, sex, age, yr)
1561 ) + M_linkage_offset_nat(sp, sex, age, yr);
1562 M_at_age(sp, sex, age, yr) = M1_at_age(sp, sex, age, yr) + M2_at_age(sp, sex, age, yr);
1563 M_at_age_dB0(sp, sex, age, yr) = M1_at_age(sp, sex, age, yr) + M2_at_age_dB0(sp, sex, age, yr);
1564 M_at_age_dBF(sp, sex, age, yr) = M1_at_age(sp, sex, age, yr) + M2_at_age_dBF(sp, sex, age, yr);
1565 Z_at_age(sp, sex, age, yr) = M1_at_age(sp, sex, age, yr) + F_at_age(sp, sex, age, yr) + M2_at_age(sp, sex, age, yr);
1577 SPRtarget.setZero();
1582 for(sp = 0; sp < nspp; sp++) {
1585 int term_yr = nyrs_hind - 1;
1586 wt_idx_ssb = 2 * sp + 1;
1592 vector<Type> Z_unfished(na), Z_limit(na), Z_target(na), Z_init(na);
1593 vector<Type> wt_term(na), wt_first(na), mature_at_age(na);
1594 Type female_split = (nsex(sp) == 1) ? Type(1.0) : sex_ratio(sp, 0);
1596 for(age = 0; age < na; age++){
1597 Z_unfished(age) = M_at_age(sp, 0, age, term_yr);
1598 Z_limit(age) = M_at_age(sp, 0, age, term_yr) + Flimit_at_age(sp, 0, age, term_yr);
1599 Z_target(age) = M_at_age(sp, 0, age, term_yr) + Ftarget_at_age(sp, 0, age, term_yr);
1600 Z_init(age) = M_at_age(sp, 0, age, 0) + Finit(sp) * sel_init(sp, 0, age);
1605 wt_term(age) = spawn_output(sp, age, term_yr);
1606 wt_first(age) = spawn_output(sp, age, 0);
1607 mature_at_age(age) = female_split;
1621 for(age = 0; age < na; age++){
1622 NbyageSPR(0, sp, age) = n_unfished(age);
1623 NbyageSPR(1, sp, age) = n_limit(age);
1624 NbyageSPR(2, sp, age) = n_target(age);
1625 NbyageSPR(3, sp, age) = n_init(age);
1634 zero_N_pen.setZero();
1635 for( sp = 0; sp < nspp ; sp++) {
1638 if((msmMode > 0) & (srr_fun > 1)){
1639 for(yr = 0; yr < nyrs; yr++){ steepness(sp, yr) = 0; }
1640 R_init(sp) = R0(sp, 0);
1641 }
else switch(srr_fun){
1644 for(yr = 0; yr < nyrs; yr++){ steepness(sp, yr) = 0.99; }
1646 R_init(sp) = R0(sp, 0);
1651 for(yr = 0; yr < nyrs; yr++){ steepness(sp, yr) = 0.99; }
1653 R_init(sp) = R0(sp, 0);
1660 for(yr = 0; yr < nyrs; yr++){
1661 steepness(sp, yr) = alpha(sp, yr) * SPR0(sp)/(4.0 + alpha(sp, yr) * SPR0(sp));
1667 R0(sp, 0) = (alpha(sp, 0) - 1.0/SPR0(sp)) / Beta(sp, 0);
1668 R_init(sp) = (alpha(sp, 0) - 1.0/SPRFinit(sp)) / Beta(sp, 0);
1670 if (rec_floor_on) { Type pen_R = 0; R0(sp, 0) =
posfun(R0(sp, 0), Type(1e-3), pen_R); R_init(sp) =
posfun(R_init(sp), Type(1e-3), pen_R); zero_N_pen(sp) += pen_R; }
1677 for(yr = 0; yr < nyrs; yr++){
1678 steepness(sp, yr) = alpha(sp, yr) * SPR0(sp)/(4.0 + alpha(sp, yr) * SPR0(sp));
1682 R0(sp, 0) = (alpha(sp, 0) - 1.0/SPR0(sp)) / Beta(sp, 0);
1683 R_init(sp) = (alpha(sp, 0) - 1.0/SPRFinit(sp)) / Beta(sp, 0);
1685 if (rec_floor_on) { Type pen_R = 0; R0(sp, 0) =
posfun(R0(sp, 0), Type(1e-3), pen_R); R_init(sp) =
posfun(R_init(sp), Type(1e-3), pen_R); zero_N_pen(sp) += pen_R; }
1692 for(yr = 0; yr < nyrs; yr++){
1693 { Type aS = alpha(sp, yr) * SPR0(sp);
1694 if (rec_floor_on) { Type pen_d = 0; aS =
posfun(aS, Type(1e-3), pen_d); }
1695 steepness(sp, yr) = 0.2 * exp(0.8*log(aS)); }
1700 ricker_intercept = alpha(sp, 0) * SPR0(sp) - 1.0;
1701 ricker_intercept =
posfun(ricker_intercept, Type(0.001), pen_rk) + 1.0;
1705 R0(sp, 0) = log(ricker_intercept)/(Beta(sp, 0) * SPR0(sp)/1000000.0);
1708 ricker_intercept = alpha(sp, 0) * SPRFinit(sp) - 1.0;
1709 ricker_intercept =
posfun(ricker_intercept, Type(0.001), pen_rk) + 1.0;
1710 R_init(sp) = log(ricker_intercept)/(Beta(sp, 0) * SPRFinit(sp)/1000000.0);
1711 zero_N_pen(sp) += pen_rk;
1718 for(yr = 0; yr < nyrs; yr++){
1719 { Type aS = alpha(sp, yr) * SPR0(sp);
1720 if (rec_floor_on) { Type pen_d = 0; aS =
posfun(aS, Type(1e-3), pen_d); }
1721 steepness(sp, yr) = 0.2 * exp(0.8*log(aS)); }
1726 ricker_intercept = alpha(sp, 0) * SPR0(sp) - 1.0;
1727 ricker_intercept =
posfun(ricker_intercept, Type(0.001), pen_rk) + 1.0;
1729 R0(sp, 0) = log(ricker_intercept)/(Beta(sp, 0) * SPR0(sp)/1000000.0);
1732 ricker_intercept = alpha(sp, 0) * SPRFinit(sp) - 1.0;
1733 ricker_intercept =
posfun(ricker_intercept, Type(0.001), pen_rk) + 1.0;
1734 R_init(sp) = log(ricker_intercept)/(Beta(sp, 0) * SPRFinit(sp)/1000000.0);
1735 zero_N_pen(sp) += pen_rk;
1740 error(
"Invalid 'srr_fun'");
1750 if((srr_fun < 2) & (srr_pred_fun > 1) & (msmMode == 0)){
1751 for(yr = 0; yr < nyrs; yr++){
1752 if((srr_pred_fun == 2) | (srr_pred_fun == 3)){
1753 steepness(sp, yr) = alpha(sp, yr) * SPR0(sp)/(4.0 + alpha(sp, yr) * SPR0(sp));
1755 if((srr_pred_fun == 4) | (srr_pred_fun == 5)){
1756 { Type aS = alpha(sp, yr) * SPR0(sp);
1757 if (rec_floor_on) { Type pen_d = 0; aS =
posfun(aS, Type(1e-3), pen_d); }
1758 steepness(sp, yr) = 0.2 * exp(0.8*log(aS)); }
1764 if((srr_fun < 2) & (srr_pred_fun > 1) & (msmMode > 0)){
1765 for(yr = 0; yr < nyrs; yr++){ steepness(sp, yr) = 0; }
1773 for(sp = 0; sp < nspp; sp++) {
1777 sex_ratio(sp, 0) = 1.0;
1786 for(age = 0; age < nages(sp); age++){
1787 for(sex = 0; sex < nsex(sp); sex ++){
1789 switch(estDynamics(sp)){
1796 R(sp, 0) = R_init(sp) * exp(rec_dev(sp, 0));
1797 N_at_age(sp, 0, 0, 0) = R(sp, 0) * sex_ratio(sp, 0);
1802 N_at_age(sp, 1, 0, 0) = R(sp, 0) * (1-sex_ratio(sp, 0));
1808 N_eq(sp, 0, 0) = R_init(sp) * exp(init_level) * sex_ratio(sp, 0);
1810 N_eq(sp, 1, 0) = R_init(sp) * exp(init_level) * (1-sex_ratio(sp, 0));
1817 N_at_age(sp, 0, age, 0) = exp(init_dev(sp, age-1)) * sex_ratio(sp, 0);
1819 N_at_age(sp, 1, age, 0) = exp(init_dev(sp, age-1)) * (1-sex_ratio(sp, 0));
1823 N_eq(sp, 0, age) = N_at_age(sp, 0, age, 0);
1825 N_eq(sp, 1, age) = N_at_age(sp, 1, age, 0);
1844 Type init_log_scalar = init_level;
1846 init_log_scalar += rec_dev(sp, 0);
1852 if((initMode == 1) | (initMode == 2) | (initMode == 3) | (initMode == 5)){
1853 mort_sum(sp, age) = 0;
1854 for(
int age_tmp = 0; age_tmp < age; age_tmp++){
1855 mort_sum(sp, age) += M1_at_age(sp, sex, age_tmp, 0) + Finit(sp);
1863 mort_sum(sp, age) = 0;
1864 for(
int age_tmp = 0; age_tmp < age; age_tmp++){
1865 mort_sum(sp, age) += M1_at_age(sp, sex, age_tmp, 0)
1866 + Finit(sp) * sel_init(sp, sex, age_tmp);
1871 mort_sum(sp, age) = 0;
1872 for(
int age_tmp = 0; age_tmp < age; age_tmp++){
1873 mort_sum(sp, age) += M1_at_age(sp, sex, age_tmp, 0);
1875 mort_sum(sp, age) += Finit(sp);
1879 if((age > 0) & (age < nages(sp) - 1)) {
1882 N_at_age(sp, 0, age, 0) = R_init(sp) * exp( - mort_sum(sp, age) + init_dev(sp, age - 1) + init_log_scalar) * sex_ratio(sp, 0);
1883 N_eq(sp, 0, age) = R_init(sp) * exp( - mort_sum(sp, age) + init_log_scalar) * sex_ratio(sp, 0);
1886 N_at_age(sp, 1, age, 0) = R_init(sp) * exp( - mort_sum(sp, age) + init_dev(sp, age - 1) + init_log_scalar) * (1-sex_ratio(sp, 0));
1887 N_eq(sp, 1, age) = R_init(sp) * exp( - mort_sum(sp, age) + init_log_scalar) * (1-sex_ratio(sp, 0));
1892 if(age == (nages(sp) - 1)) {
1898 Type Z_plus = M1_at_age(sp, sex, nages(sp) - 1, 0) + Finit(sp) *
1899 ((initMode == 6) ? sel_init(sp, sex, nages(sp) - 1) : Type(1.0));
1900 Type plus_grp = 1 - exp(-Z_plus);
1903 N_at_age(sp, 0, age, 0) = R_init(sp) * exp( - mort_sum(sp, age) + init_dev(sp, age - 1) + init_log_scalar) / plus_grp * sex_ratio(sp, 0);
1904 N_eq(sp, 0, age) = R_init(sp) * exp( - mort_sum(sp, age) + init_log_scalar) / plus_grp * sex_ratio(sp, 0);
1908 N_at_age(sp, 1, age, 0) = R_init(sp) * exp( - mort_sum(sp, age) + init_dev(sp, age - 1) + init_log_scalar) / plus_grp * (1-sex_ratio(sp, 0));
1909 N_eq(sp, 1, age) = R_init(sp) * exp( - mort_sum(sp, age) + init_log_scalar) / plus_grp * (1-sex_ratio(sp, 0));
1917 N_at_age(sp, sex, age, 0) = pop_scalar(sp) * NByageFixed(sp, sex, age, 0);
1921 N_at_age(sp, sex, age, 0) = pop_scalar(sp) * NByageFixed(sp, sex, age, 0);
1925 error(
"Invalid 'estDynamics'");
1929 wt_idx_pop = 2 * sp;
1930 biomass_at_age(sp, sex, age, 0) = N_at_age(sp, sex, age, 0) * weight_hat( wt_idx_pop, sex, age, 0);
1931 biomass(sp, 0) += N_at_age(sp, sex, age, 0) * weight_hat( wt_idx_pop, sex, age, 0);
1935 wt_idx_ssb = 2 * sp + 1;
1936 ssb(sp, 0) += N_at_age(sp, 0, age, 0) * exp(-Z_at_age(sp, 0, age, 0) * spawn_month(sp)/12) * spawn_output(sp, age, 0);
1943 for(sp = 0; sp < nspp; sp++) {
1944 for(yr = 1; yr < nyrs_hind; yr++) {
1947 int srr_switch = srr_fun;
1948 if(yr >= nyrs_srrmean){
1949 srr_switch = srr_pred_fun;
1960 int spawn_yr = yr - minage(sp);
1961 int srr_use = (spawn_yr < 0) ? 0 : srr_switch;
1962 Type ssb_tmp = (spawn_yr < 0) ? Type(0.0) : ssb(sp, spawn_yr);
1965 Type rec_mean = (spawn_yr < 0) ? R_init(sp) : R0(sp, yr);
1967 R(sp, yr) =
calculate_recruitment(srr_use, rec_mean, ssb_tmp, alpha(sp, yr), Beta(sp, yr), rec_dev(sp, yr), SPR0(sp));
1970 if (rec_floor_on) { Type pen_R = 0; R(sp, yr) =
posfun(R(sp, yr), Type(1e-3), pen_R); zero_N_pen(sp) += pen_R; }
1972 N_at_age(sp, 0, 0, yr) = R(sp, yr) * sex_ratio(sp, 0);
1974 N_at_age(sp, 1, 0, yr) = R(sp, yr) * (1.0-sex_ratio(sp, 0));
1979 for(age = 0; age < nages(sp); age++){
1980 for(sex = 0; sex < nsex(sp); sex ++){
1982 switch(estDynamics(sp)){
1986 if(age < (nages(sp) - 1)) {
1987 N_at_age(sp, sex, age + 1, yr) = N_at_age(sp, sex, age, yr - 1) * exp(-Z_at_age(sp, sex, age, yr-1));
1991 if(age == (nages(sp) - 1)) {
1992 N_at_age(sp, sex, age, yr) = N_at_age(sp, sex, age - 1, yr - 1) * exp(-Z_at_age(sp, sex, age-1, yr-1)) + N_at_age(sp, sex, age, yr - 1) * exp(-Z_at_age(sp, sex, age, yr-1));
1997 N_at_age(sp, sex, age, yr) = pop_scalar(sp) * NByageFixed(sp, sex, age, yr);
2001 N_at_age(sp, sex, age, yr) = pop_scalar(sp) * NByageFixed(sp, sex, age, yr);
2005 error(
"Invalid 'estDynamics'");
2009 { Type pen_N = 0; N_at_age(sp, sex, age, yr) =
posfun(N_at_age(sp, sex, age, yr), Type(0.001), pen_N); zero_N_pen(sp) += pen_N; }
2012 wt_idx_pop = 2 * sp ;
2013 biomass_at_age(sp, sex, age, yr) = N_at_age(sp, sex, age, yr) * weight_hat( wt_idx_pop, sex, age, yr );
2014 biomass(sp, yr) += biomass_at_age(sp, sex, age, yr);
2018 wt_idx_ssb = 2 * sp + 1;
2019 ssb(sp, yr) += N_at_age(sp, 0, age, yr) * exp(-Z_at_age(sp, 0, age, yr) * spawn_month(sp)/12.0) * spawn_output(sp, age, yr);
2029 for(sp = 0; sp < nspp; sp++) {
2030 for(yr = 0; yr < nyrs_srrmean; yr++) {
2031 avg_R(sp) += R(sp, yr)/Type(nyrs_srrmean);
2038 N_at_age_dBF.setZero();
2039 N_at_age_dB0.setZero();
2043 DynamicB0.setZero();
2044 DynamicSB0.setZero();
2045 DynamicSBF.setZero();
2046 for(sp = 0; sp < nspp; sp++) {
2047 for(yr = 0; yr < nyrs; yr++) {
2052 for(sex = 0; sex < nsex(sp); sex ++){
2053 for(age = 0; age < nages(sp); age++){
2054 NByage0(sp, sex, age , 0) = NByageF(sp, sex, age , 0) = N_at_age_dBF(sp, sex, age, 0) = N_at_age_dB0(sp, sex, age, 0) = N_at_age(sp,sex,age,0);
2062 if((proj_mean_rec == 1) & (srr_pred_fun < 2)){
2064 NByage0(sp, 0, 0, yr) = avg_R(sp);
2065 NByageF(sp, 0, 0, yr) = avg_R(sp);
2072 N_at_age_dBF(sp, 0, 0, yr) = N_at_age_dB0(sp, 0, 0, yr) = R(sp, yr);
2074 if(yr >= nyrs_hind){
2075 N_at_age_dBF(sp, 0, 0, yr) = N_at_age_dB0(sp, 0, 0, yr) = exp(log(avg_R(sp)) + rec_dev(sp, yr));
2088 if((proj_mean_rec == 0) | (srr_pred_fun >= 2)){
2093 int rp_yr = yr - minage(sp);
2094 if(rp_yr < 0){ rp_yr = 0; }
2097 NByage0(sp, 0, 0, yr) =
calculate_recruitment(srr_pred_fun, R0(sp, yr), SB0(sp, rp_yr), alpha(sp, yr), Beta(sp, yr), Type(0.0), SPR0(sp));
2098 NByageF(sp, 0, 0, yr) =
calculate_recruitment(srr_pred_fun, R0(sp, yr), SBF(sp, rp_yr), alpha(sp, yr), Beta(sp, yr), Type(0.0), SPR0(sp));
2103 Type rdev = rec_dev(sp, yr);
2104 if(yr < minage(sp)){
2105 Type R_early = (yr < nyrs_hind) ? R(sp, yr) : R_init(sp) * exp(rdev);
2106 N_at_age_dB0(sp, 0, 0, yr) = R_early;
2107 N_at_age_dBF(sp, 0, 0, yr) = R_early;
2112 if((srr_fun != srr_pred_fun) & (yr < nyrs_srrmean)){
2113 Type R_curve =
calculate_recruitment(srr_pred_fun, R0(sp, yr), ssb(sp, rp_yr), alpha(sp, yr), Beta(sp, yr), Type(0.0), SPR0(sp));
2115 if (rec_floor_on) { Type pen_R = 0; R_curve =
posfun(R_curve, Type(1e-3), pen_R); zero_N_pen(sp) += pen_R; }
2116 rdev = log(R(sp, yr)) - log(R_curve);
2119 N_at_age_dB0(sp, 0, 0, yr) =
calculate_recruitment(srr_pred_fun, R0(sp, yr), DynamicSB0(sp, rp_yr), alpha(sp, yr), Beta(sp, yr), rdev, SPR0(sp));
2120 N_at_age_dBF(sp, 0, 0, yr) =
calculate_recruitment(srr_pred_fun, R0(sp, yr), DynamicSBF(sp, rp_yr), alpha(sp, yr), Beta(sp, yr), rdev, SPR0(sp));
2128 NByage0(sp, 0, 0, yr) = NByage0(sp, 0, 0, yr) * sex_ratio(sp, 0);
2129 NByageF(sp, 0, 0, yr) = NByageF(sp, 0, 0, yr) * sex_ratio(sp, 0);
2130 N_at_age_dB0(sp, 0, 0, yr) = N_at_age_dB0(sp, 0, 0, yr) * sex_ratio(sp, 0);
2131 N_at_age_dBF(sp, 0, 0, yr) = N_at_age_dBF(sp, 0, 0, yr) * sex_ratio(sp, 0);
2135 NByage0(sp, 1, 0, yr) = NByage0(sp, 0, 0, yr) / sex_ratio(sp, 0) * (1.0-sex_ratio(sp, 0));
2136 NByageF(sp, 1, 0, yr) = NByageF(sp, 0, 0, yr) / sex_ratio(sp, 0) * (1.0-sex_ratio(sp, 0));
2137 N_at_age_dB0(sp, 1, 0, yr) = N_at_age_dB0(sp, 0, 0, yr) / sex_ratio(sp, 0) * (1.0-sex_ratio(sp, 0));
2138 N_at_age_dBF(sp, 1, 0, yr) = N_at_age_dBF(sp, 0, 0, yr) / sex_ratio(sp, 0) * (1.0-sex_ratio(sp, 0));
2143 for(sex = 0; sex < nsex(sp); sex ++){
2144 for(age = 1; age < nages(sp)-1; age++) {
2145 NByage0(sp, sex, age, yr) = NByage0(sp, sex, age-1, yr-1) * exp(-M_at_age(sp, sex, age-1, yr-1));
2147 NByageF(sp, sex, age, yr) = NByageF(sp, sex, age-1, yr-1) * exp(-M_at_age(sp, sex, age-1, yr-1) - Ftarget_at_age(sp, sex, age-1, yr-1));
2152 N_at_age_dB0(sp, sex, age, yr) = N_at_age_dB0(sp, sex, age-1, yr-1) * exp(-M_at_age_dB0(sp, sex, age-1, yr - 1));
2154 N_at_age_dBF(sp, sex, age, yr) = N_at_age_dBF(sp, sex, age-1, yr-1) * exp(-M_at_age_dBF(sp, sex, age-1, yr - 1) - Ftarget_at_age(sp, sex, age-1, yr-1));
2158 NByage0(sp, sex, nages(sp)-1, yr) = NByage0(sp, sex, nages(sp)-2, yr - 1) * exp(-M_at_age(sp, sex, nages(sp)-2, yr - 1)) + NByage0(sp, sex, nages(sp)-1, yr - 1) * exp(-M_at_age(sp, sex, nages(sp)-1, yr - 1));
2160 NByageF(sp, sex, nages(sp)-1, yr) = NByageF(sp, sex, nages(sp)-2, yr - 1) * exp(-M_at_age(sp, sex, nages(sp)-2, yr - 1) - Ftarget_at_age(sp, sex, nages(sp)-2, yr-1)) + NByageF(sp, sex, nages(sp)-1, yr - 1) * exp(-M_at_age(sp, sex, nages(sp)-1, yr - 1) - Ftarget_at_age(sp, sex, nages(sp)-1, yr-1));
2162 N_at_age_dB0(sp, sex, nages(sp)-1, yr) = N_at_age_dB0(sp, sex, nages(sp)-2, yr - 1) * exp(-M_at_age_dB0(sp, sex, nages(sp)-2, yr - 1)) + N_at_age_dB0(sp, sex, nages(sp)-1, yr - 1) * exp(-M_at_age_dB0(sp, sex, nages(sp)-1, yr - 1));
2164 N_at_age_dBF(sp, sex, nages(sp)-1, yr) = N_at_age_dBF(sp, sex, nages(sp)-2, yr - 1) * exp(-M_at_age_dBF(sp, sex, nages(sp)-2, yr - 1) - Ftarget_at_age(sp, sex, nages(sp)-2, yr-1)) + N_at_age_dBF(sp, sex, nages(sp)-1, yr - 1) * exp(-M_at_age_dBF(sp, sex, nages(sp)-1, yr - 1) - Ftarget_at_age(sp, sex, nages(sp)-1, yr-1));
2171 if(estDynamics(sp) > 0){
2172 for(sex = 0; sex < nsex(sp); sex ++){
2173 for(age = 0; age < nages(sp); age++){
2174 Type n_fixed = (yr < nyrs_hind) ? N_at_age(sp, sex, age, yr) :
2175 pop_scalar(sp) * NByageFixed(sp, sex, age, yr);
2176 N_at_age_dB0(sp, sex, age, yr) = n_fixed;
2177 N_at_age_dBF(sp, sex, age, yr) = n_fixed;
2184 for(age = 0; age < nages(sp); age++) {
2186 wt_idx_ssb = 2 * sp + 1;
2190 SB0(sp, yr) += NByage0(sp, 0, age, yr) * spawn_output(sp, age, nyrs_hind - 1) * exp(-M_at_age(sp, 0, age, yr) * spawn_month(sp)/12.0);
2191 SBF(sp, yr) += NByageF(sp, 0, age, yr) * spawn_output(sp, age, nyrs_hind - 1) * exp(-(M_at_age(sp, 0, age, yr) + Ftarget_at_age(sp, 0, age, yr)) * spawn_month(sp)/12.0);
2192 DynamicSB0(sp, yr) += N_at_age_dB0(sp, 0, age, yr) * spawn_output(sp, age, yr) * exp(-M_at_age_dB0(sp, 0, age, yr) * spawn_month(sp)/12.0);
2193 DynamicSBF(sp, yr) += N_at_age_dBF(sp, 0, age, yr) * spawn_output(sp, age, yr) * exp(-(M_at_age_dBF(sp, 0, age, yr) + Ftarget_at_age(sp, 0, age, yr)) * spawn_month(sp)/12.0);
2195 for(sex = 0; sex < nsex(sp); sex ++){
2197 wt_idx_pop = 2 * sp;
2198 B0(sp, yr) += NByage0(sp, sex, age, yr) * weight_hat( wt_idx_pop, sex, age, nyrs_hind - 1 );
2199 DynamicB0(sp, yr) += N_at_age_dB0(sp, sex,age, yr) * weight_hat( wt_idx_pop, sex, age, yr );
2206 SB0(sp, yr) = MSSB0(sp);
2207 B0(sp, yr) = MSB0(sp);
2214 for(sp = 0; sp < nspp; sp++) {
2215 for(yr = nyrs_hind; yr < nyrs; yr++){
2219 Type ref_SB0 = (DynamicHCR == 1) ? DynamicSB0(sp, yr-1) : SB0(sp, nyrs-1);
2220 Type ref_SBF = (DynamicHCR == 1) ? DynamicSBF(sp, yr-1) : SBF(sp, nyrs-1);
2224 proj_F(sp, yr) = 0.0;
2244 if(ssb(sp, yr-1) < ref_SBF){
2245 proj_F(sp, yr) = Ftarget(sp) * (((ssb(sp, yr-1)/ref_SBF)-Alpha(sp))/(1-Alpha(sp)));
2247 if((ssb(sp, nyrs_hind-1) < ref_SB0 * Plimit(sp)) || (ssb(sp, yr-1) / ref_SBF < Alpha(sp))){
2248 proj_F(sp, yr) = 0.0;
2253 if(ssb(sp, yr-1) < ref_SB0 * Ptarget(sp)){
2254 proj_F(sp, yr) = (Flimit(sp) + QnormHCR(sp)) * (ref_SB0 * Ptarget(sp) * (ssb(sp, yr-1) - ref_SB0 * Plimit(sp))) / (ssb(sp, yr-1) * (ref_SB0 * (Ptarget(sp) - Plimit(sp))));
2256 if(ssb(sp, yr-1) < ref_SB0 * Plimit(sp)){
2257 proj_F(sp, yr) = 0.0;
2262 if(ssb(sp, yr-1) < ref_SB0 * Ptarget(sp)){
2263 proj_F(sp, yr) = Ftarget(sp) * ((ssb(sp, yr-1)/(ref_SB0 * Plimit(sp)))-1);
2265 if(ssb(sp, yr-1) < ref_SB0 * Plimit(sp)){
2266 proj_F(sp, yr) = 0.0;
2273 if((forecast(sp) == 0) | (estDynamics(sp) > 0)){
2274 proj_F(sp, yr) = 0.0;
2278 for(age = 0; age < nages(sp); age++) {
2279 for(sex = 0; sex < nsex(sp); sex ++){
2280 F_at_age(sp, sex, age, yr) = 0.0;
2285 F_spp(sp, yr) = proj_F(sp, yr);
2286 for(flt = 0; flt < n_flt; flt++) {
2287 if((sp == flt_spp(flt)) & (flt_type(flt) == 1)){
2288 F_flt(flt, yr) = proj_F_prop(flt) * proj_F(sp, yr);
2289 for(age = 0; age < nages(sp); age++) {
2290 for(sex = 0; sex < nsex(sp); sex ++){
2291 F_flt_age(flt, sex, age, yr) = sel_at_age(flt, sex, age, nyrs_hind - 1) * proj_F_prop(flt) * proj_F(sp, yr);
2292 if(flt_type(flt) == 1){
2293 F_at_age(sp, sex, age, yr) += F_flt_age(flt, sex, age, yr);
2302 for(age = 0; age < nages(sp); age++) {
2303 for(sex = 0; sex < nsex(sp); sex ++){
2304 M_at_age(sp, sex, age, yr) = M1_at_age(sp, sex, age, yr) + M2_at_age(sp, sex, age, yr);
2305 Z_at_age(sp, sex, age, yr) = M1_at_age(sp, sex, age, yr) + F_at_age(sp, sex, age, yr) + M2_at_age(sp, sex, age, yr);
2313 if(proj_mean_rec == 1){
2314 R(sp, yr) = avg_R(sp);
2318 if(proj_mean_rec == 0){
2323 int proj_spawn_yr = yr - minage(sp);
2324 int proj_srr_use = (proj_spawn_yr < 0) ? 0 : srr_pred_fun;
2325 Type ssb_tmp = (proj_spawn_yr < 0) ? Type(0.0) : ssb(sp, proj_spawn_yr);
2326 Type proj_rec_mean = (proj_spawn_yr < 0) ? R_init(sp) : R0(sp, yr);
2328 R(sp, yr) =
calculate_recruitment(proj_srr_use, proj_rec_mean, ssb_tmp, alpha(sp, yr), Beta(sp, yr), rec_dev(sp, yr), SPR0(sp));
2331 N_at_age(sp, 0, 0 , yr) = R(sp, yr) * sex_ratio(sp, 0);
2333 N_at_age(sp, 1, 0 , yr) = R(sp, yr) * (1 - sex_ratio(sp, 0));
2338 for(age = 0; age < nages(sp); age++) {
2339 for(sex = 0; sex < nsex(sp); sex ++){
2340 switch(estDynamics(sp)){
2344 if(age < (nages(sp) - 1)) {
2345 N_at_age(sp, sex, age + 1, yr) = N_at_age(sp, sex, age, yr - 1) * exp(-Z_at_age(sp, sex, age, yr-1));
2349 if(age == (nages(sp) - 1)) {
2350 N_at_age(sp, sex, age, yr) = N_at_age(sp, sex, age - 1, yr - 1) * exp(-Z_at_age(sp, sex, age-1, yr-1)) + N_at_age(sp, sex, age, yr - 1) * exp(-Z_at_age(sp, sex, age, yr-1));
2355 N_at_age(sp, sex, age, yr) = pop_scalar(sp) * NByageFixed(sp, sex, age, yr);
2359 N_at_age(sp, sex, age, yr) = pop_scalar(sp) * NByageFixed(sp, sex, age, yr);
2363 error(
"Invalid 'estDynamics'");
2367 wt_idx_pop = 2 * sp;
2368 biomass_at_age(sp, sex, age, yr) = N_at_age(sp, sex, age, yr) * weight_hat( wt_idx_pop, sex, age, nyrs_hind-1 );
2369 biomass(sp, yr) += biomass_at_age(sp, sex, age, yr);
2373 wt_idx_ssb = 2 * sp + 1;
2374 ssb(sp, yr) += N_at_age(sp, 0, age, yr) * exp(-Z_at_age(sp, 0, age, yr) * spawn_month(sp)/12.0) * spawn_output(sp, age, nyrs_hind-1);
2385 const int first_yr = 0;
2386 if (R0.cols() != nyrs || alpha.cols() != nyrs || Beta.cols() != nyrs) {
2387 error(
"Section 6.9: R0/alpha/Beta column count does not equal nyrs; "
2388 "recruitment-parameter matrices were sized inconsistently.");
2390 for(sp = 0; sp < nspp; sp++) {
2395 if((msmMode > 0) & (srr_pred_fun > 1)){
2396 R_hat(sp, first_yr) = R_init(sp);
2398 switch(srr_pred_fun){
2400 R_hat(sp, first_yr) = R0(sp, first_yr);
2404 R_hat(sp, first_yr) = R0(sp, first_yr);
2408 R_hat(sp, first_yr) = (alpha(sp, first_yr) - 1/SPRFinit(sp)) / Beta(sp, first_yr);
2412 R_hat(sp, first_yr) = (alpha(sp, first_yr) - 1/SPRFinit(sp)) / Beta(sp, first_yr);
2416 { Type aS = alpha(sp, first_yr) * SPRFinit(sp);
2417 if (rec_floor_on) { Type pen_d = 0; aS =
posfun(aS, Type(1e-3), pen_d); }
2418 R_hat(sp, first_yr) = log(aS) / (Beta(sp, first_yr) * SPRFinit(sp)/1000000.0); }
2422 { Type aS = alpha(sp, first_yr) * SPRFinit(sp);
2423 if (rec_floor_on) { Type pen_d = 0; aS =
posfun(aS, Type(1e-3), pen_d); }
2424 R_hat(sp, first_yr) = log(aS) / (Beta(sp, first_yr) * SPRFinit(sp)/1000000.0); }
2427 error(
"Invalid 'srr_pred_fun'");
2431 if (rec_floor_on) { Type pen_R = 0; R_hat(sp, first_yr) =
posfun(R_hat(sp, first_yr), Type(1e-3), pen_R); zero_N_pen(sp) += pen_R; }
2434 for(yr = 1; yr < nyrs; yr++){
2445 int hat_spawn_yr = yr - minage(sp);
2446 int hat_srr_use = (hat_spawn_yr < 0) ? 0 : srr_pred_fun;
2447 Type ssb_tmp = (hat_spawn_yr < 0) ? Type(0.0) : ssb(sp, hat_spawn_yr);
2448 Type hat_rec_mean = (hat_spawn_yr < 0) ? R_init(sp) : R0(sp, yr);
2451 R_hat(sp, yr) =
calculate_recruitment(hat_srr_use, hat_rec_mean, ssb_tmp, alpha(sp, yr), Beta(sp, yr), Type(0.0), SPR0(sp));
2453 if (rec_floor_on) { Type pen_R = 0; R_hat(sp, yr) =
posfun(R_hat(sp, yr), Type(1e-3), pen_R); zero_N_pen(sp) += pen_R; }
2460 for(sp = 0; sp < nspp; sp++) {
2461 for(sex = 0; sex < nsex(sp); sex ++){
2462 for(age = 0; age < nages(sp); age++) {
2463 for(yr = 0; yr < nyrs; yr++) {
2465 avgN_at_age(sp, sex, age, yr) = N_at_age(sp, sex, age, yr) * (1 - exp(-Z_at_age(sp, sex, age, yr))) / Z_at_age(sp, sex, age, yr);
2466 avgN_at_age_dB0(sp, sex, age, yr) = N_at_age_dB0(sp, sex, age, yr) * (1 - exp(-M_at_age_dB0(sp, sex, age, yr))) / M_at_age_dB0(sp, sex, age, yr);
2467 avgN_at_age_dBF(sp, sex, age, yr) = N_at_age_dBF(sp, sex, age, yr) * (1 - exp(-M_at_age_dBF(sp, sex, age, yr) - Ftarget_at_age(sp, sex, age, yr))) / (M_at_age_dBF(sp, sex, age, yr) + Ftarget_at_age(sp, sex, age, yr));
2481 const bool predation_needed = (iter < niter - 1) || diet_used;
2482 if((msmMode > 0) && predation_needed) {
2488 nspp, nyrs, suit_styr, suit_endyr, nyrs_suit, msmMode,
2489 nsex, nages, suitMode, diet_prop, avgN_at_age, weight_hat, other_food_diet_prop);
2493 if((msmMode == 1) | (msmMode == 2)) {
2495 M2_at_age, M2_at_age_dB0, M2_at_age_dBF, M2_prop,
2496 B_eaten, B_eaten_as_prey, diet_prop_hat, other_diet_prop_hat,
2497 nspp, nyrs, nsex, nages, msmMode,
2498 avgN_at_age, avgN_at_age_dB0, avgN_at_age_dBF,
2499 suitability, suit_other, weight_hat, consumption_at_age, other_food,
2500 reporting , diet_used ,
2513 for(index_ind = 0; index_ind < index_ctl.rows(); index_ind++){
2515 index = index_ctl(index_ind, 0) - 1;
2516 sp = index_ctl(index_ind, 1) - 1;
2517 flt_yr = index_ctl(index_ind, 2);
2518 wt_idx_flt = nspp * 2 + index;
2519 mo = index_n(index_ind, 0);
2521 index_hat(index_ind) = 0.0;
2524 flt_yr = flt_yr - styr;
2526 else if(flt_yr < 0){
2527 flt_yr = -flt_yr - styr;
2530 for(age = 0; age < nages(sp); age++) {
2531 for(sex = 0; sex < nsex(sp); sex++){
2547 if(flt_type(index) == 1){
2548 n_index = N_at_age(sp, sex, age, flt_yr) * (1.0 - exp( - Z_at_age(sp, sex, age, flt_yr))) / Z_at_age(sp, sex, age, flt_yr);
2550 n_index = N_at_age(sp, sex, age, flt_yr) * exp( - (mo/12.0) * Z_at_age(sp, sex, age, flt_yr));
2554 if(flt_units(index) == 1){
2555 index_hat(index_ind) += n_index * sel_at_age(index, sex, age, flt_yr) * weight_hat(wt_idx_flt, sex, age, flt_yr );
2558 if(flt_units(index) == 2){
2559 index_hat(index_ind) += n_index * sel_at_age(index, sex, age, flt_yr);
2571 index_n_obs.setZero();
2572 index_q_analytical.setZero();
2573 for(index_ind = 0; index_ind < index_ctl.rows(); index_ind++){
2574 index = index_ctl(index_ind, 0) - 1;
2575 flt_yr = index_ctl(index_ind, 2);
2577 if((flt_yr <= 0) || (flt_yr - styr >= nyrs_hind) || !(index_obs(index_ind, 0) > 0))
continue;
2579 if(index_ll_type(index) == 3){
2580 Type sd2 = (est_sigma_index(index) == 0) ?
square(index_obs(index_ind, 1)) : exp(2.0 * index_log_sd(index));
2581 index_n_obs(index) +=
square(index_hat(index_ind)) / sd2;
2582 index_q_analytical(index) += index_obs(index_ind, 0) * index_hat(index_ind) / sd2;
2586 Type log_ratio = log(index_obs(index_ind, 0) / index_hat(index_ind));
2587 Type b = (index_ll_type(index) == 0) ? bias_adjust_obs : Type(0.0);
2588 switch(est_sigma_index(index)){
2590 index_n_obs(index) += 1 /
square(index_obs(index_ind, 1));
2591 index_q_analytical(index) += log_ratio /
square(index_obs(index_ind, 1)) + b / 2.0;
2594 index_n_obs(index) += 1;
2595 index_q_analytical(index) += log_ratio + b * exp(2.0 * index_log_sd(index)) / 2.0;
2598 index_n_obs(index) += 1;
2599 index_q_analytical(index) += log_ratio;
2603 index_q_analytical = index_q_analytical / index_n_obs;
2606 vector<Type> index_q_resid_ss(n_flt); index_q_resid_ss.setZero();
2607 for(index_ind = 0; index_ind < index_ctl.rows(); index_ind++){
2608 index = index_ctl(index_ind, 0) - 1;
2609 flt_yr = index_ctl(index_ind, 2);
2610 if((flt_yr <= 0) || (flt_yr - styr >= nyrs_hind) || !(index_obs(index_ind, 0) > 0) ||
2611 (est_sigma_index(index) != 2))
continue;
2612 index_q_resid_ss(index) +=
square(log(index_obs(index_ind, 0) / index_hat(index_ind)) - index_q_analytical(index));
2615 for(index = 0 ; index < n_flt; index ++){
2616 if((est_sigma_index(index) == 2) && (index_ll_type(index) == 0)){
2617 index_q_analytical(index) += bias_adjust_obs * index_q_resid_ss(index) / index_n_obs(index) / 2.0;
2619 if(index_ll_type(index) != 3) index_q_analytical(index) = exp(index_q_analytical(index));
2622 if((est_index_q(index) == 3) && (flt_type(index) > 0) &&
2623 ((index_ll_type(index) == 1) || (index_ll_type(index) == 2))){
2625 for(index_ind = 0; index_ind < index_ctl.rows(); index_ind++){
2626 flt_yr = index_ctl(index_ind, 2);
2627 if(((index_ctl(index_ind, 0) - 1) == index) && (flt_yr > 0) && (flt_yr <= endyr) &&
2628 (index_obs(index_ind, 0) > 0)) n_mvn++;
2631 matrix<Type> mvn_obs(n_mvn, 1), mvn_pred(n_mvn, 1);
2633 for(index_ind = 0; index_ind < index_ctl.rows(); index_ind++){
2634 flt_yr = index_ctl(index_ind, 2);
2635 if(((index_ctl(index_ind, 0) - 1) == index) && (flt_yr > 0) && (flt_yr <= endyr) &&
2636 (index_obs(index_ind, 0) > 0)){
2637 mvn_obs(k, 0) = index_obs(index_ind, 0);
2638 mvn_pred(k, 0) = index_hat(index_ind);
2642 Eigen::LLT<Eigen::Matrix<Type, Eigen::Dynamic, Eigen::Dynamic> > llt(index_cov_mat(index));
2643 matrix<Type> w = llt.solve(mvn_pred);
2644 index_q_analytical(index) = (w.transpose() * mvn_obs)(0, 0) / (w.transpose() * mvn_pred)(0, 0);
2649 if(est_index_q(index) == 3){
2650 for(yr = 0; yr < nyrs_hind; yr++){
2651 index_q(index, yr) = index_q_analytical(index);
2662 vector<Type> index_obs_sum(n_flt); index_obs_sum.setZero();
2663 vector<Type> index_hat_sum(n_flt); index_hat_sum.setZero();
2664 for(index_ind = 0; index_ind < index_ctl.rows(); index_ind++){
2665 index = index_ctl(index_ind, 0) - 1;
2666 flt_yr = index_ctl(index_ind, 2);
2668 flt_yr = flt_yr - styr;
2669 if((flt_yr < nyrs_hind) && (index_obs(index_ind, 0) > 0)){
2670 index_obs_sum(index) += index_obs(index_ind, 0);
2671 index_hat_sum(index) += index_hat(index_ind);
2675 for(index = 0; index < n_flt; index++){
2676 if(est_index_q(index) == 7){
2677 Type q_arith = index_obs_sum(index) / index_hat_sum(index);
2678 for(yr = 0; yr < nyrs_hind; yr++){
2679 index_q(index, yr) = q_arith;
2686 for(index_ind = 0; index_ind < index_ctl.rows(); index_ind++){
2688 index = index_ctl(index_ind, 0) - 1;
2689 flt_yr = index_ctl(index_ind, 2);
2692 flt_yr = flt_yr - styr;
2694 else if(flt_yr < 0){
2695 flt_yr = -flt_yr - styr;
2699 int yr_ind = (flt_yr < nyrs_hind) ? flt_yr : (nyrs_hind - 1);
2701 index_hat(index_ind) = index_q(index, yr_ind) * index_hat(index_ind);
2709 index_n_obs.setZero();
2710 index_analytical_sd.setZero();
2711 for(index_ind = 0; index_ind < index_ctl.rows(); index_ind++){
2713 index = index_ctl(index_ind, 0) - 1;
2714 flt_yr = index_ctl(index_ind, 2);
2717 flt_yr = flt_yr - styr;
2719 if((flt_yr < nyrs_hind) && (index_obs(index_ind, 0) > 0)){
2720 index_n_obs(index) += 1;
2721 index_analytical_sd(index) +=
square( log(index_obs(index_ind, 0)) - log(index_hat(index_ind)));
2726 for(index = 0 ; index < n_flt; index ++){
2727 if(index_n_obs(index) > 0){
2728 index_analytical_sd(index) =
concentrated_lognormal_sd(index_analytical_sd(index) / index_n_obs(index), bias_adjust_obs);
2737 for(fsh_ind = 0; fsh_ind < catch_ctl.rows(); fsh_ind++){
2739 flt = catch_ctl(fsh_ind, 0) - 1;
2740 sp = catch_ctl(fsh_ind, 1) - 1;
2741 flt_yr = catch_ctl(fsh_ind, 2);
2742 mo = catch_n(fsh_ind, 0);
2743 wt_idx_flt = nspp * 2 + flt;
2745 catch_hat(fsh_ind) = 0.0;
2746 max_catch_hat(fsh_ind) = 0.0;
2749 flt_yr = flt_yr - styr;
2751 else if(flt_yr < 0){
2752 flt_yr = -flt_yr - styr;
2755 for(sex = 0; sex < nsex(sp); sex ++){
2756 for(age = 0; age < nages(sp); age++) {
2759 if(flt_units(flt) == 1){
2760 catch_hat(fsh_ind) += F_flt_age(flt, sex, age, flt_yr) / Z_at_age(sp, sex, age, flt_yr) * (1.0 - exp(-Z_at_age(sp, sex, age, flt_yr))) * N_at_age(sp, sex, age, flt_yr) * weight_hat( wt_idx_flt, sex, age, flt_yr );
2761 max_catch_hat(fsh_ind) += N_at_age(sp, sex, age, flt_yr) * weight_hat( wt_idx_flt, sex, age, flt_yr ) * sel_at_age(flt, sex, age, flt_yr) * proj_F_prop(flt);
2765 if(flt_units(flt) == 2){
2766 catch_hat(fsh_ind) += F_flt_age(flt, sex, age, flt_yr) / Z_at_age(sp, sex, age, flt_yr) * (1.0 - exp(-Z_at_age(sp, sex, age, flt_yr))) * N_at_age(sp, sex, age, flt_yr);
2767 max_catch_hat(fsh_ind) += N_at_age(sp, sex, age, flt_yr) * sel_at_age(flt, sex, age, flt_yr) * proj_F_prop(flt);
2781 vector<Type> equil_catch_hat(equil_catch_ctl.rows()); equil_catch_hat.setZero();
2782 for(
int eq_ind = 0; eq_ind < equil_catch_ctl.rows(); eq_ind++){
2783 flt = equil_catch_ctl(eq_ind, 0) - 1;
2784 sp = equil_catch_ctl(eq_ind, 1) - 1;
2785 wt_idx_flt = nspp * 2 + flt;
2786 for(sex = 0; sex < nsex(sp); sex++){
2787 for(age = 0; age < nages(sp); age++){
2788 Type s_a = sel_at_age(flt, sex, age, 0);
2789 Type Z_a = M1_at_age(sp, sex, age, 0) + Finit(sp) * s_a;
2790 Type baranov = Finit(sp) * s_a / Z_a * (1.0 - exp(-Z_a)) * N_eq(sp, sex, age);
2791 if(flt_units(flt) == 1){
2792 equil_catch_hat(eq_ind) += baranov * weight_hat( wt_idx_flt, sex, age, 0 );
2794 if(flt_units(flt) == 2){
2795 equil_catch_hat(eq_ind) += baranov;
2812 catch_n_obs.setZero();
2813 catch_analytical_sd.setZero();
2814 for(fsh_ind = 0; fsh_ind < catch_ctl.rows(); fsh_ind++){
2816 flt = catch_ctl(fsh_ind, 0) - 1;
2817 flt_yr = catch_ctl(fsh_ind, 2);
2819 if((flt_yr > 0) && (flt_yr <= endyr) && (flt_type(flt) == 1) && (catch_obs(fsh_ind, 0) > 0)){
2820 catch_n_obs(flt) += 1;
2821 catch_analytical_sd(flt) +=
square( log(catch_obs(fsh_ind, 0)) - log(catch_hat(fsh_ind)) );
2825 for(flt = 0; flt < n_flt; flt++){
2826 if(catch_n_obs(flt) > 0){
2833 exploitable_biomass.setZero();
2835 for(flt = 0; flt < n_flt; flt++) {
2838 wt_idx_flt = nspp * 2 + flt;
2840 if(flt_type(flt) == 1){
2841 for(yr = 0; yr < nyrs; yr++) {
2842 for(age = 0; age < nages(sp); age++) {
2843 for(sex = 0; sex < nsex(sp); sex ++){
2844 exploitable_biomass(sp, yr) += N_at_age(sp, sex, age, yr) * weight_hat( wt_idx_flt, sex, age, yr ) * sel_at_age(flt, sex, age, yr) * proj_F_prop(flt);
2858 age_obs_hat.setZero();
2860 for(comp_ind = 0; comp_ind < comp_hat.rows(); comp_ind++){
2862 flt = comp_ctl(comp_ind, 0) - 1;
2863 sp = comp_ctl(comp_ind, 1) - 1;
2864 flt_sex = comp_ctl(comp_ind, 2);
2865 comp_type = comp_ctl(comp_ind, 3);
2866 yr = comp_ctl(comp_ind, 4);
2867 mo = (growth_model(sp) > 0) ? flt_month(flt) : comp_n(comp_ind, 0);
2868 int wtind = nspp * 2 + flt;
2869 n_hat(comp_ind) = 0.0;
2883 Frate = exp(log_F(flt, yr));
2887 if(yr >= nyrs_hind){
2888 yr_ind = nyrs_hind - 1;
2889 Frate = proj_F_prop(flt) * proj_F(sp, yr);
2893 if(growth_model(sp) > 0){
2894 for(age = 0; age < nages(sp); age++) {
2895 for(ln = 0; ln < nlengths(sp); ln++) {
2896 for(sex = 0; sex < nsex(sp); sex ++){
2898 switch(flt_type(flt)){
2900 if(flt_sel_dim(flt) == 1){
2904 for(
int lp = pop_bin_lo(sp, ln); lp <= pop_bin_hi(sp, ln); lp++){
2905 ps_ln += sel_at_length(flt, sex, lp, yr) * growth_matrix(wtind, sex, age, lp, yr);
2907 pred_CAAL(flt, sex, age, ln, yr) = ps_ln * (Frate / Z_at_age(sp, sex, age, yr) * (1 - exp(-Z_at_age(sp, sex, age, yr))) * N_at_age(sp, sex, age, yr));
2914 if(flt_sel_dim(flt) == 1){
2918 for(
int lp = pop_bin_lo(sp, ln); lp <= pop_bin_hi(sp, ln); lp++){
2919 ps_ln += sel_at_length(flt, sex, lp, yr) * growth_matrix(wtind, sex, age, lp, yr);
2921 pred_CAAL(flt, sex, age, ln, yr) = ps_ln * (N_at_age(sp, sex, age, yr) * index_q(flt, yr_ind) * exp( - Type(mo/12.0) * Z_at_age(sp, sex, age, yr)));
2928 comp_pred(sex, age, ln) = pred_CAAL(flt, sex, age, ln, yr);
2929 if(flt_sel_dim(flt) != 1 || flt_sel_type(flt) == 0){
2931 for(
int lp = pop_bin_lo(sp, ln); lp <= pop_bin_hi(sp, ln); lp++){
2932 p_ln += growth_matrix(wtind, sex, age, lp, yr);
2934 switch(flt_type(flt)){
2936 comp_pred(sex, age, ln) = p_ln * (F_flt_age(flt, sex, age, yr) / Z_at_age(sp, sex, age, yr) * (1 - exp(-Z_at_age(sp, sex, age, yr))) * N_at_age(sp, sex, age, yr));
2940 comp_pred(sex, age, ln) = p_ln * (N_at_age(sp, sex, age, yr) * sel_at_age(flt, sex, age, yr) * index_q(flt, yr_ind) * exp( - Type(mo/12.0) * Z_at_age(sp, sex, age, yr)));
2949 for(age = 0; age < nages(sp); age++) {
2950 for(ln = 0; ln < nlengths(sp); ln++) {
2957 for(sex = 0; sex < nsex(sp); sex ++){
2958 age_hat(comp_ind, age ) += comp_pred(sex, age, ln);
2964 age_hat(comp_ind, age ) += comp_pred(sex, age, ln);
2968 for(sex = 0; sex < nsex(sp); sex ++){
2969 age_hat(comp_ind, age + nages(sp) * sex ) += comp_pred(sex, age, ln);
2981 for(sex = 0; sex < nsex(sp); sex ++){
2982 comp_hat(comp_ind, ln ) += comp_pred(sex, age, ln);
2988 comp_hat(comp_ind, ln ) += comp_pred(sex, age, ln);
2992 for(sex = 0; sex < nsex(sp); sex ++){
2993 comp_hat(comp_ind, ln + nlengths(sp) * sex ) += comp_pred(sex, age, ln) ;
3002 for(
int obs_age = 0; obs_age < nages(sp); obs_age++) {
3003 for(
int true_age = 0; true_age < nages(sp); true_age++) {
3004 age_obs_hat(comp_ind, obs_age) += age_hat(comp_ind, true_age ) * age_error(flt_ageing_error_index(flt), true_age, obs_age);
3010 for(
int obs_age = nages(sp); obs_age < nages(sp) * 2; obs_age++) {
3011 for(
int true_age = nages(sp); true_age < nages(sp) * 2; true_age++) {
3014 int true_age_tmp = true_age - nages(sp);
3015 int obs_age_tmp = obs_age - nages(sp);
3017 age_obs_hat(comp_ind, obs_age) += age_hat(comp_ind, true_age ) * age_error(flt_ageing_error_index(flt), true_age_tmp, obs_age_tmp);
3023 if( comp_type == 0) {
3024 joint_adjust(comp_ind) = 1;
3026 joint_adjust(comp_ind) = 2;
3028 for(age = 0; age < nages(sp) * joint_adjust(comp_ind); age++) {
3029 comp_hat(comp_ind, age ) = age_obs_hat(comp_ind, age );
3036 if(growth_model(sp) == 0){
3037 for(age = 0; age < nages(sp); age++) {
3039 switch(flt_type(flt)){
3043 for(sex = 0; sex < nsex(sp); sex ++){
3045 age_hat(comp_ind, age ) += F_flt_age(flt, sex, age, yr) / Z_at_age(sp, sex, age, yr) * (1 - exp(-Z_at_age(sp, sex, age, yr))) * N_at_age(sp, sex, age, yr);
3052 age_hat(comp_ind, age ) = F_flt_age(flt, sex, age, yr) / Z_at_age(sp, sex, age, yr) * (1 - exp(-Z_at_age(sp, sex, age, yr))) * N_at_age(sp, sex, age, yr);
3056 for(sex = 0; sex < nsex(sp); sex ++){
3058 age_hat(comp_ind, age + nages(sp) * sex ) = F_flt_age(flt, sex, age, yr) / Z_at_age(sp, sex, age, yr) * (1 - exp(-Z_at_age(sp, sex, age, yr))) * N_at_age(sp, sex, age, yr);
3068 for(sex = 0; sex < nsex(sp); sex ++){
3070 age_hat(comp_ind, age ) += N_at_age(sp, sex, age, yr) * sel_at_age(flt, sex, age, yr) * index_q(flt, yr_ind) * exp( - Type(mo/12.0) * Z_at_age(sp, sex, age, yr));
3077 age_hat(comp_ind, age ) = N_at_age(sp, sex, age, yr) * sel_at_age(flt, sex, age, yr) * index_q(flt, yr_ind) * exp( - Type(mo/12.0) * Z_at_age(sp, sex, age, yr));
3081 for(sex = 0; sex < nsex(sp); sex ++){
3083 age_hat(comp_ind, age + nages(sp) * sex ) = N_at_age(sp, sex, age, yr) * sel_at_age(flt, sex, age, yr) * index_q(flt, yr_ind) * exp( - Type(mo/12.0) * Z_at_age(sp, sex, age, yr));
3092 for(
int obs_age = 0; obs_age < nages(sp); obs_age++) {
3093 for(
int true_age = 0; true_age < nages(sp); true_age++) {
3094 age_obs_hat(comp_ind, obs_age) += age_hat(comp_ind, true_age ) * age_error(flt_ageing_error_index(flt), true_age, obs_age);
3100 for(
int obs_age = nages(sp); obs_age < nages(sp) * 2; obs_age++) {
3101 for(
int true_age = nages(sp); true_age < nages(sp) * 2; true_age++) {
3104 int true_age_tmp = true_age - nages(sp);
3105 int obs_age_tmp = obs_age - nages(sp);
3107 age_obs_hat(comp_ind, obs_age) += age_hat(comp_ind, true_age ) * age_error(flt_ageing_error_index(flt), true_age_tmp, obs_age_tmp);
3113 if( comp_type == 0) {
3114 joint_adjust(comp_ind) = 1;
3116 joint_adjust(comp_ind) = 2;
3118 for(age = 0; age < nages(sp) * joint_adjust(comp_ind); age++) {
3119 comp_hat(comp_ind, age ) = age_obs_hat(comp_ind, age );
3127 if( comp_type == 1) {
3128 for(ln = 0; ln < nlengths(sp); ln++) {
3129 for(age = 0; age < nages(sp); age++) {
3134 if((flt_sex > 0) & (flt_sex < 3)){
3137 comp_hat(comp_ind, ln ) += age_hat(comp_ind, age) * age_trans_matrix(flt_age_transition_index(flt), sex, age, ln );
3144 for(ln = nlengths(sp); ln < nlengths(sp) * 2; ln++) {
3145 for(age = nages(sp); age < nages(sp) * 2; age++) {
3148 int obs_log_tmp = ln;
3149 int obs_age_tmp = age;
3151 obs_age_tmp = age - nages(sp);
3152 obs_log_tmp = ln - nlengths(sp);
3155 comp_hat(comp_ind, ln ) += age_hat(comp_ind, age) * age_trans_matrix(flt_age_transition_index(flt), sex, obs_age_tmp, obs_log_tmp );
3164 comp_hat.row(comp_ind) /=
posfun(comp_hat.row(comp_ind).sum(), Type(0.001), penalty);
3172 for(
int caal_ind = 0; caal_ind < caal_hat.rows(); caal_ind++){
3174 flt = caal_ctl(caal_ind, 0) - 1;
3175 sp = caal_ctl(caal_ind, 1) - 1;
3176 flt_sex = caal_ctl(caal_ind, 2);
3177 yr = caal_ctl(caal_ind, 3);
3178 ln = caal_ctl(caal_ind, 4) - 1;
3179 mo = flt_month(flt);
3181 if(growth_model(sp) == 0)
continue;
3189 int wtind = nspp * 2 + flt;
3202 Frate = exp(log_F(flt, yr));
3206 if(yr >= nyrs_hind){
3207 yr_ind = nyrs_hind - 1;
3208 Frate = proj_F_prop(flt) * proj_F(sp, yr);
3212 for(age = 0; age < nages(sp); age++) {
3214 switch(flt_type(flt)){
3216 if(flt_sel_dim(flt) == 1){
3220 for(
int lp = pop_bin_lo(sp, ln); lp <= pop_bin_hi(sp, ln); lp++){
3221 ps_ln += sel_at_length(flt, sex, lp, yr) * growth_matrix(wtind, sex, age, lp, yr);
3223 pred_CAAL(flt, sex, age, ln, yr) = ps_ln * (Frate / Z_at_age(sp, sex, age, yr) * (1 - exp(-Z_at_age(sp, sex, age, yr))) * N_at_age(sp, sex, age, yr));
3230 if(flt_sel_dim(flt) == 1){
3234 for(
int lp = pop_bin_lo(sp, ln); lp <= pop_bin_hi(sp, ln); lp++){
3235 ps_ln += sel_at_length(flt, sex, lp, yr) * growth_matrix(wtind, sex, age, lp, yr);
3237 pred_CAAL(flt, sex, age, ln, yr) = ps_ln * (N_at_age(sp, sex, age, yr) * index_q(flt, yr_ind) * exp( - Type(mo/12.0) * Z_at_age(sp, sex, age, yr)) );
3245 for(
int obs_age = 0; obs_age < nages(sp); obs_age++) {
3246 for(
int true_age = 0; true_age < nages(sp); true_age++) {
3247 caal_hat(caal_ind, obs_age) += pred_CAAL(flt, sex, true_age, ln, yr) * age_error(flt_ageing_error_index(flt), true_age, obs_age);
3253 caal_hat.row(caal_ind) /=
posfun(caal_hat.row(caal_ind).sum(), Type(0.0001), penalty);
3266 minage, nsex, nages, diet_obs, diet_ctl,
3267 avgN_at_age, diet_prop_hat, diet_hat);
3277 biomass_depletion.setZero();
3278 ssb_depletion.setZero();
3279 for(sp = 0; sp < nspp; sp++){
3280 for(yr = 0; yr < nyrs; yr++){
3282 if(DynamicHCR == 0){
3283 biomass_depletion(sp, yr) = biomass(sp, yr)/B0(sp, nyrs-1);
3284 ssb_depletion(sp, yr) = ssb(sp, yr)/SB0(sp, nyrs-1);
3287 if(DynamicHCR == 1){
3288 biomass_depletion(sp, yr) = biomass(sp, yr)/DynamicB0(sp, yr);
3289 ssb_depletion(sp, yr) = ssb(sp, yr)/DynamicSB0(sp, yr);
3296 if((HCR == 0) & (msmMode > 0) & (DynamicHCR == 0)){
3297 biomass_depletion(sp, yr) = biomass(sp, yr)/biomass(sp, nyrs-1);
3298 ssb_depletion(sp, yr) = ssb(sp, yr)/ssb(sp, nyrs-1);
3318 int n_col = std::max(n_flt, nspp);
3326 JNLL_SEL_NONPARAM = 4,
3333 JNLL_SRR_PENALTY = 11,
3334 JNLL_REFPT_PENALTY = 12,
3335 JNLL_ZERO_N_PENALTY = 13,
3339 JNLL_RATION_PENALTY = 17,
3341 JNLL_LINKAGE_PRIOR = 19,
3342 JNLL_LINKAGE_RE = 20,
3343 JNLL_EQUIL_CATCH = 21,
3346 matrix<Type> jnll_comp(JNLL_N_ROWS, n_col); jnll_comp.setZero();
3347 matrix<Type> unweighted_jnll_comp(JNLL_N_ROWS, n_col); unweighted_jnll_comp.setZero();
3358 Type index_std_dev = 0;
3359 for(index_ind = 0; index_ind < index_obs.rows(); index_ind++) {
3361 index = index_ctl(index_ind, 0) - 1;
3362 sp = index_ctl(index_ind, 1) - 1;
3363 flt_yr = index_ctl(index_ind, 2);
3364 int family = index_ll_type(index);
3366 switch (est_sigma_index(index)) {
3367 case 0: index_std_dev = index_obs(index_ind, 1);
break;
3368 case 1: index_std_dev = exp(index_log_sd(index));
break;
3369 case 2: index_std_dev = index_analytical_sd(index);
break;
3370 default: error(
"Invalid 'Estimate_sigma_index'");
3372 index_sd(index_ind) = index_std_dev;
3375 bool fit_row = (flt_yr > 0) && (flt_yr <= endyr) && (flt_type(index) > 0) && (index_obs(index_ind, 0) > 0);
3378 if(fit_row && (family == 0)){
3379 int pos = index_obsvec_idx(index_ind);
3381 Type index_mu = log(index_hat(index_ind)) - bias_adjust_obs*
square(index_std_dev)/2.0;
3382 jnll_comp(JNLL_INDEX, index) -= keep(pos) * dnorm(obsvec(pos), index_mu, index_std_dev,
true);
3385 obsvec(pos), index_mu, index_std_dev, keep.cdf_lower(pos), keep.cdf_upper(pos));
3392 if(fit_row && ((family == 3) || (family == 4))){
3393 Type log_Z = (family == 4) ? log(pnorm(index_hat(index_ind) / index_std_dev)) : Type(0.0);
3395 jnll_comp(JNLL_INDEX, index) -= dnorm(index_obs(index_ind, 0), index_hat(index_ind), index_std_dev,
true);
3396 jnll_comp(JNLL_INDEX, index) += log_Z;
3399 int pos = index_obsvec_idx(index_ind);
3401 jnll_comp(JNLL_INDEX, index) -= keep(pos) * dnorm(obsvec(pos), index_hat(index_ind), index_std_dev,
true);
3402 jnll_comp(JNLL_INDEX, index) += keep(pos) * log_Z;
3406 obsvec(pos), index_hat(index_ind), index_std_dev,
3407 keep.cdf_lower(pos), keep.cdf_upper(pos));
3410 obsvec(pos), index_hat(index_ind), index_std_dev,
3411 keep.cdf_lower(pos), keep.cdf_upper(pos));
3421 bool sim_row = (flt_yr <= 0) || (flt_yr > endyr) || (index_obs(index_ind, 0) > 0);
3422 if(sim_row && (flt_type(index) > 0) && (family == 0)){
3423 index_obs(index_ind, 0) = exp(rnorm(log(index_hat(index_ind)) - bias_adjust_obs*
square(index_std_dev)/2.0, index_std_dev));
3424 int sim_pos = index_obsvec_idx(index_ind);
3426 obsvec(sim_pos) = log(index_obs(index_ind, 0));
3429 if(sim_row && (flt_type(index) > 0) && (family == 3)){
3432 Type sim_draw = rnorm(index_hat(index_ind), index_std_dev);
3434 while((sim_draw <= Type(0)) && (tn_tries < index_trunc_max_tries)){
3435 index_trunc_rejects_sim(index_ind) += 1;
3436 sim_draw = rnorm(index_hat(index_ind), index_std_dev);
3439 index_trunc_tries_sim(index_ind) += tn_tries + 1;
3440 index_trunc_sd_sim(index_ind) = index_std_dev;
3441 index_trunc_budget_sim(index_ind) = index_trunc_max_tries;
3442 index_obs(index_ind, 0) = sim_draw;
3444 if(sim_row && (flt_type(index) > 0) && (family == 4)){
3446 Type a = pnorm(-index_hat(index_ind) / index_std_dev);
3447 Type u = a + (Type(1.0) - a) * runif(Type(0.0), Type(1.0));
3448 index_obs(index_ind, 0) = index_hat(index_ind) + index_std_dev * qnorm(u);
3451 if((flt_type(index) > 0) && ((family == 3) || (family == 4))){
3452 int sim_pos = index_obsvec_idx(index_ind);
3455 if(index_obs(index_ind, 0) > Type(0)) obsvec(sim_pos) = log(index_obs(index_ind, 0));
3457 obsvec(sim_pos) = index_obs(index_ind, 0);
3468 for(index = 0; index < n_flt; index++){
3469 if((flt_type(index) > 0) && (index_ll_type(index) == 1 || index_ll_type(index) == 2)){
3471 for(index_ind = 0; index_ind < index_obs.rows(); index_ind++){
3472 if((index_ctl(index_ind, 0) - 1) == index){
3473 flt_yr = index_ctl(index_ind, 2);
3474 if((flt_yr > 0) && (flt_yr <= endyr) && (index_obs(index_ind, 0) > 0)) n_mvn++;
3479 vector<Type> resid(n_mvn);
3480 vector<Type> mu(n_mvn);
3481 vector<int> posv(n_mvn);
3482 vector<int> rowv(n_mvn);
3484 for(index_ind = 0; index_ind < index_obs.rows(); index_ind++){
3485 if((index_ctl(index_ind, 0) - 1) == index){
3486 flt_yr = index_ctl(index_ind, 2);
3487 if((flt_yr > 0) && (flt_yr <= endyr) && (index_obs(index_ind, 0) > 0)){
3488 resid(k) = index_obs(index_ind, 0) - index_hat(index_ind);
3489 mu(k) = index_hat(index_ind);
3490 posv(k) = index_obsvec_idx(index_ind);
3491 rowv(k) = index_ind;
3499 Type dens = MVNORM(index_cov_mat(index))(resid);
3500 jnll_comp(JNLL_INDEX, index) += (index_ll_type(index) == 2) ? dens : (dens - index_cov_const(index));
3504 Eigen::LLT<Eigen::Matrix<Type, Eigen::Dynamic, Eigen::Dynamic> > llt(index_cov_mat(index));
3505 matrix<Type> Lmat = llt.matrixL();
3506 matrix<Type> muc(n_mvn, 1);
3507 for(k = 0; k < n_mvn; k++) muc(k, 0) = mu(k);
3508 matrix<Type> mwhite = Lmat.template triangularView<Eigen::Lower>().solve(muc);
3509 for(k = 0; k < n_mvn; k++){
3512 jnll_comp(JNLL_INDEX, index) -= keep(pos) * dnorm(obsvec(pos), mwhite(k, 0), Type(1.0),
true);
3515 obsvec(pos), mwhite(k, 0), Type(1.0),
3516 keep.cdf_lower(pos), keep.cdf_upper(pos));
3526 vector<Type> sim_dev = MVNORM(index_cov_mat(index)).simulate();
3529 bool any_nonpos =
false;
3530 for(k = 0; k < n_mvn; k++){
3531 if(mu(k) + sim_dev(k) <= Type(0)) any_nonpos =
true;
3533 if(!any_nonpos)
break;
3534 if(mvn_tries >= index_trunc_max_tries * n_mvn)
break;
3535 for(k = 0; k < n_mvn; k++){
3536 if(mu(k) + sim_dev(k) <= Type(0)) index_trunc_rejects_sim(rowv(k)) += 1;
3538 sim_dev = MVNORM(index_cov_mat(index)).simulate();
3541 for(k = 0; k < n_mvn; k++){
3542 index_trunc_tries_sim(rowv(k)) += mvn_tries + 1;
3543 index_trunc_sd_sim(rowv(k)) = sqrt(index_cov_mat(index)(k, k));
3544 index_trunc_budget_sim(rowv(k)) = index_trunc_max_tries * n_mvn;
3545 index_obs(rowv(k), 0) = mu(k) + sim_dev(k);
3547 int sim_pos = posv(k);
3548 if((sim_pos >= 0) && (osa_mode == 0) && (index_obs(rowv(k), 0) > Type(0))){
3549 obsvec(sim_pos) = log(index_obs(rowv(k), 0));
3559 matrix<Type> index_obs_sim = index_obs;
3560 REPORT(index_obs_sim);
3561 REPORT(index_trunc_tries_sim);
3562 REPORT(index_trunc_rejects_sim);
3563 REPORT(index_trunc_sd_sim);
3564 REPORT(index_trunc_budget_sim);
3570 Type fsh_std_dev = 0;
3571 for(fsh_ind = 0; fsh_ind < catch_obs.rows(); fsh_ind++) {
3573 flt = catch_ctl(fsh_ind, 0) - 1;
3574 sp = catch_ctl(fsh_ind, 1) - 1;
3575 flt_yr = catch_ctl(fsh_ind, 2);
3578 switch (est_sigma_fsh(flt)) {
3579 case 0: fsh_std_dev = catch_obs(fsh_ind, 1);
break;
3580 case 1: fsh_std_dev = exp(catch_log_sd(flt));
break;
3581 case 2: fsh_std_dev = catch_analytical_sd(flt);
break;
3582 default: error(
"Invalid 'Estimate_sigma_catch'");
3584 catch_sd(fsh_ind) = fsh_std_dev;
3587 if((flt_yr > 0) && (flt_yr <= endyr) && (flt_type(flt) == 1) && (catch_obs(fsh_ind, 0) > 0)){
3588 int pos = catch_obsvec_idx(fsh_ind);
3590 Type catch_mu = log(catch_hat(fsh_ind)) - bias_adjust_obs*
square(fsh_std_dev)/2.0;
3591 jnll_comp(JNLL_CATCH, flt) -= keep(pos) * dnorm(obsvec(pos), catch_mu, fsh_std_dev,
true) ;
3594 obsvec(pos), catch_mu, fsh_std_dev, keep.cdf_lower(pos), keep.cdf_upper(pos));
3604 catch_obs(fsh_ind, 0) = exp(rnorm(log(catch_hat(fsh_ind)) - bias_adjust_obs*
square(fsh_std_dev)/2.0, fsh_std_dev));
3605 int sim_pos = catch_obsvec_idx(fsh_ind);
3607 obsvec(sim_pos) = log(catch_obs(fsh_ind, 0));
3616 for(
int eq_ind = 0; eq_ind < equil_catch_ctl.rows(); eq_ind++){
3617 int eq_flt = equil_catch_ctl(eq_ind, 0) - 1;
3618 if((equil_catch_obs(eq_ind, 0) > 0) && (flt_type(eq_flt) == 1)){
3619 Type eq_sd = equil_catch_obs(eq_ind, 1);
3620 Type eq_mu = log(equil_catch_hat(eq_ind)) - bias_adjust_obs*
square(eq_sd)/2.0;
3621 jnll_comp(JNLL_EQUIL_CATCH, eq_flt) -=
3622 dnorm(log(equil_catch_obs(eq_ind, 0)), eq_mu, eq_sd,
true);
3627 equil_catch_obs(eq_ind, 0) = exp(rnorm(eq_mu, eq_sd));
3635 matrix<Type> catch_obs_sim = catch_obs;
3636 matrix<Type> equil_catch_obs_sim = equil_catch_obs;
3637 vector<Type> obsvec_sim = obsvec;
3638 REPORT(catch_obs_sim);
3639 REPORT(equil_catch_obs_sim);
3649 Type comp_prop_offset = comp_offset;
3650 matrix<Type> comp_sim = comp_obs;
3652 for(comp_ind = 0; comp_ind < comp_obs.rows(); comp_ind++) {
3654 flt = comp_ctl(comp_ind, 0) - 1;
3655 sp = comp_ctl(comp_ind, 1) - 1;
3656 flt_sex = comp_ctl(comp_ind, 2);
3657 comp_type = comp_ctl(comp_ind, 3);
3658 yr = comp_ctl(comp_ind, 4);
3661 joint_adjust(comp_ind) = 1;
3663 joint_adjust(comp_ind) = 2;
3665 int nbins_blk = (comp_type == 0) ? nages(sp) : nlengths(sp);
3667 if((comp_type == 0) || (comp_type == 1)) n_comp = nbins_blk * joint_adjust(comp_ind);
3669 vector<Type> comp_obs_tmp = comp_obs.row(comp_ind).segment(0, n_comp);
3670 vector<Type> comp_hat_tmp = comp_hat.row(comp_ind).segment(0, n_comp);
3674 fold_comp_tails(comp_obs_tmp, comp_hat_tmp, n_comp, nbins_blk, joint_adjust(comp_ind),
3675 comp_accum_young(flt), comp_accum_old(flt));
3677 if((yr <= endyr) && (yr > 0) && (flt_type(flt) > 0) && (comp_n(comp_ind, 1) > 0)){
3678 comp_row_nll(comp_ll_type(flt), osa_mode, comp_obs_tmp, comp_hat_tmp,
3679 comp_prop_offset, comp_n(comp_ind, 1), comp_weights(flt), DM_pars_comp(flt),
3680 obsvec, keep, comp_obsvec_idx(comp_ind),
"comp_ll_type",
3681 jnll_comp(JNLL_COMP, flt), unweighted_jnll_comp(JNLL_COMP, flt));
3687 int nbin_raw = ((comp_type == 0) ? nages(sp) : nlengths(sp)) * joint_adjust(comp_ind);
3688 vector<Type> sim_p = comp_hat.row(comp_ind).segment(0, nbin_raw);
3689 Type sim_tot = sim_p.sum();
3690 Type n_nom = comp_n(comp_ind, 1);
3693 vector<Type> sim_out(nbin_raw); sim_out.setZero();
3695 if((sim_tot > Type(0)) && (n_nom > Type(0))){
3696 vector<Type> p = sim_p / sim_tot;
3697 if(comp_ll_type(flt) == 1){
3699 vector<Type> sim_alphas = p * n_nom * DM_pars_comp(flt);
3707 for(
int b = 0; b < nbin_raw; b++){
3708 comp_sim(comp_ind, b) = sim_out(b);
3712 for(
int b = nbin_raw; b < comp_sim.cols(); b++){
3713 comp_sim(comp_ind, b) = Type(0);
3719 matrix<Type> comp_obs_sim = comp_sim;
3720 REPORT(comp_obs_sim);
3726 matrix<Type> caal_sim = caal_obs;
3728 for(
int caal_ind = 0; caal_ind < caal_hat.rows(); caal_ind++){
3730 flt = caal_ctl(caal_ind, 0) - 1;
3731 sp = caal_ctl(caal_ind, 1) - 1;
3732 flt_sex = caal_ctl(caal_ind, 2);
3733 yr = caal_ctl(caal_ind, 3);
3734 ln = caal_ctl(caal_ind, 4) - 1;
3736 int n_caal = nages(sp);
3738 vector<Type> caal_obs_tmp = caal_obs.row(caal_ind).segment(0, n_caal);
3739 vector<Type> caal_hat_tmp = caal_hat.row(caal_ind).segment(0, n_caal);
3741 if((yr <= endyr) && (yr > 0) && (flt_type(flt) > 0) && (caal_n(caal_ind, 0) > 0)){
3742 comp_row_nll(caal_ll_type(flt), osa_mode, caal_obs_tmp, caal_hat_tmp,
3743 comp_prop_offset, caal_n(caal_ind, 0), caal_weights(flt), DM_pars_caal(flt),
3744 obsvec, keep, caal_obsvec_idx(caal_ind),
"caal_ll_type",
3745 jnll_comp(JNLL_CAAL, flt), unweighted_jnll_comp(JNLL_CAAL, flt));
3750 vector<Type> sim_p = caal_hat.row(caal_ind).segment(0, n_caal);
3751 Type sim_tot = sim_p.sum();
3752 Type n_nom = caal_n(caal_ind, 0);
3753 vector<Type> sim_out(n_caal); sim_out.setZero();
3755 if((sim_tot > Type(0)) && (n_nom > Type(0))){
3756 vector<Type> p = sim_p / sim_tot;
3757 if(caal_ll_type(flt) == 1){
3758 vector<Type> sim_alphas = p * n_nom * DM_pars_caal(flt);
3764 for(
int b = 0; b < n_caal; b++){
3765 caal_sim(caal_ind, b) = sim_out(b);
3767 for(
int b = n_caal; b < caal_sim.cols(); b++){
3768 caal_sim(caal_ind, b) = Type(0);
3774 matrix<Type> caal_obs_sim = caal_sim;
3775 REPORT(caal_obs_sim);
3782 matrix<Type> diet_sim = diet_obs;
3788 for (
int i = 0; i < n_stomach_obs; ++i) {
3793 int start_j = current_j;
3794 while((current_j < diet_ctl.rows()) && (stomach_id(current_j) == i)) {
3797 int n_prey = current_j - start_j;
3798 if (n_prey == 0)
continue;
3800 int rsp = diet_ctl(start_j, 0) - 1;
3801 Type N_s = diet_obs(start_j, 0);
3803 if (suitMode(rsp) <= 0)
continue;
3806 vector<Type> obs_diet_prop(n_prey + 1); obs_diet_prop.setZero();
3807 vector<Type> pred_diet_prop(n_prey + 1); pred_diet_prop.setZero();
3809 for (
int k = 0; k < n_prey; ++k) {
3810 obs_diet_prop(k) = diet_obs(start_j + k, 1);
3811 pred_diet_prop(k) = diet_hat(start_j + k, 1);
3814 Type sum_obs_p = obs_diet_prop.head(n_prey).sum();
3815 if (sum_obs_p > 1.0) sum_obs_p = 1.0;
3816 obs_diet_prop(n_prey) = 1.0 - sum_obs_p;
3818 Type sum_est_p = pred_diet_prop.head(n_prey).sum();
3819 pred_diet_prop(n_prey) =
posfun(1.0 - sum_est_p, Type(0.00001), penalty);
3822 obs_diet_prop += comp_offset;
3823 pred_diet_prop += comp_offset;
3825 obs_diet_prop /= obs_diet_prop.sum();
3826 pred_diet_prop /= pred_diet_prop.sum();
3828 vector<Type> obs_diet_content = obs_diet_prop * N_s;
3833 Type stomach_log_likelihood = 0;
3834 Type unweighted_stomach_log_likelihood = 0;
3835 Type DM_diet_par = DM_diet_pars(rsp);
3837 vector<Type> diet_alphas = pred_diet_prop * N_s * DM_diet_par;
3838 vector<Type> unweighted_diet_alphas = pred_diet_prop * N_s;
3841 switch(diet_ll_type(rsp)){
3844 stomach_log_likelihood = dmultinom(obs_diet_content, pred_diet_prop,
true);
3846 unweighted_jnll_comp(JNLL_STOMACH, rsp) -= stomach_log_likelihood;
3847 jnll_comp(JNLL_STOMACH, rsp) -= diet_comp_weights(rsp) * stomach_log_likelihood;
3850 stomach_log_likelihood =
ddirmultinom(obs_diet_content, diet_alphas,
true);
3851 unweighted_stomach_log_likelihood =
ddirmultinom(obs_diet_content, unweighted_diet_alphas,
true);
3853 unweighted_jnll_comp(JNLL_STOMACH, rsp) -= unweighted_stomach_log_likelihood;
3854 jnll_comp(JNLL_STOMACH, rsp) -= stomach_log_likelihood;
3858 error(
"Invalid 'diet_ll_type'");
3862 int start = diet_obsvec_idx(i);
3864 vector<Type> osa_x = obsvec.segment(start, n_prey + 1);
3865 if(diet_ll_type(rsp) == 1){
3866 jnll_comp(JNLL_STOMACH, rsp) -=
ddirmultinom_osa(osa_x, diet_alphas, keep.segment(start, n_prey + 1), 1, 1);
3868 jnll_comp(JNLL_STOMACH, rsp) -=
dmultinom_osa(osa_x, pred_diet_prop, keep.segment(start, n_prey + 1), 1, 1, osa_mode == 2);
3876 vector<Type> sim_p(n_prey + 1);
3877 Type prey_sum = 0.0;
3878 for(
int k = 0; k < n_prey; k++){
3879 sim_p(k) = diet_hat(start_j + k, 1);
3880 prey_sum += sim_p(k);
3882 sim_p(n_prey) = (prey_sum < Type(1.0)) ? Type(1.0) - prey_sum : Type(0.0);
3883 Type sim_tot = sim_p.sum();
3884 if(sim_tot > Type(0)) sim_p /= sim_tot;
3886 if((N_s > Type(0)) && (sim_tot > Type(0))){
3887 vector<Type> sim_counts(n_prey + 1);
3888 switch(diet_ll_type(rsp)){
3891 Type n_eff = N_s * diet_comp_weights(rsp);
3896 vector<Type> sim_alphas = sim_p * N_s * DM_diet_par;
3901 error(
"Invalid 'diet_ll_type'");
3905 Type n_drawn = sim_counts.sum();
3906 for(
int k = 0; k < n_prey; k++){
3907 diet_sim(start_j + k, 1) = (n_drawn > Type(0))
3908 ? sim_counts(k) / n_drawn
3918 matrix<Type> diet_obs_sim = diet_sim;
3919 REPORT(diet_obs_sim);
3928 for(flt = 0; flt < n_flt; flt++){
3930 bool sel_is_length = (flt_sel_dim(flt) == 1);
3931 int nbins = sel_is_length ? nlengths_pop(sp) : nages(sp);
3933 if(flt_type(flt) > 0 && flt_sel_lead(flt) == 1){
3938 if(flt_sel_type(flt) == 2) {
3940 if((flt_varying_sel(flt) == 1) || (flt_varying_sel(flt) == 4)){
3941 nyrs_tmp = nyrs_hind;
3944 for(yr = 0; yr < nyrs_tmp; yr++){
3948 for(sex = 0; sex < nsex(sp); sex++){
3949 for(age = 0; age < (nbins - 1); age++) {
3950 Type sel_ratio_tmp = log_non_par_sel(flt, sex, age, yr) - log_non_par_sel(flt, sex, age + 1, yr);
3951 jnll_comp(JNLL_SEL_NONPARAM, flt) += sel_curve_pen(flt, 0) *
square( (CppAD::abs(sel_ratio_tmp) + sel_ratio_tmp)/2.0);
3956 for(sex = 0; sex < nsex(sp); sex++){
3957 vector<Type> sel_tmp(nbins); sel_tmp.setZero();
3958 for(age = 0; age < nbins; age++) {
3959 sel_tmp(age) = log_non_par_sel(flt, sex, age, yr);
3962 for(
int a2 = 0; a2 < sel_d2.size(); a2++) {
3963 jnll_comp(JNLL_SEL_NONPARAM, flt) += sel_curve_pen(flt, 1) * sel_d2(a2) * sel_d2(a2);
3970 if(flt_varying_sel(flt) == 4){
3972 for(sex = 0; sex < nsex(sp); sex++){
3973 for(age = 0; age < (nbins - 1); age++) {
3974 jnll_comp(JNLL_SEL_DEV, flt) -= dnorm(log_non_par_sel(flt, sex, age, yr), log_non_par_sel(flt, sex, age, yr - 1), sel_dev_sd(flt),
true);
3978 }
else if(flt_varying_sel(flt) == 1){
3979 for(sex = 0; sex < nsex(sp); sex++){
3980 for(
int bin = bin_first_selected(flt); bin < flt_n_sel_bins(flt); bin++) {
3981 jnll_comp(JNLL_SEL_DEV, flt) -= dnorm(sel_coff_dev(flt, sex, bin, yr), Type(0.0), sel_dev_sd(flt),
true);
3987 for(sex = 0; sex < nsex(sp); sex++){
3988 jnll_comp(JNLL_SEL_NONPARAM, flt) += 2.0 *
square(avg_sel(flt, sex, yr));
3998 if(flt_sel_type(flt) == 9) {
4001 if(flt_varying_sel(flt) == 4){
4002 nyrs_tmp = nyrs_hind;
4007 int start_yr = flt_sel_start_yr(flt);
4008 int shape_a0 = flt_sel_pen_first_bin(flt);
4009 if(shape_a0 < 0) shape_a0 = bin_first_selected(flt);
4010 int shape_a1 = flt_sel_pen_last_bin(flt);
4011 if(shape_a1 < 0) shape_a1 = nbins - 2;
4013 if(shape_a1 > nbins - 2) shape_a1 = nbins - 2;
4014 int shape_mode = flt_sel_shape_mode(flt);
4015 for(sex = 0; sex < nsex(sp); sex++){
4016 for(yr = start_yr; yr < nyrs_tmp; yr++){
4020 for(age = shape_a0; age <= shape_a1; age++) {
4021 Type d = log_non_par_sel(flt, sex, age, yr) - log_non_par_sel(flt, sex, age + 1, yr);
4023 jnll_comp(JNLL_SEL_NONPARAM, flt) += sel_curve_pen(flt, 0) * d * d;
4024 else if(sel_curve_pen(flt, 0) >= 0)
4025 jnll_comp(JNLL_SEL_NONPARAM, flt) += sel_curve_pen(flt, 0) *
square( (CppAD::abs(d) + d)/2.0 );
4027 jnll_comp(JNLL_SEL_NONPARAM, flt) += -sel_curve_pen(flt, 0) *
square( (CppAD::abs(d) - d)/2.0 );
4031 if((yr > start_yr) || (start_yr == 0)){
4032 vector<Type> ls(nbins); ls.setZero();
4033 for(age = 0; age < nbins; age++) ls(age) = log_non_par_sel(flt, sex, age, yr);
4035 for(
int a2 = 0; a2 < d2.size(); a2++) jnll_comp(JNLL_SEL_DEV, flt) += sel_curve_pen(flt, 1) * d2(a2) * d2(a2);
4040 for(age = 0; age < nbins; age++) {
4041 Type dd = log_non_par_sel(flt, sex, age, yr) - log_non_par_sel(flt, sex, age, yr - 1);
4042 jnll_comp(JNLL_SEL_DEV, flt) += dd * dd / (2.0 * sel_dev_sd(flt) * sel_dev_sd(flt));
4048 for(
int bin = 0; bin < flt_n_sel_bins(flt); bin++){
4049 for(yr = start_yr; yr < nyrs_tmp; yr++){
4050 jnll_comp(JNLL_SEL_DEV, flt) += sel_curve_pen(flt, 2) * sel_coff_dev(flt, sex, bin, yr) * sel_coff_dev(flt, sex, bin, yr);
4056 if(flt_sel_avgsel_pen(flt) > 0){
4057 int n_base = flt_n_sel_bins(flt) - bin_first_selected(flt);
4058 vector<Type> base(n_base);
4059 for(
int bin = 0; bin < n_base; bin++){
4060 base(bin) = sel_coff(flt, sex, bin + bin_first_selected(flt));
4063 jnll_comp(JNLL_SEL_NONPARAM, flt) += flt_sel_avgsel_pen(flt) * avgsel * avgsel;
4071 if(flt_sel_type(flt) == 13) {
4072 int n_sel_bins = flt_n_sel_bins(flt);
4073 int start_yr = flt_sel_start_yr(flt);
4074 for(sex = 0; sex < nsex(sp); sex++){
4078 vector<Type> base(nbins);
4079 for(
int bin = 0; bin < nbins; bin++){
4080 base(bin) = sel_coff(flt, sex, (bin < n_sel_bins ? bin : n_sel_bins - 1));
4082 vector<Type> est(n_sel_bins);
4083 for(
int bin = 0; bin < n_sel_bins; bin++) est(bin) = base(bin);
4086 for(
int bin = 0; bin < nbins; bin++) base(bin) -= ctr;
4089 for(
int bin = 0; bin < (nbins - 1); bin++) {
4090 Type d = base(bin) - base(bin + 1);
4091 jnll_comp(JNLL_SEL_NONPARAM, flt) += sel_curve_pen(flt, 0) *
square( (CppAD::abs(d) + d)/2.0 );
4095 for(
int a2 = 0; a2 < d2.size(); a2++) {
4096 jnll_comp(JNLL_SEL_NONPARAM, flt) += sel_curve_pen(flt, 1) * d2(a2) * d2(a2);
4099 jnll_comp(JNLL_SEL_NONPARAM, flt) += 2.0 *
square(avg_base);
4103 bool scored = (flt_varying_sel(flt) == 1) || (flt_varying_sel(flt) == 4);
4104 int yr_lo = (flt_varying_sel(flt) == 4) ? start_yr + 1 : 0;
4106 for(yr = yr_lo; yr < nyrs_hind; yr++){
4107 for(
int bin = bin_first_selected(flt); bin < n_sel_bins; bin++) {
4108 jnll_comp(JNLL_SEL_DEV, flt) -= dnorm(sel_coff_dev(flt, sex, bin, yr), Type(0.0), sel_dev_sd(flt),
true);
4120 bool asc_limb = (flt_sel_type(flt) == 1) || (flt_sel_type(flt) == 3) || (flt_sel_type(flt) == 8);
4121 bool desc_limb = (flt_sel_type(flt) == 3) || (flt_sel_type(flt) == 4) || (flt_sel_type(flt) == 8);
4124 if((flt_varying_sel(flt) == 1) || (flt_varying_sel(flt) == 2)){
4125 for(sex = 0; sex < nsex(sp); sex ++){
4126 for(yr = 0; yr < nyrs_hind; yr++){
4128 jnll_comp(JNLL_SEL_DEV, flt) -= dnorm(sel_inf_dev(0, flt, sex, yr), Type(0.0), sel_dev_sd(flt),
true);
4129 jnll_comp(JNLL_SEL_DEV, flt) -= dnorm(log_sel_slp_dev(0, flt, sex, yr), Type(0.0), 4 * sel_dev_sd(flt),
true);
4132 jnll_comp(JNLL_SEL_DEV, flt) -= dnorm(sel_inf_dev(1, flt, sex, yr), Type(0.0), sel_dev_sd(flt),
true);
4133 jnll_comp(JNLL_SEL_DEV, flt) -= dnorm(log_sel_slp_dev(1, flt, sex, yr), Type(0.0), 4 * sel_dev_sd(flt),
true);
4141 if((flt_varying_sel(flt) == 4) || (flt_varying_sel(flt) == 5)){
4142 for(sex = 0; sex < nsex(sp); sex ++){
4143 for(yr = 1; yr < nyrs_hind; yr++){
4145 jnll_comp(JNLL_SEL_DEV, flt) -= dnorm(log_sel_slp_dev(0, flt, sex, yr) - log_sel_slp_dev(0, flt, sex, yr-1), Type(0.0), sel_dev_sd(flt),
true);
4146 jnll_comp(JNLL_SEL_DEV, flt) -= dnorm(sel_inf_dev(0, flt, sex, yr) - sel_inf_dev(0, flt, sex, yr-1), Type(0.0), 4 * sel_dev_sd(flt),
true);
4148 if((flt_varying_sel(flt) == 4) && desc_limb){
4149 jnll_comp(JNLL_SEL_DEV, flt) -= dnorm(sel_inf_dev(1, flt, sex, yr) - sel_inf_dev(1, flt, sex, yr-1), Type(0.0), sel_dev_sd(flt),
true);
4150 jnll_comp(JNLL_SEL_DEV, flt) -= dnorm(log_sel_slp_dev(1, flt, sex, yr) - log_sel_slp_dev(1, flt, sex, yr-1), Type(0.0), sel_dev_sd(flt) * 4,
true);
4160 if(flt_sel_type(flt) == 11){
4161 int start_yr = flt_sel_start_yr(flt);
4162 int alo = sel_norm_bin1(flt);
int ahi = sel_norm_bin2(flt);
4163 if(alo < 0) alo = bin_first_selected(flt);
4164 if(ahi < 0) ahi = nbins - 1;
4165 for(sex = 0; sex < nsex(sp); sex ++){
4166 for(yr = start_yr + 1; yr < nyrs_hind; yr++){
4167 for(age = alo; age <= ahi; age++){
4168 Type s_now = sel_is_length ? sel_at_length(flt, sex, age, yr) : sel_at_age(flt, sex, age, yr);
4169 Type s_prev = sel_is_length ? sel_at_length(flt, sex, age, yr - 1) : sel_at_age(flt, sex, age, yr - 1);
4170 Type d = log(s_now) - log(s_prev);
4171 jnll_comp(JNLL_SEL_DEV, flt) += sel_curve_pen(flt, 0) * d * d;
4173 Type da1 = sel_inf_dev(1, flt, sex, yr) - sel_inf_dev(1, flt, sex, yr - 1);
4174 jnll_comp(JNLL_SEL_DEV, flt) += sel_curve_pen(flt, 2) * da1 * da1;
4181 if(((flt_varying_sel(flt) == 1) || (flt_varying_sel(flt) == 2)) && (flt_sel_type(flt) == 5)){
4182 for(
int bin = 0; bin < flt_n_sel_bins(flt); bin++){
4183 for(sex = 0; sex < nsex(sp); sex++){
4184 for(yr = 0; yr < nyrs_hind; yr++){
4185 jnll_comp(JNLL_SEL_DEV, flt) -= dnorm(sel_coff_dev(flt, sex, bin, yr), Type(0.0), sel_dev_sd(flt),
true);
4193 if(flt_sel_type(flt) == 6){
4194 int n_sel_bins = flt_n_sel_bins(flt);
4195 array<Type> tmp_AR2(n_sel_bins, nyrs_hind);
4197 for(sex = 0; sex < nsex(sp); sex ++){
4198 for (
int bin = 0; bin < n_sel_bins; bin++) {
4199 for (
int yr = 0; yr < nyrs_hind; yr++) {
4200 tmp_AR2(bin, yr) = sel_coff_dev(flt, sex, bin, yr);
4204 Type rho_a =
rho_trans(sel_curve_pen(flt, 0));
4205 Type rho_y =
rho_trans(sel_curve_pen(flt, 1));
4207 Type Sigma_sig_sel = pow(pow(sel_dev_sd(flt),2) / ((1-pow(rho_y,2))*(1-pow(rho_a,2))),0.5);
4210 jnll_comp(JNLL_SEL_DEV, flt) += SCALE(SEPARABLE(AR1(rho_y),AR1(rho_a)), Sigma_sig_sel)(tmp_AR2);
4216 if(flt_sel_type(flt) == 7){
4217 int n_sel_bins = flt_n_sel_bins(flt);
4218 int num_rows = n_sel_bins * nyrs_hind;
4219 matrix<Type> ay_index(num_rows, 2);
4221 for(sex = 0; sex < nsex(sp); sex ++){
4224 for (
int year = 1; year <= nyrs_hind; ++year) {
4225 for (
int bin = 1; bin <= n_sel_bins; ++bin) {
4226 ay_index(row, 0) = bin;
4227 ay_index(row, 1) = year;
4234 Eigen::SparseMatrix<Type> Q_sparse(num_rows, num_rows);
4235 Q_sparse =
construct_Q(nyrs_hind, n_sel_bins, ay_index,
4236 sel_curve_pen(flt, 0), sel_curve_pen(flt, 1), sel_curve_pen(flt, 2),
4237 log(
square(sel_dev_sd(flt))), 0);
4239 array<Type> tmp_AR2(n_sel_bins, nyrs_hind);
4240 for (
int bin = 0; bin < n_sel_bins; bin++) {
4241 for (
int yr = 0; yr < nyrs_hind; yr++) {
4242 tmp_AR2(bin, yr) = sel_coff_dev(flt, sex, bin, yr);
4245 jnll_comp(JNLL_SEL_DEV, flt) += GMRF(Q_sparse)(tmp_AR2);
4254 for(flt = 0; flt < n_flt; flt++){
4255 if(flt_q_lead(flt) == 1){
4259 if( est_index_q(flt) == 2){
4260 jnll_comp(JNLL_Q_PRIOR, flt) -= dnorm(index_log_q(flt), index_log_q_prior(flt) - bias_adjust_proc*
square(index_q_sd(flt))/2.0, index_q_sd(flt),
true);
4264 bool q_devs = (flt_type(flt) > 0) && ((est_index_q(flt) == 1) || (est_index_q(flt) == 2));
4267 if(q_devs && ((index_varying_q(flt) == 1) || (index_varying_q(flt) == 2))){
4268 for(yr = 0; yr < nyrs_hind; yr++){
4269 jnll_comp(JNLL_Q_DEV, flt) -= dnorm(index_q_dev(flt, yr), Type(0.0), index_q_dev_sd(flt),
true );
4274 if(q_devs && (index_varying_q(flt) == 4)){
4275 for(yr = 1; yr < nyrs_hind; yr++){
4276 jnll_comp(JNLL_Q_DEV, flt) -= dnorm(index_q_dev(flt, yr) - index_q_dev(flt, yr-1), Type(0.0), index_q_dev_sd(flt),
true );
4286 for(sp = 0; sp < nspp; sp++) {
4289 int srr_terms_on = (estDynamics(sp) == 0);
4292 if(srr_terms_on && (srr_est_mode == 2) & ((srr_pred_fun == 2) | (srr_pred_fun == 3))){
4293 jnll_comp(JNLL_SRR_PRIOR, sp) -= dnorm(log(steepness(sp, 0)), log(srr_prior(sp)) - bias_adjust_proc*
square(srr_prior_sd(sp))/2.0, srr_prior_sd(sp),
true);
4297 if(srr_terms_on && (srr_est_mode == 3) & ((srr_pred_fun == 2) | (srr_pred_fun == 3))){
4298 Type beta_alpha = ((1 - srr_prior(sp))/
square(srr_prior_sd(sp)) - 1/srr_prior(sp)) *
square(srr_prior(sp));
4299 Type beta_beta = beta_alpha * (1/srr_prior(sp) - 1);
4300 jnll_comp(JNLL_SRR_PRIOR, sp) -= dbeta(steepness(sp, 0), beta_alpha, beta_beta,
true);
4304 if(srr_terms_on && (srr_est_mode == 2) & ((srr_pred_fun == 4) | (srr_pred_fun == 5))){
4305 jnll_comp(JNLL_SRR_PRIOR, sp) -= dnorm(rec_pars(sp, 1), log(srr_prior(sp)) - bias_adjust_proc*
square(srr_prior_sd(sp))/2.0, srr_prior_sd(sp),
true);
4310 if(srr_terms_on && (!
isNA(Bmsy_lim(sp))) && ((srr_pred_fun == 4) || (srr_pred_fun == 5))){
4311 Type bmsy = 1.0/exp(rec_pars(sp, 2));
4312 bmsy =
posfun(Bmsy_lim(sp)/Type(1000000.0) - bmsy, Type(0.001), penalty);
4313 jnll_comp(JNLL_SRR_PRIOR, sp) += 100 * penalty;
4318 if(initMode > 1 && initMode != 5){
4319 for(age = 1; age < nages(sp); age++) {
4320 jnll_comp(JNLL_INIT_DEV, sp) -= dnorm( init_dev(sp, age - 1), -bias_adjust_proc*
square(R_sd(sp))/2.0, R_sd(sp),
true);
4325 for(yr = 0; yr < nyrs_hind; yr++) {
4326 jnll_comp(JNLL_REC_DEV, sp) -= dnorm( rec_dev(sp, yr), -bias_adjust_proc*
square(R_sd(sp))/2.0, R_sd(sp),
true);
4332 if(srr_terms_on && (srr_fun == 0) & (srr_pred_fun > 0)){
4333 for(yr = srr_hat_styr; yr <= srr_hat_endyr; yr++) {
4334 jnll_comp(JNLL_SRR_PENALTY, sp) -= dnorm( log(R(sp, yr)), log(R_hat(sp,yr)) - bias_adjust_proc*
square(R_sd(sp))/2.0, R_sd(sp),
true);
4347 for(fsh_ind = 0; fsh_ind < catch_ctl.rows(); fsh_ind++){
4348 flt = catch_ctl(fsh_ind, 0) - 1;
4349 sp = catch_ctl(fsh_ind, 1) - 1;
4350 flt_yr = catch_ctl(fsh_ind, 2);
4351 if((flt_type(flt) == 1) && (forecast(sp) == 1) && (estDynamics(sp) == 0)){
4352 if(flt_yr == projyr){
4353 CMSY += catch_hat(fsh_ind);
4358 jnll_comp(JNLL_REFPT_PENALTY, 0) = -
square(CMSY/1000000.0);
4360 for(sp = 0; sp < nspp; sp++) {
4361 if((forecast(sp) == 1) && (estDynamics(sp) == 0)){
4363 Type nothing_useful =
posfun( (ssb_depletion(sp, nyrs-1) - Plimit(sp)), Type(0.0001), penalty); (void) nothing_useful;
4364 jnll_comp(JNLL_REFPT_PENALTY, sp) += 500.0 *
square(CMSY/1000.0) * penalty;
4369 for(sp = 0; sp < nspp; sp++) {
4372 if((forecast(sp) == 1) && (msmMode == 0) && (estDynamics(sp) == 0) && ((DynamicHCR == 0) || (DynamicHCR == 1))){
4376 jnll_comp(JNLL_REFPT_PENALTY, sp) += 200*
square((SPRlimit(sp)/SPR0(sp))-Flimit_percent(sp));
4382 if(DynamicHCR == 0){
4383 jnll_comp(JNLL_REFPT_PENALTY, sp) += 200*
square((ssb(sp, nyrs-1)/SB0(sp, nyrs-1))-Ftarget_percent(sp));
4385 for(yr = 1; yr < nyrs; yr++){
4386 jnll_comp(JNLL_REFPT_PENALTY, sp) += 200*
square((DynamicSBF(sp, yr)/DynamicSB0(sp, yr))-Ftarget_percent(sp));
4393 jnll_comp(JNLL_REFPT_PENALTY, sp) += 200*
square((SPRlimit(sp)/SPR0(sp))-Flimit_percent(sp));
4394 jnll_comp(JNLL_REFPT_PENALTY, sp) += 200*
square((SPRtarget(sp)/SPR0(sp))-Ftarget_percent(sp));
4400 if((forecast(sp) == 1) && (msmMode > 0) && (estDynamics(sp) == 0)){
4402 jnll_comp(JNLL_REFPT_PENALTY, sp) += 200*
square((ssb(sp, nyrs-1)/SB0(sp, nyrs-1))-Flimit_percent(sp));
4405 jnll_comp(JNLL_REFPT_PENALTY, sp) += 200*
square((ssb(sp, nyrs-1)/SB0(sp, nyrs-1))-Ftarget_percent(sp));
4408 jnll_comp(JNLL_REFPT_PENALTY, sp) += 200*
square((ssb(sp, nyrs-1)/SB0(sp, nyrs-1))-Flimit_percent(sp));
4416 for(sp = 0; sp < nspp; sp++){
4417 if(estDynamics(sp) == 0){
4418 jnll_comp(JNLL_ZERO_N_PENALTY, sp) += zero_N_pen(sp);
4424 for(sp = 0; sp < nspp; sp++) {
4427 Type M_prior_mean = log(M_prior(sp)) - bias_adjust_proc*
square(M_prior_sd(sp)) / 2.0;
4431 if( (M1_use_prior(sp) == 1) && (M2_use_prior(sp) == 0) ) {
4432 int nsex_tmp = (M1_model(sp) > 1) ? nsex(sp) : 1;
4433 int nage_tmp = (M1_model(sp) == 3) ? nages(sp) : 1;
4435 for(
int sex = 0; sex < nsex_tmp; sex++) {
4436 for(
int age = 0; age < nage_tmp; age++) {
4437 jnll_comp(JNLL_M_PRIOR, sp) -= dnorm(log_M1(sp, sex, age), M_prior_mean, M_prior_sd(sp),
true);
4444 if( M2_use_prior(sp) == 1 ) {
4445 for(
int sex = 0; sex < nsex(sp); sex++) {
4446 for(
int age = 0; age < nages(sp); age++) {
4447 for(
int yr = 0; yr < nyrs_hind; yr++) {
4448 jnll_comp(JNLL_M_PRIOR, sp) -= dnorm(log(M_at_age(sp, sex, age, yr)), M_prior_mean, M_prior_sd(sp),
true);
4458 int num_re_sexes = (M1_model(sp) == 2 && nsex(sp) > 1) ? 2 : 1;
4461 if( (M1_re(sp) == 1) || (M1_re(sp) == 4) ) {
4462 Type sigma_M = exp(M1_dev_log_sd(sp, 0));
4463 Type rho_M_a =
rho_trans(M1_rho(sp, 0, 0));
4464 Type Sigma_M = pow(pow(sigma_M, 2) / (1.0 - pow(rho_M_a, 2)), 0.5);
4465 vector<Type> M_re_age(nages(sp));
4467 for(
int sex = 0; sex < num_re_sexes; sex++) {
4468 for(
int age = 0; age < nages(sp); age++) {
4469 M_re_age(age) = log_M1_dev(sp, sex, age, 0);
4471 jnll_comp(JNLL_M_RE, sp) += SCALE(AR1(rho_M_a), Sigma_M)(M_re_age);
4476 if( (M1_re(sp) == 2) || (M1_re(sp) == 5) ) {
4477 Type sigma_M = exp(M1_dev_log_sd(sp, 0));
4478 Type rho_M_y =
rho_trans(M1_rho(sp, 0, 1));
4479 Type Sigma_M = pow(pow(sigma_M, 2) / (1.0 - pow(rho_M_y, 2)), 0.5);
4480 vector<Type> M_re_yr(nyrs_hind);
4482 for(
int sex = 0; sex < num_re_sexes; sex++) {
4483 for(
int yr = 0; yr < nyrs_hind; yr++) {
4484 M_re_yr(yr) = log_M1_dev(sp, sex, 0, yr);
4486 jnll_comp(JNLL_M_RE, sp) += SCALE(AR1(rho_M_y), Sigma_M)(M_re_yr);
4491 if( (M1_re(sp) == 3) || (M1_re(sp) == 6) ) {
4492 Type sigma_M = exp(M1_dev_log_sd(sp, 0));
4493 Type rho_M_a =
rho_trans(M1_rho(sp, 0, 0));
4494 Type rho_M_y =
rho_trans(M1_rho(sp, 0, 1));
4495 Type Sigma_M = pow(pow(sigma_M, 2) / ((1.0 - pow(rho_M_y, 2)) * (1.0 - pow(rho_M_a, 2))), 0.5);
4496 array<Type> M_re_a_yr(nages(sp), nyrs_hind);
4498 for(
int sex = 0; sex < num_re_sexes; sex++) {
4499 for(
int age = 0; age < nages(sp); age++) {
4500 for(
int yr = 0; yr < nyrs_hind; yr++) {
4501 M_re_a_yr(age, yr) = log_M1_dev(sp, sex, age, yr);
4506 jnll_comp(JNLL_M_RE, sp) += SCALE(SEPARABLE(AR1(rho_M_y), AR1(rho_M_a)), Sigma_M)(M_re_a_yr);
4517 for (
int i = 0; i < beta_linkage.size(); ++i) {
4518 int fam = linkage_prior_family(i);
4519 if (fam == 0)
continue;
4520 Type p1 = linkage_prior_p1(i);
4521 Type p2 = linkage_prior_p2(i);
4522 int slot_col = linkage_species(i) - 1;
4523 if (slot_col < 0) slot_col = 0;
4524 if (slot_col >= n_col) slot_col = 0;
4526 Type b = beta_linkage(i);
4527 bool base_is_log =
true;
4528 if (linkage_is_intercept(i) == 1) {
4530 int sp_in = linkage_species(i);
4531 int sx_in = linkage_sex(i);
4532 int ab_in = linkage_age_bin(i);
4533 int sp_idx = (sp_in == 0) ? 0 : (sp_in - 1);
4534 int sx_idx = (sx_in == 0) ? 0 : (sx_in - 1);
4535 int ab_idx = (ab_in == 0) ? 0 : (ab_in - 1);
4537 int proc = linkage_process(i);
4538 int param = linkage_param(i);
4543 b = rec_pars(sp_idx, param);
4546 b = log_M1(sp_idx, sx_idx, ab_idx);
4550 b = log_growth_pars(sp_idx, sx_idx, param);
4556 int fl_in = linkage_fleet(i);
4557 int fl_idx = (fl_in == 0) ? 0 : (fl_in - 1);
4558 b = index_log_q(fl_idx);
4561 int fl_in = linkage_fleet(i);
4562 int fl_idx = (fl_in == 0) ? 0 : (fl_in - 1);
4571 int fl_in = linkage_fleet(i);
4572 int fl_idx = (fl_in == 0) ? 0 : (fl_in - 1);
4579 base_is_log =
false;
4581 b = log_sel_apical(fl_idx, sx_idx);
4586 base_is_log =
false;
4591 Type log_prior =
linkage_log_prior(fam, b, (linkage_is_intercept(i) == 1) && base_is_log,
4592 p1, p2, bias_adjust_proc);
4593 jnll_comp(JNLL_LINKAGE_PRIOR, slot_col) -= log_prior;
4594 unweighted_jnll_comp(JNLL_LINKAGE_PRIOR, slot_col) -= log_prior;
4600 if (log_sigma_linkage.size() > 0) {
4601 for (
int g = 0; g < log_sigma_linkage.size(); ++g) {
4602 int fam = linkage_re_sigma_prior_family(g);
4603 if (fam == 0)
continue;
4604 Type sd = exp(log_sigma_linkage(g));
4605 Type log_prior =
linkage_log_prior(fam, sd,
false, linkage_re_sigma_prior_p1(g),
4606 linkage_re_sigma_prior_p2(g), bias_adjust_proc);
4607 jnll_comp(JNLL_LINKAGE_PRIOR, 0) -= log_prior;
4608 unweighted_jnll_comp(JNLL_LINKAGE_PRIOR, 0) -= log_prior;
4615 if (trans_rho_linkage.size() > 0) {
4616 for (
int g = 0; g < linkage_re_rho.size(); ++g) {
4617 int fam = linkage_re_rho_prior_family(g);
4618 if ((fam != 1) && (fam != 4))
continue;
4619 Type rho =
rho_trans(trans_rho_linkage(linkage_re_rho(g)));
4620 Type x = (fam == 4) ? (rho + Type(1)) / Type(2) : rho;
4622 linkage_re_rho_prior_p2(g), bias_adjust_proc);
4623 jnll_comp(JNLL_LINKAGE_PRIOR, 0) -= log_prior;
4624 unweighted_jnll_comp(JNLL_LINKAGE_PRIOR, 0) -= log_prior;
4633 for (
int grp = 0; grp < log_sigma_linkage.size(); ++grp) {
4635 int len = slots.size();
4636 if (len == 0)
continue;
4637 vector<Type> re(len);
4638 for (
int t = 0; t < len; ++t) re(t) = beta_linkage_re_all(slots(t));
4640 int st = linkage_re_struct(grp);
4641 Type sigma = exp(log_sigma_linkage(grp));
4644 jnll_comp(JNLL_LINKAGE_RE, 0), unweighted_jnll_comp(JNLL_LINKAGE_RE, 0));
4648 if (linkage_re_obs(grp) >= 0) {
4649 Type osd = exp(log_obs_sd_linkage(linkage_re_obs(grp)));
4650 for (
int t = 0; t < len; ++t) {
4652 if (linkage_re_obs_mask(s) == 0)
continue;
4653 if (osa_mode == 0) {
4654 Type log_dens = dnorm(linkage_re_obs_value(s), re(t), osd,
true);
4655 jnll_comp(JNLL_LINKAGE_RE, 0) -= log_dens;
4656 unweighted_jnll_comp(JNLL_LINKAGE_RE, 0) -= log_dens;
4660 int pos = linkage_re_obsvec_idx(s);
4662 jnll_comp(JNLL_LINKAGE_RE, 0) -= keep(pos) * dnorm(obsvec(pos), re(t), osd,
true);
4663 unweighted_jnll_comp(JNLL_LINKAGE_RE, 0) -= keep(pos) * dnorm(obsvec(pos), re(t), osd,
true);
4665 if (osa_mode == 2) {
4667 obsvec(pos), re(t), osd, keep.cdf_lower(pos), keep.cdf_upper(pos));
4680 for(flt = 0; flt < n_col; flt++){
4681 for(
int ind = 0; ind < JNLL_N_ROWS; ind++){
4682 bool written_above = (ind == JNLL_COMP) || (ind == JNLL_CAAL) || (ind == JNLL_STOMACH) ||
4683 (ind == JNLL_LINKAGE_PRIOR) || (ind == JNLL_LINKAGE_RE);
4685 unweighted_jnll_comp(ind, flt) = jnll_comp(ind, flt);
4696 REPORT( pop_scalar );
4697 ADREPORT( pop_scalar );
4701 REPORT( avgN_at_age );
4702 REPORT( biomass_at_age );
4703 REPORT( biomass_depletion );
4704 REPORT( ssb_depletion );
4707 REPORT( exploitable_biomass );
4719 REPORT( weight_hat );
4720 REPORT( growth_matrix );
4721 REPORT( length_hat );
4723 ADREPORT( biomass );
4731 ADREPORT( exploitable_biomass );
4732 ADREPORT( biomass_depletion );
4733 ADREPORT( ssb_depletion );
4741 matrix<Type> log_biomass = biomass; log_biomass = log(biomass.array());
4742 matrix<Type> log_ssb = ssb; log_ssb = log(ssb.array());
4743 matrix<Type> log_R = R; log_R = log(R.array());
4744 REPORT( log_biomass );
4747 ADREPORT( log_biomass );
4748 ADREPORT( log_ssb );
4766 if(adreport_sel == 1){
4769 vector<int> sel_first_bin(n_flt); sel_first_bin.setZero();
4770 vector<int> sel_rep_bins(n_flt); sel_rep_bins.setZero();
4771 for(
int flt = 0; flt < n_flt; flt++){
4772 if((flt_type(flt) <= 0) || (flt_sel_lead(flt) != 1) ||
4773 (flt_sel_type(flt) == 0) || (flt_sel_dim(flt) == 1))
continue;
4774 int n_age = nages(flt_spp(flt));
4775 int first = bin_first_selected(flt);
4776 if(first < 0) first = 0;
4777 if(first > n_age) first = n_age;
4778 sel_first_bin(flt) = first;
4779 sel_rep_bins(flt) = n_age - first;
4783 for(
int flt = 0; flt < n_flt; flt++){
4784 n_sel += nsex(flt_spp(flt)) * sel_rep_bins(flt) * nyrs_hind;
4787 vector<Type> log_sel_at_age(n_sel); log_sel_at_age.setZero();
4788 matrix<Type> log_sel_at_age_index(n_sel, 4); log_sel_at_age_index.setZero();
4791 for(
int flt = 0; flt < n_flt; flt++){
4792 if(sel_rep_bins(flt) == 0)
continue;
4793 int sel_sp = flt_spp(flt);
4794 for(
int sel_sex = 0; sel_sex < nsex(sel_sp); sel_sex++){
4795 for(
int sel_age = sel_first_bin(flt); sel_age < nages(sel_sp); sel_age++){
4796 for(
int sel_yr = 0; sel_yr < nyrs_hind; sel_yr++){
4797 log_sel_at_age(i_sel) = log(
max2(sel_at_age(flt, sel_sex, sel_age, sel_yr),
4799 log_sel_at_age_index(i_sel, 0) = Type(flt + 1);
4800 log_sel_at_age_index(i_sel, 1) = Type(sel_sex + 1);
4801 log_sel_at_age_index(i_sel, 2) = Type(minage(sel_sp) + sel_age);
4802 log_sel_at_age_index(i_sel, 3) = Type(styr + sel_yr);
4809 REPORT( log_sel_at_age );
4810 REPORT( log_sel_at_age_index );
4811 ADREPORT( log_sel_at_age );
4822 REPORT( DynamicB0 );
4823 REPORT( DynamicSB0 );
4824 REPORT( DynamicSBF );
4828 REPORT( SPRtarget );
4829 REPORT( steepness );
4835 REPORT( sel_at_age );
4836 REPORT( sel_at_length );
4840 REPORT( index_hat );
4843 vector<Type> log_index_hat = index_hat; log_index_hat = log(index_hat.array());
4844 REPORT( log_index_hat );
4845 ADREPORT( log_index_hat );
4846 if(adreport_catch == 1){
4847 ADREPORT( catch_hat );
4854 REPORT( beta_linkage );
4855 ADREPORT( beta_linkage );
4856 REPORT( beta_linkage_re );
4857 REPORT( beta_linkage_re_pen );
4860 REPORT( beta_linkage_re_all );
4861 REPORT( beta_linkage_obs );
4862 ADREPORT( beta_linkage_obs );
4868 REPORT( F_flt_age );
4870 REPORT( catch_hat );
4871 REPORT( max_catch_hat );
4873 REPORT( catch_analytical_sd );
4877 REPORT( age_obs_hat );
4882 REPORT( equil_catch_hat );
4884 REPORT( pred_CAAL );
4888 REPORT( jnll_comp );
4889 REPORT( unweighted_jnll_comp );
4893 REPORT( consumption_at_age );
4899 REPORT( suitability );
4900 REPORT( M1_at_age );
4901 REPORT( M2_at_age );
4903 REPORT( B_eaten_as_prey );
4905 REPORT( vulnerability );
4906 REPORT( vulnerability_other );
4910 REPORT( suit_other );
4911 REPORT( stom_div_bio );
4912 REPORT( avail_food );
4926 if(estimateMode < 4) {
4927 jnll = jnll_comp.sum();
4933 if(estimateMode > 3) {
4934 jnll = dummy * dummy;
void calculate_temperature_function(matrix< Type > &fT, int nspp, int nyrs, const vector< int > &Ceq, const vector< int > &Cindex, const vector< Type > &Qc, const vector< Type > &Tcm, const vector< Type > &Tco, const vector< Type > &Tcl, const vector< Type > &CK1, const vector< Type > &CK4, const matrix< Type > &env_index)
Temperature-dependent function of consumption (fT) for bioenergetics.
void calculate_ration(array< Type > &consumption_at_age, int nspp, int nyrs, int nyrs_hind, const vector< int > &nsex, const vector< int > &nages, const vector< int > &Ceq, const vector< Type > &CA, const vector< Type > &CB, const vector< Type > &fday, const vector< Type > &Pvalue, matrix< Type > &fT, array< Type > &ration_data, array< Type > &weight_hat)
Annual ration of a predator at age, in kg per predator per year.
One-step-ahead (OSA) residual helpers for composition data.
Type ddirmultinom_osa(vector< Type > obs, vector< Type > alpha, data_indicator< vector< Type >, Type > keep, int do_osa, int give_log)
Keep-aware Dirichlet-multinomial log-density via conditional beta-binomials.
Type dmultinom_osa(vector< Type > x, vector< Type > p, data_indicator< vector< Type >, Type > keep, int do_osa, int give_log, int do_cdf=0)
Keep-aware multinomial log-density via conditional binomials.
Type osa_norm_cdf_terms(Type x, Type mu, Type sd, Type cdf_lower, Type cdf_upper)
One-step-ahead CDF terms for a normal observation.
Type osa_trunc_norm_cdf_terms(Type x, Type mu, Type sd, Type cdf_lower, Type cdf_upper)
One-step-ahead CDF terms for a normal left-truncated at zero.
Random draws for composition-style observations, used by SIMULATE.
vector< Type > rdirmultinom_rce(Type N, vector< Type > alpha)
Draw Dirichlet-multinomial counts.
vector< Type > rmultinom_rce(Type N, vector< Type > p)
Draw multinomial counts by sequential conditional binomials.
void organize_diet_obs(const int &nspp, const int &nyrs, const int &nyrs_hind, const int &styr, const vector< int > &minage, const vector< int > &nsex, matrix< Type > &diet_obs, matrix< int > &diet_ctl, array< Type > &diet_prop)
Parses empirical diet observations and maps them into a 5D proportion array.
void predict_stomach_content(const int &nspp, const int &nyrs_hind, const int &styr, const vector< int > &suit_styr, const vector< int > &suit_endyr, const vector< int > &minage, const vector< int > &nsex, const vector< int > &nages, matrix< Type > &diet_obs, matrix< int > &diet_ctl, array< Type > &avgN_at_age, array< Type > &diet_prop_hat, matrix< Type > &diet_hat)
Expected stomach content proportions given the modeled population state.
void calculate_other_food_diet_prop(const int &nspp, const int &nyrs, const vector< int > &nsex, const vector< int > &nages, const vector< Type > &other_food, array< Type > &diet_prop, array< Type > &other_food_diet_prop)
Residual stomach content: the "other food" not explicitly modeled.
void calculate_weight(array< Type > &weight_hat, array< Type > &length_hat, array< Type > &growth_matrix, array< Type > &mat_weight_hat, array< Type > &weight_obs, const vector< int > &growth_model, const vector< int > &growth_sd_style, const vector< int > &growth_sd_form, const vector< int > &growth_plus_length, const vector< Type > &plus_group_decay, const int &nspp, const int &nyrs, const int &nyrs_hind, const int &n_flt, const vector< int > &flt_spp, vector< Type > flt_month, const vector< int > &nsex, const vector< int > &minage, const vector< Type > &growth_age_L1, const vector< int > &nages, const vector< int > &nlengths, const vector< int > &pop_wt_index, const vector< int > &ssb_wt_index, const vector< int > &flt_wt_index, vector< Type > spawn_month, matrix< Type > &lengths, const vector< int > &nlengths_pop, matrix< Type > &lengths_pop, array< Type > &growth_parameters, array< Type > &growth_log_sd, matrix< Type > weight_length_pars, const vector< int > &mat_len_use, matrix< Type > &mat_len_pars, array< Type > &log_M1)
Population, SSB and fleet weight-at-age (kg) for every species and fleet.
Small differentiable (AD-safe) utilities used across the CEATTLE template: numeric guards and transfo...
Type concentrated_lognormal_sd(Type mean_sq_resid, Type bias_adjust)
Concentrated (analytical) sd of a lognormal observation likelihood.
Eigen::SparseMatrix< Type > construct_Q(int n_years, int n_ages, matrix< Type > ay_Index, Type rho_y, Type rho_a, Type rho_c, Type log_sigma2, int Var_Param)
Construct a sparse GMRF precision matrix separable along year, age, and cohort.
Type log_mean_exp(const vector< Type > &x)
log(mean(exp(x))) over a non-empty vector, computed so it cannot overflow.
Type imax(const vector< Type > &x)
Type max2(Type x, Type y)
Type ddirmultinom(vector< Type > obs, vector< Type > alpha, int do_log)
Type posfun(Type x, Type eps, Type &penalty)
vector< Type > first_difference(const vector< Type > &x)
Likelihood math for section 13 of ceattle.cpp: the composition row density and linkage prior families...
Type linkage_log_prior(int family, Type value, bool value_is_log, Type p1, Type p2, Type bias_adjust_proc)
Log density of a linkage prior on the natural scale.
void fold_comp_tails(vector< Type > &obs, vector< Type > &hat, int &n_comp, int nbins_blk, int nblk, int yng, int old)
Composition tail accumulation (AFSC ac_yng / ac_old).
void comp_row_nll(int ll_type, int osa_mode, vector< Type > obs_prop, vector< Type > hat_prop, Type offset, Type n_samp, Type weight, Type dm_theta, vector< Type > &obsvec, data_indicator< vector< Type >, Type > &keep, int osa_start, const char *type_name, Type &nll, Type &unweighted_nll)
Negative log-likelihood of one composition row: an age or length composition, or one length bin of co...
#define RCEATTLE_SEL_DN_PEAK
#define RCEATTLE_N_REC_PARAMS
#define RCEATTLE_SEL_APICAL
#define RCEATTLE_COMP_THETA_DIET
#define RCEATTLE_REC_BETA
#define RCEATTLE_SEL_END_LOGIT
#define RCEATTLE_PROC_RECRUIT
void rceattle_apply_q_linkages(matrix< Type > &q_offset, int link_code, const LinkageTable< Type > <, int n_flt, int nyrs)
Add catchability offsets to q_offset[fleet, yr].
#define RCEATTLE_REC_R_INIT
#define RCEATTLE_PROC_GROWTH
#define RCEATTLE_N_GROWTH_PARAMS
#define RCEATTLE_REC_ALPHA
void rceattle_apply_sel_linkages(array< Type > &slp_offset, array< Type > &inf_offset, array< Type > &coff_offset, array< Type > &apical_offset, array< Type > &dn6_offset, int link_code, const LinkageTable< Type > <, int n_flt, int max_sex, int n_sel_bins, int nyrs)
Add selectivity offsets, by fleet, sex and year, to one of five arrays.
void rceattle_apply_recruitment_linkages(array< Type > &rec_offset, int link_code, const LinkageTable< Type > <, int nspp, int nyrs)
Add recruitment offsets to rec_offset[sp, param, yr].
#define RCEATTLE_COMP_THETA_COMP
#define RCEATTLE_LINK_IDENTITY
void rceattle_apply_growth_linkages(array< Type > &growth_offset, int link_code, const LinkageTable< Type > <, int nspp, const vector< int > &nsex, int nyrs)
Add growth offsets to growth_offset[sp, sex, yr, param].
#define RCEATTLE_SEL_SLP_ASC
void rceattle_re_group_nll(int structure, vector< Type > re, Type sigma, Type rho, Type &nll, Type &nll_unweighted)
Add the negative log density of one group's deviations to two accumulators.
#define RCEATTLE_PROC_COMP
void rceattle_apply_M_linkages(array< Type > &M_offset, int link_code, const LinkageTable< Type > <, int nspp, const vector< int > &nsex, const vector< int > &nages, int nyrs)
Add natural-mortality offsets to M_offset[sp, sex, age, yr].
#define RCEATTLE_LINK_LOG
#define RCEATTLE_SEL_INF_DESC
vector< int > rceattle_group_slots(const vector< int > &group_of_slot, int grp)
The random-effect slots of one group, in slot order, which is time order within a group (....
#define RCEATTLE_SEL_SLP_DESC
#define RCEATTLE_LINK_EXPONENTIAL
#define RCEATTLE_SEL_INF_ASC
#define RCEATTLE_COMP_THETA_CAAL
#define RCEATTLE_PROC_SEL
void calculate_msvpa_predation(array< Type > &avail_food, array< Type > &avail_food_dB0, array< Type > &avail_food_dBF, array< Type > &M2_at_age, array< Type > &M2_at_age_dB0, array< Type > &M2_at_age_dBF, array< Type > &M2_prop, array< Type > &B_eaten, array< Type > &B_eaten_as_prey, array< Type > &diet_prop_hat, array< Type > &other_diet_prop_hat, int nspp, int nyrs, const vector< int > &nsex, const vector< int > &nages, int msmMode, array< Type > &avgN_at_age, array< Type > &avgN_at_age_dB0, array< Type > &avgN_at_age_dBF, array< Type > &suitability, array< Type > &suit_other, array< Type > &weight_hat, array< Type > &ration, const vector< Type > &other_food, bool fill_report_only, bool fill_diet, bool fill_dynamic_B0)
Available food, predation mortality (M2), and consumed biomass (MSVPA).
void calculate_parametric_suitability(array< Type > &suitability, array< Type > &suit_other, int nspp, int nyrs, const vector< int > &nsex, const vector< int > &nages, const vector< int > &suitMode, array< Type > &length_hat, array< Type > &weight_hat, matrix< Type > &vulnerability, const vector< Type > &vulnerability_other, const vector< Type > &gam_a, const vector< Type > &gam_b)
Parametric suitability (Gamma, Lognormal, Normal) from size ratios.
void calculate_vulnerability(matrix< Type > &vulnerability, vector< Type > &vulnerability_other, int nspp, const vector< int > &suitMode, matrix< Type > &log_phi)
Transforms predator-prey preference parameters into vulnerability.
void calculate_msvpa_suitability(array< Type > &stom_div_bio, array< Type > &suitability, array< Type > &suit_other, int nspp, int nyrs, const vector< int > &suit_styr, const vector< int > &suit_endyr, const vector< int > &nyrs_suit, int msmMode, const vector< int > &nsex, const vector< int > &nages, const vector< int > &suitMode, array< Type > &diet_prop, array< Type > &avgN_at_age, array< Type > &weight_hat, array< Type > &other_food_diet_prop)
MSVPA/empirical suitability, from stomach content and prey biomass.
Type calculate_recruitment(int srr_fun, Type R0, Type ssb, Type srr_alpha, Type srr_beta, Type rec_dev, Type spr0)
Calculates the recruitment for a given species and year.
void calculate_selectivity(const int &nspp, const int &n_flt, const int &nyrs, const int &nyrs_hind, const int &styr, const vector< int > &nsex, const vector< int > &nages, const vector< int > &nlengths, matrix< Type > &lengths, const vector< int > &nlengths_pop, matrix< Type > &lengths_pop, const vector< int > &flt_spp, const vector< int > &flt_sel_type, const vector< int > &flt_varying_sel, const vector< int > &flt_sel_dim, const vector< int > &bin_first_selected, const vector< int > &flt_n_sel_bins, const vector< int > &flt_sel_cap_bin, const vector< int > &sel_norm_bin1, const vector< int > &sel_norm_bin2, const vector< int > &sel_norm_scope, const vector< int > &flt_sel_start_yr, matrix< Type > &emp_sel_obs, matrix< int > &emp_sel_ctl, array< Type > &log_sel_slp, array< Type > &log_sel_slp_dev, array< Type > &sel_inf, array< Type > &sel_inf_dev, array< Type > &sel_coff, array< Type > &sel_coff_dev, array< Type > &avg_sel, array< Type > &non_par_sel, array< Type > &log_non_par_sel, array< Type > &sel_at_length, array< Type > &sel_at_age, array< Type > &growth_matrix, array< Type > &sel_slp_off, array< Type > &sel_slp_off_nat, array< Type > &sel_inf_off, array< Type > &sel_inf_off_nat, array< Type > &sel_coff_off, array< Type > &sel_coff_off_nat, const int &sel_apical_on, array< Type > &log_sel_apical, array< Type > &sel_apical_off, array< Type > &sel_apical_off_nat, array< Type > &sel_dn6, array< Type > &sel_dn6_off, array< Type > &sel_dn6_off_nat, matrix< int > &sel_dn6_ends)
Calculates selectivity for every fleet, sex, bin (age or length) and year.
Spawning biomass per recruit, for the Amendment-56 SPR proxies.
Type spawning_biomass_per_recruit(const vector< Type > &n, const vector< Type > &Z, const vector< Type > &weight, const vector< Type > &mature_female, Type spawn_month)
Female spawning biomass per recruit, given a survivor schedule.
vector< Type > per_recruit_survivors(const vector< Type > &Z)
Survivors at age from one recruit under a fixed mortality schedule.
The linkage table: one entry per row, and the shared design matrix.