Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Supplement to Crowley et al.

(2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.

Supplementary Material: Matlab code.

Here are the two computer programs ThresherLL and ThresherPP and the two functions BetaVec

and FDraw used to produce the figures. Specifically, ThresherLL, BetaVec, and FDraw produced

Figure 3, and ThresherPP and BetaVec produced Figures 4-6, B-1, and C-1.

The code is thoroughly commented. To create the different figures and panels, parameter

magnitudes are changed in the early lines of code and (for ThresherPP) further down in the block

that establishes the parameters being varied to create the x-axes. Note that in this block, all but

the relevant limits and parameter variation are removed during an individual run. In the Output

block at the end of the programs, all but the relevant axes and plots are also removed.

These programs are also available through GitHub

(https://github.com/OrrSpiegel/Crowley-et-al-Amer-Natur-2019-Matlab-Codes).

%THRESHERLL finds the vector of threshhold patch fitnesses that are


%acceptance criteria during natal dispersal and runs simulations. It then
%alters one or more parameters in response to HIREC and runs simulations
%using the pre-HIREC thresholds. Finally, it finds new thresholds for the
%new post-HIREC parameters and runs more simulations.

tic
Fx = 100; %100; Top end of the patch fitness beta distribution, fitness
a = 4; %4; Alpha parameter of the beta distribution, dimensionless
b = 4; %4; Beta parameter of the beta distribution, dimensionless
n = 30; %30; Number of time steps in a season, dimensionless
h = 0.5; %0.5; Fraction of time steps in which a patch is found, d'less
s = 0.9;%0.98; Chance of surviving a time step, dimensionless
del = 3; %1; Deferred cost of searching per time step, fitness
c = 1; %1; Deferred cost exponent to express non-linearity

FxH = 75; %Fx; Top end of the patch fitness beta distribution, fitness
aH = a; %a; Alpha parameter of the beta distribution, dimensionless
bH = b; %b; Beta parameter of the beta distribution, dimensionless
nH = n; %n; Number of time steps in a season, dimensionless
hH = 0.25; %h; Fraction of time steps when a patch is found, d'less
sH = s; %s; Chance of surviving a time step, dimensionless
delH= del; %del; Deferred cost of searching per time step, fitness
cH = c; %c; Deferred cost exponent to express non-linearity

1
Supplement to Crowley et al. (2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.

res = 1000; %1000; Resolution of the continuous graphs and functions


m = 1000; %1000; Number of individuals simulated
seePre = 1; %1; Show simulated individual results pre-HIREC, 0=no, 1=yes
seeMix = 1; %1; Show simulated individual results pre/post mix, 0=no, 1=yes
seePost= 1; %1; Show simulated individual results post-HIREC, 0=no, 1=yes

ETS = zeros(1,3); %Expected time to settle or die for each case

%Calculate functions for pre-HIREC parameters


[p q beta] = BetaVec(Fx,a,b,res); %p is res res'n p vector for Feta
%q is res res'n psi vec for FetaP
I = zeros(1,n); %Vector of threshold psi values
F = zeros(1,n); %Vector of threshold patch fitness values
P = zeros(1,n); %Vector of p* values
for i = n:-1:1 %Track backwards through the time steps
prod = 1; %This accumulates a multiplier term
for j = i:n-1 %This sums terms for each i
if j > i %The multiplier constraint
prod = prod*(1-h*P(j));
end
step = s^(j-i+1)*h*P(j+1)*(I(j+1)-((j+1)^c - i^c)*del)*prod;
if step < 0
step = 0;
end
F(i) = F(i) + step;
end
frac = F(i)/Fx; %F is fraction frac of interval
if frac > 1
frac = 1;
end
ii = ceil(res*frac); %Index of 1/res piece containing F(j)
if ii == 0 %Avoid having the index go to zero
ii = 1;
end
jj = res*frac - floor(res*frac); %Piece to subtract off
P(i) = p(ii) - (beta(ii)/res)*jj; %P is p(ii) minus the piece
I(i) = q(ii) - (beta(ii)/res)*((ii-0.5)/res)*Fx*jj; %Same
end
prod = 1; %This block calculates fitness for the entire 1 to n sequence
F00 = 0; %This variable sums fitness
for j = 0:n-1
if j > 0
prod = prod*(1-h*P(j));
end
step = s^(j+1)*h*P(j+1)*(I(j+1)-((j+1)^c)*del)*prod;
if step < 0
step = 0;
end
F00 = F00 + step;
end
%ETS (expec tm to settle) & ESN (expec surv tm if not settled): F & HIREC
ETS(1) = 1;
prod = 1; %ETS accumlates time surviving but not settling
for kk = 2:nH
prod = prod*(1-h*P(kk-1)); %Chance of not settling steps 1 -> kk-1

2
Supplement to Crowley et al. (2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.
ETS(1) = ETS(1) + s^kk*prod; %Chance surv kk steps+didn't settle kk-1
end

%Simulate with pre-HIREC parameters


Sim = zeros(m,2); %Matrix to record simulation results
Cumbeta = zeros(1,res); %The cumulative beta function
Cumbeta(1) = beta(1)/res; %Each slice has height beta(i) and width 1/res
for i = 2:res
Cumbeta(i) = Cumbeta(i-1) + beta(i)/res; %Accumulate them
end
for mm = 1:m %m is the total number of individuals simulated
flag = 1; %Signals that threshold not yet surpassed
i = 1; %Initialize the time step
while flag&&(i<=n)
mort = rand;
if mort > s %Individual dies
Sim(mm,1) = 0; %Fitness set to 0
Sim(mm,2) = i; %Time set to the time step number
flag = 0; %Signal that individual has finished
else %Individual lives and seeks patch
patch = rand; %Random check for habitat
if patch < h %patch is found
FF = FDraw(Cumbeta,Fx,res); %Draw patch fitness
if FF >= F(i) %Fitness >= threshhold
Sim(mm,1) = FF-(i^c)*del; %Patch fitness - delay cost
Sim(mm,2) = i; %Completion time is step #
flag = 0; %Signal completion
end
end
end
i = i+1; %Increment steps
end
if (i > n)&&flag %If individual doesn't settle within n steps
Sim(mm,1) = 0; %Its fitness is zero
Sim(mm,2) = n; %Its time is set to n
end
end
FitmeanAndStepmean = mean(Sim,1); %MATLAB takes means of fitness & time
EmFitMean = FitmeanAndStepmean(1,1) %Simulation mean fitness
EmStopMean = FitmeanAndStepmean(1,2) %Simulation mean time to settle or die

%Calculate functions for the adapted post-HIREC world*******************


[pH qH betaH] = BetaVec(FxH,aH,bH,res);%p=p vector @ res res'n for Feta
%q= psi vec @ same res for FetaP
FH = zeros(1,nH); %Vector of threshold patch fitness values
PH = zeros(1,nH); %Vector of p* values
IH = zeros(1,nH); %Vector of psi values
for i = nH:-1:1 %Track backwards through the time steps
prod = 1; %This accumulates a multiplier term
for j = i:nH-1 %This sums terms for each i
if j > i %The multiplier constraint
prod = prod*(1-hH*PH(j));
end
step = sH^(j-i+1)*hH*PH(j+1)*(IH(j+1)-((j+1)^cH - i^cH)*delH)*prod;
if step < 0
step = 0;

3
Supplement to Crowley et al. (2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.
end
FH(i) = FH(i) + step;
end
frac = FH(i)/FxH; %F is fraction frac of interval
if frac > 1
frac = 1;
end
ii = ceil(res*frac); %Index of 1/res piece containing F(j)
if ii == 0 %Avoid having the index go to zero
ii = 1;
end
jj = res*frac - floor(res*frac); %Piece to subtract off
PH(i) = pH(ii) - (betaH(ii)/res)*jj; %P is p(ii) minus the piece
IH(i) = qH(ii) - (betaH(ii)/res)*((ii-0.5)/res)*FxH*jj; %Same
end
prod = 1; %This block calculates fitness for the entire 1 to n sequence
F00H = 0; %This variable sums fitness
for j = 0:nH-1
if j > 0
prod = prod*(1-hH*PH(j));
end
step = sH^(j+1)*hH*PH(j+1)*(IH(j+1)-((j+1)^cH)*delH)*prod;
if step < 0
step = 0;
end
F00H = F00H + step;
end
%ETS (expec tm to settle) & ESN (expec surv tm if not settled): FH & HIREC
ETS(3) = 1;
prod = 1; %ETS accumlates time surviving but not settling
for kk = 2:nH
prod = prod*(1-hH*PH(kk-1)); %Chance of not settling steps 1 -> kk-1
ETS(3) = ETS(3) + sH^kk*prod; %Chance surv kk steps+didn't settle kk-1
end

%The pre-HIREC and post-HIREC fitness distributions


bet = zeros(1,res); %Intermediate step in the beta distribution calc'n
betH = zeros(1,res);
beta = zeros(1,res); %Beta distribution
betaH= zeros(1,res);
x = 0.5/res:1/res:(res-0.5)/res; %Increments along (0,1) for beta
xx = x.*Fx; %x-axis transformed for fitnesses in [0,Fx]
yy = x.*FxH; %for fitnesses in [0,FxH]
bet = (x.^(a-1)).*((1-x).^(b-1)); %Unnormalized beta function
betH = (x.^(aH-1)).*((1-x).^(bH-1));
sum = 0; sumH = 0;
for i = 1:res
sum = sum + bet(i)./res; %Adding all unnormalized values
sumH = sumH + betH(i)./res;
end
beta = bet./sum; %Normalizing so that the area = 1
betaH = betH./sum;
Feta = beta./Fx; %Adjusting the distribution for [0,Fx]
FetaH = betaH./FxH;
maxF = max(Feta);
maxFH = max(FetaH);
if maxF > maxFH

4
Supplement to Crowley et al. (2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.
maxx = maxF;
else
maxx = maxFH;
end
figure
hold on
plot(xx,Feta,'k') %Plot the fitness distribution
plot(yy,FetaH,'r')
xlabel('Patch fitness')
ylabel('Frequency density of patch fitnesses')
axis([0 Fx 0 maxx])
hold off

%Calculate functions with pre-HIREC thresholds but post-HIREC parameters


PX = zeros(1,nH); %p values for pre-H thresh but post-H pms
IX = zeros(1,nH); %I values for pre-H thresh but post-H pms
for i = 1:nH; %Begin FbarX calculation
frac = F(i)/FxH; %Get F as frac of FxH-F0H
if frac > 1
frac = 1;
end
ii = ceil(res*frac); %Calculate fine adjustments
if ii == 0
ii = 1;
end
jj = res*frac-floor(res*frac);
PX(i) = pH(ii)-(betaH(ii)/res)*jj; %Calc PX & IX via F & post-H params
IX(i) = qH(ii)-(betaH(ii)/res)*((ii-0.5)/res)*FxH*jj;
end

prod = 1; %This block calculates fitness for the entire 1 to n sequence


F00X = 0; %This variable sums fitness
for j = 0:nH-1
if j > 0
prod = prod*(1-hH*PX(j));
end
step = sH^(j+1)*hH*PX(j+1)*(IX(j+1)-((j+1)^cH)*delH)*prod;
if step < 0
step = 0;
end
F00X = F00X + step;
end
%ETS (expec tm to settle) & ESN (expec surv tm if not settled): FX & HIREC
ETS(2) = 1;
prod = 1; %ETS accumlates time surviving but not settling
for kk = 2:nH
prod = prod*(1-hH*PX(kk-1)); %Chance of not settling steps 1 -> kk-1
ETS(2) = ETS(2) + sH^kk*prod; %Chance surv kk steps+didn't settle kk-1
end

%Simulation pre/post follows


SimX = zeros(m,2); %Matrix to record simulation results pre/post
Cumbeta = zeros(1,res); %The cumulative beta function for HIREC
Cumbeta(1) = betaH(1)/res; %Each slice has height beta(i) and width 1/res
for i = 2:res
Cumbeta(i) = Cumbeta(i-1) + betaH(i)/res; %Accumulate them

5
Supplement to Crowley et al. (2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.
end
for mm = 1:m %m is the total number of individuals simulated
flag = 1; %Signals that threshold not yet surpassed
i = 1; %Initialize the time step
while flag&&(i<=nH)
mort = rand;
if mort > sH %Individual dies
SimX(mm,1) = 0; %Fitness set to 0
SimX(mm,2) = i; %Time set to the time step number
flag = 0; %Signal that individual has finished
else %Individual lives and seeks patch
patch = rand; %Random check for habitat
if patch < hH %patch is found
FF = FDraw(Cumbeta,FxH,res); %Draw patch fitness
if FF >= F(i) %Fitness >= threshhold
SimX(mm,1) = FF-(i^cH)*delH; %Patch fitness - delay cst
SimX(mm,2) = i; %Completion time is step #
flag = 0; %Signal completion
end
end
end
i = i+1; %Increment steps
end
if (i > nH)&&flag %If individual doesn't settle within n steps
SimX(mm,1) = 0; %Its fitness is zero
SimX(mm,2) = nH; %Its time is set to n
end
end
FitmeanAndStepmean = mean(SimX,1); %MATLAB takes means of fitness & time
EmFitMeanX = FitmeanAndStepmean(1,1) %Simulation mean fitness
EmStopMeanX = FitmeanAndStepmean(1,2) %Simulat'n mean time to settle or die

%Simulate with post-HIREC parameters and thresholds


SimH = zeros(m,2); %Matrix to record simulation results
for mm = 1:m %m is the total number of individuals simulated
flag = 1; %Signals that threshold not yet surpassed
i = 1; %Initialize the time step
while flag&&(i<=nH)
mort = rand;
if mort > sH %Individual dies
SimH(mm,1) = 0; %Fitness set to 0
SimH(mm,2) = i; %Time set to the time step number
flag = 0; %Signal that individual has finished
else %Individual lives and seeks patch
patch = rand; %Random check for habitat
if patch < hH %patch is found
FFH = FDraw(Cumbeta,FxH,res); %Draw patch fitness
if FFH >= FH(i) %Fitness >= threshhold
SimH(mm,1) = FFH-(i^cH)*delH; %Patch fit's - delay cost
SimH(mm,2) = i; %Completion time is step #
flag = 0; %Signal completion
end
end
end
i = i+1; %Increment steps
end
if (i > nH)&&flag %If individual doesn't settle within n steps

6
Supplement to Crowley et al. (2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.
SimH(mm,1) = 0; %Its fitness is zero
SimH(mm,2) = n; %Its time is set to n
end
end
FitmeanAndStepmean = mean(SimH,1); %MATLAB takes means of fitness & time
EmFitMeanH = FitmeanAndStepmean(1,1) %Simulation mean fitness
EmStopMeanH = FitmeanAndStepmean(1,2) %Simulat'n mean time to settle or die

if nH > n %Determine the x-axis length as max of Fx and FxH


nmx = nH;
else
nmx = n;
end
if FxH > Fx
Fxmx = FxH;
else
Fxmx = Fx;
end

%Output plot
figure %red=adapted pre-HIREC, black=mix, blue=adapted post-HIREC
hold on
xx = 1:n; %x axis for the threshold function F vs time step
xxx = 1:nH; %x axis for the threshold functioins FX and FH
%plot([1 nH],[FxH FxH],'b:')
%plot([1 n],[Fx Fx],'r:') %Upper and lower limits of patch fitnesses
plot([1 n],[F00 F00],'k') %The theoretical fitness mean pre-HIREC
plot([1 nH],[F00X F00X],'r') %Theoretical fitness mean pre/post mix
plot([1 nH],[F00H F00H],'c') %The theoretical fitness mean post-HIREC
plot(xx,F,'kx') %pre-HIREC thresholds
plot(xxx,FH,'cx')%post-HIREC thresholds
%plot([1 n],[EmFitMean EmFitMean],'k--') %Sim fit mean pre-HIREC
%plot([1 nH],[EmFitMeanX EmFitMeanX],'r--') %Simulation mixed
%plot([1 nH],[EmFitMeanH EmFitMeanH],'c--') %Simulation post-HIREC
%plot([1 nH],[EmFitMeanL EmFitMeanL],'b--') %Simulation learning
for mm = 1:m
if seePre
plot(Sim(mm,2),Sim(mm,1),'k.','MarkerSize',7) %Simulation pre-
HIREC
end
if seeMix
plot(SimX(mm,2),SimX(mm,1),'r.','MarkerSize',7) %Simulation mix
end
if seePost
plot(SimH(mm,2),SimH(mm,1),'c.','MarkerSize',7) %Simulation post-
HIREC
end
end
xlabel('Time step')
ylabel('Fitness')
axis([1 nmx 0 Fxmx])
hold off
toc

------------------------------------------------------------------------------------------------------------------

7
Supplement to Crowley et al. (2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.

%THRESHERPP addresses a parameter shift from the pre-HIREC condition to


%magnitudes changing along a gradient of possible HIREC parameter shifts.
%It plots the theoretical fitnesses pre-HIREC (black), post-HIREC shift but
%unadapted to the new conditions (red), and post-HIREC with adaptation
%(cyan).

Fx = 100; %100; Top end of the patch fitness beta distribution, fitness
a = 4; %4; Alpha parameter of the beta distribution, dimensionless
b = 4; %4; Beta parameter of the beta distribution, dimensionless
n = 30; %30; Number of time steps in a season, dimensionless
h = 0.5; %0.5; Fraction of time steps when a patch is found, d'less
s = 0.98; %0.98; Chance of surviving a time step, dimensionless
del = 1; %1; Deferred cost of searching per time step, fitness
c = 1; %1; Deferred cost exponent to express non-linearity
res = 1000; %1000; Resolution of the continuous graphs and functionsp

A1 = a/(a+b); %Mean of the beta distribution on [0 1]

FxH = zeros(1,res); %Post-HIREC upper limits of patch quality distribution


aH = zeros(1,res); %Post-HIREC alpha values for the beta distribution
bH = zeros(1,res); %Post-HIREC beta values for the beta distribution
nH = zeros(1,res); %Post-HIREC season length
hH = zeros(1,res); %Post-HIREC chance of finding a patch in a step
sH = zeros(1,res); %Post-HIREC chance of surviving a step
delH= zeros(1,res); %Post-HIREC deferred cost per step
cH = zeros(1,res); %Post-HIREC deferred cost exponent
F1 = zeros(1,res); %Pre-HIREC fitnesses
FX1 = zeros(1,res); %Immediately post-HIREC fitnesses
FH1 = zeros(1,res); %Re-adapted post-HIREC fitnesses
FH2 = zeros(1,res); %This is after-HIREC thresholds

ETS = zeros(3,res); %Expected time to settle, given survival


ESN = zeros(3,res); %Expected survival time, no settlement

%Code that deals with the adapted pre-HIREC world***********************

[p q beta] = BetaVec(Fx,a,b,res); %p is res res'n p vector for Feta


%q is res res'n psi vec for FetaP
I = zeros(1,n); %Vector of threshold psi values
F = zeros(1,n); %Vector of threshold patch fitness values
P = zeros(1,n); %Vector of p* values

for i = n:-1:1
prod = 1; %This accumulates a multiplier term
F(i) = 0; %Initialize the fractional fitness x-axis in beta
for j = i:n-1 %This sums terms for each i
if j > i %The multiplier constraint
prod = prod*(1-h*P(j)); %Chance of not settling in step j
end
step = s^(j-i+1)*h*P(j+1)*(I(j+1)-((j+1)^c - i^c)*del)*prod;
if step < 0 %Step is expected fitness contribution from step j+1
step = 0; %Checks to prevent step from going negative
end

8
Supplement to Crowley et al. (2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.
F(i) = F(i) + step; %Add step to accumulate fitness across steps
end
frac = F(i)/Fx; %F is fraction frac of interval
if frac > 1
frac = 1;
end
ii = ceil(res*frac); %Index of 1/res piece containing F(j)
if ii == 0 %Avoid having the index go to zero
ii = 1;
end
jj = res*frac - floor(res*frac); %Piece to subtract off
P(i) = p(ii) - (beta(ii)/res)*jj; %P is p(ii) minus the piece
I(i) = q(ii) - (beta(ii)/res)*((ii-0.5)/res)*Fx*jj; %Same
end

prod = 1; %This block calculates fitness for then entire 1 to n seq'ce


F00 = 0;
for j = 0:n-1
if j > 0
prod = prod*(1-h*P(j));
end
step = s^(j+1)*h*P(j+1)*(I(j+1)-((j+1)^c)*del)*prod;
if step < 0
step = 0;
end
F00 = F00 + step;
end

ETSx = 1; prod = 1; %ETSx accumlates time surviving but not settling


for kk = 2:n
prod = prod * (1-h*P(kk-1)); %Chance of not settling steps 1 -> kk-1
ETSx = ETSx + s^kk*prod; %Chance surv kk steps+didn't settle kk-1
end
ETS(1,1:res) = ETSx;

for k = 1:res %for loop over res parameter magnitudes


FxH(k) = Fx; %Fx; Top end of the patch fitness beta distribution, fit
aH(k) = a; %a; Alpha parameter of the beta distribution, d'less
bH(k) = b; %b; Beta parameter of the beta distribution, d'less
nH(k) = n; %nH; Number of time steps in a season, d'less
hH(k) = h; %h; Fraction of time steps when a patch is found, d'less
sH(k) = s; %s; Chance of surviving a time step, dimensionless
delH(k)= del; %del; Deferred cost of searching per time step, fitness
cH(k) = c; %c; Deferred cost exponent to express non-linearity

%**********HERE are the parameters being varied


%All lines for param 2 should be blocked with % unless param 2 is used!

%pmin1 = 0; %This is the minimum value of parameter 1


%pmax1 = 0.98; %This is the maximum value of parameter 1
%delH(k) = pmax4 + (pmin4-pmax4)*(k-0.5)/res;
%delH(k) = pmin1 + (pmax1-pmin1)*(k-0.5)/res; %Parameter 1 is left of =
pmin2 = 0; %This is the minimum value of parameter 2
pmax2 = 1; %This is the maximum value of parameter 2
hH(k) = pmin2 + (pmax2-pmin2)*(k-0.5)/res; %Parameter 1 is left of =
%sH(k) = pmin1 + (pmax1-pmin1)*(k-0.5)/res; %Parameter 1 is left of =

9
Supplement to Crowley et al. (2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.
%cH(k) = pmin1 + (pmax1-pmin1)*(k-0.5)/res; %Parameter 1 is left of =
%***BUT if nH is being varied, need pmax1=n & use the following nH(k) eqn:
%pmin1 = 5;
%pmax1 = 30;
%nH(k) = pmin1 + round((pmax1-pmin1)*(k-0.5)/res);
%Lines below are when a second parameter also varies
%***Varying nH backwards
%pmin2 = 5;
%pmax2 = 25;
%nH(k) = pmax2 + round((pmin2-pmax2)*(k-0.5)/res);

%sH(k) = pmin2 + (pmax2-pmin2)*(k-0.5)/res; %Parameter 2 is left of =


pmin3 = 50;
pmax3 = 150;
FxH(k) = pmin3 + (pmax3-pmin3)*(k-0.5)/res;

%***BUT use the next line instead if parameters 1 and 2 are negatively corr
%sH(k) = pmax2 + (pmin2-pmax2)*(k-0.5)/res; %Parameter 2 is left of =

%Code that deals with the adapted post-HIREC world***********************

[pH qH betaH] = BetaVec(FxH(k),aH(k),bH(k),res);


%p is res res'n p vec for Feta; q is res res'n psi vec for Feta
PH = zeros(1,nH(k)); %Vector of threshold p values
IH = zeros(1,nH(k)); %Vector of threshold psi values
FH = zeros(1,nH(k)); %Vector of thresh patch fitness values post-HIREC

for i = nH(k):-1:1 %Track backwards through the time steps


prod = 1; %This accumulates a multiplier term
for j = i:nH(k)-1 %This sums terms for each i
if j > i %The multiplier constraint
prod = prod*(1-hH(k)*PH(j));
end
step = sH(k)^(j-i+1)*hH(k)*PH(j+1)*(IH(j+1)...
-((j+1)^cH(k) - i^cH(k))*delH(k))*prod;
if step < 0
step = 0;
end
FH(i) = FH(i) + step; %Accumulates fitness from each forward step
end
frac = FH(i)/FxH(k); %F is fraction fraction of patch qual range
if frac > 1
frac = 1;
end
ii = ceil(res*frac); %Index of 1/res piece containing F(j)
if ii == 0 %Avoid having the index go to zero
ii = 1;
end
jj = res*frac - floor(res*frac); %Piece to subt off (interpolation)
PH(i) = pH(ii) - (betaH(ii)/res)*jj; %P is p(ii) minus the piece
IH(i) = qH(ii) - (betaH(ii)/res)*((ii-0.5)/res)*FxH(k)*jj;
end

prod = 1; %This block calculates fitness for then entire 1 to n sequence


F00H = 0;
for j = 0:nH(k)-1

10
Supplement to Crowley et al. (2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.
if j > 0
prod = prod*(1-hH(k)*PH(j));
end
step = sH(k)^(j+1)*hH(k)*PH(j+1)*(IH(j+1)...
-((j+1)^cH(k))*delH(k))*prod;
if step < 0
step = 0;
end
F00H = F00H + step;
end
FH2(k) = F00H;

ETS(3,k) = 1; prod = 1;
for kk = 2:nH(k)
prod = prod * (1-hH(k)*PH(kk-1));
ETS(3,k) = ETS(3,k) + sH(k)^kk*prod;
end

%Calculate with pre-HIREC thresholds but post-HIREC parameters & calc FbarX

PX = zeros(1,nH(k)); %p values for pre-H thresh but post-H pms


IX = zeros(1,nH(k)); %I values for pre-H thresh but post-H pms
for i = 1:nH(k); %Begin FbarX calculation
frac = F(i)/FxH(k); %Get F as frac of FxH-F0H
if frac > 1
frac = 1;
end
ii = ceil(res*frac); %Calculate fine adjustments
if ii == 0
ii = 1;
end
jj = res*frac-floor(res*frac);
PX(i) = pH(ii)-(betaH(ii)/res)*jj; %Calc PX & IX via F & post-H params
IX(i) = qH(ii)-(betaH(ii)/res)*((ii-0.5)/res)*FxH(k)*jj;
end

prod = 1; %This block calculates fitness for then entire 1 to n sequence


F00X = 0;
for j = 0:nH(k)-1
if j > 0
prod = prod*(1-hH(k)*PX(j));
end
step = sH(k)^(j+1)*hH(k)*PX(j+1)*(IX(j+1)...
-((j+1)^cH(k))*delH(k))*prod;
if step < 0
step = 0;
end
F00X = F00X + step;
end

ETS(2,k) = 1; prod = 1;
for kk = 2:nH(k)
prod = prod * (1-hH(k)*PX(kk-1));
ETS(2,k) = ETS(2,k) + sH(k)^kk*prod;
end

11
Supplement to Crowley et al. (2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.

F1(k) = F00; %Record the four fitnesses for each k


FX1(k) = F00X;
FH1(k) = F00H;

end %for loop over res values of the parameter

FHmax = max(FH);
if Fx > FHmax %Determine the x-axis length as max of Fx and FxH
bar = Fx;
else
bar = FHmax;
end

%Output plot
x = 0.5/res:1/res:(res-0.5)/res; %h & s on (0,1)
figure %red=adapted pre-HIREC, blk=mix, blue=adapted post-HIREC
hold on
plot(x,F1,'k') %pre-HIREC theoretical fitness
plot(x,FX1,'r') %mixed-HIREC theoretical fitness
plot(x,FH1,'c') %post-HIREC theoretical fitness
xlabel('Chance of patch-finding/step; max patch quality 50-150')
ylabel('Fitness')

axis([0 1 0 bar])
hold off

figure %Expected time to settle_ and to survive without settling--


hold on
axis([0 1 0 n])
xlabel('Chance of patch-finding/step; max patch quality 50-150')
ylabel('Time spent choosing')% (-) or or surviving without choosing (--)')
plot(x,ETS(1,1:res),'k')
plot(x,ETS(2,1:res),'r')
plot(x,ETS(3,1:res),'c')
hold off

----------------------------------------------------------------------------

function [p q beta] = BetaVec(Fx,a,b,res)


%BETAVEC takes in fitness limit Fx and the beta parameters a and b
%and res (the number of indices in the vectors)
%and finds the vectors p (the area under the distribution) and q
%(the expected F value) for all starting indices corresponding to F values
%from 0 to Fx.

p = zeros(1,res); %The p vector


q = zeros(1,res); %The psi vector
x = 0.5/res:1/res:(res-0.5)/res; %Spacing gets midpoints of 1/res segments
bet = (x.^(a-1)).*((1-x).^(b-1));%The unnormalized beta vector
summer = 0;
for i = 1:res %Add the areas
summer = summer + bet(i)/res;
end

12
Supplement to Crowley et al. (2019), "Habitat choice after environment change," The American Naturalist

Supplemental Material for: Philip H. Crowley, Pete C. Trimmer, Orr Spiegel, Sean M. Ehlman, William S. Cuello, Andrew Sih. 2019. "Predicting Habitat Choice after Rapid
Environmental Change." The American Naturalist 193(5). DOI: 10.1086/702590.
beta = bet/summer; %Normalize bet to get the beta PDF
better = 0; batter = 0;
for i = res:-1:1 %Sum backwards from the end
better = better + beta(i)/res; %Find p for each i
batter = batter + (beta(i)/res)*(i-0.5)/res; %Find psi for each i
p(i) = better; %p is based on [0 1] xaxis
q(i) = (batter/p(i))*Fx; %q based on [0 Fx] xaxis
end

----------------------------------------------------------------------------

function [ FF ] = FDraw( Cumbeta,Fx,res )


%FDRAW draws a random fitness value from the beta distribution of fitnesses

frac = rand;
i = 1; %Initialize accumulator
while (Cumbeta(i)<frac)&&(i<res)
i = i + 1;
end
if i == 1
Cmin = 0;
else
Cmin = Cumbeta(i-1);
end
ffrac = i/res - (1/res)*(Cumbeta(i)-frac)/(Cumbeta(i)-Cmin);
FF = ffrac*Fx;

13

You might also like