Skip to content

PageRank iterations on networks

The PageRank iteration r_i' = (1 - d) / n + d Σ_j A_ij r_j / k_j.

  • Class: PagerankSystem
  • Import: from skcomplex.systems.mathematics.networks import PagerankSystem
  • Field: mathematics/networks
  • Topics: network, pagerank, centrality, discrete map
  • References:
    • Brin and Page 1998
    • Thurner et al. 2018
    • Jensen 2023

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 PagerankSystem

trajectory = PagerankSystem().prepare().simulate()

Parameters

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

Parameter Default
graph None
damping 0.85
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 network
Laws update
Randomness deterministic
Domains real
Structure fixed

Class

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

class PagerankSystem(BaseSystem):
    """The PageRank iteration `r_i' = (1 - d) / n + d Σ_j A_ij r_j / k_j`.

    The network is undirected.

    The ranks start uniform, stay normalized, and converge to the PageRank of the graph. Every node needs a link. The
    system has the ranks `pages.rank`.

    Args:
        graph:
            The undirected NetworkX graph without isolated nodes, or `None` for Zachary's karate club.

        damping:
            The probability `d` that the surfer follows a link rather than jumping to a random node.

        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 networkx as nx
        >>> from skcomplex.systems.mathematics.networks import PagerankSystem
        >>> PagerankSystem(nx.cycle_graph(5)).prepare().system_.elements[0].rank.tolist()
        [0.2, 0.2, 0.2, 0.2, 0.2]
    """

    field = 'mathematics/networks'
    topics = ('network', 'pagerank', 'centrality', 'discrete map')
    references = ('Brin and Page 1998', 'Thurner et al. 2018', 'Jensen 2023')
    until = 50
    dt = 1

    def __init__(
        self,
        graph: nx.Graph | None = None,
        *,
        damping: float = 0.85,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.graph = graph
        self.damping = damping

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of the pages with uniform ranks and their iteration."""
        super()._prepare(context)
        graph = NetworkRandomWalkSystem._get_graph_or_default(self.graph)
        size = graph.number_of_nodes()
        degrees = np.array([degree for _, degree in graph.degree()], dtype=float)
        pages = NodeRankElement('pages', rank=np.full(size, 1 / size), degree=degrees, space=NetworkSpace(graph))
        self.system_ = System(
            self.label,
            elements=[pages],
            interactions=[PagerankInteraction('links', target='pages', damping=self.damping, n_nodes=size)],
        )

Components

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

Elements:

Interactions:

  • PagerankInteraction, from Interaction
class NodeRankElement(Element):
    """Pages of a network, each with its rank `rank` and its number `degree` of links.

    Args:
        label:
            The label of the pages.

        rank:
            The initial rank, one per page or shared.

        degree:
            The number of links, one per page or shared.

        space:
            The network with one page per node, or `None` for one page.

    Examples:
        >>> from skcomplex.systems.mathematics.networks import NodeRankElement
        >>> NodeRankElement('pages', degree=2.0).degree
        2.0
    """

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

class PagerankInteraction(Interaction):
    """The PageRank iteration `r_i' = Σ_j A_ij (d r_j / k_j + (1 - d) / (n k_i))` of a random surfer.

    The surfer follows a link with the probability `d` and jumps to a random page of the `n` pages otherwise, so the
    ranks of an undirected network without isolated pages stay normalized.

    Args:
        label:
            The label of the interaction.

        target:
            The pages whose ranks the iteration sets.

        damping:
            The probability `d` that the surfer follows a link rather than jumping to a random page.

        n_nodes:
            The number `n` of pages of the network.

    Examples:
        >>> from skcomplex.systems.mathematics.networks import PagerankInteraction
        >>> PagerankInteraction('links', damping=0.9).damping
        0.9
    """

    law_classes = (UpdateLaw,)

    def __init__(self, label: str, *, target: Role = 'pages', damping: float = 0.85, n_nodes: int = 1) -> None:
        super().__init__(label)
        self.target = target
        self.damping = damping
        self.n_nodes = n_nodes

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the next rank, summed over the links of every page, as the law.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        page, other, damping = context.target, context.target.other, context.damping
        term = damping * other.rank / other.degree + (1 - damping) / (context.n_nodes * page.degree)
        self.laws_: list[BaseLaw] = [UpdateLaw(page.rank, neighbors(term))]