Goal if this vignette is to measure the big-O complexity of calc_esses, for RBeast issue #10.

It proves that calc_esses's main speed is taken up by calc_act

library(RBeast)
library(coda)

Starting point

trees_file <- system.file(
  "extdata", "beast2_example_output.log", package = "RBeast"
)
testit::assert(file.exists(trees_file))
estimates <- parse_beast_log(
  filename = trees_file
)

knitr::kable(estimates)

Display the ESSes:

esses <- rep(NA, ncol(estimates))
burn_in_fraction <- 0.1
for (i in seq_along(estimates)) {
  # Trace with the burn-in still present
  trace_raw <- as.numeric(t(estimates[i]))

  # Trace with the burn-in removed
  trace <- remove_burn_in(trace = trace_raw, burn_in_fraction = 0.1)

  # Store the effectice sample size
  esses[i] <- calc_ess(trace, sample_interval = 1000)
}

# Note that the first value of three is nonsense:
# it is the index of the sample. I keep it in
# for simplicity of writing this code
expected_esses <- c(3, 10, 10, 10, 10, 7, 10, 9, 6)
testit::assert(all(expected_esses - esses < 0.5))

df_esses <- data.frame(esses)
rownames(df_esses) <- names(estimates)
knitr::kable(df_esses)

Measuring run-time speed:

rprof_tmp_output <- "~/tmp_RBeast_rprof"
Rprof(rprof_tmp_output)

for (i in 1:1) {
  estimates <- rbind(estimates, estimates)
}
print(nrow(estimates))

esses <- rep(NA, ncol(estimates))
burn_in_fraction <- 0.1
for (i in seq_along(estimates)) {
  # Trace with the burn-in still present
  trace_raw <- as.numeric(t(estimates[i]))

  # Trace with the burn-in removed
  trace <- remove_burn_in(trace = trace_raw, burn_in_fraction = 0.1)

  # Store the effectice sample size
  esses[i] <- calc_ess(trace, sample_interval = 1000)
}

Rprof(NULL)
summaryRprof(rprof_tmp_output)


beast-dev/RBeast documentation built on May 12, 2019, 10:02 a.m.