R/convolution.R

Defines functions convolution

Documented in convolution

#' Discrete-time convolution product of two sequences
#'
#' @description Computes the discrete-time convolution product \eqn{f * g} of
#'   two sequences \eqn{f} and \eqn{g} defined on \eqn{\mathbb{N}}{N} (Barbu and
#'   Limnios, 2008, Definition 2.2, p. 20):
#'   \deqn{(f * g)(k) = \sum_{l = 0}^{k} f(k - l)\, g(l), \quad k \in \mathbb{N}.}{(f * g)(k) = sum_{l = 0}^{k} f(k - l) g(l), k in N.}
#'
#' @details Notation (see [smmR-package] for the complete description of the
#'   models): the sequences are indexed by the time \eqn{k \in \mathbb{N}}{k in
#'   N}, and the element \eqn{l + 1} of a vector is the value at time \eqn{l}.
#'
#'   The vectors `f` and `g` contain the values of the sequences at
#'   times \eqn{0, 1, \dots, k}{0, 1, ..., k}. Only the values up to the time horizon \eqn{k}
#'   (the common length of `f` and `g` minus one) are needed to compute
#'   \eqn{(f * g)(0), \dots, (f * g)(k)}{(f * g)(0), ..., (f * g)(k)}, so that
#'   the result is exact (it is not a truncation of a longer convolution).
#'
#'   The convolution product is commutative and associative, and its neutral
#'   element is the sequence \eqn{\delta}{delta} defined by
#'   \eqn{\delta(0) = 1}{delta(0) = 1} and \eqn{\delta(k) = 0}{delta(k) = 0}
#'   for \eqn{k > 0}. If \eqn{f} and \eqn{g} are the distributions of two
#'   independent random variables with values in \eqn{\mathbb{N}}{N},
#'   \eqn{f * g} is the distribution of their sum. For matrix-valued
#'   sequences, see [matrixConvolution].
#'
#' @param f A numeric vector giving the values \eqn{f(0), \dots, f(k)}{f(0), ..., f(k)}.
#' @param g A numeric vector of the same length as `f` giving the values
#'   \eqn{g(0), \dots, g(k)}{g(0), ..., g(k)}.
#'
#' @return A numeric vector of the same length as `f` giving the values
#'   \eqn{(f * g)(0), \dots, (f * g)(k)}{(f * g)(0), ..., (f * g)(k)}.
#'
#' @references
#' V. S. Barbu, N. Limnios. (2008). Semi-Markov Chains and Hidden Semi-Markov
#' Models Toward Applications - Their Use in Reliability and DNA Analysis.
#' New York: Lecture Notes in Statistics, vol. 191, Springer.
#'
#' @seealso [matrixConvolution]
#'
#' @examples
#' # delta is the neutral element
#' convolution(f = c(0.2, 0.5, 0.3), g = c(1, 0, 0))
#'
#' # Distribution of the sum of two independent random variables
#' # with values in {0, 1, 2} (the result is given up to k = 2)
#' convolution(f = c(0.5, 0.5, 0), g = c(0.5, 0.5, 0))
#'
#' # Distribution of the sum of two sojourn times (geometric on {1, 2, ...}),
#' # i.e. of the time of the second jump of a renewal chain
#' k <- 0:20
#' f <- c(0, dgeom(k[-1] - 1, prob = 0.4))
#' f2 <- convolution(f, f)
#' all.equal(f2, c(0, 0, dnbinom(k[-(1:2)] - 2, size = 2, prob = 0.4)))
#'
#' @export
#'
convolution <- function(f, g) {
  #############################
  # Checking parameter f
  #############################
  
  if (!(is.numeric(f) & !anyNA(f) & is.vector(f))) {
    stop("'f' is not a numeric vector")
  }
  
  #############################
  # Checking parameter g
  #############################
  
  if (!(is.numeric(g) & !anyNA(g) & is.vector(g))) {
    stop("'g' is not a numeric vector")
  }
  
  if (!(length(f) == length(g))) {
    stop("'f' and 'g' must have the same length")
  }
  
  conv <- as.vector(C_convolution(f, g))
  
  return(conv)
}

Try the smmR package in your browser

Any scripts or data that you put into this service are public.

smmR documentation built on Nov. 28, 2026, 1:06 a.m.