Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / thinning.R
Created September 7, 2026 04:36
Simulate Nonhomogeneous Poisson process using thinning
library(dplyr)
library(ggplot2)
library(patchwork)
get_NHPP <-function(Tmax, lambda, ...,
lambda2=NULL, maxit=10000){
if(is.null(lambda2)){
opt <- optimise(lambda, lower = 0, upper = Tmax, maximum = TRUE,
...)
lambda2 <- opt$objective
@abikoushi
abikoushi / toshinosa.R
Last active September 3, 2026 13:25
夫婦の歳の差(estatapiのデモ)
library(estatapi)
library(tidyverse)
myappId <- scan("appId.txt", what = character()) #ここには自分のアプリケーションIDを入れる
# list1 <- estat_getStatsList(appId = myappId, searchWord = "人口動態調査")
# view(list1)
dat1 <-estat_getStatsData(appId = myappId, statsDataId = "0003411856")
dat_filt <- dplyr::filter(dat1, `夫の初婚・再婚`=="夫_総数", `妻の初婚・再婚`=="妻_総数") |>
@abikoushi
abikoushi / sir_ctmc.R
Last active September 7, 2026 06:13
SIR model as continuous-time Markov chain
library(ggplot2)
library(tidyr)
library(dplyr)
library(deSolve)
sir_ctmc <- function(S0, I0, R0,
beta, gamma,
t_max = 100) {
# 初期状態
@abikoushi
abikoushi / CTMC_SIR.R
Created September 1, 2026 07:49
SIR model as continuous-time Markov chain with reward
library(ggplot2)
library(tidyr)
library(dplyr)
library(deSolve)
sir_ctmc <- function(S0, I0, R0,
beta, gamma,
t_max = 100) {
# 初期状態
@abikoushi
abikoushi / birth_death_process.R
Created August 31, 2026 23:54
Simulate birth-death process
library(ggplot2)
BirthDeathSim <-function(Ninit, mu, lambda, Tmax){
N <- rpois(1, Tmax*(mu+lambda))
CTs <- sort(runif(N, 0, Tmax))
tmp <-sample(c(1,-1), N, prob=c(lambda/(mu+lambda),mu/(mu+lambda)), replace=TRUE)
CTs <- c(0,CTs)
path <-cumsum(c(Ninit,tmp))
data.frame(time = c(CTs, Tmax), population = c(path, path[length(path)]))
}
@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)