R/borel.r

Defines functions rborel dborel

Documented in dborel rborel

##' Density of the Borel distribution
##'
##' @param x vector of quantiles; integer.
##' @param mu mu parameter (the poisson mean); non-negative.
##' @param log logical; if TRUE, probabilities p are given as log(p).
##' @return probability mass.
##' @author Sebastian Funk
dborel <- function(x, mu, log = FALSE) {
  checkmate::assert_numeric(
    x, lower = 1, upper = Inf
  )
  checkmate::assert_number(
    mu, lower = 0, finite = TRUE, na.ok = FALSE
  )
  ld <- -mu * x + (x - 1) * log(mu * x) - lgamma(x + 1)
  if (!log) ld <- exp(ld)
  return(ld)
}

##' Random number generator from the Borel distribution
##'
##' Random numbers are generated by simulating from a Poisson branching process
##' @param n number of random variates to generate.
##' @param mu mu parameter (the Poisson mean).
##' @param infinite any number to treat as infinite; simulations will be stopped
##'     if this number is reached
##' @return vector of random numbers
##' @author Sebastian Funk
rborel <- function(n, mu, infinite = Inf) {
  checkmate::assert_number(
    n, lower = 1, finite = TRUE, na.ok = FALSE
  )
  checkmate::assert_number(
    mu, lower = 0, finite = TRUE, na.ok = FALSE
  )
  chain_sim(n, "pois", "size", infinite = infinite, lambda = mu)
}
sbfnk/bpmodels documentation built on Nov. 7, 2023, 5:16 a.m.