R/tvgeom-moments.R

Defines functions tvgeom_mean tvgeom_var

Documented in tvgeom_mean tvgeom_var

#' Moments for the The Time-Varying (Right-Truncated) Geometric Distribution
#'
#' Functions to calculate first moment \code{tvgeom_mean()} and second central
#' moment \code{tvgeom_var()} for the time-varying geometric distribution.
#'
#' @param prob vector of the probability of success for each trial/time step.
#' @return \code{tvgeom_mean} returns the moment (the mean), and
#' \code{tvgeom_var} returns the second central moment (the variance).
#' @name tvgeom-moments
#' @examples
#' tvgeom_mean(prob = rep(0.1, 5))
#' tvgeom_var(prob = rep(0.1, 5))
#' @rdname tvgeom-moments
#' @export
tvgeom_mean <- function(prob) {
  sum(dtvgeom(x = 1:(length(prob) + 1), prob = prob) * c(1:(length(prob) + 1)))
}

#' @rdname tvgeom-moments
#' @export
tvgeom_var <- function(prob) {
  sum(dtvgeom(x = 1:(length(prob) + 1), prob = prob) *
        (c(1:(length(prob) + 1))^2)) - tvgeom_mean(prob)^2
}

Try the tvgeom package in your browser

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

tvgeom documentation built on Dec. 10, 2019, 5:11 p.m.