-
Notifications
You must be signed in to change notification settings - Fork 0
/
atfgoa2013.tpl
1580 lines (1490 loc) · 59 KB
/
atfgoa2013.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
DATA_SECTION
// !!CLASS ofstream post("eval.csv")
!!CLASS ofstream evalout("atfgoa2013.mcmc.out");
//!!CLASS ofstream post("atfgoa2013.csv");
init_int styr //(1) start year of model
//uses separate M for males and females read in from the data file
//changed wt at age, and transition matrix to include all age data 84-96.
init_int endyr //(2) end year
init_int styr_fut //(3) start year of projections (endyr+1)
init_int endyr_fut //(4) end year of projections
//read only female M when doing profile like on male M
// init_number Mf
init_vector M(1,2) //(5)
init_int phase_F40 //(6)
init_number median_rec //(7)
init_int median_rec_yrs //(8)
init_int nages_read //(9)
init_int nages //(10)
//selectivity is set to the selectivity at nselages-1 after age nselages
init_int nselages //(11)
init_int nselages_srv1 //(12)
init_int monot_sel //(13)
init_int monot_sel_srv1 //(14)
init_int phase_logistic_sel //(15)
init_int phase_selcoffs //(16)
init_int phase_logistic_sel_srv1 //(17)
init_int phase_selcoffs_srv1 //(18)
init_vector wt_like(1,8) //(19)
// !! phase_logistic_sel = -2;
// !! if (phase_logistic_sel<0) phase_selcoffs=2; else phase_selcoffs=-2;
//sample size for length comps for weighting likelihoods
init_int nlen_r //(20)
//reduce nlen by one to cut off 1st length bin
init_int nobs_fish //(21)
//!!cout<<nobs_fish<<endl;
init_ivector yrs_fish(1,nobs_fish) //(22)
init_matrix nsamples_fish(1,2,1,nobs_fish) //(23)
init_int nobs_srv1 //(24)
init_ivector yrs_srv1(1,nobs_srv1) //(25)
init_int nobs_srv1_length //(26)
init_ivector yrs_srv1_length(1,nobs_srv1_length) //(27)
init_matrix nsamples_srv1_length(1,2,1,nobs_srv1_length) //(28)
init_int nobs_srv1_age //(29)
init_ivector yrs_srv1_age(1,nobs_srv1_age) //(30)
init_vector like_wght(1,5) //(31)
init_matrix nsamples_srv1_age(1,2,1,nobs_srv1_age) //(32)
init_3darray obs_p_srv1_len_r(1,2,1,nobs_srv1_length,1,nlen_r) //(33) 2x2x21
//!!cout<<"obs_p_srv1_len_r"<<endl;
//!!cout<<obs_p_srv1_len_r<<endl;
init_3darray obs_p_srv1_age_read(1,2,1,nobs_srv1_age,1,nages_read) //(34) 2x11x21
init_3darray obs_p_fish_r(1,2,1,nobs_fish,1,nlen_r) //(35) 2x31x21
// !!cout<<"obs_p_fish"<<endl;
// !!cout<<obs_p_fish<<endl;
init_vector catch_bio(styr,endyr) //(36)
!!cout<<catch_bio<<endl;
init_vector obs_srv1(1,nobs_srv1) //(37)
init_vector obs_srv1_sd(1,nobs_srv1) //(38)
//need wt vector by length for split sex?
//init_vector wt(1,nlen)
init_matrix wt(1,2,1,nages) //(39)
!!cout<<"wt"<<wt<<std::endl;
init_vector maturity(1,nages) //(40)
!!cout<<"maturity"<<maturity<<std::endl;
//this is how to change the name of the data file
//can put at the beginning of the file to read everything or
//part way down
// LOCAL_CALCS
// ad_comm::change_datafile_name("atka.ctl")
// END_CALCS
//Local_calcs is indented 1 space
// LOCAL_CALCS
// cout<<maturity<<endl;
// END_CALCS
//length age transition matrix
init_3darray lenage(1,2,1,nages,1,nlen_r-1) //(41)
!!cout<<"lenage"<<lenage<<std::endl;
//LOCAL_CALCS
// cout<<nages<<endl;
//END_CALCS
int styr_rec;
vector cv_srv1(1,nobs_srv1);
//year
int i
//age
int j
//sex
int k
//
int ii
int m
int nlen
LOCAL_CALCS
nlen=nlen_r-1;
styr_rec=styr-nages+1;
if(nselages>nages) nselages=nages;
if(nselages_srv1>nages) nselages_srv1=nages;
//calculate cv for survey
cv_srv1=elem_div(obs_srv1_sd,obs_srv1);
//change weights to tons
wt=wt*.001;
// nages=nages-10
// cout<<nobs_srv1<<endl;
// cout<<wt<<endl;
END_CALCS
3darray obs_p_srv1_age(1,2,1,nobs_srv1_age,1,nages)
3darray obs_p_srv1_age_r(1,2,1,nobs_srv1_age,1,nages)
3darray obs_p_srv1_length(1,2,1,nobs_srv1_length,1,nlen)
3darray obs_p_fish(1,2,1,nobs_fish,1,nlen)
vector obs_sexr(1,nobs_fish)
vector obs_sexr_srv1(1,nobs_srv1_age)
vector obs_sexr_srv1_l(1,nobs_srv1_length)
INITIALIZATION_SECTION
//can have different mortality for males and females
// Mm .35
mean_log_rec 18
log_avg_fmort -5.
q1 1.
fmort_dev 0.00001
fish_slope_f .4
fish_sel50_f 5.
fish_slope_m .1
fish_sel50_m 8.
srv1_slope_f .8
srv1_sel50_f 4.
srv1_slope_m .4
srv1_sel50_m 8
PARAMETER_SECTION
//parameters to be estimated are all ones that begin with init_ and have a positive
//phase, negative phase means are fixed.
//phase of 8 is greater than last phase so does q1 in last phase
// init_bounded_number q1(.5,2,8)
//fix q1 to be 1 otherwise it went to lower bound of .5
init_bounded_number q1(0.01,20.0,-8)
//phase of -1 means M is fixed
// init_bounded_number Mm(.1,1.0,8)
init_number mean_log_rec(1)
init_bounded_dev_vector rec_dev(styr_rec,endyr,-15,15,2)
// init_vector rec_dev_future(styr_fut,endyr_fut,phase_F40);
init_number log_avg_fmort(1)
init_bounded_dev_vector fmort_dev(styr,endyr,-5.0,3.5,1)
init_matrix log_selcoffs_fish(1,2,1,nselages,phase_selcoffs)
init_matrix log_selcoffs_srv1(1,2,1,nselages_srv1,phase_selcoffs_srv1)
init_bounded_number fish_slope_f(.1,5.,phase_logistic_sel)
init_bounded_number fish_sel50_f(1.,8.,phase_logistic_sel)
init_bounded_number fish_slope_m(.05,2.0,phase_logistic_sel)
init_bounded_number fish_sel50_m(1.,25.,phase_logistic_sel)
init_bounded_number srv1_slope_f(.1,5.,phase_logistic_sel_srv1)
init_bounded_number srv1_sel50_f(1.,10.,phase_logistic_sel_srv1)
init_bounded_number srv1_slope_m(.01,5.,phase_logistic_sel_srv1)
init_bounded_number srv1_sel50_m(1.,10.,phase_logistic_sel_srv1)
init_bounded_number sexr_param_fish(1.0,1.0,-5) //this was hitting bound of 1.0 so fixed it - should free up to check
init_bounded_number sexr_param_srv(.25,1.0,phase_selcoffs_srv1)
// Parameters for computing SPR rates
init_bounded_number F40(0.05,.5,phase_F40)
init_bounded_number F35(0.05,.5,phase_F40)
// init_bounded_number F30(0.01,1.,phase_F40)
matrix log_sel_fish(1,2,1,nages)
matrix log_sel_srv1(1,2,1,nages)
matrix sel(1,2,1,nages)
matrix sel_srv1(1,2,1,nages)
vector avgsel_fish(1,2)
vector avgsel_srv1(1,2)
matrix popn(1,2,styr,endyr)
matrix totn_srv1(1,2,styr,endyr)
vector explbiom(styr,endyr)
vector pred_bio(styr,endyr)
vector fspbio(styr,endyr)
vector pred_srv1(styr,endyr)
3darray pred_p_fish(1,2,styr,endyr,1,nlen)
// 3darray pred_p_fish_1(1,2,styr,endyr,1,nlen)
3darray pred_p_srv1_age(1,2,styr,endyr,1,nages)
// 3darray pred_p_srv1_age_1(1,2,styr,endyr,1,nages)
3darray pred_p_srv1_len(1,2,styr,endyr,1,nlen)
// 3darray pred_p_srv1_len_1(1,2,styr,endyr,1,nlen)
vector pred_catch(styr,endyr)
3darray natage(1,2,styr,endyr,1,nages)
3darray catage(1,2,styr,endyr,1,nages)
3darray natlength(1,2,styr,endyr,1,nlen)
//matrix u(styr,endyr,1,nages)
3darray Z(1,2,styr,endyr,1,nages)
3darray F(1,2,styr,endyr,1,nages)
3darray S(1,2,styr,endyr,1,nages)
vector fmort(styr,endyr)
number rbar
vector surv(1,2)
vector offset(1,3)
number rec_like
number rec_like2
number catch_like
vector age_like(1,3)
vector sel_like(1,4)
number fpen
number surv_like
sdreport_vector recruits(styr,endyr)
sdreport_vector biomassrep(styr,endyr)
sdreport_vector fspbiorep(styr,endyr)
sdreport_number endbiom
sdreport_number depletion
objective_function_value f
number tmp
vector pred_sexr(styr,endyr)
vector preds_sexr(styr,endyr)
// Stuff for SPR and yield projections
number sigmar
number ftmp
number SB0
number SBF40
number SBF35
// number SBF30
number sprpen
matrix Nspr(1,3,1,nages)
3darray nage_future(1,2,styr_fut,endyr_fut,1,nages)
// matrix fspbiom_fut(1,5,styr_fut,endyr_fut)
3darray F_future(1,2,styr_fut,endyr_fut,1,nages)
3darray Z_future(1,2,styr_fut,endyr_fut,1,nages)
3darray S_future(1,2,styr_fut,endyr_fut,1,nages)
3darray catage_future(1,2,styr_fut,endyr_fut,1,nages)
number avg_rec_dev_future
vector avg_F_future(1,3)
// sdreport_matrix catch_future(1,5,styr_fut,endyr_fut)// Note, don't put projection for F=0 (it messes up the hessian matrix)
matrix catch_future(1,5,styr_fut,endyr_fut) // use this not the sdreport when projecting with F=0 otherwise hessian is screwed up
sdreport_matrix fspbiom_fut(1,5,styr_fut,endyr_fut)
sdreport_matrix future_biomass(1,5,styr_fut,endyr_fut)
vector explbiom_fut(styr_fut,endyr_fut)
number maxsel_fish
vector maxsel_srv1(1,2)
number B0
number B40
number B35
number AMeanRec
number like_natm
number like_q
//do likelihood profile on male M - need to estimate M in model to do this
//run the model with the likelihood profile switch
// likeprof_number lp_Mm;
// vector M(1,2)
// likeprof_number lp_q1;
PRELIMINARY_CALCS_SECTION
//cout<<"to prelim calcs"<<endl;
// M(1)=Mf;
//chop lower ages off and accumulate older ages
//sex loop
for(k=1; k<=2; k++)
{
for (i=1; i <= nobs_srv1_age; i++)
{
for(j=1; j < nages; j++)
{
//ages go from 3 to 15
obs_p_srv1_age_r(k,i,j)=obs_p_srv1_age_read(k,i,j+2);
}
for(m=nages; m<=nages_read; m++) //error here you are adding 13+ rather than 15+
{
obs_p_srv1_age_r(k,i,nages)+=obs_p_srv1_age_read(k,i,m);
}
}
}
// cout << " at beg of prel calcs " <<endl;
//compute sex ratio in catch
// cout<< " sum operation "<< sum(obs_p_fish_r(1,1)(2,nlen+1)) <<endl;
for(i=1; i<=nobs_fish;i++)
{
obs_sexr(i)=(sum(obs_p_fish_r(1,i)(2,nlen+1)))/(sum(obs_p_fish_r(1,i)(2,nlen+1))+sum(obs_p_fish_r(2,i)(2,nlen+1)));
}
cout<<"obs_sexr"<<obs_sexr<<std::endl;
//age obs sex ratio in survey
for(i=1; i<=nobs_srv1_age;i++)
{
obs_sexr_srv1(i)=(sum(obs_p_srv1_age_r(1,i)))/(sum(obs_p_srv1_age_r(1,i))+sum(obs_p_srv1_age_r(2,i)));
}
//length obs sex ratio in survey
for(i=1; i<=nobs_srv1_length;i++)
{
obs_sexr_srv1_l(i)=(sum(obs_p_srv1_len_r(1,i)(2,nlen+1)))/(sum(obs_p_srv1_len_r(1,i)(2,nlen+1))+sum(obs_p_srv1_len_r(2,i)(2,nlen+1)));
}
cout<<"obs_sexr_srv1"<<obs_sexr_srv1<<std::endl;
// cout<< " thru sex ratio "<<endl;
//Compute offset for multinomial
// offset is a constant nplog(p) is added to the likelihood
// magnitude depends on nsamples(sample size) and p's_
//k is sex loop
offset=0.;
for(k=1; k<=2; k++)
{
for (i=1; i <= nobs_fish; i++)
{
//make observations proportions by year
//fishery offset
for (j=1; j<=nlen; j++)
{
obs_p_fish(k,i,j)=(obs_p_fish_r(k,i,j+1))/(sum(obs_p_fish_r(1,i)(2,nlen_r))+sum(obs_p_fish_r(2,i)(2,nlen_r)));
if (obs_p_fish(k,i,j)>0.0)
{
offset(1)-=nsamples_fish(k,i)*obs_p_fish(k,i,j)*log(obs_p_fish(k,i,j));
}
}
}
}
//survey length offset
for(k=1; k<=2;k++)
{
for (i=1; i <= nobs_srv1_length; i++)
{
// cout<< " to obs_p_srv1_length "<<endl;
for (j=1; j<=nlen; j++)
{
obs_p_srv1_length(k,i,j)=obs_p_srv1_len_r(k,i,j+1)/sum(obs_p_srv1_len_r(1,i)(2,nlen_r)+obs_p_srv1_len_r(2,i)(2,nlen_r));
if (obs_p_srv1_length(k,i,j)>0.0)
{
offset(2)-=nsamples_srv1_length(k,i)*obs_p_srv1_length(k,i,j)*log(obs_p_srv1_length(k,i,j));
}
}
}
}
//survey age offset
for(k=1; k<=2;k++)
{
for (i=1; i <= nobs_srv1_age; i++)
{
obs_p_srv1_age(k,i)=obs_p_srv1_age_r(k,i)/(sum(obs_p_srv1_age_r(1,i))+sum(obs_p_srv1_age_r(2,i)));
//cout<<obs_p_srv1(i)<<endl;
for (j=1; j<=nages; j++)
{
if (obs_p_srv1_age(k,i,j)>0.0)
{
offset(3)-=nsamples_srv1_age(k,i)*obs_p_srv1_age(k,i,j)*log(obs_p_srv1_age(k,i,j));
}
}
}
}
//cout<<endl<<"to end of offset"<<endl;
PROCEDURE_SECTION
// q1=qrun(irun);
//use this when doing like profile on male M
// M(2)=Mm;
// lp_Mm=Mm;
//this is if doing likelihood profile
// lp_q1=q1;
get_selectivity();
//cout<<"sel"<<endl;
get_mortality();
//cout<<"mort"<<endl;
surv(1)=mfexp(-1.0* M(1));
surv(2)=mfexp(-1.0* M(2));
get_numbers_at_age();
//cout<<"numbers at age"<<endl;
get_catch_at_age();
//cout<<"catch at age"<<endl;
if (active(F40))
compute_spr_rates();
if (last_phase())
{
Future_projections();
}
if (sd_phase() || mceval_phase())
{
if (mceval_phase())
{
evalout << f << " " ;
// loop over years and print in one long row.
for (i=styr;i<=endyr;i++)
evalout<< fspbio(i) << " ";
for (i=styr;i<=endyr;i++)
evalout<< natage(1,i)*wt(1) + natage(2,i)*wt(2) <<" ";
for (i=styr;i<=endyr;i++)
evalout << 2*natage(1,i,1) <<" ";
// hit carriage return on file
evalout << endl;
}
}
evaluate_the_objective_function();
//FUNCTION WriteMCMC
// post<<
//// Mm<<","<<
//// q1<<","<<
//// srv1_slope_f <<","<<
//// srv1_sel50_f <<","<<
//// srv1_slope_m <<","<<
//// srv1_sel50_m <<","<<
//// fish_slope_f <<","<<
//// fish_sel50_f <<","<<
//// fish_slope_m <<","<<
//// fish_sel50_m <<","<<
// pred_bio <<","<<
// fspbio <<","<<
// F40 <<","<<
// fspbiom_fut <<","<<
// catch_future <<","<<
// B0 <<","<<
// B40 <<","<<
// B35 <<","<<
//
// endl;
FUNCTION get_growthmatrix
// for(ilen=1;ilen<=nlenm;ilen++)
// {
// for(il2=1;il2<=nlenm;il2++)
// {
//
//cumd_norm does the cumulative normal distribution for the value length_bins(il2) with
//a mean of mean_length(ilen) and sd sd_mean_length(ilen)
// add 2.5 to the length bin value to get the upper bound of the length bin since
//length_bins is the mean of the interval
// if(il2<2)
// {
// len_len(1,ilen,il2)=cumd_norm((length_bins(il2)+2.5-mean_length(1,ilen))/sd_mean_length(1,ilen));
// len_len(2,ilen,il2)=cumd_norm((length_bins(il2)+2.5-mean_length(2,ilen))/sd_mean_length(2,ilen));
// }
// if(il2>1)
// {
// rec_len(il2)=cumd_norm((length_bins(il2)-length_rec)/var_rec)-cumd_norm((length_bins(il2-1)-length_rec)/var_rec);
// len_len(1,ilen,il2)=cumd_norm((length_bins(il2)+2.5-mean_length(1,ilen))/sd_mean_length(1,ilen))-cumd_norm((length_bins(il2-1)+2.5-mean_length(1,ilen))/sd_mean_length(1,ilen));
// len_len(2,ilen,il2)=cumd_norm((length_bins(il2)+2.5-mean_length(2,ilen))/sd_mean_length(2,ilen))-cumd_norm((length_bins(il2-1)+2.5-mean_length(2,ilen))/sd_mean_length(2,ilen));
// cout<<(length_bins(il2)-mean_length(1,ilen))/sd_mean_length(1,ilen)<<endl;
// cout<<cumd_norm((length_bins(il2)-mean_length(1,ilen))/sd_mean_length(1,ilen))<<endl;
// cout<<len_len(1,ilen,il2-1)<<endl;
// }
// sum_len(1,ilen)+=len_len(1,ilen,il2);
// sum_len(2,ilen)+=len_len(2,ilen,il2);
// }
// len_len(1,ilen,nlenm)=len_len(1,ilen,nlenm)+1-sum_len(1,ilen);
// len_len(2,ilen,nlenm)=len_len(2,ilen,nlenm)+1-sum_len(2,ilen);
// }
FUNCTION get_selectivity
//cout<<"to begin of sel"<<endl;
//fishery selectivities
if(active(log_selcoffs_fish))
{
//turn off logistic curve
// phase_logistic_sel=-2; IS 2015 testing
for(k=1;k<=2;k++)
{
for (j=1;j<=nselages;j++)
{
log_sel_fish(k,j)=log_selcoffs_fish(k,j);
}
}
//sets selectivity of ages older than nselages to selectivity at nselages
//cout<<"to nselages loop"<<endl;
for(k=1;k<=2;k++)
{
for (j=nselages+1;j<=nages;j++)
{
log_sel_fish(k,j)=log_sel_fish(k,j-1);
}
}
for(k=1;k<=2;k++)
{
avgsel_fish(k)=log(mean(mfexp(log_selcoffs_fish(k))));
}
//vector=vector-scalar same as vector-=scalar
//scaling selectivities by subracting the mean so exp(mean(s))=1.
//selectivities can be greater than 1 but mean is 1.
//cout<<"calc log_sel_fish"<<endl;
for(k=1;k<=2;k++)
{
log_sel_fish(k)-=log(mean(mfexp(log_sel_fish(k))));
sel(k)=mfexp(log_sel_fish(k));
if(k==2)
{
sel(k)=sel(k)*sexr_param_fish;
}
//cout<<"sel survey"<<sel_srv1<<endl;
}
}
else
{
//logistic selectivity curve
for (j=1;j<=nages;j++)
{
if(j<=nselages)
{
sel(1,j)=1./(1.+mfexp(-1.*fish_slope_f*(double(j)-fish_sel50_f)));
sel(2,j)=1./(1.+mfexp(-1.*fish_slope_m*(double(j)-fish_sel50_m)));
}
else
{
sel(1,j)=sel(1,j-1);
sel(2,j)=sel(2,j-1);
}
}
}
//survey selectivities
if(active(log_selcoffs_srv1))
{
//turn off logistic curve
// phase_logistic_sel_srv1=-2;
for(k=1;k<=2;k++)
{
for (j=1;j<=nselages_srv1;j++)
{
log_sel_srv1(k,j)=log_selcoffs_srv1(k,j);
}
}
//sets selectivity of ages older than nselages_srv1 to selectivity at nselages_srv1
for(k=1;k<=2;k++)
{
for (j=nselages_srv1+1;j<=nages;j++)
{
log_sel_srv1(k,j)=log_sel_srv1(k,j-1);
}
}
for(k=1;k<=2;k++)
{
avgsel_srv1(k)=log(mean(mfexp(log_selcoffs_srv1(k))));
}
//vector=vector-scalar same as vector-=scalar
//scaling selectivities by subracting the mean so exp(mean(s))=1.
//selectivities can be greater than 1 but mean is 1.
for(k=1;k<=2;k++)
{
log_sel_srv1(k)-=log(mean(mfexp(log_sel_srv1(k))));
sel_srv1(k)=mfexp(log_sel_srv1(k));
if(k==2)
{
sel_srv1(k)=sel_srv1(k)*sexr_param_srv;
}
//cout<<"sel survey"<<sel_srv1<<endl;
}
}
else
{
//logistic selectivity curve
for (j=1;j<=nages;j++)
{
if(j<=nselages_srv1)
{
sel_srv1(1,j)=1./(1.+mfexp(-1.*srv1_slope_f*(double(j)-srv1_sel50_f)));
sel_srv1(2,j)=1./(1.+mfexp(-1.*srv1_slope_m*(double(j)-srv1_sel50_m)));
}
else
{
sel_srv1(1,j)=sel_srv1(1,j-1);
sel_srv1(2,j)=sel_srv1(2,j-1);
}
}
}
//cout<<"sel fishery"<<sel<<std::endl;
//cout<<"sel survey "<<sel_srv1<<std::endl;
FUNCTION get_mortality
maxsel_fish=max(sel(1));
if(maxsel_fish<max(sel(2)))
maxsel_fish=max(sel(2));
// if(active(log_selcoffs_srv1))
// {
maxsel_srv1(1)=max(sel_srv1(1));
maxsel_srv1(2)=maxsel_srv1(1);
fmort=mfexp(log_avg_fmort+fmort_dev);
for(k=1;k<=2;k++)
{
for (i=styr;i<=endyr;i++)
{
F(k,i)=(sel(k)/maxsel_fish)*fmort(i);
Z(k,i)=F(k,i) + M(k);
}
}
S=mfexp(-1.0*Z);
// cout<<"to end of get_mortality"<<endl;
FUNCTION get_numbers_at_age
// maxsel_fish=max(sel(1));
// if(maxsel_fish<max(sel(2)))
// maxsel_fish=max(sel(2));
// if(active(log_selcoffs_srv1))
// {
// maxsel_srv1(1)=max(sel_srv1(1));
// maxsel_srv1(2)=maxsel_srv1(1);
if(maxsel_srv1(1)<max(sel_srv1(2)))
{
maxsel_srv1(1)=max(sel_srv1(2));
maxsel_srv1(2)=maxsel_srv1(1);
}
// }
// else
// {
// maxsel_srv1(1)=max(sel_srv1(1));
// maxsel_srv1(2)=max(sel_srv1(2));
// }
// cout<<"begin numbers at age"<<endl;
int itmp;
//calc initial population
for (j=1;j<nages;j++)
{
itmp=styr+1-j;
natage(1,styr,j)=mfexp(mean_log_rec-(M(1)*double(j-1))+rec_dev(itmp));
natage(2,styr,j)=mfexp(mean_log_rec-(M(2)*double(j-1))+rec_dev(itmp));
}
itmp=styr+1-nages;
//cout<<"initial 1"<<endl;
//cout<<"to last age "<<endl;
//last age
natage(1,styr,nages)=mfexp(mean_log_rec+rec_dev(itmp)-(M(1)*(nages-1)))/(1.- surv(1));
natage(2,styr,nages)=mfexp(mean_log_rec+rec_dev(itmp)-(M(2)*(nages-1)))/(1.- surv(2));
//cout<<"to next years"<<endl;
// Now do for next several years----------------------------------
for (i=styr+1;i<=endyr;i++) //added else clause in 2015 to match BSAI code
{
if(i<=(endyr-1))
{
natage(1,i,1)=mfexp(mean_log_rec+rec_dev(i));
natage(2,i,1)=natage(1,i,1);
}
else
{
natage(1,i,1)=median_rec;
natage(2,i,1)=natage(1,i,1);
}
}
//cout<<"initial 2"<<endl;
//numbers at age
for(k=1;k<=2;k++)
{
for (i=styr;i< endyr;i++)
{
//cout<<"to subvector op"<<endl;
//subvector - avoids writing a j loop =++ increments the right side
//(1,nages-1) to 1+1 to nages-1+1 then does the assignment x(i)(1,n)
//takes the ith row of x the columns 1 to n
// natage(k,i+1)(2,nages)=++elem_prod(natage(k,i)(1,nages-1),S(k,i)(1,nages-1));
for(j=1;j<nages;j++)
{
natage(k,i+1,j+1)=natage(k,i,j)*S(k,i,j);
}
//accumulates oldest ages
// cout<<"done with j loop"<<endl;
natage(k,i+1,nages)+=natage(k,i,nages)*S(k,i,nages);
// cout<<"done with natage nages"<<endl;
//popn is exploitable numbers
popn(k,i)= natage(k,i)*sel(k);
// cout<<"popn "<<endl;
// cout<<popn(k,i)<<endl;
}
// cout<<"to popn"<<endl;
popn(k,endyr)=natage(k,endyr)*sel(k);
}
for (i=styr;i<=endyr;i++)
{
for(k=1;k<=2;k++)
{
//population numbers at length
natlength(k,i)=natage(k,i)*lenage(k);
//numbers to rescale age comps and length comps
totn_srv1(k,i)=(natage(k,i)*sel_srv1(k));
}
}
//cout<<"2nd loop"<<endl;
//predicted survey values
fspbio=0.;
for (i=styr;i<=endyr;i++)
{
fspbio(i)+=natage(1,i)*elem_prod(wt(1),maturity);
explbiom(i)=0.;
pred_bio(i)=0.;
pred_srv1(i)=0.;
for(k=1;k<=2;k++)
{
pred_srv1(i)+=q1*(natage(k,i)*elem_prod(sel_srv1(k)/maxsel_srv1(k),wt(k)));
// pred_srv1(i)+=q1*(natage(k,i)*elem_prod(sel_srv1(k),wt(k)));
//next line used to fix q1 to 1.0 - problem is if you start from a bin file, even if the bounds
// are set different in the tpl file the program will take to value from the bin file and use that
// pred_srv1(i)=1.0*(natage(i)*elem_prod(sel_srv1,wt));
explbiom(i)+=natage(k,i)*elem_prod(sel(k),wt(k))/maxsel_fish;
// explbiom(i)+=natage(k,i)*elem_prod(sel(k),wt(k));
pred_bio(i)+=natage(k,i)*wt(k);
// cout<<" to lenage calc"<<endl;
pred_p_srv1_len(k,i)=elem_prod(sel_srv1(k),natage(k,i))*lenage(k)/(totn_srv1(1,i)+totn_srv1(2,i));
// pred_p_srv1_len(k,i)=pred_p_srv1_len_1(k,i)/sum(pred_p_srv1_len_1(1,i)+pred_p_srv1_len_1(2,i));
pred_p_srv1_age(k,i)=elem_prod(sel_srv1(k),natage(k,i))/(totn_srv1(1,i)+totn_srv1(2,i));
// if(i==61 && k==1)
// {
// for(j=1;j<=nages;j++)
// {
// cout<<(sel_srv1(k,j)*natage(k,i,j))/(totn_srv1(1,i)+totn_srv1(2,i))<<endl;
// }
// }
// pred_p_srv1_age(k,i)=pred_p_srv1_age_1(k,i)/sum(pred_p_srv1_age_1(1,i)+pred_p_srv1_age_1(2,i));
}
}
//cout<<"3rd loop"<<endl;
// cout<<" predicted age proportions sex 1 year 61 "<<endl;
// cout<<pred_p_srv1_age(1,61)<<endl;
// cout<<" selectivities "<<endl;
// cout<<sel_srv1<<endl;
// cout<<" sel params "<<endl;
// cout<<srv1_slope_f<<" "<<srv1_sel50_f<<endl;
// cout<<srv1_slope_m<<" "<<srv1_sel50_m<<endl;
// cout<<" numbers at age sex 1 year 61 "<<endl;
// cout<<natage(1,61)<<endl;
// cout<<"total numbers for survey year 61"<<endl;
// cout<<totn_srv1(1,61)<<endl;
// cout<<totn_srv1(2,61)<<endl;
//variables for standard dev report
biomassrep=pred_bio;
fspbiorep=fspbio;
for(i=styr;i<=endyr;i++)
{
recruits(i)=mfexp(mean_log_rec+rec_dev(i));
}
depletion=pred_bio(endyr)/pred_bio(styr);
endbiom=pred_bio(endyr);
// cout<<"end get numbers at age"<<endl;
FUNCTION get_catch_at_age
for (i=styr; i<=endyr; i++)
{
pred_catch(i)=0.;
for(k=1;k<=2;k++)
{
//--Baranov's equation here-----------------------------------
for (j = 1 ; j<= nages; j++)
{
catage(k,i,j) = natage(k,i,j)*F(k,i,j)*(1.-S(k,i,j))/Z(k,i,j);
pred_catch(i)+=catage(k,i,j)*wt(k,j);
}
pred_p_fish(k,i)=elem_prod(sel(k),natage(k,i))*lenage(k)/(popn(1,i)+popn(2,i));
// pred_p_fish(k,i)=pred_p_fish_1(k,i)/(sum(pred_p_fish_1(1,i))+sum(pred_p_fish_1(2,i)));
}
}
// cout<<"end catch at age"<<endl;
FUNCTION Future_projections
//cout<<"to future proj"<<endl;
for(k=1;k<=2;k++)
{
nage_future(k,styr_fut)(2,nages)=++elem_prod(natage(k,endyr)(1,nages-1),S(k,endyr)(1,nages-1));
nage_future(k,styr_fut,nages)+=natage(k,endyr,nages)*S(k,endyr,nages);
}
future_biomass=0.;
catch_future=0.;
fspbiom_fut=0.;
for (int l=1;l<=5;l++)
{
switch (l)
{
case 1:
ftmp=F40;
break;
case 2:
ftmp=F35;
break;
case 3:
ftmp=0.0;
break;
case 4:
ftmp=mean(mfexp(log_avg_fmort+fmort_dev(endyr-4,endyr)));
break;
// cout<<ftmp<<endl;
case 5:
ftmp=mean(mfexp(log_avg_fmort+fmort_dev(endyr-4,endyr)));
// cout<<ftmp<<endl;
break;
}
// Get future F's
for(k=1;k<=2;k++)
{
for (i=endyr+1;i<=endyr_fut;i++)
{
if(l>3){ftmp=mean(mfexp(log_avg_fmort+fmort_dev(endyr-4,endyr)));}
if(i>(endyr+1) && l==5) {ftmp=F35;}
if(i>(endyr+1) && l==4) {ftmp=F40;}
// cout<<ftmp<<endl;
for (j=1;j<=nages;j++)
{
F_future(k,i,j) = (sel(k,j)/maxsel_fish)*ftmp;
Z_future(k,i,j) = F_future(k,i,j)+M(k);
S_future(k,i,j) = mfexp(-1.*Z_future(k,i,j));
}
}
// Future Recruitment (and spawners)
for (i=styr_fut;i<endyr_fut;i++)
{
nage_future(k,i,1) = AMeanRec;
// Now graduate for the next year....
nage_future(k,i+1)(2,nages) = ++elem_prod(nage_future(k,i)(1,nages-1),S_future(k,i)(1,nages-1));
nage_future(k,i+1,nages) += nage_future(k,i,nages)*S_future(k,i,nages);
}
nage_future(k,endyr_fut,1) = AMeanRec;
// Now get catch at future ages
fspbiom_fut(l)=0.;
// catch_future(l)=0.;
for (i=styr_fut; i<=endyr_fut; i++)
{
for (j = 1 ; j<= nages; j++)
{
catage_future(k,i,j) = nage_future(k,i,j) * F_future(k,i,j) * ( 1.- S_future(k,i,j) ) / Z_future(k,i,j);
if(k==1)
{
fspbiom_fut(l,i) += nage_future(1,i,j)*wt(1,j)*maturity(j);
}
}
if (l!=3){
catch_future(l,i) += catage_future(k,i)*wt(k);
}
// if(l==3){
// catch_future(l,i) = 0.0;
// }
future_biomass(l,i) += nage_future(k,i)*wt(k);
} //end loop over future years
} //end loop over sex
fspbiom_fut(l)=0.;
for(i=styr_fut;i<=endyr_fut;i++)
{
for(j=1;j<=nages;j++)
{
fspbiom_fut(l,i)+= nage_future(1,i,j)*wt(1,j)*maturity(j);
}
}
} //End of loop over F's
FUNCTION compute_spr_rates
//Compute SPR Rates and add them to the likelihood for Females
SB0=0.;
SBF40=0.;
SBF35=0.;
//SBF30=0.;
//average recruitment from 1981 to last year of estimated recruitment to make it consistent
//with projection model (recruits start at 1978 age 1, so age 3 1981)
AMeanRec=mean(mfexp(mean_log_rec+rec_dev(81,endyr)));
// Initialize the recruit (1) for each F (F40 etc)
for (i=1;i<=3;i++)
Nspr(i,1)=1.;
for (j=2;j<nages;j++)
{
Nspr(1,j)=Nspr(1,j-1)*mfexp(-1.*M(1));
Nspr(2,j)=Nspr(2,j-1)*mfexp(-1.*(M(1)+F40*sel(1,j-1)/maxsel_fish));
Nspr(3,j)=Nspr(3,j-1)*mfexp(-1.*(M(1)+F35*sel(1,j-1)/maxsel_fish));
}
//cout<<F40<<" "<<F30<<" "<<Nspr<<endl;
// cout<<"spr calc"<<endl;
// Now do plus group
Nspr(1,nages)=Nspr(1,nages-1)*mfexp(-1.*M(1))/(1.-mfexp(-1.*M(1)));
Nspr(2,nages)=Nspr(2,nages-1)*mfexp(-1.* (M(1)+F40*sel(1,nages-1)/maxsel_fish))/(1.-mfexp(-1.*(M(1)+F40*sel(1,nages)/maxsel_fish)));
Nspr(3,nages)=Nspr(3,nages-1)*mfexp(-1.* (M(1)+F35*sel(1,nages-1)/maxsel_fish))/(1.-mfexp(-1.*(M(1)+F35*sel(1,nages)/maxsel_fish)));
// Nspr(3,nages)=Nspr(3,nages-1)*mfexp(-1.* (M(1)+F30*sel(1,nages-1)/maxsel_fish))/ (1.-mfexp(-1.*(M(1)+F30*sel(1,nages)/maxsel_fish)));
//cout<<"plus group"<<endl;
for (j=1;j<=nages;j++)
{
// Kill them off till- use fraction of the year e.g. april=0.25 - not for atf they spawn in winter so put in 0.0
// Number ProportMat Wt Amount die off prior to spawning (within that year)
SB0 += Nspr(1,j)*maturity(j)*wt(1,j)*mfexp(-0.0*M(1));
SBF40 += Nspr(2,j)*maturity(j)*wt(1,j)*mfexp(-0.0*(M(1)+F40*sel(1,j)/maxsel_fish));
SBF35 += Nspr(3,j)*maturity(j)*wt(1,j)*mfexp(-0.0*(M(1)+F35*sel(1,j)/maxsel_fish));
// SBF30 += Nspr(3,j)*maturity(j)*wt(1,j)*mfexp(-0.0*(M(1)+F30*sel(1,j)/maxsel_fish));
}
// cout<<"kill thenm off"<<endl;
sprpen = 300.*square((SBF40/SB0)-0.4);
// sprpen += 300.*square((SBF30/SB0)-0.30);
sprpen += 300.*square((SBF35/SB0)-0.35);
//AMeanRec is mean of recruitment(2*recruitment is total recruitment(male+female))
B0 = AMeanRec*SB0 ;
B40 = AMeanRec*SBF40 ;
B35 = AMeanRec*SBF35 ;
//cout<<"sbr/sno "<<endl;
//cout<<SBF40/SB0<<" "<<SBF30/SB0<<endl;
FUNCTION evaluate_the_objective_function
age_like=0.;
sel_like=0.;
fpen=.0;
rec_like=.0;
rec_like2=0.;
surv_like=.0;
catch_like=.0;
f=.0;
like_natm=0.; //not used
like_q=0.; //not used
if (active(rec_dev))
{
age_like=0.;
int ii;
//recruitment likelihood - norm2 is sum of square values
//cout<<"to rec_like"<<endl;
//cout<<" rec_devs = "<<rec_dev<<endl;
rec_like=norm2(rec_dev(styr_rec,endyr));
// rec_like2=norm2(rec_dev(endyr-median_rec_yrs+1,endyr))/(2*norm2(rec_dev(81,endyr-median_rec_yrs))/(endyr-median_rec_yrs-1981+1-1)+0.001);
// 0.038 is the variance of the rec_dev from 1981 to 1999 - this restricts recruits in last years
//to mean log recruits if don't have survey data in current year.
// rec_like2 =0.00000001*0.4*(5./(2*.038))*norm2(rec_dev(endyr-median_rec_yrs+1,endyr)-mean(rec_dev(81,endyr-median_rec_yrs)));
// cout<<rec_like<<" , "<<rec_like2<<endl;
f+=rec_like;
f+=rec_like2;
//cout<<"to loop"<<endl;
// if(active(fish_slope_f))
// {
for(k=1;k<=2;k++)
{
for (i=1; i <= nobs_fish; i++)
{
ii=yrs_fish(i);
// cout<<pred_p_fish(ii)<<endl<<endl;
//fishery length likelihood
for (j=1; j<=nlen; j++)
{
age_like(1)-=nsamples_fish(k,i)*(0.0001+obs_p_fish(k,i,j))*log(pred_p_fish(k,ii,j)+0.0001); //was 1e-5
//cout << age_like << " " << i << " "<<j<< endl;
}
}
}
//add the offset to the likelihood
age_like(1)-=offset(1);
//survey
for(k=1;k<=2;k++)
{
for (i=1; i <=nobs_srv1_length; i++)
{
ii=yrs_srv1_length(i);
//survey likelihood
for (j=1; j<=nlen; j++)
{
age_like(2)-=nsamples_srv1_length(k,i)*(1e-4+obs_p_srv1_length(k,i,j))*log(pred_p_srv1_len(k,ii,j)+1e-4); //was 1e-3
//cout << age_like << " " << i << " "<<j<< endl;
}
}
}
age_like(2)-=offset(2);
//bracket for active(fish_slope_f)
// }
//survey ages
for(k=1;k<=2;k++)
{
for (i=1; i <=nobs_srv1_age; i++)
{
ii=yrs_srv1_age(i);
//survey likelihood
for (j=1; j<=nages; j++)
{
age_like(3)-=nsamples_srv1_age(k,i)*(1e-4+obs_p_srv1_age(k,i,j))*log(pred_p_srv1_age(k,ii,j)+1e-4); //was 1e-3
// cout << age_like << " " << i << " "<<j<< endl;
}
}
}
age_like(3)-=offset(3);
// #(31) likelihood weights for fishery length data, survey length, age data, catch likelihood, survey biomass likelihood
// 0.25 1.0 1.0 50.0 1.0
f+=like_wght(1)*age_like(1); //fishery length data
f+=like_wght(2)*age_like(2); //survey length
f+=like_wght(3)*age_like(3); //age data
//end of if(active (rec_dev))
}
//cout<<" f before survey = "<<f<<endl;
// Fit to indices (lognormal)
//weight each years estimate by 1/(2*variance) - use cv of biomass in sqrt(log(cv^2+1)) as sd of log(biomass)
surv_like = norm2(elem_div(log(obs_srv1+.0001)-log(pred_srv1(yrs_srv1)+.0001),sqrt(2)*sqrt(log(elem_prod(cv_srv1,cv_srv1)+1.0)))); //was 0.000001
//this subtracts the log(sd) from the likelihood - is a constant so I'm not adding it.
// surv_like-= sum(log(sqrt(log(elem_prod(cv_srv1,cv_srv1)+1.0))));
// surv_like = norm2(log(obs_srv1+.01)-log(pred_srv1(yrs_srv1)+.01));
catch_like=norm2(log(catch_bio+.0001)-log(pred_catch+.0001)); //was 0.000001
//selectivity likelihood is penalty on how smooth selectivities are
//here are taking the sum of squares of the second differences
if(active(log_selcoffs_fish))
{
sel_like(1)=wt_like(1)*norm2(first_difference(first_difference(log_sel_fish(1))));
sel_like(3)=wt_like(3)*norm2(first_difference(first_difference(log_sel_fish(2))));
for (j=1;j<nages;j++)
{
if(monot_sel==1)
{
if (log_sel_fish(1,j)>log_sel_fish(1,j+1))