Wright-Fisher model of evolution
This is one of the simplest models of population genetics that demonstrates the use of sample!
. We implement a simple case of the model where we study haploids (cells with a single set of chromosomes) while for simplicity, focus only on one locus (a specific gene). In this example we will be dealing with a population of constant size.
It is also available from the Models
module as Models.wright_fisher
.
A neutral model
- Imagine a population of
n
haploid individuals. - At each generation,
n
offsprings replace the parents. - Each offspring chooses a parent at random and inherits its genetic material.
using Agents
numagents = 100
100
Let's define an agent. The genetic value of an agent is a number (trait
field).
@agent struct Haploid(NoSpaceAgent)
trait::Float64
end
The model can be run for many generations and we can collect the average trait value of the population. To do this we will use a model-step function (see step!
) that utilizes sample!
:
modelstep_neutral!(model::ABM) = sample!(model, nagents(model))
modelstep_neutral! (generic function with 1 method)
And make a model without any spatial structure:
model = StandardABM(Haploid; model_step! = modelstep_neutral!)
StandardABM with 0 agents of type Haploid
agents container: Dict
space: nothing (no spatial structure)
scheduler: fastest
Create n
random individuals:
for i in 1:numagents
add_agent!(model, rand(abmrng(model)))
end
To create a new generation, we can use the sample!
function. It chooses random individuals with replacement from the current individuals and updates the model. For example:
sample!(model, nagents(model))
StandardABM with 100 agents of type Haploid
agents container: Dict
space: nothing (no spatial structure)
scheduler: fastest
We can now run the model and collect data. We use dummystep
for the agent-step function (as the agents perform no actions).
using Statistics: mean
data, _ = run!(model, 20; adata = [(:trait, mean)])
data
Row | time | mean_trait |
---|---|---|
Int64 | Float64 | |
1 | 0 | 0.42726 |
2 | 1 | 0.497929 |
3 | 2 | 0.513011 |
4 | 3 | 0.571239 |
5 | 4 | 0.583501 |
6 | 5 | 0.624367 |
7 | 6 | 0.679946 |
8 | 7 | 0.724317 |
9 | 8 | 0.725342 |
10 | 9 | 0.689457 |
11 | 10 | 0.705233 |
12 | 11 | 0.704372 |
13 | 12 | 0.697773 |
14 | 13 | 0.725385 |
15 | 14 | 0.72586 |
16 | 15 | 0.731266 |
17 | 16 | 0.765105 |
18 | 17 | 0.744987 |
19 | 18 | 0.744663 |
20 | 19 | 0.74374 |
21 | 20 | 0.753556 |
As expected, the average value of the "trait" remains around 0.5.
A model with selection
We can sample individuals according to their trait values, supposing that their fitness is correlated with their trait values.
modelstep_selection!(model::ABM) = sample!(model, nagents(model), :trait)
model = StandardABM(Haploid; model_step! = modelstep_selection!)
for i in 1:numagents
add_agent!(model, rand(abmrng(model)))
end
data, _ = run!(model, 20; adata = [(:trait, mean)])
data
Row | time | mean_trait |
---|---|---|
Int64 | Float64 | |
1 | 0 | 0.505639 |
2 | 1 | 0.655232 |
3 | 2 | 0.737398 |
4 | 3 | 0.771379 |
5 | 4 | 0.785315 |
6 | 5 | 0.818285 |
7 | 6 | 0.83646 |
8 | 7 | 0.860588 |
9 | 8 | 0.86836 |
10 | 9 | 0.889281 |
11 | 10 | 0.907066 |
12 | 11 | 0.90858 |
13 | 12 | 0.908554 |
14 | 13 | 0.920063 |
15 | 14 | 0.930113 |
16 | 15 | 0.927999 |
17 | 16 | 0.936381 |
18 | 17 | 0.943464 |
19 | 18 | 0.945925 |
20 | 19 | 0.942305 |
21 | 20 | 0.940535 |
Here we see that as time progresses, the trait becomes closer and closer to 1, which is expected - since agents with higher traits have higher probability of being sampled for the next "generation".