R/plcon.R

Defines functions rplcon pplcon dplcon

Documented in dplcon pplcon rplcon

#' @title The continuous power-law distribution
#'
#' @description Density and distribution function of the continuous power-law distribution, with
#' parameters xmin and alpha.
#' @inheritParams ppldis
#' @param x,q vector of quantiles. The discrete
#' power-law distribution is defined for x > xmin
#' @return dplcon gives the denisty and pplcon gives the distribution function.
#' @export
#' @examples
#' xmin = 1; alpha = 1.5
#' x = seq(xmin, 10, length.out=1000)
#' plot(x, dplcon(x, xmin, alpha), type="l")
dplcon = function(x, xmin, alpha, log = FALSE) {
  if (log) {
    pdf = log(alpha - 1) - log(xmin) - alpha * (log(x / xmin))
    pdf[x < xmin] = -Inf
  } else {
    pdf = (alpha - 1) / xmin * (x / xmin) ^ (-alpha)
    pdf[x < xmin] = 0
  }
  pdf
}


#'@rdname dplcon
#'@export
#'@examples
#' plot(x, pplcon(x, xmin, alpha), type="l", main="Distribution function")
pplcon = function(q, xmin, alpha, lower.tail = TRUE) {
  cdf = 1 - (q / xmin) ^ (-alpha + 1)
  if (!lower.tail)
    cdf = 1 - cdf
  cdf[q < round(xmin)] = 0
  cdf
}


#'@rdname dplcon
#'@note The discrete random number generator is very inefficient
#'@export
#'@examples
#' n = 1000
#' con_rns = rplcon(n, xmin, alpha)
#' con_rns = sort(con_rns)
#' p = rep(1/n, n)
#' #Zipfs plot
#' plot(con_rns, rev(cumsum(p)), log="xy", type="l")
rplcon = function(n, xmin, alpha) {
  xmin * (1 - runif(n)) ^ (-1 / (alpha - 1))
}

Try the poweRlaw package in your browser

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

poweRlaw documentation built on April 25, 2020, 9:06 a.m.