## ----include = FALSE----------------------------------------------------------
set.seed(20260828)
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(nhppp)
K <- 10^5

## ----fixture------------------------------------------------------------------
rate <- c(rep(0.25, K - 100), rep(10, 100))
lambda_matrix <- matrix(rep(rate, 5), ncol = 5)
time_breaks <- seq(0, 4, length.out = 6)

## ----dense--------------------------------------------------------------------
Z_dense <- vdraw_sc_step(
  lambda_matrix = lambda_matrix,
  time_breaks = time_breaks
)
dim(Z_dense)
mean(is.na(Z_dense)) # fraction of the matrix that is padding

## ----long---------------------------------------------------------------------
Z_long <- vdraw_sc_step(
  lambda_matrix = lambda_matrix,
  time_breaks = time_breaks,
  output = "long"
)
str(Z_long)

## ----no-events----------------------------------------------------------------
counts <- tabulate(Z_long$id, nbins = Z_long$n_draws)
sum(counts == 0) # processes with no events: present in n_draws, absent in id

## ----data-table, eval = requireNamespace("data.table", quietly = TRUE)--------
events <- data.table::data.table(id = Z_long$id, time = Z_long$time)
events[c(1, .N)]

## ----same-seed----------------------------------------------------------------
set.seed(1)
Zd <- vdraw_sc_step(
  lambda_matrix = lambda_matrix[1:1000, ], time_breaks = time_breaks
)
set.seed(1)
Zl <- vdraw_sc_step(
  lambda_matrix = lambda_matrix[1:1000, ], time_breaks = time_breaks,
  output = "long"
)
# flatten the dense matrix by row (the long format's grouping) and compare
dense_events <- as.vector(t(Zd))
identical(Zl$time, dense_events[!is.na(dense_events)])
identical(tabulate(Zl$id, Zl$n_draws), as.integer(rowSums(!is.na(Zd))))

## ----memory-------------------------------------------------------------------
mem_case <- function(rate_vector, label) {
  lm <- matrix(rep(rate_vector, 5), ncol = 5)
  Zd <- vdraw_sc_step(lambda_matrix = lm, time_breaks = time_breaks)
  Zl <- vdraw_sc_step(lambda_matrix = lm, time_breaks = time_breaks, output = "long")
  data.frame(
    population = label,
    max_events = ncol(Zd),
    dense_MB = round(as.numeric(object.size(Zd)) / 2^20, 1),
    long_MB = round(as.numeric(object.size(Zl)) / 2^20, 1)
  )
}
# equal expected totals (~1.25 e5 events), increasingly concentrated
homogeneous <- rep(0.3125, K)
mild <- c(rep(0.25, K - 5000), rep(1.5, 5000))
extreme <- c(rep(0.25, K - 100), rep(10, 100))
tab <- rbind(
  mem_case(homogeneous, "homogeneous"),
  mem_case(mild, "mildly heterogeneous"),
  mem_case(extreme, "extremely heterogeneous")
)
knitr::kable(tab)

## ----speed--------------------------------------------------------------------
time_case <- function(rate_vector, label, n_rep = 3) {
  lm <- matrix(rep(rate_vector, 5), ncol = 5)
  td <- system.time(for (i in seq_len(n_rep)) {
    vdraw_sc_step(lambda_matrix = lm, time_breaks = time_breaks)
  })["elapsed"]
  tl <- system.time(for (i in seq_len(n_rep)) {
    vdraw_sc_step(lambda_matrix = lm, time_breaks = time_breaks, output = "long")
  })["elapsed"]
  data.frame(
    population = label,
    dense_ms = round(1000 * td / n_rep),
    long_ms = round(1000 * tl / n_rep)
  )
}
knitr::kable(rbind(
  time_case(homogeneous, "homogeneous"),
  time_case(mild, "mildly heterogeneous"),
  time_case(extreme, "extremely heterogeneous")
))

## ----conditioned--------------------------------------------------------------
Z <- vdraw_sc_step(
  lambda_matrix = lambda_matrix[1:1000, ],
  time_breaks = time_breaks,
  generate_at_least_K = 2,
  output = "long"
)
min(tabulate(Z$id, Z$n_draws)) # every process has at least 2 events

## ----conditioned-upper--------------------------------------------------------
Z <- vdraw_sc_step(
  lambda_matrix = lambda_matrix[1:1000, ],
  time_breaks = time_breaks,
  generate_at_most_K = 1,
  output = "long"
)
table(tabulate(Z$id, Z$n_draws))

