Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / treeplot_branchingprocess.R
Created August 20, 2026 23:47
Visualization of continuous-time Markov branching process with death
library(ggplot2)
library(dplyr)
simulate_branching_death <- function(alpha,
beta,
Tmax) {
death_time <- rexp(1,beta)
death <- min(Tmax, death_time)
N0 <- rpois(1, death * alpha)
@abikoushi
abikoushi / RHawkes.R
Last active August 20, 2026 05:13
try package RHawkes
library(RHawkes)
simulate_branching_death <- function(mu,
alpha,
beta,
Tmax) {
N0 <- rpois(1, Tmax * mu)
if(N0==0L){
return(
@abikoushi
abikoushi / simulate_branching_death.R
Last active August 18, 2026 13:15
Visualization continuous-time Markov branching process with death
library(dplyr)
library(ggplot2)
simulate_branching_death <- function(mu,
lambda,
beta,
Tmax) {
N0 <- rpois(1, Tmax * mu)
t0 <- sort(runif(N0, 0, Tmax))
@abikoushi
abikoushi / continuoustime_markov_branching_process.R
Last active August 18, 2026 04:42
Simulate continuous-time Markov branching process
library(ggplot2)
library(dplyr)
branching_process <- function(mu, lambda, Tmax, G){
gen <- vector("list", G)
X <- rpois(1, mu*Tmax) #第1世代
if(X>0){
id <- 1L:X
parent <- 0L
birth_time <- sort(runif(X, 0, Tmax))
@abikoushi
abikoushi / simulate_multitype_gillespie.R
Created August 17, 2026 05:18
Simulate multi-type continuous-time Markov branching process
library(dplyr)
library(ggplot2)
simulate_multitype_gillespie <- function(
R,
Tmax,
initial_type = 1L
) {
K <- nrow(R)
@abikoushi
abikoushi / sim_Hawkes_MLE.R
Created August 17, 2026 02:18
A simulation of maximum likelihood estimation of Hawkes process
library(hawkes)
mu <- 0.5
a <- 0.7
b <- 1.0
Tau <- 100
set.seed(12345)
dat <- simulateHawkes(
lambda0 = mu,
alpha = a*b,
beta = b,
@abikoushi
abikoushi / multitype_branching_process.R
Created August 14, 2026 22:29
A visualization of multi-type discrete-time branching process
library(ggplot2)
library(dplyr)
multitype_branching_process <- function(M, G, initial_type = 1L) {
# M[i, j] = type i の個体1個体あたりの
# type j の平均子孫数
K <- nrow(M)
if (ncol(M) != K) {
stop("M must be a square matrix.")
}
if (any(M < 0)) {
@abikoushi
abikoushi / branching_process.R
Last active August 5, 2026 10:36
Visualization of continuous-time branching processes
library(dplyr)
library(ggplot2)
simulate_branching <- function(mu,
lambda,
Tmax) {
N0 <- rpois(1, Tmax*mu)
t <- sort(runif(N0, 0, Tmax))
individuals <- data.frame(
@abikoushi
abikoushi / branching_process.R
Last active August 6, 2026 10:53
A visualization of (discrete time) branching process
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,
@abikoushi
abikoushi / NGS.R
Created July 23, 2026 01:48
numerical example of next generation matrix
iter_nextstep <- function(n, K){
I <- c(1, 0)
I_hist <- matrix(0, nrow = 2, ncol = n+1)
I_hist[, 1] <- I
for(i in 1:n){
I <- K%*%I
I_hist[,i+1] <- I
}
return(t(I_hist))
}