---
title: "Method"
subtitle: "Structural Dependency Criticality via PageRank: the model, the solver and its validation"
---
```{r load}
#| include: false
for (f in sort(list.files("R", pattern = "^[0-9].*\\.R$", full.names = TRUE))) source(f)
res <- load_results()
nodes <- res$nodes
n_assets <- nrow(nodes)
sol <- res$solution
alpha <- res$params$pagerank$alpha
sinks <- nodes$name[sol$sink]
sink_mass <- sum(sol$criticality_vector[sol$sink])
floor_teleport <- (1 - alpha) / n_assets
floor_observed <- min(sol$criticality_vector)
```
## PageRank on a dependency graph
Picture a random walker moving through the dependency map. At each step, with probability $\alpha$ (the **damping factor**, here `r alpha`), the walker follows one of the current system's dependencies, chosen at random. Otherwise it restarts at a system drawn from a **restart distribution** $v$. A system's PageRank score is the long-run share of time the walker spends there. Systems that many important systems depend on are visited often and score highly.
In matrix form, with $M$ the column-stochastic transition matrix ($M_{ij} = 1/d_j$ when system $j$ depends on system $i$ and $d_j$ is $j$'s number of dependencies), the scores $p^*$ solve
$$
p^* = \alpha \left( M p^* + \Big(\textstyle\sum_{j \in \mathcal{D}} p^*_j\Big)\, u \right) + (1 - \alpha)\, v ,
$$
where $\mathcal{D}$ is the set of **dangling** systems (systems with no dependencies of their own) and $u$ is where their mass goes. The scores are non-negative and sum to one. They rank systems relative to each other. They carry no units of probability of failure, loss or exposure.
**What $\alpha$ is and is not.** $\alpha$ is the probability that the walk follows a dependency rather than restarting. As $\alpha$ falls toward zero, every score tends to $1/n$, so the model says all systems matter equally. It is not a measure of how tightly or loosely coupled the architecture is. Coupling strength belongs in edge weights or in the cascade model ([Failure Cascades](cascades.qmd)). The [Sensitivity](sensitivity.qmd#sec-alpha) page shows how the scores move across $\alpha$ from `r min(res$params$alpha_sweep$values)` to `r max(res$params$alpha_sweep$values)`.
**Why not eigenvector centrality.** This graph is a directed acyclic graph: following dependencies never leads back to the start. The adjacency matrix of such a graph has every eigenvalue equal to zero, so there is no leading eigenvector to rank by, and eigenvector centrality degenerates. The restart term in PageRank guarantees a unique, strictly positive solution.
## Dangling systems {#sec-dangling}
`r length(sinks)` systems have no dependencies of their own: `r knitr::combine_words(sinks)`. Together they hold `r pct(sink_mass, 0.1)` of PageRank mass. Something must happen to the walker when it reaches one, and the choice affects every score:
- **Uniform** (default, $u = 1/n$): the walker jumps to a random system. This is igraph's convention and is used for the headline results. It raises the lowest possible score from the restart floor $(1-\alpha)/n =$ `r pct(floor_teleport, 0.01)` to the observed minimum of `r pct(floor_observed, 0.01)`, because mass collected by the dangling systems is recycled to every system.
- **Self-loop**: the dangling system keeps the walker. Mass accumulates at the foundation systems.
- **Personalization** ($u = v$): the walker restarts according to the restart distribution. This is the natural choice for business-weighted PageRank below, and it is igraph's convention for personalized PageRank.
Operationally, none of these means much ("after reaching the vault, jump to a random service"). The [Sensitivity](sensitivity.qmd#sec-dangling-sens) page reports how much the ranking changes between them.
## Business-weighted PageRank {#sec-business}
With a uniform restart distribution, every system starts the walk equally often, so a back-office batch job counts as much as the payment API. Setting $v$ to the business weights from `params.yaml` makes the walk start only at the `r sum(nodes$business_weight > 0)` client-facing systems, in proportion to the business value delivered through them. The resulting score reads as "the share of business-weighted dependency paths that pass through this system". Business weights are illustrative assumptions.
## Edge weights {#sec-weights}
The solver accepts an optional edge weight. Each system's outgoing weights are normalized by its total outgoing weight, so a heavier edge carries a larger share of the walk. All weights in this study are 1. The validation below includes a test case in which every dependency on Ledger_DB has weight 2.
## Implementation
The solver builds the adjacency matrix as a sparse matrix (`Matrix::sparseMatrix`), normalizes columns in one step, and runs power iteration until the L1 change between successive iterates falls below `r sci(res$params$pagerank$tol, 1)`. It returns a convergence flag and warns if `max_iter` is reached first.
```{r solver-code}
#| file: R/02_pagerank.R
#| eval: false
```
## Validation against igraph {#sec-validation}
The custom solver is compared with `igraph::page_rank()` (PRPACK backend) in three configurations: uniform restart, business-weighted restart, and weighted edges.
```{r solver-validation}
res$validation |>
select(rank_custom, name, tier, p_custom, p_igraph, abs_delta) |>
slice_head(n = 10) |>
gt_house(
title = "Custom power iteration vs. igraph: top 10 systems",
subtitle = "Uniform restart, α = 0.85, convergence tolerance 10⁻⁹"
) |>
cols_label(
rank_custom = "Rank", name = "Asset", tier = "Tier",
p_custom = "Custom solver", p_igraph = "igraph", abs_delta = "Absolute difference"
) |>
fmt_number(columns = c(p_custom, p_igraph), decimals = 6) |>
fmt_scientific(columns = abs_delta, decimals = 2)
```
**How to read the table.** Each row is one of the ten systems with the highest PageRank, in rank order. The two score columns show the PageRank share computed by this project's own solver and by igraph, a widely used reference library, written as fractions of one (0.15 means 15%). The last column is the gap between them in scientific notation, where 5e-11 means 0.00000000005.
**What the table tells you.** The two solvers agree to within `r format(signif(res$max_discrepancy, 2))` for every system, far below any precision a decision could use, and they rank all `r n_assets` systems identically. The project's solver is therefore built correctly, and the later pages can rely on it. Agreement shows that the arithmetic is right. It does not show that the dependency map, or PageRank itself, is the right model of the estate.
```{r validation-assert}
v <- res$validation
stopifnot(
res$solution$converged,
res$max_discrepancy < 1e-8,
res$max_discrepancy_business < 1e-8,
res$max_discrepancy_weighted < 1e-8,
all(v$rank_custom == v$rank_igraph)
)
cat(sprintf(
"Validation passed\nIterations to convergence: %d\nMax |difference|, uniform restart: %.2e\nMax |difference|, business-weighted: %.2e\nMax |difference|, weighted edges: %.2e\n",
res$solution$iterations, res$max_discrepancy, res$max_discrepancy_business, res$max_discrepancy_weighted
))
```
The chunk above stops the render if any check fails, so a published page always means the checks passed. The largest difference across all `r n_assets` systems is `r sci(res$max_discrepancy * 100)` percentage points. This confirms the solver is implemented correctly. It does not show that the model is a good description of the estate; the remaining pages address that.
## Convergence
```{r power-iteration-residual-decay-plot}
#| fig-height: 5
tibble(iteration = seq_along(sol$residuals), residual = sol$residuals) |>
ggplot(aes(iteration, residual)) +
geom_hline(yintercept = res$params$pagerank$tol, linetype = "dashed", color = "#888888") +
geom_line(color = "#2a78d6", linewidth = 0.8) +
geom_point(color = "#2a78d6", size = 2) +
annotate("text", x = 1, y = res$params$pagerank$tol, label = "stopping tolerance",
hjust = 0, vjust = -0.6, size = 3.5, color = "#555555") +
scale_y_log10(labels = trans_format("log10", math_format(10^.x))) +
labs(
title = "Power iteration convergence",
subtitle = "L1 change between successive iterates, ||p(k+1) − p(k)||",
x = "Iteration",
y = "L1 change between iterates (log scale)"
)
```
**How to read the plot.** The horizontal axis counts the solver's repeated passes over the dependency map. The vertical axis shows how much the scores changed from one pass to the next, summed over all systems, on a logarithmic scale where each gridline is ten times smaller than the one above it. The dashed line marks the stopping tolerance: once the change falls below it, the solver stops.
**What the plot tells you.** The change shrinks by a steady factor at every pass, which shows as a straight downward line on this scale, and the solver stops after `r sol$iterations` passes. The plotted change is not the exact error, but the true error is at most about `r round(alpha / (1 - alpha), 1)` times the last change, which is still negligible. The scores are stable: more passes would not alter any figure on this site.