Last active
August 6, 2026 10:53
-
-
Save abikoushi/68ac7dc3355c0d6a0c9d763cffd56b17 to your computer and use it in GitHub Desktop.
A visualization of (discrete time) branching process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(dplyr) | |
| library(ggplot2) | |
| ### | |
| branching_process <- function(theta, gamma, G){ | |
| gen <- vector("list", G) | |
| K <- rpois(1, theta) | |
| id <- 1L:K | |
| parent <- 1L | |
| gen[[1]] <- data.frame(id = id, | |
| parent = parent, | |
| g = 1L) | |
| parent <- id | |
| next_id <- K | |
| for(i in 2L:G){ | |
| K <- rpois(length(parent), gamma) | |
| sumK <- sum(K) | |
| if(sumK == 0){ break } | |
| parent <- rep(parent, K) | |
| id <- (next_id+1L):(next_id+sumK) | |
| gen[[i]] <- data.frame(id = id, | |
| parent = parent, | |
| g = i) | |
| parent <- id | |
| next_id <- next_id+sumK | |
| } | |
| dplyr::bind_rows(gen) | |
| } | |
| theta <- 5 | |
| gamma <- 0.5 | |
| G <- 5 | |
| set.seed(12345) | |
| df <- branching_process(theta, gamma, G) | |
| ggplot(df)+ | |
| geom_segment(aes(yend=g, xend=id, x = parent, y=g-0.95), | |
| arrow = arrow(length = unit(0.2,"cm")))+ | |
| scale_y_reverse()+ | |
| labs(x = "node id", y = "generation")+ | |
| theme_classic() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment