-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevil_short.m
2336 lines (1840 loc) · 61.1 KB
/
devil_short.m
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
% program for analysis of Tasmanian devil population projection
% ----------------------------------------
% model with Guiler (1978) survival rates
% ----------------------------------------
format long g
% define max age
age_max=6;
% define survival
s0g=0.3983; % Guiler (1978)
s1g=0.6278; % Guiler (1978)
s2g=0.6524; % Guiler (1978)
s3g=0.6220; % Guiler (1978)
s4g=0.5285; % Guiler (1978)
s5g=0.2711; % Guiler (1978)
% define fecundity parameters
m_imm=0.061; % 1 female seen with pouch young (Pemberton 1990)
m_primi=2.0; % average pouch young per primiparous female (Guiler 1978)
m_prime=3.6; % average pouch young per 'prime' (3-5) female (Guiler 1978)
m_old=2.0; % average pouch young per old (6) female (Guiler 1978)
rs_lo=0.80; % proportion of females carrying young (Guiler 1978)
%rs_hi=0.81; % proportion of females carrying young (Pemberton 1990)
rs=rs_lo;
primi=2; % age at first reproduction
% initial population sizes
Nmin=130000; % N. Mooney, pers. comm (2003)
Nmax=150000; % N. Mooney, pers. comm (2003)
Navgmax = mean([Nmin Nmax]);
Navg50 = Navgmax / 2; % start out 1/2 population size
% sex ratios
sr_tot1 = 0.5 % proportion female - Pemberton (1990)
sr_tot2 = 0.5362 % Guiler (1970a)
sr_avg = mean([sr_tot1,sr_tot2]);
x1=0.48; % pouch young proportion female (Guiler 1978)
x2=0.58; % pouch young proportion female (Guiler 1970a)
x3=0.57; % pouch young proportion female (Guiler 1970a)
x4=0.56; % pouch young proportion female (Hughes 1982)
x5=0.50; % pouch young proportion female (Pemberton 1990)
x_avg = mean([x1,x2,x3,x4,x5]);
sr = sr_tot2
x=x_avg
%total females in population
f=Navg50/2;
%Initial population size vector
N=f;
% the normal matrix
ag=[0 s0g*m_primi*x*rs s0g*m_prime*x*rs s0g*m_prime*x*rs s0g*m_prime*x*rs s0g*m_old*x*rs
s1g 0 0 0 0 0
0 s2g 0 0 0 0
0 0 s3g 0 0 0
0 0 0 s4g 0 0
0 0 0 0 s5g 0];
% eigenvalues & eigenvectors
[w,d] = eig(ag);
v=conj(inv(w));
%lambda
lambda=diag(d);
% max lambdas
[maxlambda,imax]=max(diag(d));
%logmaxlambda=exponential rate of increase (r)
r=log(maxlambda);
% stable age distribution
w=w(:,imax);
% reproductive values
v=real(v(imax,:))';
% sensitivities and elasticities
senmat=(v*w')/(v'*w);
emat=(ag.*senmat)/maxlambda;
%damping ratios
rho=lambda(1)./abs(lambda);
% periods of oscillation
period=2*pi./angle(lambda);
% stable size distribution (scaled to sum to 1)
ssd_g=w(:,1)/sum(w(:,1));
% ssd classes
ssd_juv = sum(ssd_g(1:1,1));
ssd_ad = sum(ssd_g(2:6,1));
% reproductive value (scaled so stage 1 = 1)
reprovalue=real(v(1,:)')/real(v(1,1));
% mean generation time (avg age breeding females)
%gen=log(0.9846)/(log(maxlambda));
% size of matrix
k=size(ag,1);
% pick initial vectors
ng=ones(k,1);
%n(1:2,1)=0.2382*N/2; % Guiler 1978
%n(3:6,1)=0.7618*N/4; % Guiler 1978
ng=ssd_g*N;
% age vector
age_vec=zeros(k,1);
for a=1:k-1
age_vec(a+1,1)=a;
end % a loop
age_vec=age_vec+1;
%Calculate Quasi-extinction times
Qg=(log(50/sum(ng)))/log(maxlambda);
if Qg < 0;
Qg='infinity';
else Qg=Qg;
end
% do a simulation
% first specify the initial condition and length of simulation
tlimit=25;
%set population size year step vector
pop_vecg=ones(tlimit+1,1);
pop_vecg(1,1)=sum(ng);
%set year step vector
yr_vec=ones(tlimit+1,1);
for c=1:tlimit
yr_vec(c+1,1)=c;
yr_vec(1,1)=0;
end % c loop
% survival variance vectors
s1_vec = ones(tlimit+1,1);
s1_vec(1,1) = (s1g*(1-s1g))/ng(1,1);
s2_vec = ones(tlimit+1,1);
s2_vec(1,1) = (s2g*(1-s2g))/ng(1,1);
% assume stable-age distribution
N_stable = ssd_g*N;
%then iterate
for i=1:tlimit;
ng=ag*ng;
s1_vec(i+1,1)=(s1g*(1-s1g))/ng(1,1);
s2_vec(i+1,1)=(s2g*(1-s2g))/ng(2,1);
pop_vecg(i+1,1)=(sum(ng));
end
log_pop_vecg=log10(pop_vecg);
%total population size after 'tlimit' years
pop_st=N
pop_end=sum(ng)
%total population size after 'tlimit' years
N
tlimit
pop_end=sum(ng)
maxlambda
r
Qg
format short
% construct survival vector for display
survg=ones(k,1);
survg(1,1)=s0g;
survg(2,1)=s1g;
survg(3,1)=s2g;
survg(4,1)=s3g;
survg(5,1)=s4g;
survg(6,1)=s5g;
% construct fertility vector for display
fertg=ones(k,1);
fertg(1,1)=m_imm*x;
fertg(2,1)=m_primi*x*rs;
fertg(3,1)=m_prime*x*rs;
fertg(4,1)=m_prime*x*rs;
fertg(5,1)=m_prime*x*rs;
fertg(6,1)=m_old&x*rs;
% continue displays
ag
ssd_juv
ssd_ad
emat
%Make density independent plots
subplot(2,2,1), plot(yr_vec,pop_vecg);
axis square;
axis([-0.5 tlimit+1 0 (max(pop_vecg)+(0.25*(max(pop_vecg))))]);
xlabel('year');
ylabel('N females');
subplot(2,2,2), plot(yr_vec,log_pop_vecg);
axis square;
axis([-0.5 tlimit+1 0 (max(log_pop_vecg)+(0.25*(max(log_pop_vecg))))]);
xlabel('year');
ylabel('log N females');
%Make survival and fecundity plots
subplot(2,2,3), plot(age_vec,survg);
axis square;
axis([0.5 k+0.5 0 1]);
xlabel('age in years');
ylabel('survival probability');
subplot(2,2,4), plot(age_vec,fertg);
axis square;
axis([0.5 k+0.5 0 2]);
xlabel('age in years');
ylabel('m (fertility)');
pause
% -----------------------------------------------------------
% model with U-shaped mortality to produce 'max' growth rate
% -----------------------------------------------------------
format long g
% define max age
age_max=6;
% re-define survival
s0_hi=0.500; % Pemberton (1990) - 1983/84 (weanlings)
s0_lo=0.360; % Pemberton (1990) - 1984/85 (weanlings)
s0_avg = mean([s0_hi,s0_lo])
s0 = s0_avg;
s1_lo=0.170; % Pemberton (1990) - mean annual rates - lower
s1_hi=0.300;
s2_lo=0.820; % Pemberton (1990) - mean annual rates - lower
s2_hi=0.90;
%s1=mean([s1_lo,s1_hi]);
%s2=mean([s2_lo,s2_hi]);
s2=0.82 % Pemberton (1990), Method 3
s1=s1g; % using Guiler's data for U-shaped curve
s3=s2; % assuming no change in survival until last 1 years
s4=s2; % assuming no change in survival until last 1 years
s5=s5g; % using Guiler's data for U-shaped curve
% define fecundity parameters
m_imm=0.061; % 1 female seen with pouch young (Pemberton 1990)
m_primi=2.0; % average pouch young per primiparous female (Guiler 1978)
m_prime=3.6; % average pouch young per 'prime' (3-5) female (Guiler 1978)
m_old=2.0; % average pouch young per old (6) female (Guiler 1978)
%rs_lo=0.80; % proportion of females carrying young (Guiler 1978)
rs_hi=0.81; % proportion of females carrying young (Pemberton 1990)
rs=rs_hi;
primi=2; % age at first reproduction
% initial population sizes
Nmin=130000; % N. Mooney, pers. comm (2003)
Nmax=150000; % N. Mooney, pers. comm (2003)
Navgmax = mean([Nmin Nmax]);
Navg50 = Navgmax / 2; % start out 1/2 population size
% sex ratios
sr_tot1 = 0.5 % proportion female - Pemberton (1990)
sr_tot2 = 0.5362 % Guiler (1970a)
sr_avg = mean([sr_tot1,sr_tot2]);
x1=0.48; % pouch young proportion female (Guiler 1978)
x2=0.58; % pouch young proportion female (Guiler 1970a)
x3=0.57; % pouch young proportion female (Guiler 1970a)
x4=0.56; % pouch young proportion female (Hughes 1982)
x5=0.50; % pouch young proportion female (Pemberton 1990)
x_avg = mean([x1,x2,x3,x4,x5]);
sr = sr_avg
x=x_avg
%total females in population
f=Navg50/2;
%Initial population size vector
N=f;
% the normal matrix
amax=[s0*m_imm*x s0*m_primi*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_old*x*rs
s1 0 0 0 0 0
0 s2 0 0 0 0
0 0 s3 0 0 0
0 0 0 s4 0 0
0 0 0 0 s5 0];
% eigenvalues & eigenvectors
[w,d] = eig(amax);
v=conj(inv(w));
%lambda
lambda=diag(d);
% max lambdas
[maxlambda,imax]=max(diag(d));
%logmaxlambda=exponential rate of increase (r)
r=log(maxlambda);
% stable age distribution
w=w(:,imax);
% reproductive values
v=real(v(imax,:))';
% sensitivities and elasticities
senmat=(v*w')/(v'*w);
emat=(amax.*senmat)/maxlambda;
%damping ratios
rho=lambda(1)./abs(lambda);
% periods of oscillation
period=2*pi./angle(lambda);
% stable size distribution (scaled to sum to 1)
ssd_inc=w(:,1)/sum(w(:,1));
% ssd classes
ssd_juv = sum(ssd_inc(1:1,1));
ssd_ad = sum(ssd_inc(2:6,1));
% reproductive value (scaled so stage 1 = 1)
reprovalue=real(v(1,:)')/real(v(1,1));
% mean generation time (avg age breeding females)
%gen=log(0.9846)/(log(maxlambda));
% size of matrix
k=size(amax,1);
% pick initial vectors
n=ones(k,1);
%n(1:2,1)=0.2382*N/2; % Guiler 1978
%n(3:6,1)=0.7618*N/4; % Guiler 1978
n=ssd_inc*N;
% age vector
age_vec=zeros(k,1);
for a=1:k-1
age_vec(a+1,1)=a;
end % a loop
age_vec=age_vec+1;
%Calculate Quasi-extinction times
Q=(log(50/sum(n)))/log(maxlambda);
if Q < 0;
Q='infinity';
else Q=Q;
end
% do a simulation
% first specify the initial condition and length of simulation
tlimit=25;
%set population size year step vector
pop_vec=ones(tlimit+1,1);
pop_vec(1,1)=sum(n);
%set year step vector
yr_vec=ones(tlimit+1,1);
for c=1:tlimit
yr_vec(c+1,1)=c;
yr_vec(1,1)=0;
end % c loop
% survival variance vectors
s1_vec = ones(tlimit+1,1);
s1_vec(1,1) = (s1*(1-s1))/n(1,1);
s2_vec = ones(tlimit+1,1);
s2_vec(1,1) = (s2*(1-s2))/n(1,1);
% assume stable-age distribution
N_stable = ssd_inc*N;
%then iterate
for i=1:tlimit;
n=amax*n;
s1_vec(i+1,1)=(s1*(1-s1))/n(1,1);
s2_vec(i+1,1)=(s2*(1-s2))/n(2,1);
pop_vec(i+1,1)=(sum(n));
end
log_pop_vec=log10(pop_vec);
%total population size after 'tlimit' years
pop_st=N
pop_end=sum(n)
%total population size after 'tlimit' years
N
tlimit
pop_end=sum(n)
maxlambda
r
Q
format short
% construct survival vector for display
surv=ones(k,1);
surv(1,1)=s0;
surv(2,1)=s1;
surv(3,1)=s2;
surv(4,1)=s3;
surv(5,1)=s4;
surv(6,1)=s5;
% construct fertility vector for display
fert=ones(k,1);
fert(1,1)=m_imm*x;
fert(2,1)=m_primi*x*rs;
fert(3,1)=m_prime*x*rs;
fert(4,1)=m_prime*x*rs;
fert(5,1)=m_prime*x*rs;
fert(6,1)=m_old&x*rs;
% continue displays
amax
ssd_juv
ssd_ad
emat
%Make density independent plots
subplot(2,2,1), plot(yr_vec,pop_vec);
axis square;
axis([-0.5 tlimit+1 0 (max(pop_vec)+(0.25*(max(pop_vec))))]);
xlabel('year');
ylabel('N females');
subplot(2,2,2), plot(yr_vec,log_pop_vec);
axis square;
axis([-0.5 tlimit+1 0 (max(log_pop_vec)+(0.25*(max(log_pop_vec))))]);
xlabel('year');
ylabel('log N females');
%Make survival and fecundity plots
subplot(2,2,3), plot(age_vec,surv);
axis square;
axis([0.5 k+0.5 0 1]);
xlabel('age in years');
ylabel('survival probability');
subplot(2,2,4), plot(age_vec,fert);
axis square;
axis([0.5 k+0.5 0 2]);
xlabel('age (years)');
ylabel('m (fertility)');
% ----------------------------------------------------------------------------
% deterministic model with negative density feedback in survival (no disease)
% ----------------------------------------------------------------------------
% Re-define new N females
Nnew = 35000;
% set new time limit
tlimit = 200;
% reset amax and adisease to original values
amax=[s0*m_imm*x s0*m_primi*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_old*x*rs
s1 0 0 0 0 0
0 s2 0 0 0 0
0 0 s3 0 0 0
0 0 0 s4 0 0
0 0 0 0 s5 0];
addd=amax;
% pick initial vectors
nddd=ones(k,1);
nddd=ssd_inc*Nnew;
%set year step vector
yr_vecddd=ones(tlimit+1,1);
for c=1:tlimit
yr_vecddd(c+1,1)=c;
yr_vecddd(1,1)=0;
end % c loop
%set population size year step vector
pop_vecddd=ones(tlimit+1,1);
pop_vecddd(1,1)=sum(nddd);
% set dd survival vector
sddd_vec = zeros(tlimit+1,1);
sddd_vec(1,1)=s2;
%then iterate
for iy=1:tlimit;
nddd=addd*nddd;
% add to population vector
pop_vecddd(iy+1,1)=(sum(nddd));
% Set negative density feedback function (f(x)=y0+(acoeff/(1+(x/x0)^bcoeff)))
% parameters with 35000 mid-point
acoeffd=0.1968;
bcoeffd=2.9838;
x0d=70000;
y0d=0.6234;
% Redefine survival probabilities
s2_ddd = y0d+(acoeffd/(1+(sum(nddd)/x0d)^bcoeffd));
s3_ddd = s2_ddd;
s4_ddd = s2_ddd;
s5_ddd = s2_ddd;
% the new matrix
addd=[s0*m_imm*x s0*m_primi*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_old*x*rs
s1 0 0 0 0 0
0 s2_ddd 0 0 0 0
0 0 s3_ddd 0 0 0
0 0 0 s4_ddd 0 0
0 0 0 0 s5_ddd 0];
% add updated survival probability to survival vector
sddd_vec(iy+1,1)=s2_ddd;
end % for iy
% calculate instantaneous growth rates
lpvddd = length(pop_vecddd);
rddd = pop_vecddd(2:lpvddd) ./ pop_vecddd(1:lpvddd-1);
lrddd = log(rddd);
% log-transform population vector
log_pop_vecddd=log10(pop_vecddd);
% number of years to stability
diff_thresh = 10; % define 'stability'
pop_diff = diff(pop_vecddd);
[Iddd,Jddd] = find(abs(pop_diff)<diff_thresh);
clear Jddd;
stab_yrs = Iddd(1) % years to 'stability'
pop_vecddd(tlimit+1) % final population size
sddd_vec(tlimit+1) % final adult survival at stability
% Plot trajectory
subplot(1,3,1), plot(yr_vecddd,pop_vecddd);
axis square;
axis([-0.5 tlimit+1 0 (max(pop_vecddd)+(0.25*(max(pop_vecddd))))]);
xlabel('year');
ylabel('N females');
hold
% create stability line
popmax = (max(pop_vecddd)+(0.25*(max(pop_vecddd))));
plot(stab_yrs,0:popmax,'r-');
hold
subplot(1,3,2), plot(yr_vecddd,sddd_vec);
axis square;
axis([-0.5 tlimit+1 0.5 1]);
xlabel('year');
ylabel('p adult survival');
hold
plot(stab_yrs,0:1,'r:');
hold
subplot(1,3,3), plot(yr_vecddd(1:tlimit),lrddd);
axis square;
axis([-0.5 tlimit+1 min(lrddd)-(0.1*(max(lrddd))) max(lrddd)+0.1*(abs(min(lrddd))) ]);
xlabel('year');
ylabel('r');
% ----------------------------------------
% model with disease-related mortality
% ----------------------------------------
% lower survival with disease
s_ad_disease_lo = 0.10;
s_ad_disease_hi = 0.60;
s_ad_disease_avg = mean([s_ad_disease_lo,s_ad_disease_hi]);
%s_ad_disease_avg = 0.7
sc2=s_ad_disease_avg;
sc3=s_ad_disease_avg;
sc4=s_ad_disease_avg;
sc5=s_ad_disease_avg;
% the disease matrix
adisease=[0 s0*m_imm*x s0*m_prime*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_old*x*rs
s1 0 0 0 0 0
0 sc2 0 0 0 0
0 0 sc3 0 0 0
0 0 0 sc4 0 0
0 0 0 0 sc5 0];
% eigenvalues & eigenvectors
[wc,dc] = eig(adisease);
vc=conj(inv(wc));
%lambda
lambdac=diag(dc);
% max lambdas
[maxlambdac,imax]=max(diag(dc));
%logmaxlambda=exponential rate of increase (r)
rc=log(maxlambdac);
% stable age distribution
wc=wc(:,imax);
% reproductive values
vc=real(vc(imax,:))';
% sensitivities and elasticities
senmatc=(vc*wc')/(vc'*wc);
ematc=(adisease.*senmatc)/maxlambdac;
% stable age distribution
w=w(:,imax);
% reproductive values
v=real(v(imax,:))';
%damping ratios
rho_c=lambda(1)./abs(lambdac);
% periods of oscillation
period_c=2*pi./angle(lambdac);
% stable size distribution (scaled to sum to 1)
ssd_c=wc(:,1)/sum(wc(:,1));
% ssd classes
ssdc_juv = sum(ssd_c(1:1,1));
ssdc_ad = sum(ssd_c(2:6,1));
% reproductive value (scaled so stage 1 = 1)
reprovalue=real(v(1,:)')/real(v(1,1));
% pick initial vectors
nc=ones(k,1);
%nc(1:2,1)=0.2382*N/2; % Guiler 1978
%nc(3:6,1)=0.7618*N/4; % Guiler 1978
nc=ssd_inc*N;
% half of population
N_half = N/2;
%Calculate Quasi-extinction times
Qc_half=(log(N_half/sum(nc)))/log(maxlambdac);
if Qc_half < 0;
Qc_half='infinity';
else Qc_half=Qc_half;
end
% set disease tlimit
tlimitc = 6;
%set year step vector
yr_vecc=ones(tlimitc+1,1);
for c=1:tlimitc
yr_vecc(c+1,1)=c;
yr_vecc(1,1)=0;
end % c loop
%set population size year step vector
pop_vecc=ones(tlimitc+1,1);
pop_vecc(1,1)=sum(nc);
%then iterate
for i=1:tlimitc;
nc=adisease*nc;
pop_vecc(i+1,1)=(sum(nc));
end
log_pop_vecc=log10(pop_vecc);
%total population size after 'tlimit' years
pop_stc=N
pop_endc=sum(nc)
%total population size after 'tlimit' years
N
tlimitc
pop_endc=sum(nc)
maxlambdac
rc
Qc_half
format short
adisease
ematc
nc
ssdc_juv
ssdc_ad
%Make density independent plots
subplot(1,2,1), plot(yr_vecc,pop_vecc);
axis square;
axis([-0.5 tlimitc+1 0 (max(pop_vecc)+(0.25*(max(pop_vecc))))]);
xlabel('year');
ylabel('N females (disease)');
hold
N_half_vec=ones(tlimitc+1,1)*N_half;
subplot(1,2,1), plot(yr_vecc,N_half_vec,'r:');
hold
subplot(1,2,2), plot(yr_vecc,log_pop_vecc);
axis square;
axis([-0.5 tlimitc+1 0 (max(log_pop_vecc)+(0.25*(max(log_pop_vecc))))]);
xlabel('year');
ylabel('log N females (disease)');
% ----------------------------------------------------------------------------------------------------------------------------------
% stochastic model to build disease bouts (Markhov Chain dependency) with neg dens feedback in survival - estimate p_dep & p_disease
% ----------------------------------------------------------------------------------------------------------------------------------
% reset amax and adisease to original values
amax=[s0*m_imm*x s0*m_primi*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_old*x*rs
s1 0 0 0 0 0
0 s2 0 0 0 0
0 0 s3 0 0 0
0 0 0 s4 0 0
0 0 0 0 s5 0];
adisease=[0 s0*m_imm*x s0*m_prime*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_old*x*rs
s1 0 0 0 0 0
0 sc2 0 0 0 0
0 0 sc3 0 0 0
0 0 0 sc4 0 0
0 0 0 0 sc5 0];
adisdd = adisease;
% specify a time limit
tlimit=200;
% specify range of p_disease values
parraypdd = linspace(-1,1,40);
% specify range of p_dep values
parraydd=linspace(0,1,20);
% set storage matrices
all_loglambdasdd = zeros(40,20);
all_extdd = zeros(40,20);
all_minpcdd = zeros(40,20);
% set irpdd loop
for irpdd = 1:length(parraypdd);
% set probability of p_disease
p_disease = parraypdd(irpdd);
% set irdd loop
for irdd = 1:length(parraydd);
% set probability of inter-year disease dependency
p_depdd = parraydd(irdd);
% Number of iterations to do
iter = 1000;
% define matrix to hold population sizes
pop_vecdd_ci = ones(tlimit+1,iter);
log_vecdd_ci = ones(tlimit+1,iter);
% set extinction sum vector
extinct=zeros(iter,1);
% do loop to estimate population size confidence intervals
for idd=1:iter;
% generate random number tlimit vector
random=rand(1,tlimit);
% initial stochastic determination (1=disease; 2=no disease);
ydd=(p_disease<=random);
disdd=ydd+1;
% start Markhov Chain
disdd_new=disdd;
for ip=1:tlimit-1;
if (disdd_new(ip)==1) & (rand(1,1) <= p_depdd)
disdd_new(ip+1) = 1;
else
disdd_new(ip+1) = disdd_new(ip+1);
end
end % for ip
% pick initial vectors
ndd=ones(k,1);
%ndd(1:2,1)=0.2382*N/2; % Guiler 1978
%ndd(3:6,1)=0.7618*N/4; % Guiler 1978
ndd=ssd_inc*N;
%set year step vector
yr_vecdd=ones(tlimit+1,1);
for c=1:tlimit
yr_vecdd(c+1,1)=c;
yr_vecdd(1,1)=0;
end % c loop
%set population size year step vector
pop_vecdd=ones(tlimit+1,1);
pop_vecdd(1,1)=sum(ndd);
% set population extinction indicator vector
ext_vecdd=zeros(tlimit+1,1);
ext_vecdd(1,1)=0;
%then iterate
for iy=1:tlimit;
if (disdd_new(iy)==1)
ndd=adisdd*ndd;
else
ndd=amax*ndd;
end
% add to population vector
pop_vecdd(iy+1,1)=(sum(ndd));
% has the population gone extinct (i.e., n vector sums to < 1)?
ext_thresh = 50; % set extinction threshold
if sum(ndd) < ext_thresh;
ext_vecdd(iy+1,1)=1;
else
ext_vecdd(iy+1,1)=0;
end
% Set negative density feedback function for the 'normal' matrix (f(x)=y0+(acoeff/(1+(x/x0)^bcoeff)))
% parameters with 70000 mid-point
acoeffn=0.1968;
bcoeffn=2.9838;
x0n=70000;
y0n=0.6234;
% Redefine survival probabilities
s2_n = y0n+(acoeffn/(1+(sum(ndd)/x0n)^bcoeffn));
s3_n = s2_n;
s4_n = s2_n;
s5_n = s2_n;
% the new matrix
amax=[s0*m_imm*x s0*m_primi*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_old*x*rs
s1 0 0 0 0 0
0 s2_n 0 0 0 0
0 0 s3_n 0 0 0
0 0 0 s4_n 0 0
0 0 0 0 s5_n 0];
% Set negative density feedback function for the disease matrix (f(x)=y0+(acoeff/(1+(x/x0)^bcoeff)))
acoeffdd=0.5629868;
bcoeffdd=3.00328;
x0dd=35000;
y0dd=0.037572;
% Redefine survival probabilities
sc1_dd = s1;
sc2_dd = y0dd+(acoeffdd/(1+(sum(ndd)/x0dd)^bcoeffdd));
sc3_dd = sc2_dd;
sc4_dd = sc2_dd;
sc5_dd = sc2_dd;
% the new disease matrix
adisdd=[s0*m_imm*x s0*m_primi*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_prime*x*rs s0*m_old*x*rs
sc1_dd 0 0 0 0 0
0 sc2_dd 0 0 0 0
0 0 sc3_dd 0 0 0
0 0 0 sc4_dd 0 0
0 0 0 0 sc5_dd 0];
end % for iy
% log-transform population vector
log_pop_vecdd=log10(pop_vecdd);
% place population size vectors into storage matrix
pop_vecdd_ci(:,idd) = pop_vecdd;
log_vecdd_ci(:,idd) = log_pop_vecdd;
% did the population go extinct during this run?
extinctdd(idd,1) = sum(ext_vecdd);
end % idd loop
% Calculate mean population sizes
mean_popdd = mean(pop_vecdd_ci,2);
mean_log_popdd = mean(log_vecdd_ci,2);
% calculate stochastic r
mean_popdd1=zeros(length(mean_popdd)+1,1);
mean_popdd1(2:length(mean_popdd)+1,1)=mean_popdd;
rdd = mean_popdd(2:length(mean_popdd),1) ./ mean_popdd1(2:length(mean_popdd),1);
% calculate lowest percentage of original population size
pcpop_vecdd_ci = (pop_vecdd_ci/N);
minpcdd = min(pcpop_vecdd_ci,[],1);
mean_minpcdd = (mean(minpcdd))*100;
% Calculate probability of extinction
extdd = zeros(iter,1);
for ie = 1:iter;
if extinctdd(ie,1) > 0;
extdd(ie,1) = 1;
else
extdd(ie,1) = 0;
end
end
pr_extdd = (sum(extdd)) / iter;
% stochastic growth rate
loglambdasdd(irdd) = mean(log(rdd));
% create probability of extinction vector
pr_ext_vecdd(irdd) = pr_extdd;
% create mean minimum per cent of start N vector
mean_minpc_vecdd(irdd) = mean_minpcdd;
irdd
end %irdd loop
% store values in respective rows in storage matrices
all_loglambdasdd(irpdd,:) = loglambdasdd;
all_extdd(irpdd,:) = pr_ext_vecdd;
all_minpcdd(irpdd,:) = mean_minpc_vecdd;
irpdd
end %irpdd loop
% Plot loglambdas as a function of p_dep & p_disease
mesh(all_loglambdasdd);
view(-123,15)
shading interp;
colormap([0 0 0]);
axis tight
set(gca,'XTickLabels',[0.25 0.50 0.75 1.00]);
set(gca,'YTickLabels',[-0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 1.00]);
zlabel('log lambda');
xlabel('p disease occurrence');
ylabel('temporal autocorrelation');
hold
lab = zeros(40,20);
surf(lab);
shading interp;
% Find where loglambdas are ~ 0
nearz_low = -0.0005;
nearz_hi = 0.0005;
[I,J] = find(all_loglambdasdd > nearz_low & all_loglambdasdd < nearz_hi); % finds coordinates
p_disease_rangedd = parraypdd(I)
p_dep_rangedd = parraydd(J)
min_p_disease = min(p_disease_rangedd)
max_p_disease = max(p_disease_rangedd)
min_p_dep = min(p_dep_rangedd)
max_p_dep = max(p_dep_rangedd)
% Plot pr_ext as a function of p_dep & p_disease
mesh(all_extdd);
view(56,21)
shading flat;
colormap([0 0 0]);
axis tight
set(gca,'XTickLabels',[0.25 0.50 0.75 1.00]);
set(gca,'YTickLabels',[-0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 1.00]);
zlabel('p quasi-extinction');
xlabel('p disease occurrence');
ylabel('p temporal autocorrelation');