Competitive Jain-Krishna model with signed couplings
The Jain-Krishna model with signed interactions.
- Class:
CompetitiveJainKrishnaSystem - Import:
from skcomplex.systems.biology.evolution import CompetitiveJainKrishnaSystem - Field:
biology/evolution - Topics:
autocatalytic set,competition,cooperation,punctuated equilibrium,agents - References:
- Jain and Krishna 2002
- Thurner et al. 2018
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 CompetitiveJainKrishnaSystem
trajectory = CompetitiveJainKrishnaSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n |
50 |
p |
0.05 |
duration |
20.0 |
seed |
1 |
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 | real, vector |
| Structure | variable membership |
Class
The source of the class shows how it builds its components from its model parameters.
class CompetitiveJainKrishnaSystem(BaseSystem):
"""The Jain-Krishna model with signed interactions.
Its surviving species come to cooperate.
Every link of the interaction matrix is present with the probability `p` and drawn from `[-1, 1]`, and every species
degrades at a rate drawn from `[-1, 0]`. The populations follow the linear catalytic dynamics with a floor at zero,
and at every step the least populated species is replaced by a new random one. Selection keeps the species that
catalyse each other, so once an autocatalytic set forms the links among the surviving species turn mostly positive,
the emergence of cooperation, and diversity grows until a keystone species is lost. The system has the populations
`x` and the rows `links` of the interaction matrix of the group `species`.
Args:
n:
The number of species.
p:
The probability of every link.
duration:
The time of the population dynamics between replacements.
seed:
The seed of the initial matrix.
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 CompetitiveJainKrishnaSystem
>>> CompetitiveJainKrishnaSystem(10).prepare().system_.elements[0].size
10
"""
field = 'biology/evolution'
topics = ('autocatalytic set', 'competition', 'cooperation', 'punctuated equilibrium', 'agents')
references = ('Jain and Krishna 2002', 'Thurner et al. 2018')
until = 400
def __init__(
self,
n: int = 50,
*,
p: float = 0.05,
duration: float = 20.0,
seed: int | None = 1,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.n = n
self.p = p
self.duration = duration
self.seed = seed
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of the Jain-Krishna model with signed interactions.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
rng = np.random.default_rng(self.seed)
matrix = rng.uniform(-1, 1, (self.n, self.n)) * (rng.random((self.n, self.n)) < self.p)
np.fill_diagonal(matrix, -rng.random(self.n))
species = CompetingSpeciesElement('species', x=np.full(self.n, 1 / self.n), links=matrix, size=self.n)
selection = CompetitiveSelectionInteraction('selection', species='species', p=self.p, duration=self.duration)
self.system_ = System(self.label, elements=[species], interactions=[selection])
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
CompetingSpeciesElement, fromElement
Interactions:
CompetitiveSelectionInteraction, fromInteraction
class CompetingSpeciesElement(Element):
"""Species of the competitive Jain-Krishna model.
It holds a relative population and the signed effects of all species on it.
The links are the row of the interaction matrix of the species, `M_ij` for every species `j`, between -1 and 1, with
the degradation `M_ii` between -1 and 0 on the diagonal.
Args:
label:
The label of the species or of the group of species.
x:
The relative population.
links:
The effects of every species on this one, one row of the interaction matrix per member.
size:
The number of species, or `None` for one.
Examples:
>>> from skcomplex.systems.biology.evolution import CompetingSpeciesElement
>>> CompetingSpeciesElement('species', x=0.1).x
0.1
"""
def __init__(
self,
label: str,
*,
x: Annotated[float | Array, RealSet.closed_open(0, oo)] = 0.0,
links: float | Array = 0.0,
size: int | None = None,
) -> None:
super().__init__(label, size=size)
self.x = x
self.links = links
class CompetitiveSelectionInteraction(Interaction):
"""Population dynamics of signed catalytic interactions.
The least populated species is replaced at every step.
Between replacements the relative populations follow Eq. (5.37) of Thurner et al. (2018), `dx_i/dt = f_i` with
`f_i = sum_j M_ij x_j - x_i phi`, where `phi = sum_jk M_jk x_k` keeps the total at one, except that a species at
zero stays there while `f_i < 0`. The rule integrates them for the time `duration` by Euler steps of `step`, then
replaces the least populated species, ties broken at random, by a new one whose links to and from every other
species are drawn from `[-1, 1]` with the probability `p`, whose degradation is drawn from `[-1, 0]`, and whose
population starts at `1 / N`.
Args:
label:
The label of the interaction.
species:
The path of the group of species.
p:
The probability of every link of a new species.
duration:
The time of the population dynamics between replacements.
step:
The Euler step of the population dynamics.
Examples:
>>> from skcomplex.systems.biology.evolution import CompetitiveSelectionInteraction
>>> CompetitiveSelectionInteraction('selection', species='species', p=0.1).p
0.1
"""
draws = True
law_classes = (UpdateLaw,)
def __init__(
self,
label: str,
*,
species: Role,
p: float = 0.05,
duration: float = 20.0,
step: float = 0.05,
) -> None:
super().__init__(label)
self.species = species
self.p = p
self.duration = duration
self.step = step
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the selection, a rule of the whole system.
Args:
context:
The namespace of the system.
"""
super()._prepare(context)
self.laws_: list[BaseLaw] = [UpdateLaw(context, self.select)]
def select(self, system: SystemView) -> None:
"""Run the population dynamics, then replace the least populated species.
Args:
system:
The system.
"""
species = cast(list[CompetingSpeciesView], system.get_agents(self.species))
matrix = np.array([np.asarray(item.links, dtype=float) for item in species])
populations = self._relax(matrix, np.array([item.x for item in species]))
size = len(species)
weakest = np.flatnonzero(populations <= populations.min() + 1e-12)
k = int(system.rng.choice(weakest))
drawn = system.rng.uniform(-1, 1, (2, size)) * (system.rng.random((2, size)) < self.p)
matrix[k, :], matrix[:, k] = drawn[0], drawn[1]
matrix[k, k] = -system.rng.random()
populations[k] = 1 / size
populations /= populations.sum()
for index, item in enumerate(species):
item.x = float(populations[index])
item.links = matrix[index]
def _relax(self, matrix: Array, populations: Array) -> Array:
"""Return the populations after the dynamics of Eq.
(5.37) for the time `duration`.
"""
x = populations.copy()
for _ in range(round(self.duration / self.step)):
drive = matrix @ x
f = drive - x * drive.sum()
f[(x <= 0) & (f < 0)] = 0.0
x = np.clip(x + self.step * f, 0.0, None)
total = x.sum()
x = x / total if total > 0 else np.full_like(x, 1 / x.size)
return x
Equations
The equations are those that symbolic returns for the system with its default parameters.