-
Notifications
You must be signed in to change notification settings - Fork 1
/
bbm_functions_structs.jl
365 lines (253 loc) · 9.54 KB
/
bbm_functions_structs.jl
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
# data type that holds population and parameters
@with_kw mutable struct population
x::Vector{Float64} # trait values
N::Int64 # poulation size
x̄::Float64 # mean trait
σ²::Float64 # phenotypic variance
R::Float64 # innate rate of growth
a::Float64 # strength of abiotic selection
θ::Float64 # abiotic optimum
c::Float64 # strength of competition
μ::Float64 # rate of diffusion (mutation)
V::Float64 # variance in reproductive output
end
@with_kw mutable struct community
S::Int64 # number of species
x::Vector{Vector{Float64}} # trait values
g::Vector{Vector{Float64}} # breeding values
N::Vector{Int64} # population sizes
n::Vector{Int64} # index of rescaled sequence
x̄::Vector{Float64} # mean traits
σ²::Vector{Float64} # phenotypic variances
G::Vector{Float64} # additive genetic variances
R::Vector{Float64} # innate rates of growth
a::Vector{Float64} # strengths of abiotic selection
θ::Vector{Float64} # abiotic optima
c::Vector{Float64} # strengths of competition
w::Vector{Float64} # individual niche widths
U::Vector{Float64} # total niche uses
η::Vector{Float64} # segregation variance
μ::Vector{Float64} # rates of diffusion (mutation)
V::Vector{Float64} # variances in reproductive output
end
# update for community
function comm_update(X)
@unpack S, x, N, x̄, σ², R, a, θ, c, w, U, μ, V = X
# creates array of offspring trait values
# first index is species
# second index is individual
xₚ = fill(zeros(0),S)
for i in 1:S
W = fill(0,N[i])
for j in 1:N[i]
#
# mean fitness of individual j in species i
# this follows exactly from SM §5.6
#
# container for aggregating effects of competition
B = 0.0
# collect effects of competition with other individuals
# within the same population
for k in filter(x -> x≠j, 1:N[i])
B += U[i]^2*exp( (x[i][j] - x[i][k])^2 / (4*w[i]) ) / √(4*π*w[i])
end
# collect effects of competition with other individuals
# in other populations
for k in filter(x -> x≠i, 1:S)
for l in 1:N[k]
B += U[i]*U[k]*exp( (x[i][j] - x[k][l])^2 / (2*(w[i]+w[k])) ) / √(2*π*(w[i]+w[k]))
end
end
w = exp( R[i] - a[i]*(θ[i]-x[i][j])^2/2.0 - c[i]*B )
# parameterizing the NegativeBinomial
q = w/V
s = w^2/(V-w)
# draw random number of offspring
W[j] = rand( NegativeBinomial( s, q ), 1)[1]
end
# total number of offspring
Nₚ = sum(W)
# container for locations of offspring
xₚ = fill(0.0,Nₚ)
# keeps track of which individual is being born
ct = 0
# loop throug parents
for j in 1:N[i]
# birth each offspring
for k in 1:W[j]
# consider next individual
ct += 1
# draw random trait for this individual
xₚ[ct] = rand( Normal( x[i,j], √μ[i] ), 1)[1]
end
end
x̄ₚ[i] = mean(xₚ)
σₚ²[i]= var(xₚ)
end
Xₚ = community(x=xₚ,N=Nₚ,x̄=x̄ₚ,σ²=σₚ²,R=R,a=a,θ=θ,c=c,μ=μ,V=V)
return Xₚ
end
# rescaled update for community
function rescaled_update(X)
@unpack S, x, N, x̄, σ², R, a, θ, c, w, U, μ, V = X
# creates array of offspring trait values
# first index is species
# second index is individual
xₚ = fill(zeros(0),S)
for i in 1:S
W = fill(0,N[i])
for j in 1:N[i]
#
# mean fitness of individual j in species i
# this follows exactly from SM §5.6
#
# container for aggregating effects of competition
B = 0.0
# collect effects of competition with other individuals
# within the same population
for k in filter(x -> x≠j, 1:N[i])
B += U[i]^2*exp( (x[i,j] - x[i,k])^2 / (4*w[i]) ) / √(4*π*w[i])
end
# collect effects of competition with other individuals
# in other populations
for k in filter(x -> x≠i, 1:S)
for l in 1:N[k]
B += U[i]*U[k]*exp( (x[i,j] - x[k,l])^2 / (2*(w[i]+w[k])) ) / √(2*π*(w[i]+w[k]))
end
end
w = exp( R[i] - a[i]*(θ[i]-x[i,j])^2/2.0 - c[i]*B )
# parameterizing the NegativeBinomial
q = w/V
s = w^2/(V-w)
# draw random number of offspring
W[j] = rand( NegativeBinomial( s, q ), 1)[1]
end
# total number of offspring
Nₚ = sum(W)
# container for locations of offspring
xₚ = fill(0.0,Nₚ)
# keeps track of which individual is being born
ct = 0
# loop throug parents
for j in 1:N[i]
# birth each offspring
for k in 1:W[j]
# consider next individual
ct += 1
# draw random trait for this individual
xₚ[ct] = rand( Normal( x[i,j], √μ[i] ), 1)[1]
end
end
x̄ₚ[i] = mean(xₚ)
σₚ²[i]= var(xₚ)
end
Xₚ = community(x=xₚ,N=Nₚ,x̄=x̄ₚ,σ²=σₚ²,R=R,a=a,θ=θ,c=c,μ=μ,V=V)
return Xₚ
end
# update for community using lower bound on fitness
function update_lower(X)
@unpack S, x, N, x̄, σ², R, a, θ, c, w, U, μ, V = X
# creates array of offspring trait values
# first index is species
# second index is individual
xₚ = fill(zeros(0),S)
for i in 1:S
W = fill(0,N[i])
for j in 1:N[i]
#
# mean fitness of individual j in species i
# this follows exactly from SM §5.6
#
# container for aggregating effects of competition
B = 0.0
# collect effects of competition with other individuals
# within the same population
for k in filter(x -> x≠j, 1:N[i])
B += U[i]^2*exp( (x[i,j] - x[i,k])^2 / (4*w[i]) ) / √(4*π*w[i])
end
# collect effects of competition with other individuals
# in other populations
for k in filter(x -> x≠i, 1:S)
for l in 1:N[k]
B += U[i]*U[k]*exp( (x[i,j] - x[k,l])^2 / (2*(w[i]+w[k])) ) / √(2*π*(w[i]+w[k]))
end
end
w = exp( R[i] - a[i]*(θ[i]-x[i,j])^2/2.0 - c[i]*B )
# parameterizing the NegativeBinomial
q = w/V
s = w^2/(V-w)
# draw random number of offspring
W[j] = rand( NegativeBinomial( s, q ), 1)[1]
end
# total number of offspring
Nₚ = sum(W)
# container for locations of offspring
xₚ = fill(0.0,Nₚ)
# keeps track of which individual is being born
ct = 0
# loop throug parents
for j in 1:N[i]
# birth each offspring
for k in 1:W[j]
# consider next individual
ct += 1
# draw random trait for this individual
xₚ[ct] = rand( Normal( x[i,j], √μ[i] ), 1)[1]
end
end
x̄ₚ[i] = mean(xₚ)
σₚ²[i]= var(xₚ)
end
Xₚ = community(x=xₚ,N=Nₚ,x̄=x̄ₚ,σ²=σₚ²,R=R,a=a,θ=θ,c=c,μ=μ,V=V)
return Xₚ
end
# rescaled update for community using lower bound on fitness
function rescaled_lower(X)
@unpack S, x, g, N, n, x̄, σ², G, R, a, θ, c, w, U, η, μ, V = X
#
x̄ₚ = fill(0.0,S)
σₚ²= fill(0.0,S)
Gₚ = fill(0.0,S)
Nₚ = fill(0.0,S)
# creates array of offspring
# breeding and trait values
# first index is species
# second index is individual
gₚ = fill(zeros(0),S)
xₚ = fill(zeros(0),S)
for i in 1:S
W = fill(0,N[i])
for j in 1:N[i]
#
# mean fitness of individual j in species i
# this follows exactly from SM §5.6
#
𝒲 = exp( ( R[i] - (a[i]*(θ[i]-x[i][j])^2/2.0) - c[i]*N[i]/n[i] ) / n[i] )
# parameterizing the NegativeBinomial
q = 𝒲/V[i]
s = 𝒲^2/(V[i]-𝒲)
# draw random number of offspring
W[j] = rand( NegativeBinomial( s, q ), 1)[1]
end
# tracks the current offspring
count = Int64(1)
# loop through parents
for j in 1:N[i]
# birth each offspring
for k in 1:W[j]
# draw random breeding value for this individual
append!( gₚ[i], rand( Normal( g[i][j], √(μ[i]/n[i]) ), 1)[1] )
# draw random trait value for this individual
append!( xₚ[i], rand( Normal( gₚ[i][count], √η[i] ), 1)[1] )
count += 1
end
end
x̄ₚ[i] = mean(xₚ[i])
σₚ²[i]= var(xₚ[i])
Gₚ[i] = var(gₚ[i])
Nₚ[i] = sum(W)
end
Xₚ = community(S=S,x=xₚ,g=gₚ,N=Nₚ,n=n,x̄=x̄ₚ,σ²=σₚ²,G=Gₚ,R=R,
a=a,θ=θ,c=c,w=w,U=U,η=η,μ=μ,V=V)
return Xₚ
end