Skip to content

Size-biased random walk on hypergraphs

The size-biased random walk on a hypergraph, p_j' = sum_i p_i T_ij - p_j.

  • Class: HypergraphRandomWalkSystem
  • Import: from skcomplex.systems.mathematics.networks import HypergraphRandomWalkSystem
  • Field: mathematics/networks
  • Topics: hypergraph, random walk, master equation, ode
  • References:
    • Carletti et al. 2020
    • Majhi et al. 2022

The hypergraph_random_walk example

Example

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.mathematics.networks import HypergraphRandomWalkSystem

trajectory = HypergraphRandomWalkSystem().prepare().simulate()

Parameters

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

Parameter Default
hypergraph None
source 0
seed 0
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time continuous
Space hypergraph
Laws rate
Randomness deterministic
Domains real
Structure fixed

Class

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

class HypergraphRandomWalkSystem(BaseSystem):
    """The size-biased random walk on a hypergraph, `p_j' = sum_i p_i T_ij - p_j`.

    The walk starts from one node.

    A walker at node `i` moves to node `j` at rate `T_ij = k_ij / sum_l k_il`, with `k_ij = sum_e (|e| - 1) e_i e_j`
    over the hyperedges `e` of the incidence matrix, so large hyperedges carry more of the walk, as in Carletti et al.
    The probability of the walker spreads from the source, keeps its sum of 1, and settles on the stationary
    distribution `p_i = D_i / sum_l D_l`, with `D_i = sum_e (|e| - 1)^2 e_i` the weight of the hyperedges of `i`. The
    system has the probabilities `nodes.p` and the weights `nodes.D`.

    Args:
        hypergraph:
            The XGI hypergraph, or `None` for a random hypergraph of 30 nodes with hyperedges of sizes 2 to 5.

        source:
            The node the walker starts at.

        seed:
            The seed of the random hypergraph.

        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:
        >>> import xgi
        >>> from skcomplex.systems.mathematics.networks import HypergraphRandomWalkSystem
        >>> system = HypergraphRandomWalkSystem(xgi.Hypergraph([[0, 1, 2], [2, 3]])).prepare().system_
        >>> system.elements[0].D.tolist()
        [4.0, 4.0, 5.0, 1.0]
    """

    field = 'mathematics/networks'
    topics = ('hypergraph', 'random walk', 'master equation', 'ode')
    references = ('Carletti et al. 2020', 'Majhi et al. 2022')
    until = 20.0
    dt = 0.1

    def __init__(
        self,
        hypergraph: xgi.Hypergraph | None = None,
        *,
        source: int = 0,
        seed: int = 0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.hypergraph = hypergraph
        self.source = source
        self.seed = seed

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set `system_` to the size-biased random walk `p_j' = sum_i p_i T_ij - p_j` from one node."""
        super()._prepare(context)
        hypergraph = self.hypergraph
        if hypergraph is None:
            hypergraph = xgi.fast_random_hypergraph(30, [0.1, 0.01, 0.001, 0.0005], seed=self.seed)
        space = NetworkSpace(nx.empty_graph(sorted(hypergraph.nodes)), hyperedges=hypergraph)
        prepared = space.prepare()
        weights = np.zeros(prepared.size_)
        for hyperedge in prepared.hyperedges_:
            weights[list(hyperedge)] += (len(hyperedge) - 1) ** 2
        probabilities = np.zeros(prepared.size_)
        probabilities[self.source] = 1.0
        nodes = WalkerDensityElement('nodes', p=probabilities, D=np.maximum(weights, 1.0), space=space)
        self.system_ = System(self.label, elements=[nodes], interactions=[SizeBiasedMoveInteraction('moves')])

Components

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

Elements:

  • WalkerDensityElement, from Element

Interactions:

  • SizeBiasedMoveInteraction, from Interaction
class WalkerDensityElement(Element):
    """Probability that a random walker is at a node of a hypergraph.

    The node has the weight `D`.

    Args:
        label:
            The label of the node or the group on the nodes.

        p:
            The probability of the walker at the node.

        D:
            The total weight of the moves out of the node, the sum of `(|e| - 1)^2` over its hyperedges `e`.

        space:
            The network whose nodes hold one member each, or `None` for one node.

    Attributes:
        default_visual_:
            How the node is drawn by default, the `visual` of its class.

    Examples:
        >>> from skcomplex.systems.mathematics.networks import WalkerDensityElement
        >>> WalkerDensityElement('node', D=4.0).D
        4.0
    """

    visual = VisualSpec('sites', {'states': ['p'], 'low': 0.0, 'high': 1.0})

    def __init__(
        self,
        label: str,
        *,
        p: Annotated[object, RealSet.closed(0, 1)] = 0.0,
        D: Annotated[object, RealSet.open(0, oo)] = 1.0,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, space=space)
        self.p = p
        self.D = D

class SizeBiasedMoveInteraction(Interaction):
    """Moves of a random walker across a hyperedge.

    The walker moves to every other member with weight `|e| - 1`.

    A walker at node `i` moves to node `j` at rate `T_ij = k_ij / D_i`, where `k_ij = sum_e (|e| - 1)` over the
    hyperedges `e` that hold both, and `D_i = sum_j k_ij`. On a hyperedge, the focal member gains `(|e| - 1) p_j / D_j`
    from every other member `j` and loses `(|e| - 1)^2 p_i / D_i`.

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

        among:
            The path of the group of walker densities on the nodes.

        along:
            `'hyperedges'`, the hyperedges the walker moves across.

    Examples:
        >>> from skcomplex.systems.mathematics.networks import SizeBiasedMoveInteraction
        >>> SizeBiasedMoveInteraction('moves', among='nodes').among
        'nodes'
    """

    law_classes = (RateLaw,)

    def __init__(self, label: str, *, among: Role = 'nodes', along: str = 'hyperedges') -> None:
        super().__init__(label, among=among, along=along)

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the flow into and out of the focal member across one hyperedge.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        s = context
        weight = len(s.others)
        member = s.member
        inflow = total(s.members.p / s.members.D, over=s.members) - member.p / member.D
        self.laws_: list[BaseLaw] = [
            RateLaw(member.p.diff(s.t), weight * inflow - weight**2 * member.p / member.D),
        ]