Moran process
The Moran process, a population of constant size.
- Class:
MoranProcessSystem - Import:
from skcomplex.systems.biology.evolution import MoranProcessSystem - Field:
biology/evolution - Topics:
population genetics,agents,fixation,genetic drift,mutation - References:
- Moran 1958
- Nowak 2006
- Jensen 2023
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.biology.evolution import MoranProcessSystem
trajectory = MoranProcessSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
size |
50 |
mutants |
1 |
selection |
0.0 |
mutation |
0.0 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | discrete |
| Space | none |
| Laws | update |
| Randomness | stochastic |
| Domains | integer, real |
| Structure | variable membership |
Class
The source of the class shows how it builds its components from its model parameters.
class MoranProcessSystem(BaseSystem):
"""The Moran process, a population of constant size.
Fitter traits fix by selection and drift.
The population starts with `mutants` individuals of trait 1 among individuals of trait 0. Without mutation one trait
eventually fixes, the mutant with probability `(1 - 1 / r^m) / (1 - 1 / r^N)` for the relative fitness `r = 1 +
selection`. With mutation the traits diffuse along the trait axis, in clusters rather than as a spreading Gaussian,
which distinguishes evolutionary diffusion from a random walk. The system has the group `population`.
Args:
size:
The number of individuals.
mutants:
The initial number of individuals of trait 1.
selection:
The selective advantage of one unit of the trait.
mutation:
The probability that an offspring's trait mutates by one unit.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for the one the system chooses.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Examples:
>>> from skcomplex.systems.biology.evolution import MoranProcessSystem
>>> float(MoranProcessSystem(10, mutants=3).prepare().system_.elements[0].trait.sum())
3.0
"""
field = 'biology/evolution'
topics = ('population genetics', 'agents', 'fixation', 'genetic drift', 'mutation')
references = ('Moran 1958', 'Nowak 2006', 'Jensen 2023')
until = 100
def __init__(
self,
size: int = 50,
*,
mutants: int = 1,
selection: float = 0.0,
mutation: float = 0.0,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.size = size
self.mutants = mutants
self.selection = selection
self.mutation = mutation
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of the Moran process.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
traits = np.zeros(self.size)
traits[: self.mutants] = 1.0
self.system_ = System(
self.label,
elements=[
IndividualElement('population', trait=traits, fitness=(1 + self.selection) ** traits, size=self.size),
],
interactions=[
MoranStepInteraction('moran', target='population', advantage=self.selection, mutation=self.mutation),
],
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
IndividualElement, fromElement
Interactions:
MoranStepInteraction, fromInteraction, through the shared recipeEvolutionInteraction
class IndividualElement(Element):
"""Individual of a finite population, with an integer trait or genome.
Args:
label:
The label of the agent or group.
trait:
The trait, a type index or the bits of a genome.
fitness:
The fitness of the trait.
values:
Further values by name, or `None` for none.
size:
The number of individuals, or `None` for one.
space:
The space of the individuals, or `None` for none.
Examples:
>>> from skcomplex.systems.biology.evolution import IndividualElement
>>> IndividualElement('population', trait=0.0, size=10).size
10
"""
visual = VisualSpec('agents', {'color_by': 'trait'})
def __init__(
self,
label: str,
*,
trait: Annotated[float | npt.ArrayLike, ZZ] = 0.0,
fitness: Annotated[float | npt.ArrayLike, RealSet.closed_open(0, oo)] = 1.0,
values: Mapping[str, object] | None = None,
size: int | None = None,
space: BaseSpace | None = None,
) -> None:
super().__init__(label, values=values, size=size, space=space)
self.trait = trait
self.fitness = fitness
class MoranStepInteraction(EvolutionInteraction):
"""One generation of the Moran process.
A generation is as many birth and death events as there are individuals.
At every event an individual reproduces with probability proportional to its fitness `(1 + advantage)^trait`,
its offspring mutates to the trait one above or one below with probability `mutation / 2` each, and it replaces an
individual chosen uniformly at random, so the size of the population stays constant. It is the Moran replacement
of `EvolutionInteraction` with proportional selection and steps of the trait.
Args:
label:
The label of the interaction.
target:
The path of the group of individuals.
advantage:
The selective advantage of one unit of the trait.
mutation:
The probability that an offspring mutates.
phase:
The phase of the step the generation runs in, or `None` for the default phase.
Examples:
>>> from skcomplex.systems.biology.evolution import MoranStepInteraction
>>> MoranStepInteraction('moran', target='population', advantage=0.1).replacement
'moran'
"""
def __init__(
self,
label: str,
*,
target: Role,
advantage: float = 0.0,
mutation: Annotated[float, RealSet.closed(0, 1)] = 0.0,
phase: str | None = None,
) -> None:
super().__init__(
label,
target=target,
strategy='trait',
selection='proportional',
replacement='moran',
mutation=mutation,
alphabet=None,
change='step',
phase=phase,
)
self.advantage = advantage
def _evaluate(self, strategies: Array, system: SystemViewProtocol) -> Array:
"""Return the fitness `(1 + advantage)^trait` of every trait.
Args:
strategies:
The traits.
system:
The system view of the interaction.
Returns:
fitness:
The fitness of every trait.
"""
return (1 + system.get_parameter('advantage')) ** np.ravel(strategies)
Equations
The equations are those that symbolic returns for the system with its default parameters.