Skip to content

SIR Epidemics

Susceptible-Infected-Recovered model as a concrete ContinuousTimeProcess implementation.

Configuration

sir

SIR epidemic as a continuous-time point process.

Implements the ContinuousTimeProcess interface for the Susceptible-Infected-Recovered model on a static network.

SIRConfig

Bases: BaseModel

SIR epidemic model configuration.

Contains only SIR-specific parameters. Convergence and ensemble parameters live in ConvergenceConfig.

Example

config = SIRConfig(tau=2.0, gamma=0.5) config.R0 4.0

R0 property

R0: float

Basic reproduction number (for homogeneous mixing).

SIRProcess

SIRProcess(config: SIRConfig, adjacency: csr_array)

Bases: ContinuousTimeProcess

SIR epidemic dynamics on a static network.

All SIR-specific logic (infection pressure, recovery rates, S->I->R transitions) lives here. The Gillespie engine sees only the ContinuousTimeProcess interface.

Example

from craeft.point_processes.gillespie import run_once from craeft.point_processes.epidemics.sir import ( ... SIRConfig, SIRProcessFactory, ... ) config = SIRConfig(tau=0.5, gamma=1.0) factory = SIRProcessFactory( ... config=config, adjacency=adj, convergence_config=cc, ... ) trajectory, scalar, accepted = run_once( ... factory, t_end=20.0, rng=rng, ... )

rates

rates() -> NDArray[float64]

Concatenated [infection_rates..., recovery_rates...].

Event index < n -> infection of node index. Event index >= n -> recovery of node index - n.

scalar_output

scalar_output() -> float

Final epidemic size (total recovered).

SIRProcessFactory dataclass

SIRProcessFactory(
    config: SIRConfig,
    adjacency: csr_array,
    _convergence_config: ConvergenceConfig,
)

Bases: ProcessFactory

Creates fresh SIRProcess instances for each realisation.

Holds the immutable SIR config and adjacency matrix. The Gillespie engine calls create() once per run.

Simulator

simulator

Epidemic simulator protocol and concrete implementations.

Defines a structural typing contract for epidemic simulation and provides a frozen dataclass implementation wrapping the Gillespie ensemble.

EpidemicSimulator

Bases: Protocol

Structural interface for epidemic simulation.

Implementations consume a CSR adjacency matrix (the sole contract with network generators) and produce ensemble statistics.

run

run(
    adjacency: csr_matrix, rng: Generator
) -> EnsembleResult

Run a converged epidemic ensemble on the given network.

Parameters:

Name Type Description Default
adjacency csr_matrix

Symmetric adjacency matrix in CSR format.

required
rng Generator

Random number generator for this ensemble.

required

Returns:

Type Description
EnsembleResult

Ensemble statistics from multiple realisations.

SIRSimulator dataclass

SIRSimulator(
    config: SIRConfig,
    convergence: ConvergenceConfig = ConvergenceConfig(),
    n_points: int = 200,
)

SIR epidemic simulation via Gillespie algorithm.

Wraps the convergence-based ensemble in a convenient interface matching the EpidemicSimulator protocol.

Each call to run() executes multiple realisations until the relative standard error of the mean final size drops below the configured threshold.