Skip to content

Spreading

Spreading: rumours, cascades, contagions on hypergraphs, and growing texts.

ExposureInteraction(label, *, target, rate=0.06, exponent=2.0)

Bases: Interaction

Infection of participants by the doses of their environments, and recovery.

The infected participants are susceptible, and the time is discrete. An environment, a hyperedge, gives each susceptible member the dose rate * rho ** exponent, where rho is the share of the other members that are infected, and the doses of the environments of a participant add up to D. In every step a susceptible participant is infected with probability 1 - exp(-D), and an infected one recovers with probability recovery, a parameter of the participants.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the participants on the network.

required
rate Annotated[float, closed_open(0, oo)]

The dose of an environment whose other members are all infected.

0.06
exponent Annotated[float, open(0, oo)]

The exponent of the infection kernel, sublinear below 1 and supralinear above.

2.0

Examples:

>>> from skcomplex.systems.social.spreading import ExposureInteraction
>>> ExposureInteraction('exposure', target='participants', exponent=1.5).exponent
1.5
Source code in src/skcomplex/systems/social/spreading/_exposure.py
102
103
104
105
106
107
108
109
110
111
112
113
def __init__(
    self,
    label: str,
    *,
    target: Role,
    rate: Annotated[float, RealSet.closed_open(0, oo)] = 0.06,
    exponent: Annotated[float, RealSet.open(0, oo)] = 2.0,
) -> None:
    super().__init__(label)
    self.target = target
    self.rate = rate
    self.exponent = exponent

GrowingWordElement(label, *, count=1.0, innovation=0.1, size=None)

Bases: Element

Distinct word of the Simon system.

It holds the number of times it occurs in the text.

Parameters:

Name Type Description Default
label str

The label of the agent or group.

required
count Annotated[object, NN]

The number of occurrences, one per word or shared.

1.0
innovation Annotated[float, closed_open(0, oo)]

The probability that the next word of the text is new.

0.1
size int | None

The number of words, or None for one.

None

Examples:

>>> from skcomplex.systems.social.spreading import GrowingWordElement
>>> group = GrowingWordElement('words', count=1.0, size=1)
>>> group.label, group.count, group.size
('words', 1.0, 1)
Source code in src/skcomplex/systems/social/spreading/_growth.py
60
61
62
63
64
65
66
67
68
69
70
def __init__(
    self,
    label: str,
    *,
    count: Annotated[object, NN] = 1.0,
    innovation: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
    size: int | None = None,
) -> None:
    super().__init__(label, size=size)
    self.count = count
    self.innovation = innovation

GrowingWordView

Bases: AgentViewProtocol, Protocol

View of a word of the Simon system.

Its states and parameters are attributes.

HypergraphExposureSisSystem(n_nodes=1000, *, sizes=(10, 20, 30), hyperdegree=5.0, rate=0.06, exponent=2.0, recovery=0.05, share=0.3, seed=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The SIS contagion of St-Onge et al. in the environments of a hypergraph.

Its kernel is nonlinear. Participants meet in environments, the hyperedges of a random hypergraph. In every step an environment gives a susceptible member the dose rate * rho ** exponent from the share rho of its other members that are infected, the member is infected with probability 1 - exp(-D) of the sum D of its doses, and an infected participant recovers with probability recovery. Heterogeneous exposure times give the power law rho ** exponent to the infection kernel. With a sublinear or linear kernel the epidemic sets in continuously, and with a supralinear kernel, exponent > 1, the healthy state stays stable: an endemic state coexists with it above a critical rate, so a small seed dies out and a large one persists. In mean field the prevalence I is endemic where k rate I ** (exponent - 1) (1 - I) = recovery for k environments per participant.

The system has the states participants.infected and the prevalence prevalence.

Parameters:

Name Type Description Default
n_nodes int

The number of participants.

1000
sizes Sequence[int]

The sizes of the environments.

(10, 20, 30)
hyperdegree float

The mean number of environments of a participant, split evenly over the sizes, whose members are drawn at random.

5.0
rate float

The dose of an environment whose other members are all infected.

0.06
exponent float

The exponent of the infection kernel.

2.0
recovery float

The probability of recovery in a step.

0.05
share float

The share of infected participants at the start.

0.3
seed int

The seed of the hypergraph and the infected participants.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.social.spreading import HypergraphExposureSisSystem
>>> len(HypergraphExposureSisSystem(40).prepare().system_.elements[0].infected)
40
Source code in src/skcomplex/systems/social/spreading/_exposure.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
def __init__(
    self,
    n_nodes: int = 1000,
    *,
    sizes: Sequence[int] = (10, 20, 30),
    hyperdegree: float = 5.0,
    rate: float = 0.06,
    exponent: float = 2.0,
    recovery: float = 0.05,
    share: float = 0.3,
    seed: int = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n_nodes = n_nodes
    self.sizes = sizes
    self.hyperdegree = hyperdegree
    self.rate = rate
    self.exponent = exponent
    self.recovery = recovery
    self.share = share
    self.seed = seed

InfectiveElement(label, *, infected=0.0, recovery=1.0, space=None)

Bases: Element

Nodes of a contagion, susceptible at 0 and infected at 1.

They recover at a rate, by a RecoveryInteraction.

Parameters:

Name Type Description Default
label str

The label of the node or the group.

required
infected Annotated[object, FiniteEnumeratedSet([0, 1])]

The initial state, 0 or 1, one per node or shared.

0.0
recovery Annotated[float, closed_open(0, oo)]

The rate at which an infected node recovers.

1.0
space BaseSpace | None

The network with one node per site, or None for one node.

None

Examples:

>>> from skcomplex.systems.social.spreading import InfectiveElement
>>> InfectiveElement('node').recovery
1.0
Source code in src/skcomplex/systems/social/spreading/_simplicial.py
62
63
64
65
66
67
68
69
70
71
72
def __init__(
    self,
    label: str,
    *,
    infected: Annotated[object, FiniteEnumeratedSet([0, 1])] = 0.0,
    recovery: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
    space: BaseSpace | None = None,
) -> None:
    super().__init__(label, space=space)
    self.infected = infected
    self.recovery = recovery

ListenersElement(label, *, n=0)

Bases: Element

Number of people in one state of a rumour: ignorant, spreading, or stifling.

Parameters:

Name Type Description Default
label str

The label of the element.

required
n Annotated[float, NN]

The initial number of people.

0

Examples:

>>> from skcomplex.systems.social.spreading import ListenersElement
>>> ListenersElement('spreaders', n=1).n
1
Source code in src/skcomplex/systems/social/spreading/_rumours.py
39
40
41
42
43
44
45
46
def __init__(
    self,
    label: str,
    *,
    n: Annotated[float, NN] = 0,
) -> None:
    super().__init__(label)
    self.n = n

MakiThompsonSystem(n_people=1000, *, spread=1.0, stifle=1.0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The Maki-Thompson rumour system started by one spreader.

It leaves about 20% of people ignorant. The system has the counts ignorants.n, spreaders.n, and stiflers.n.

Parameters:

Name Type Description Default
n_people int

The size of the population.

1000
spread float

The contact rate that spreads the rumour.

1.0
stifle float

The contact rate that stifles a spreader.

1.0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.social.spreading import MakiThompsonSystem
>>> [element.n for element in MakiThompsonSystem(100).prepare().system_.elements]
[99, 1, 0]
Source code in src/skcomplex/systems/social/spreading/_rumours.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
def __init__(
    self,
    n_people: int = 1000,
    *,
    spread: float = 1.0,
    stifle: float = 1.0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n_people = n_people
    self.spread = spread
    self.stifle = stifle

ParticipantElement(label, *, infected=0.0, recovery=0.05, space=None)

Bases: Element

Participant in the environments of a contagion.

It is susceptible at 0 and infected at 1. The infection and the recovery are an ExposureInteraction of the hypergraph, which HypergraphExposureSisSystem adds.

Parameters:

Name Type Description Default
label str

The label of the participant or the group.

required
infected Annotated[object, FiniteEnumeratedSet([0, 1])]

The initial state, 0 or 1, one per participant or shared.

0.0
recovery Annotated[float, closed(0, 1)]

The probability that an infected participant recovers in a step.

0.05
space BaseSpace | None

The network with one participant per node, or None for one participant.

None

Examples:

>>> from skcomplex.systems.social.spreading import ParticipantElement
>>> ParticipantElement('participant').recovery
0.05
Source code in src/skcomplex/systems/social/spreading/_exposure.py
58
59
60
61
62
63
64
65
66
67
68
def __init__(
    self,
    label: str,
    *,
    infected: Annotated[object, FiniteEnumeratedSet([0, 1])] = 0.0,
    recovery: Annotated[float, RealSet.closed(0, 1)] = 0.05,
    space: BaseSpace | None = None,
) -> None:
    super().__init__(label, space=space)
    self.infected = infected
    self.recovery = recovery

RecoveryInteraction(label, *, target)

Bases: Interaction

Recovery of an infected node at the rate recovery of the nodes.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the group of nodes.

required

Examples:

>>> from skcomplex.systems.social.spreading import RecoveryInteraction
>>> RecoveryInteraction('recovery', target='nodes').target
'nodes'
Source code in src/skcomplex/systems/social/spreading/_simplicial.py
93
94
95
def __init__(self, label: str, *, target: Role) -> None:
    super().__init__(label)
    self.target = target

RumourContactsInteraction(label, *, ignorants, spreaders, stiflers, spread=1.0, stifle=1.0)

Bases: Interaction

Contacts of the Maki-Thompson rumour system in a well-mixed population.

The population has size N. A spreader who meets an ignorant tells the rumour at rate spread * X * Y / N, and a spreader who meets another spreader or a stifler stops spreading at rate stifle * Y * (Y - 1 + Z) / N.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
ignorants Role

The path of the ignorants X.

required
spreaders Role

The path of the spreaders Y.

required
stiflers Role

The path of the stiflers Z.

required
spread Annotated[float, closed_open(0, oo)]

The contact rate that spreads the rumour.

1.0
stifle Annotated[float, closed_open(0, oo)]

The contact rate that stifles a spreader.

1.0

Examples:

>>> from skcomplex.systems.social.spreading import RumourContactsInteraction
>>> RumourContactsInteraction('contacts', ignorants='X', spreaders='Y', stiflers='Z').spread
1.0
Source code in src/skcomplex/systems/social/spreading/_rumours.py
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def __init__(
    self,
    label: str,
    *,
    ignorants: Role,
    spreaders: Role,
    stiflers: Role,
    spread: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
    stifle: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
) -> None:
    super().__init__(label)
    self.ignorants = ignorants
    self.spreaders = spreaders
    self.stiflers = stiflers
    self.spread = spread
    self.stifle = stifle

SimonYuleSystem(*, innovation=0.1, words_per_step=100, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The Simon system of a text that grows from one word.

Its word counts follow a power law. The rank-frequency exponent is 1 / (1 - innovation), and the number of distinct words grows as innovation times the length of the text. The system has the group words.

Parameters:

Name Type Description Default
innovation float

The probability that the next word is new.

0.1
words_per_step int

The number of words written per step.

100
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.social.spreading import SimonYuleSystem
>>> SimonYuleSystem().prepare().system_.elements[0].size
1
Source code in src/skcomplex/systems/social/spreading/_growth.py
178
179
180
181
182
183
184
185
186
187
188
189
def __init__(
    self,
    *,
    innovation: float = 0.1,
    words_per_step: int = 100,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.innovation = innovation
    self.words_per_step = words_per_step

SimplicialContagionSystem(n_nodes=200, *, infectivity=0.75, triangle_infectivity=3.0, mean_degree=20.0, mean_triangles=6.0, recovery=1.0, share=0.4, seed=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The simplicial contagion of Iacopini et al.

It is an SIS process on the links and triangles of a complex. A random simplicial complex joins every pair of nodes with probability p1 and fills every triple with probability p2, chosen so that a node has mean_degree links and mean_triangles triangles on average. A susceptible node is infected at rate beta = infectivity * recovery / <k> by every infected neighbour, and at rate beta_triangle = triangle_infectivity * recovery / <k_triangle> by every triangle whose two other members are infected, and an infected node recovers at rate recovery. The mean-field prevalence follows rho' = -rho + infectivity rho (1 - rho) + triangle_infectivity rho^2 (1 - rho) in units of the recovery time. For triangle_infectivity > 1 and infectivities below the epidemic threshold 1, an endemic state coexists with the healthy one, so the epidemic dies out from a small seed and persists from a large one.

The system has the states nodes.infected and the prevalence prevalence.

Parameters:

Name Type Description Default
n_nodes int

The number of nodes.

200
infectivity float

The rescaled rate of infection through links.

0.75
triangle_infectivity float

The rescaled rate of infection through triangles.

3.0
mean_degree float

The mean number of links of a node, which sets p1 together with the sides of the triangles.

20.0
mean_triangles float

The mean number of triangles of a node, which sets p2.

6.0
recovery float

The rate of recovery.

1.0
share float

The share of infected nodes at the start.

0.4
seed int

The seed of the complex and the infected nodes.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.social.spreading import SimplicialContagionSystem
>>> system = SimplicialContagionSystem(30, mean_degree=6.0, mean_triangles=2.0).prepare().system_
>>> len(system.elements[0].infected)
30
Source code in src/skcomplex/systems/social/spreading/_simplicial.py
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
def __init__(
    self,
    n_nodes: int = 200,
    *,
    infectivity: float = 0.75,
    triangle_infectivity: float = 3.0,
    mean_degree: float = 20.0,
    mean_triangles: float = 6.0,
    recovery: float = 1.0,
    share: float = 0.4,
    seed: int = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n_nodes = n_nodes
    self.infectivity = infectivity
    self.triangle_infectivity = triangle_infectivity
    self.mean_degree = mean_degree
    self.mean_triangles = mean_triangles
    self.recovery = recovery
    self.share = share
    self.seed = seed

SimplicialInfectionInteraction(label, *, among, beta=0.1, beta_triangle=0.1, along='hyperedges')

Bases: Interaction

Infection of a susceptible node by the other members of a link or a triangle.

The triangle is filled, and the infection needs all of them infected. A susceptible node of a link is infected at rate beta when its neighbour is infected, and a susceptible node of a triangle at rate beta_triangle when both other members are infected. The laws run once for every link and triangle of the network.

Parameters:

Name Type Description Default
label str

The label of the interaction, unique among the components of its system.

required
among Role

The path of the group of nodes.

required
beta Annotated[float, closed_open(0, oo)]

The rate of infection through a link.

0.1
beta_triangle Annotated[float, closed_open(0, oo)]

The rate of infection through a triangle.

0.1
along str

The links of the network the laws run on, 'hyperedges' for its links and triangles.

'hyperedges'

Examples:

>>> from skcomplex.systems.social.spreading import SimplicialInfectionInteraction
>>> SimplicialInfectionInteraction('infection', among='nodes', beta_triangle=0.5).beta_triangle
0.5
Source code in src/skcomplex/systems/social/spreading/_simplicial.py
142
143
144
145
146
147
148
149
150
151
152
153
def __init__(
    self,
    label: str,
    *,
    among: Role,
    beta: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
    beta_triangle: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
    along: str = 'hyperedges',
) -> None:
    super().__init__(label, among=among, along=along)
    self.beta = beta
    self.beta_triangle = beta_triangle

ThresholdActivationInteraction(label, *, target)

Bases: Interaction

Activation of the Watts cascade system.

A node becomes active for good once the share of active nodes among its neighbouring nodes reaches its threshold. The nodes update one after the other in a new random order every step, each reading the activations the nodes before it set, so a cascade can cross several nodes in one step.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the group of nodes.

required

Examples:

>>> from skcomplex.systems.social.spreading import ThresholdActivationInteraction
>>> ThresholdActivationInteraction('activation', target='nodes').target
'nodes'
Source code in src/skcomplex/systems/social/spreading/_cascades.py
61
62
63
def __init__(self, label: str, *, target: Role) -> None:
    super().__init__(label)
    self.target = target

ThresholdNodeElement(label, *, active=0.0, threshold=0.18, size=None)

Bases: Element

Node of the Watts cascade system.

The activation turns it active once enough of its neighbours are active.

Parameters:

Name Type Description Default
label str

The label of the agent or group.

required
active Annotated[object, FiniteEnumeratedSet([0, 1])]

Whether the node starts active, one per node or shared.

0.0
threshold Annotated[object, closed_open(0, oo)]

The share of active neighbours at which the node becomes active, one per node or shared.

0.18
size int | None

The number of nodes, or None for one.

None

Examples:

>>> from skcomplex.systems.social.spreading import ThresholdNodeElement
>>> group = ThresholdNodeElement('nodes', threshold=0.18, size=10)
>>> group.label, group.threshold, group.size
('nodes', 0.18, 10)
Source code in src/skcomplex/systems/social/spreading/_cascades.py
122
123
124
125
126
127
128
129
130
131
132
def __init__(
    self,
    label: str,
    *,
    active: Annotated[object, FiniteEnumeratedSet([0, 1])] = 0.0,
    threshold: Annotated[object, RealSet.closed_open(0, oo)] = 0.18,
    size: int | None = None,
) -> None:
    super().__init__(label, size=size)
    self.active = active
    self.threshold = threshold

ThresholdNodeView

Bases: AgentViewProtocol, Protocol

View of a node of the Watts cascade system.

Its states and parameters are attributes.

WattsCascadesSystem(graph=None, *, threshold=0.18, seeds=1, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The Watts threshold system of global cascades started by a few random seeds.

The system has the group nodes.

Parameters:

Name Type Description Default
graph object

The NetworkX graph, or None for a random graph of 200 nodes with mean degree 4.

None
threshold float

The share of active neighbours at which a node becomes active.

0.18
seeds int

The number of nodes active at the start.

1
random_state int | Generator | None

The seed or generator of the default graph and of the seeds.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> import networkx as nx
>>> from skcomplex.systems.social.spreading import WattsCascadesSystem
>>> WattsCascadesSystem(nx.cycle_graph(10), random_state=0).prepare().system_.elements[0].size
10
Source code in src/skcomplex/systems/social/spreading/_cascades.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def __init__(
    self,
    graph: object = None,
    *,
    threshold: float = 0.18,
    seeds: 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.graph = graph
    self.threshold = threshold
    self.seeds = seeds
    self.random_state = random_state

WritingInteraction(label, *, target, words_per_step=100)

Bases: Interaction

Writing of the Simon system, a number of words per step.

Every next word is new with probability innovation, and otherwise repeats a random earlier occurrence, so a word is repeated with probability proportional to its count.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the group of words.

required
words_per_step int

The number of words written per step.

100

Examples:

>>> from skcomplex.systems.social.spreading import WritingInteraction
>>> WritingInteraction('writing', target='words').words_per_step
100
Source code in src/skcomplex/systems/social/spreading/_growth.py
 99
100
101
102
103
104
105
106
107
108
def __init__(
    self,
    label: str,
    *,
    target: Role,
    words_per_step: int = 100,
) -> None:
    super().__init__(label)
    self.target = target
    self.words_per_step = words_per_step

write(system)

Write the words of one step.

Parameters:

Name Type Description Default
system SystemView

The system.

required
Source code in src/skcomplex/systems/social/spreading/_growth.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def write(self, system: SystemView) -> None:
    """Write the words of one step.

    Args:
        system:
            The system.
    """
    words = cast(list[GrowingWordView], system.get_agents('target'))
    if not words:
        return
    innovation = words[0].innovation
    counts = [int(word.count) for word in words]
    tokens = np.repeat(np.arange(len(counts)), counts).tolist()
    for _ in range(self.words_per_step):
        index = len(counts) if system.rng.random() < innovation else tokens[int(system.rng.integers(len(tokens)))]
        if index == len(counts):
            counts.append(0)
        counts[index] += 1
        tokens.append(index)
    for word, count in zip(words, counts, strict=False):
        word.count = float(count)
    for count in counts[len(words) :]:
        system.create('target', count=float(count))