How to rerun Multi-Stage Attack Graph Simulation Via Markov Chains

Every number on this site is computed at render time from the parameters in params.yaml. No data are downloaded or read from elsewhere, every random draw is seeded, and package versions are pinned with renv, so rendering the same commit gives the same results. This page explains how to rerun the analysis, how the scripts fit together, what the tests check, and records the software environment.

Quick start

  1. The code and data are in the GitHub repository patrick-lefler/attack-graph-simulation-via-markov-chains. You need R 4.3 or later and Quarto 1.5 or later. To rebuild the results, run the tests and render the site from a clean copy:

  2. Restore the packages with renv::restore() (see Software environment).

  3. Rebuild everything from the project root:

Rscript run_all.R

run_all.R checks that the installed packages match renv.lock, runs the tests, and then re-executes and renders every page into docs/. It stops at the first problem. To run the analysis in an R console without rendering, source the scripts in order from the project root:

source("R/00_setup.R")
source("R/01_model.R")
source("R/02_robustness.R")

Each script depends on objects and packages from the one before it, so sourcing a later script on its own in a fresh session fails. The full analysis takes about 20 seconds.

Pipeline

File What it does Main outputs
params.yaml Holds every parameter: the two transition matrices, the step length, the Dirichlet settings, perturbation sizes, simulation sizes and the seed params
R/00_setup.R Loads packages and params.yaml, sources R/functions.R, sets the plot theme and the global seed, defines percentage-formatting helpers params, pct1(), pp1()
R/functions.R Defines every function: the closed-form solver, the jump chain, the outcome distribution by step, the one-row perturbations, the Dirichlet and paired draws, and the simulated walk solve_absorbing_markov(), draw_P() and others
R/01_model.R Builds the stages and both matrices from params.yaml, solves the chain in closed form, checks the jump-chain invariance, computes the time-to-outcome distributions P_baseline_raw, P_hardened_raw, base_solution, hard_solution, res, pmf_b, floor_cat_h
R/02_robustness.R One-row sensitivity and its identity check, Dirichlet parameter uncertainty (concentration sweep, correlated detection, paired comparison, ranking robustness) and the Monte Carlo check of the solver sens_df, identity_df, unc_summary, alpha_sweep, paired_summary, sim_check
tests/testthat/ Unit tests for the functions and the model (see Tests) Test results
run_all.R Checks packages, runs the tests, renders every page The site in docs/

Each page sources the three numbered scripts in a hidden first chunk, so every page works from the same objects. The Model and Robustness pages display each function next to the code that uses it. The scripts also contain stopifnot() checks: every probability row must sum to 1, the closed form must match the jump chain and the Monte Carlo run, and numbers quoted as fixed text must match the model. A failed check stops the render.

Data

The model uses no external data. Both transition matrices are expert-elicited, illustrative values in params.yaml; they are not calibrated to any benchmark (see Parameterization and Assumptions). The figures from published reports quoted in the text (Mandiant M-Trends, Sophos, CrowdStrike) are cited for comparison only and do not enter any calculation.

Because the inputs are the parameter file and the code, the checksums below identify the exact files used in this render. A different checksum means a different model or analysis.

Display code
input_files <- c(
  "params.yaml",
  "R/00_setup.R",
  "R/functions.R",
  "R/01_model.R",
  "R/02_robustness.R"
)

tibble(
  File = input_files,
  `Size (KB)` = file.size(input_files) / 1e3,
  MD5 = unname(tools::md5sum(input_files))
) |>
  gt() |>
  tab_header(
    title = md("**Parameter and script files used in this render**"),
    subtitle = paste0("Rendered ", Sys.Date())
  ) |>
  fmt_number(columns = `Size (KB)`, decimals = 1) |>
  cols_align(align = "left", columns = c(File, MD5))
Parameter and script files used in this render
Rendered 2026-11-20
File Size (KB) MD5
params.yaml 4.0 616b1152e08d435beea00ad91cbd635a
R/00_setup.R 0.7 43df2ce96989790dd14fceab6b446309
R/functions.R 4.6 36c0626808deed2e671e80803184712e
R/01_model.R 4.4 9eb7e34bbf980fdd8e66dd7b4de36dec
R/02_robustness.R 7.0 0f36c796743b5cce9f3236594209d633

How to read the table. Each row is one of the files that define the model and run every analysis on this site: the parameter file and the four R scripts. The size is in kilobytes, and the MD5 column is a checksum: a short fingerprint computed from the file’s exact contents. Changing even one character, such as a single transition probability, produces a completely different fingerprint. The subtitle gives the date this page was rendered.

What the table tells you. Anyone rerunning the analysis can compute the same checksums on their copies of these files. If every fingerprint matches, they are running the identical model, with the same transition probabilities and seeds, and with the package versions pinned in renv.lock they should get identical results. A mismatch means a file has changed, so any difference in the results should be traced to that file first.

Parameters and seeds

All parameters live in params.yaml. Each is labelled as an assumption (an illustrative or elicited value with no empirical source) or a design choice (a simulation or perturbation size that affects precision or framing, not the model), with the range the analysis explores. Several numbers derived from these values are quoted as fixed text on the site, and stopifnot() guards in R/01_model.R stop the render if a parameter change breaks one, so the text must be updated along with the value.

Every random stream is seeded explicitly with the seed value (42): once in R/00_setup.R and again at the start of each random section of R/02_robustness.R. Each section therefore gives the same draws regardless of which sections run before it. The closed-form solver itself is deterministic. The full file:

#> # Every model and analysis parameter for this project.
#> # type: assumption = an illustrative or elicited value with no empirical source;
#> #       design     = an analysis choice (simulation size, perturbation size) that
#> #                    affects precision or framing, not the model itself.
#> # range: the values the analysis actually explores, or "not varied".
#> #
#> # Several numbers derived from these values are quoted as fixed text on the site.
#> # stopifnot() guards in R/01_model.R stop the render if a change here breaks one;
#> # update the text along with the guard.
#> 
#> seed:
#>   value: 42
#>   type: design
#>   note: Set before every random block, so results do not depend on execution order.
#> 
#> states:
#>   transient:
#>     - "S1: Recon & Phishing"
#>     - "S2: Initial Foothold"
#>     - "S3: Privilege Escalation"
#>     - "S4: Lateral Movement"
#>     - "S5: Domain Dominance"
#>     - "S6: Payload Staging"
#>   absorbing:
#>     - "A1: Contained / Terminated"
#>     - "A2: Data Exfiltration"
#>     - "A3: Enterprise Ransomware"
#> 
#> step_hours:
#>   value: 4
#>   type: assumption
#>   range: not varied
#>   note: >
#>     Mean duration of one step, the same for every stage. The per-step probabilities
#>     only have meaning for this step length, so changing it requires re-eliciting
#>     the transition matrices.
#> 
#> transition_matrices:
#>   type: assumption
#>   range: >
#>     Uncertainty around each row is represented by a Dirichlet(alpha * row)
#>     distribution; see dirichlet.alpha_conc.
#>   note: >
#>     Expert-elicited, illustrative values, not calibrated to any benchmark or
#>     telemetry. Each transient row lists the per-step probability of moving to
#>     S1..S6, A1..A3, in that order, and must sum to 1. Absorbing rows are the
#>     identity and are not listed.
#>   baseline:
#>     S1: [0.10, 0.65, 0.00, 0.00, 0.00, 0.00, 0.25, 0.00, 0.00]
#>     S2: [0.00, 0.15, 0.55, 0.05, 0.00, 0.00, 0.25, 0.00, 0.00]
#>     S3: [0.00, 0.00, 0.10, 0.60, 0.10, 0.00, 0.20, 0.00, 0.00]
#>     S4: [0.00, 0.00, 0.05, 0.15, 0.55, 0.10, 0.15, 0.00, 0.00]
#>     S5: [0.00, 0.00, 0.00, 0.00, 0.10, 0.70, 0.10, 0.05, 0.05]
#>     S6: [0.00, 0.00, 0.00, 0.00, 0.00, 0.05, 0.10, 0.40, 0.45]
#>   improved:
#>     # A hand-set scenario: A1 probabilities rise 2.0x to 3.5x by stage. Not derived
#>     # from any specific control.
#>     S1: [0.05, 0.40, 0.00, 0.00, 0.00, 0.00, 0.55, 0.00, 0.00]
#>     S2: [0.00, 0.10, 0.35, 0.05, 0.00, 0.00, 0.50, 0.00, 0.00]
#>     S3: [0.00, 0.00, 0.05, 0.40, 0.05, 0.00, 0.50, 0.00, 0.00]
#>     S4: [0.00, 0.00, 0.02, 0.10, 0.40, 0.08, 0.40, 0.00, 0.00]
#>     S5: [0.00, 0.00, 0.00, 0.00, 0.05, 0.50, 0.35, 0.05, 0.05]
#>     S6: [0.00, 0.00, 0.00, 0.00, 0.00, 0.05, 0.35, 0.30, 0.30]
#> 
#> sensitivity:
#>   shift_delta:
#>     value: 0.05
#>     type: design
#>     range: not varied
#>     note: Probability moved from a row's largest non-self, non-A1 edge to A1.
#>   prop_rel:
#>     value: 0.10
#>     type: design
#>     range: not varied
#>     note: Relative increase in a row's A1 probability, taken pro rata from the other exits.
#> 
#> dirichlet:
#>   alpha_conc:
#>     value: 50
#>     type: assumption
#>     range: [10, 200]
#>     note: >
#>       Concentration standing in for "moderate confidence" in the elicited rows.
#>       The alpha sweep shows it changes where the tipping point falls.
#>   alpha_grid:
#>     value: [10, 20, 50, 200]
#>     type: design
#>     note: Concentrations compared in the alpha sweep.
#>   n_draws:
#>     value: 5000
#>     type: design
#>     note: Draws per regime for the main uncertainty, correlated-detection and paired analyses.
#>   n_sweep:
#>     value: 2000
#>     type: design
#>     note: Draws per alpha in the sweep.
#>   n_rank:
#>     value: 2000
#>     type: design
#>     note: Draws for the one-row ranking robustness check.
#>   shared_sd_log:
#>     value: 0.35
#>     type: assumption
#>     range: not varied
#>     note: >
#>       Log-scale SD of the common factor applied to every row's A1 probability,
#>       representing detection quality that is correlated across stages.
#> 
#> simulation:
#>   n_sim:
#>     value: 20000
#>     type: design
#>     note: Monte Carlo walks from S1 for the solver correctness check.

Tests

Unit tests in tests/testthat/ cover the functions and the model. They load only the parameters, the functions and the closed-form model, so they run in a few seconds. Run them with Rscript -e "testthat::test_dir('tests/testthat')"; run_all.R runs them before rendering. The table below shows the results of running the tests during this render.

Display code
test_results <- testthat::test_dir("tests/testthat", reporter = "silent", stop_on_failure = FALSE) |>
  as_tibble()

test_results |>
  transmute(
    File = str_remove(file, "^test-") |> str_remove("\\.R$"),
    Test = test,
    Checks = nb,
    Result = if_else(failed == 0 & !error & skipped == FALSE, "Pass", "Fail")
  ) |>
  gt(groupname_col = "File") |>
  tab_header(
    title = md("**Unit test results for this render**"),
    subtitle = sprintf("%d tests, %d individual checks", nrow(test_results), sum(test_results$nb))
  ) |>
  cols_align(align = "left", columns = Test) |>
  tab_options(table.font.size = 11, row_group.font.weight = "bold")
Unit test results for this render
22 tests, 111 individual checks
Test Checks Result
model
absorption probabilities sum to 1 from every stage 2 Pass
B is unchanged by removing self-loops (jump-chain invariance) 2 Pass
the outcome distribution by step matches the closed form 3 Pass
no catastrophic outcome is possible before four steps from S1 2 Pass
the improved posture raises containment from every stage 1 Pass
headline values quoted in the text are reproduced 7 Pass
params
every transient row is a probability distribution 4 Pass
absorbing rows are the identity 2 Pass
matrices keep the documented structure 6 Pass
matrix_from_params rejects malformed rows 1 Pass
scalar parameters carry a type label and plausible values 24 Pass
robustness-functions
the standardized shift moves exactly delta to A1 and keeps the row summing to 1 18 Pass
the proportional change raises A1 by the stated share and keeps zeros at zero 18 Pass
Dirichlet draws keep structural zeros and valid rows 3 Pass
a higher concentration keeps draws closer to the elicited matrix 1 Pass
paired draws use common random numbers 1 Pass
containment from S1 rises when any row is shifted toward A1 7 Pass
a simulated walk always ends in an absorbing outcome 2 Pass
solver
the solver reproduces hand-computed N, B and expected steps 3 Pass
conditional time to catastrophe matches the hand calculation 1 Pass
hours scale with the step durations h 1 Pass
the solver refuses a chain with a trapped transient state 2 Pass

How to read the table. Each row is one test, grouped by the file it lives in: parameters, solver, model, and the functions used in the robustness analysis. The Checks column counts the individual comparisons inside the test, and Result shows whether every one of them passed. The solver tests compare the code against a two-stage chain small enough to solve by hand, so they check the arithmetic against an independent answer.

What the table tells you. Every test passed in this render. That means the solver reproduces hand-calculated answers, the matrices have the documented structure, and the headline numbers quoted in the text are what the model produces. The tests check that the code does what it describes. They cannot check whether the illustrative transition probabilities are realistic. If a later change breaks one of these properties, run_all.R stops before rendering any page, and the failing row names the test.

Caching

  • No results cache. The full analysis runs in about 20 seconds, so every page recomputes it when rendered.
  • Quarto freeze (_freeze/). _quarto.yml sets freeze: auto, so a full quarto render re-executes a page only when that page’s .qmd file changes. Edits to params.yaml or the R/ scripts do not count as a change. After editing either, run Rscript run_all.R, which renders each page explicitly, because Quarto always executes a page that is rendered on its own.

Licenses

The code on this site is released under the MIT License. The site contains no third-party data. Figures quoted from published reports are cited with links to their sources, and those reports keep their own terms.

Software environment

The site was built with R and Quarto. The analysis loads tidyverse, scales, patchwork and gt, and also calls yaml, knitr, testthat, withr, renv and sessioninfo. Package versions are pinned in renv.lock; run renv::restore() to install them.

#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.5.2 (2025-10-31)
#>  os       macOS Tahoe 26.6.2
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/New_York
#>  date     2026-11-20
#>  pandoc   3.10 @ /Applications/Positron.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
#>  quarto   1.8.26 @ /Applications/quarto/bin/quarto
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  ! package      * version date (UTC) lib source
#>  P base64enc      0.1-6   2026-02-02 [?] CRAN (R 4.5.2)
#>  P brio           1.1.5   2024-04-24 [?] CRAN (R 4.5.0)
#>  P cli            3.6.6   2026-04-09 [?] CRAN (R 4.5.2)
#>  P commonmark     2.0.0   2025-07-07 [?] CRAN (R 4.5.0)
#>  P desc           1.4.3   2023-12-10 [?] CRAN (R 4.5.0)
#>  P digest         0.6.39  2025-11-19 [?] CRAN (R 4.5.2)
#>  P dplyr        * 1.2.1   2026-04-03 [?] CRAN (R 4.5.2)
#>  P evaluate       1.0.5   2025-08-27 [?] CRAN (R 4.5.0)
#>  P farver         2.1.2   2024-05-13 [?] CRAN (R 4.5.0)
#>  P fastmap        1.2.0   2024-05-15 [?] CRAN (R 4.5.0)
#>  P forcats      * 1.0.1   2025-09-25 [?] CRAN (R 4.5.0)
#>  P fs             2.1.0   2026-04-18 [?] CRAN (R 4.5.2)
#>  P generics       0.1.4   2025-05-09 [?] CRAN (R 4.5.0)
#>  P ggplot2      * 4.0.3   2026-04-22 [?] CRAN (R 4.5.2)
#>  P glue           1.8.1   2026-04-17 [?] CRAN (R 4.5.2)
#>  P gt           * 1.3.0   2026-01-22 [?] CRAN (R 4.5.2)
#>  P gtable         0.3.6   2024-10-25 [?] CRAN (R 4.5.0)
#>  P hms            1.1.4   2025-10-17 [?] CRAN (R 4.5.0)
#>  P htmltools      0.5.9   2025-12-04 [?] CRAN (R 4.5.2)
#>  P htmlwidgets    1.6.4   2023-12-06 [?] CRAN (R 4.5.0)
#>  P jsonlite       2.0.0   2025-03-27 [?] CRAN (R 4.5.0)
#>  P knitr          1.50    2025-03-16 [?] CRAN (R 4.5.0)
#>  P lifecycle      1.0.5   2026-01-08 [?] CRAN (R 4.5.2)
#>  P litedown       0.8     2025-11-02 [?] CRAN (R 4.5.0)
#>  P lubridate    * 1.9.4   2024-12-08 [?] CRAN (R 4.5.0)
#>  P magrittr       2.0.5   2026-04-04 [?] CRAN (R 4.5.2)
#>  P markdown       2.0     2025-03-23 [?] CRAN (R 4.5.0)
#>  P otel           0.2.0   2025-08-29 [?] CRAN (R 4.5.0)
#>  P patchwork    * 1.3.2   2025-08-25 [?] CRAN (R 4.5.0)
#>  P pillar         1.11.1  2025-09-17 [?] CRAN (R 4.5.0)
#>  P pkgconfig      2.0.3   2019-09-22 [?] CRAN (R 4.5.0)
#>  P pkgload        1.5.3   2026-06-15 [?] CRAN (R 4.5.2)
#>  P purrr        * 1.2.2   2026-04-10 [?] CRAN (R 4.5.2)
#>  P R6             2.6.1   2025-02-15 [?] CRAN (R 4.5.0)
#>  P RColorBrewer   1.1-3   2022-04-03 [?] CRAN (R 4.5.0)
#>  P readr        * 2.1.5   2024-01-10 [?] CRAN (R 4.5.0)
#>    renv           1.1.5   2025-07-24 [1] CRAN (R 4.5.0)
#>  P rlang          1.3.0   2026-07-05 [?] CRAN (R 4.5.2)
#>  P rmarkdown      2.30    2025-09-28 [?] CRAN (R 4.5.0)
#>  P rprojroot      2.1.1   2025-08-26 [?] CRAN (R 4.5.0)
#>  P rstudioapi     0.19.0  2026-06-11 [?] CRAN (R 4.5.2)
#>  P S7             0.2.2   2026-04-22 [?] CRAN (R 4.5.2)
#>  P sass           0.4.10  2025-04-11 [?] CRAN (R 4.5.0)
#>  P scales       * 1.4.0   2025-04-24 [?] CRAN (R 4.5.0)
#>  P sessioninfo    1.2.3   2025-02-05 [?] CRAN (R 4.5.2)
#>  P stringi        1.8.7   2025-03-27 [?] CRAN (R 4.5.2)
#>  P stringr      * 1.6.0   2025-11-04 [?] CRAN (R 4.5.0)
#>  P testthat     * 3.3.2   2026-01-11 [?] CRAN (R 4.5.2)
#>  P tibble       * 3.3.1   2026-01-11 [?] CRAN (R 4.5.2)
#>  P tidyr        * 1.3.1   2024-01-24 [?] CRAN (R 4.5.0)
#>  P tidyselect     1.2.1   2024-03-11 [?] CRAN (R 4.5.0)
#>  P tidyverse    * 2.0.0   2023-02-22 [?] CRAN (R 4.5.0)
#>  P timechange     0.3.0   2024-01-18 [?] CRAN (R 4.5.0)
#>  P tzdb           0.5.0   2025-03-15 [?] CRAN (R 4.5.0)
#>  P vctrs          0.7.3   2026-04-11 [?] CRAN (R 4.5.2)
#>  P waldo          0.6.2   2025-07-11 [?] CRAN (R 4.5.0)
#>  P withr          3.0.3   2026-06-19 [?] CRAN (R 4.5.2)
#>  P xfun           0.54    2025-10-30 [?] CRAN (R 4.5.0)
#>  P xml2           1.4.1   2025-10-27 [?] CRAN (R 4.5.0)
#>  P yaml           2.3.10  2024-07-26 [?] CRAN (R 4.5.0)
#> 
#>  [1] /Users/patricklefler/Claude/r/2026-08-27_rQuarto_attack-graph-simulation-via-markov-chains/renv/library/macos/R-4.5/aarch64-apple-darwin20
#>  [2] /Users/patricklefler/Library/Caches/org.R-project.R/R/renv/sandbox/macos/R-4.5/aarch64-apple-darwin20/4cd76b74
#> 
#>  * ── Packages attached to the search path.
#>  P ── Loaded and on-disk path mismatch.
#> 
#> ──────────────────────────────────────────────────────────────────────────────