Skip to content

Neutral Moran system with speciation

A neutral community of constant size with speciation and drift.

  • Class: NeutralBiodiversitySystem
  • Import: from skcomplex.systems.biology.ecology import NeutralBiodiversitySystem
  • Field: biology/ecology
  • Topics: neutral theory, biodiversity, speciation, agents, species abundance
  • References:
    • Bertin 2021
    • Hubbell 2001
    • Vallade and Houchmandzadeh 2003

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.ecology import NeutralBiodiversitySystem

trajectory = NeutralBiodiversitySystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
individuals 200
nu 0.01
species 1
random_state 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
Structure variable membership

Class

The source of the class shows how it builds its components from its model parameters.

class NeutralBiodiversitySystem(BaseSystem):
    """A neutral community of constant size with speciation and drift.

    Its species arise by speciation and vanish by drift.

    Every species is transient, yet the number of species settles around `S_N = sum_n theta / (theta + n)` for `n` from
    0 to `N - 1`, with `theta = (N - 1) nu / (1 - nu)`, and the mean number of species of `k` individuals around `phi_k
    = theta / k prod_j (N - j + 1) / (N - j + theta)` for `j` from 1 to `k`. The system has the group `community` and
    the number `richness` of species.

    Args:
        individuals:
            The number of individuals `N`.

        nu:
            The probability that an offspring founds a new species.

        species:
            The initial number of species, to which the individuals belong at random.

        random_state:
            The seed or generator of the initial species.

        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.ecology import NeutralBiodiversitySystem
        >>> NeutralBiodiversitySystem(20).prepare().system_.elements[0].size
        20
    """

    field = 'biology/ecology'
    topics = ('neutral theory', 'biodiversity', 'speciation', 'agents', 'species abundance')
    references = ('Bertin 2021', 'Hubbell 2001', 'Vallade and Houchmandzadeh 2003')
    until = 500
    observables = (Observable('richness', group='community', variable='species', reduce=_get_richness),)

    def __init__(
        self,
        individuals: int = 200,
        *,
        nu: float = 0.01,
        species: int = 1,
        random_state: int | np.random.Generator | None = 0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.individuals = individuals
        self.nu = nu
        self.species = species
        self.random_state = random_state

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of a neutral community of constant size.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        labels = np.random.default_rng(self.random_state).integers(self.species, size=self.individuals).astype(float)
        self.system_ = System(
            self.label,
            elements=[
                OrganismElement('community', species=labels, size=self.individuals),
            ],
            interactions=[NeutralReplacementInteraction('replacement', target='community', mutation=self.nu)],
        )

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

Interactions:

class OrganismElement(Element):
    """Individual of a neutral community, labelled by its species.

    Args:
        label:
            The label of the agent or group.

        species:
            The label of the species.

        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.ecology import OrganismElement
        >>> OrganismElement('community', species=0.0, size=10).size
        10
    """

    visual = VisualSpec('agents', {'color_by': 'species'})

    def __init__(
        self,
        label: str,
        *,
        species: Annotated[object, NN] = 0.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.species = species

class NeutralReplacementInteraction(EvolutionInteraction):
    """Deaths of random individuals at unit rate, each replaced by an offspring.

    The offspring is of another individual, and of a new species at rate `nu`.

    One unit of time holds `N` deaths. The offspring belongs to the species of a parent drawn among the individuals, or
    with the probability `mutation` to a new species, labelled one above the largest label present. It is the neutral
    Moran replacement of `EvolutionInteraction` with mutation to new labels.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the group of individuals.

        mutation:
            The probability `nu` that an offspring founds a new species.

    Examples:
        >>> from skcomplex.systems.biology.ecology import NeutralReplacementInteraction
        >>> NeutralReplacementInteraction('replacement', target='community', mutation=0.01).change
        'new'
    """

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        mutation: Annotated[float, RealSet.closed(0, 1)] = 0.01,
    ) -> None:
        super().__init__(
            label,
            target=target,
            strategy='species',
            fitness=None,
            selection='proportional',
            replacement='moran',
            mutation=mutation,
            alphabet=None,
            change='new',
        )

Equations

The equations are those that symbolic returns for the system with its default parameters.