├── .Rbuildignore ├── .github └── workflows │ └── main.yml ├── .gitignore ├── DESCRIPTION ├── Justfile ├── NAMESPACE ├── NEWS.md ├── R ├── RcppExports.R ├── bernoulli-distribution.R ├── beta-binomial-distribution.R ├── beta-negative-binomial-distribution.R ├── beta-prime-distribution.R ├── bhattacharjee-distribution.R ├── birnbaum-saunders-distribution.R ├── bivariate-normal-distribution.R ├── bivariate-poisson-distribution.R ├── categorical-distribution.R ├── dirichlet-distribution.R ├── dirichlet-multinomial-distribution.R ├── discrete-gamma-distribution.R ├── discrete-laplace-distribution.R ├── discrete-normal-distribution.R ├── discrete-uniform-distribution.R ├── discrete-weibull-distribution.R ├── extraDistr-deprecated.R ├── extraDistr-package.R ├── frechet-distribution.R ├── gamma-poisson-distribution.R ├── gev-distribution.R ├── gompertz-distribution.R ├── gpd-distribution.R ├── gumbel-distribution.R ├── half-cauchy-distribution.R ├── half-normal-distribution.R ├── half-t-distribution.R ├── huber-distribution.R ├── inverse-chi-squared-distribution.R ├── inverse-gamma-distribution.R ├── kumaraswamy-distribution.R ├── laplace-distribution.R ├── location-scale-t-distribution.R ├── logarithmic-series-distribution.R ├── lomax-distribution.R ├── mixture-of-normal-distributions.R ├── mixture-of-poisson-distributions.R ├── multinomial-distribution.R ├── multivariate-hypergeometric-distribution.R ├── negative-hypergeometric-distribution.R ├── non-standard-beta-distribution.R ├── pareto-distribution.R ├── power-distribution.R ├── proportion-distribution.R ├── rademacher-distribution.R ├── rayleigh-distribution.R ├── shifted-gompertz-distribution.R ├── skellam-distribution.R ├── slash-distribution.R ├── triangular-distribution.R ├── truncated-binomial-distribution.R ├── truncated-normal-distribution.R ├── truncated-poisson-distributtion.R ├── tukey-lambda-distribution.R ├── wald-distribution.R ├── zero-inflated-binomial-distribution.R ├── zero-inflated-negative-binomial-distribution.R ├── zero-inflated-poisson-distribution.R └── zzz.R ├── README.md ├── appveyor.yml ├── cran-comments.md ├── extraDistr.Rproj ├── inst └── include │ ├── extraDistr.h │ └── extraDistr_RcppExports.h ├── man ├── Bernoulli.Rd ├── BetaBinom.Rd ├── BetaNegBinom.Rd ├── BetaPrime.Rd ├── Bhattacharjee.Rd ├── BirnbaumSaunders.Rd ├── BivNormal.Rd ├── BivPoiss.Rd ├── Categorical.Rd ├── DirMnom.Rd ├── Dirichlet.Rd ├── DiscreteGamma.Rd ├── DiscreteLaplace.Rd ├── DiscreteNormal.Rd ├── DiscreteUniform.Rd ├── DiscreteWeibull.Rd ├── Frechet.Rd ├── GEV.Rd ├── GPD.Rd ├── GammaPoiss.Rd ├── Gompertz.Rd ├── Gumbel.Rd ├── HalfCauchy.Rd ├── HalfNormal.Rd ├── HalfT.Rd ├── Huber.Rd ├── InvChiSq.Rd ├── InvGamma.Rd ├── Kumaraswamy.Rd ├── Laplace.Rd ├── LocationScaleT.Rd ├── LogSeries.Rd ├── Lomax.Rd ├── MultiHypergeometric.Rd ├── Multinomial.Rd ├── NSBeta.Rd ├── NegHyper.Rd ├── NormalMix.Rd ├── Pareto.Rd ├── PoissonMix.Rd ├── PowerDist.Rd ├── PropBeta.Rd ├── Rademacher.Rd ├── Rayleigh.Rd ├── ShiftGomp.Rd ├── Skellam.Rd ├── Slash.Rd ├── Triangular.Rd ├── TruncBinom.Rd ├── TruncNormal.Rd ├── TruncPoisson.Rd ├── TukeyLambda.Rd ├── Wald.Rd ├── ZIB.Rd ├── ZINB.Rd ├── ZIP.Rd └── extraDistr-package.Rd ├── misc ├── benchmarks.R └── interactive-visualization.R ├── src ├── RcppExports.cpp ├── bernoulli-distribution.cpp ├── beta-binomial-distribution.cpp ├── beta-negative-binomial-distribution.cpp ├── beta-prime-distribution.cpp ├── bhattacharjee-distribution.cpp ├── birnbaum-saunders-distribution.cpp ├── bivariate-normal-distribution.cpp ├── bivariate-poisson-distribution.cpp ├── categorical-distribution-lp.cpp ├── categorical-distribution.cpp ├── dirichlet-distribution.cpp ├── dirichlet-multinomial-distribution.cpp ├── discrete-gamma-distribution.cpp ├── discrete-laplace-distribution.cpp ├── discrete-normal-distribution.cpp ├── discrete-uniform-distribution.cpp ├── discrete-weibull-distribution.cpp ├── frechet-distribution.cpp ├── gamma-poisson-distribution.cpp ├── gev-distribution.cpp ├── gompertz-distribution.cpp ├── gpd-distribution.cpp ├── gumbel-distribution.cpp ├── half-cauchy-distribution.cpp ├── half-normal-distribution.cpp ├── half-t-distribution.cpp ├── huber-distribution.cpp ├── inverse-gamma-distribution.cpp ├── kumaraswamy-distribution.cpp ├── laplace-distribution.cpp ├── location-scale-t-distribution.cpp ├── logarithmic-series-distribution.cpp ├── lomax-distribution.cpp ├── mixture-of-normal-distributions.cpp ├── mixture-of-poisson-distributions.cpp ├── multinomial-distribution.cpp ├── multivariate-hypergeometric-distribution.cpp ├── negative-hypergeometric-distribution.cpp ├── non-standart-beta-distribution.cpp ├── pareto-distribution.cpp ├── power-distribution.cpp ├── proportion-distribution.cpp ├── rademacher-distribution.cpp ├── rayleigh-distribution.cpp ├── shared.cpp ├── shared.h ├── shared_inline.h ├── shifted-gompertz-distribution.cpp ├── skellam-distribution.cpp ├── slash-distribution.cpp ├── triangular-distribution.cpp ├── truncated-binomial-distribution.cpp ├── truncated-normal-distribution.cpp ├── truncated-poisson-distribution.cpp ├── tuckey-lambda-distribution.cpp ├── wald-distribution.cpp ├── zero-inflated-binomial-distribution.cpp ├── zero-inflated-negative-binomial-distribution.cpp └── zero-inflated-poisson-distribution.cpp └── tests ├── testthat.R └── testthat ├── helper_pure_r_implementations.R ├── test-NAs.R ├── test-discrete-sum-to-unity.R ├── test-discrete.R ├── test-inappropriate-parameters.R ├── test-infinity.R ├── test-log-probs.R ├── test-misc.R ├── test-multivariate-distributions.R ├── test-non-negative.R ├── test-p-r-random-tests.R ├── test-probabilities.R ├── test-pure-r-benchmarks.R ├── test-quantile-functions.R ├── test-rng-coverge.R ├── test-rtnorm-in-bounds.R ├── test-zero-length.R └── test_vectorization.R /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | .Rhistory 4 | .RData 5 | .RDataTmp 6 | cran-comments.md 7 | ^\.travis\.yml$ 8 | ^appveyor\.yml$ 9 | Readme.rmd 10 | Readme.md 11 | misc/ 12 | revdep/ 13 | Justfile 14 | CRAN-RELEASE 15 | CRAN-SUBMISSION 16 | extraDistr.Rproj 17 | .git/ 18 | .github/ 19 | ..Rcheck/ 20 | ^.*\.o$ 21 | ^CRAN-SUBMISSION$ 22 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | schedule: 5 | - cron: "0 0 1 * *" 6 | push: 7 | branches: 8 | - master 9 | pull_request: 10 | branches: 11 | - master 12 | 13 | jobs: 14 | R-CMD-check: 15 | runs-on: ${{ matrix.os }} 16 | strategy: 17 | fail-fast: true 18 | matrix: 19 | os: [ubuntu-latest, macos-latest, windows-latest] 20 | steps: 21 | - uses: actions/checkout@v1 22 | - uses: r-lib/actions/setup-r@v2 23 | - if: matrix.os == 'ubuntu-latest' 24 | name: Install system dependencies 25 | run: sudo apt-get install libcurl4-openssl-dev 26 | - name: Install packages 27 | run: Rscript -e "install.packages(c('remotes', 'rcmdcheck'))" -e "remotes::install_deps(dependencies = TRUE)" 28 | - name: Check 29 | run: Rscript -e "rcmdcheck::rcmdcheck(args = '--no-manual', error_on = 'error')" 30 | - name: Run examples 31 | run: Rscript -e "rcmdcheck::rcmdcheck(args = '--no-manual', error_on = 'error')" 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .RDataTmp 5 | src/*.o 6 | src/*.so 7 | src/*.dll 8 | inst/doc 9 | revdep/* 10 | *.pdf 11 | CRAN-RELEASE 12 | CRAN-SUBMISSION 13 | ..Rcheck/ 14 | src/symbols.rds 15 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: extraDistr 2 | Type: Package 3 | Title: Additional Univariate and Multivariate Distributions 4 | Version: 1.10.0 5 | Date: 2023-12-30 6 | Author: Tymoteusz Wolodzko 7 | Maintainer: Tymoteusz Wolodzko 8 | Description: Density, distribution function, quantile function 9 | and random generation for a number of univariate 10 | and multivariate distributions. This package implements the 11 | following distributions: Bernoulli, beta-binomial, beta-negative 12 | binomial, beta prime, Bhattacharjee, Birnbaum-Saunders, 13 | bivariate normal, bivariate Poisson, categorical, Dirichlet, 14 | Dirichlet-multinomial, discrete gamma, discrete Laplace, 15 | discrete normal, discrete uniform, discrete Weibull, Frechet, 16 | gamma-Poisson, generalized extreme value, Gompertz, 17 | generalized Pareto, Gumbel, half-Cauchy, half-normal, half-t, 18 | Huber density, inverse chi-squared, inverse-gamma, Kumaraswamy, 19 | Laplace, location-scale t, logarithmic, Lomax, multivariate 20 | hypergeometric, multinomial, negative hypergeometric, 21 | non-standard beta, normal mixture, Poisson mixture, Pareto, 22 | power, reparametrized beta, Rayleigh, shifted Gompertz, Skellam, 23 | slash, triangular, truncated binomial, truncated normal, 24 | truncated Poisson, Tukey lambda, Wald, zero-inflated binomial, 25 | zero-inflated negative binomial, zero-inflated Poisson. 26 | License: GPL-2 27 | URL: https://github.com/twolodzko/extraDistr 28 | BugReports: https://github.com/twolodzko/extraDistr/issues 29 | Encoding: UTF-8 30 | Depends: R (>= 3.1.0) 31 | LinkingTo: Rcpp 32 | Imports: Rcpp 33 | Suggests: testthat, LaplacesDemon, VGAM, evd, skellam, triangle, actuar 34 | SystemRequirements: C++ 35 | RoxygenNote: 7.2.3 36 | -------------------------------------------------------------------------------- /Justfile: -------------------------------------------------------------------------------- 1 | # Run unit tests 2 | test: 3 | #!/usr/bin/env -S Rscript --no-save --no-restore 4 | devtools::test() 5 | 6 | # CRAN checks 7 | r-cmd-check: 8 | R CMD check --as-cran . 9 | 10 | # Run all examples from the docs 11 | run-examples: 12 | #!/usr/bin/env -S Rscript --no-save --no-restore 13 | devtools::run_examples() 14 | 15 | # Generate test coverage report 16 | coverage: 17 | #!/usr/bin/env -S Rscript --no-save --no-restore 18 | devtools::test_coverage() 19 | 20 | # Run additional checks for the package 21 | check-pacakge: 22 | #!/usr/bin/env -S Rscript --no-save --no-restore 23 | devtools::check() 24 | 25 | # Deploy to CRAN 26 | cran-release: build docs manual _check-for-docs check-pacakge 27 | #!/usr/bin/env -S Rscript --no-save --no-restore 28 | devtools::release() 29 | 30 | # Build docs 31 | docs: 32 | #!/usr/bin/env -S Rscript --no-save --no-restore 33 | devtools::document() 34 | 35 | _check-for-docs: 36 | #!/bin/bash 37 | if [ -z "$(ls -A ./man)" ]; then 38 | echo "./man directory is empty" 39 | exit 30 40 | fi 41 | 42 | # Build the PDF manual 43 | manual: 44 | #!/usr/bin/env -S Rscript --no-save --no-restore 45 | devtools::build_manual() 46 | 47 | # Install a local development package 48 | install: 49 | #!/usr/bin/env -S Rscript --no-save --no-restore 50 | devtools::install() 51 | 52 | # Build the package and the manual 53 | build: 54 | #!/usr/bin/env -S Rscript --no-save --no-restore 55 | devtools::build() 56 | devtools::build_manual() 57 | 58 | # Setup development environment 59 | dev: 60 | #!/usr/bin/env -S Rscript --no-save --no-restore 61 | install.packages(c('devtools', 'tinytex')) 62 | devtools::install_deps() 63 | devtools::install_dev_deps() 64 | tinytex::install_tinytex() 65 | 66 | # Remove build files 67 | clean: 68 | rm -rf man/*.Rd 69 | rm -rf src/*.o 70 | rm -rf src/*.so 71 | -------------------------------------------------------------------------------- /R/bernoulli-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Bernoulli distribution 4 | #' 5 | #' Probability mass function, distribution function, quantile function and random generation 6 | #' for the Bernoulli distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param prob probability of success; (\code{0 < prob < 1}). 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @seealso \code{\link[stats]{Binomial}} 18 | #' 19 | #' @examples 20 | #' 21 | #' prop.table(table(rbern(1e5, 0.5))) 22 | #' 23 | #' @name Bernoulli 24 | #' @aliases Bernoulli 25 | #' @aliases dbern 26 | #' 27 | #' @keywords distribution 28 | #' @concept Univariate 29 | #' @concept Discrete 30 | #' 31 | #' @export 32 | 33 | dbern <- function(x, prob = 0.5, log = FALSE) { 34 | cpp_dbern(x, prob, log[1L]) 35 | } 36 | 37 | 38 | #' @rdname Bernoulli 39 | #' @export 40 | 41 | pbern <- function(q, prob = 0.5, lower.tail = TRUE, log.p = FALSE) { 42 | cpp_pbern(q, prob, lower.tail[1L], log.p[1L]) 43 | } 44 | 45 | 46 | #' @rdname Bernoulli 47 | #' @export 48 | 49 | qbern <- function(p, prob = 0.5, lower.tail = TRUE, log.p = FALSE) { 50 | cpp_qbern(p, prob, lower.tail[1L], log.p[1L]) 51 | } 52 | 53 | 54 | #' @rdname Bernoulli 55 | #' @export 56 | 57 | rbern <- function(n, prob = 0.5) { 58 | if (length(n) > 1) n <- length(n) 59 | cpp_rbern(n, prob) 60 | } 61 | 62 | -------------------------------------------------------------------------------- /R/beta-prime-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Beta prime distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the beta prime distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param shape1,shape2 non-negative parameters. 13 | #' @param scale positive valued scale parameter. 14 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 15 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 16 | #' otherwise, \eqn{P[X > x]}. 17 | #' 18 | #' @details 19 | #' 20 | #' If \eqn{X \sim \mathrm{Beta}(\alpha, \beta)}{X ~ Beta(\alpha, \beta)}, then 21 | #' \eqn{\frac{X}{1-X} \sim \mathrm{BetaPrime}(\alpha, \beta)}{X/(1-X) ~ BetaPrime(\alpha, \beta)}. 22 | #' 23 | #' Probability density function 24 | #' 25 | #' \deqn{ 26 | #' f(x) = \frac{(x/\sigma)^{\alpha-1} (1+x/\sigma)^{-\alpha -\beta}}{\mathrm{B}(\alpha,\beta)\sigma} 27 | #' }{ 28 | #' f(x) = ((x/\sigma)^(\alpha-1) * (1 + x/\sigma)^(-\alpha-\beta)) / (B(\alpha,\beta) * \sigma) 29 | #' } 30 | #' 31 | #' Cumulative distribution function 32 | #' 33 | #' \deqn{ 34 | #' F(x) = I_{\frac{x/\sigma}{1+x/\sigma}}(\alpha, \beta) 35 | #' }{ 36 | #' F(x) = pbeta((x/\sigma)/(1+(x/\sigma)), \alpha, \beta) 37 | #' } 38 | #' 39 | #' @seealso \code{\link[stats]{Beta}} 40 | #' 41 | #' @examples 42 | #' 43 | #' x <- rbetapr(1e5, 5, 3, 2) 44 | #' hist(x, 350, freq = FALSE, xlim = c(0, 100)) 45 | #' curve(dbetapr(x, 5, 3, 2), 0, 100, col = "red", add = TRUE, n = 500) 46 | #' hist(pbetapr(x, 5, 3, 2)) 47 | #' plot(ecdf(x), xlim = c(0, 100)) 48 | #' curve(pbetapr(x, 5, 3, 2), 0, 100, col = "red", add = TRUE, n = 500) 49 | #' 50 | #' @name BetaPrime 51 | #' @aliases BetaPrime 52 | #' @aliases dbetapr 53 | #' 54 | #' @keywords distribution 55 | #' @concept Univariate 56 | #' @concept Continuous 57 | #' 58 | #' @export 59 | 60 | dbetapr <- function(x, shape1, shape2, scale = 1, log = FALSE) { 61 | cpp_dbetapr(x, shape1, shape2, scale, log[1L]) 62 | } 63 | 64 | 65 | #' @rdname BetaPrime 66 | #' @export 67 | 68 | pbetapr <- function(q, shape1, shape2, scale = 1, lower.tail = TRUE, log.p = FALSE) { 69 | cpp_pbetapr(q, shape1, shape2, scale, lower.tail[1L], log.p[1L]) 70 | } 71 | 72 | 73 | #' @rdname BetaPrime 74 | #' @export 75 | 76 | qbetapr <- function(p, shape1, shape2, scale = 1, lower.tail = TRUE, log.p = FALSE) { 77 | cpp_qbetapr(p, shape1, shape2, scale, lower.tail[1L], log.p[1L]) 78 | } 79 | 80 | 81 | #' @rdname BetaPrime 82 | #' @export 83 | 84 | rbetapr <- function(n, shape1, shape2, scale = 1) { 85 | if (length(n) > 1) n <- length(n) 86 | cpp_rbetapr(n, shape1, shape2, scale) 87 | } 88 | 89 | -------------------------------------------------------------------------------- /R/bivariate-normal-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Bivariate normal distribution 4 | #' 5 | #' Density, distribution function and random generation 6 | #' for the bivariate normal distribution. 7 | #' 8 | #' @param x,y vectors of quantiles; alternatively x may be a two-column 9 | #' matrix (or data.frame) and y may be omitted. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param mean1,mean2 vectors of means. 13 | #' @param sd1,sd2 vectors of standard deviations. 14 | #' @param cor vector of correlations (\code{-1 < cor < 1}). 15 | #' @param log logical; if TRUE, probabilities p are given as log(p). 16 | #' 17 | #' @details 18 | #' 19 | #' Probability density function 20 | #' \deqn{ 21 | #' f(x) = \frac{1}{2\pi\sqrt{1-\rho^2}\sigma_1\sigma_2} 22 | #' \exp\left\{-\frac{1}{2(1-\rho^2)} \left[\left(\frac{x_1 - \mu_1}{\sigma_1}\right)^2 - 23 | #' 2\rho \left(\frac{x_1 - \mu_1}{\sigma_1}\right) \left(\frac{x_2 - \mu_2}{\sigma_2}\right) + 24 | #' \left(\frac{x_2 - \mu_2}{\sigma_2}\right)^2\right]\right\} 25 | #' }{ 26 | #' f(x) = 1/(2*\pi*sqrt(1-\rho^2)*\sigma1*\sigma2) * exp(-(1/(2*(1-\rho^2)* 27 | #' (((x1-\mu1)/\sigma1)^2 - 2*\rho*((x1-\mu1)/\sigma2)*((x2-\mu2)/\sigma2) * 28 | #' ((x2-\mu2)/\sigma2)^2)))) 29 | #' } 30 | #' 31 | #' @references 32 | #' Krishnamoorthy, K. (2006). Handbook of Statistical Distributions 33 | #' with Applications. Chapman & Hall/CRC 34 | #' 35 | #' @references 36 | #' Mukhopadhyay, N. (2000). Probability and statistical inference. 37 | #' Chapman & Hall/CRC 38 | #' 39 | #' @examples 40 | #' 41 | #' y <- x <- seq(-4, 4, by = 0.25) 42 | #' z <- outer(x, y, function(x, y) dbvnorm(x, y, cor = -0.75)) 43 | #' persp(x, y, z) 44 | #' 45 | #' y <- x <- seq(-4, 4, by = 0.25) 46 | #' z <- outer(x, y, function(x, y) dbvnorm(x, y, cor = -0.25)) 47 | #' persp(x, y, z) 48 | #' 49 | #' @seealso \code{\link[stats]{Normal}} 50 | #' 51 | #' @name BivNormal 52 | #' @aliases BivNormal 53 | #' @aliases dbvnorm 54 | #' 55 | #' @keywords distribution 56 | #' @concept Multivariate 57 | #' @concept Continuous 58 | #' 59 | #' @export 60 | 61 | dbvnorm <- function(x, y = NULL, mean1 = 0, mean2 = mean1, sd1 = 1, sd2 = sd1, cor = 0, log = FALSE) { 62 | if (is.null(y)) { 63 | if ((is.matrix(x) || is.data.frame(x)) && ncol(x) == 2) { 64 | y <- x[, 2] 65 | x <- x[, 1] 66 | } else { 67 | stop("y is not provided while x is not a two-column matrix") 68 | } 69 | } 70 | cpp_dbnorm(x, y, mean1, mean2, sd1, sd2, cor, log[1L]) 71 | } 72 | 73 | 74 | #' @rdname BivNormal 75 | #' @export 76 | 77 | rbvnorm <- function(n, mean1 = 0, mean2 = mean1, sd1 = 1, sd2 = sd1, cor = 0) { 78 | if (length(n) > 1) n <- length(n) 79 | cpp_rbnorm(n, mean1, mean2, sd1, sd2, cor) 80 | } 81 | 82 | -------------------------------------------------------------------------------- /R/bivariate-poisson-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Bivariate Poisson distribution 4 | #' 5 | #' Probability mass function and random generation for the bivariate Poisson distribution. 6 | #' 7 | #' @param x,y vectors of quantiles; alternatively x may be a two-column 8 | #' matrix (or data.frame) and y may be omitted. 9 | #' @param n number of observations. If \code{length(n) > 1}, 10 | #' the length is taken to be the number required. 11 | #' @param a,b,c positive valued parameters. 12 | #' @param log logical; if TRUE, probabilities p are given as log(p). 13 | #' 14 | #' @details 15 | #' 16 | #' Probability mass function 17 | #' \deqn{ 18 | #' f(x,y) = \exp \{-(a+b+c)\} \frac{a^x}{x!} \frac{b^y}{y!} \sum_{k=0}^{\min(x,y)} 19 | #' {x \choose k} {y \choose k} k! \left( \frac{c}{ab} \right)^k 20 | #' }{ 21 | #' f(x,y) = exp(-(a+b+c)) * (a^x)/x! * (b^y)/y! * 22 | #' sum(choose(x,k)*choose(y,k)*k!*(c/(a*b))^k) 23 | #' } 24 | #' 25 | #' @references 26 | #' Karlis, D. and Ntzoufras, I. (2003). Analysis of sports data by using bivariate Poisson models. 27 | #' Journal of the Royal Statistical Society: Series D (The Statistician), 52(3), 381-393. 28 | #' 29 | #' @references 30 | #' Kocherlakota, S. and Kocherlakota, K. (1992) Bivariate Discrete Distributions. 31 | #' New York: Dekker. 32 | #' 33 | #' @references 34 | #' Johnson, N., Kotz, S. and Balakrishnan, N. (1997). Discrete Multivariate Distributions. 35 | #' New York: Wiley. 36 | #' 37 | #' @references 38 | #' Holgate, P. (1964). Estimation for the bivariate Poisson distribution. 39 | #' Biometrika, 51(1-2), 241-287. 40 | #' 41 | #' @references 42 | #' Kawamura, K. (1984). Direct calculation of maximum likelihood estimator for the bivariate 43 | #' Poisson distribution. Kodai mathematical journal, 7(2), 211-221. 44 | #' 45 | #' @examples 46 | #' 47 | #' x <- rbvpois(5000, 7, 8, 5) 48 | #' image(prop.table(table(x[,1], x[,2]))) 49 | #' colMeans(x) 50 | #' 51 | #' @seealso \code{\link[stats]{Poisson}} 52 | #' 53 | #' @name BivPoiss 54 | #' @aliases BivPoiss 55 | #' @aliases dbvpois 56 | #' 57 | #' @keywords distribution 58 | #' @concept Multivariate 59 | #' @concept Discrete 60 | #' 61 | #' @export 62 | 63 | dbvpois <- function(x, y = NULL, a, b, c, log = FALSE) { 64 | if (is.null(y)) { 65 | if ((is.matrix(x) || is.data.frame(x)) && ncol(x) == 2) { 66 | y <- x[, 2] 67 | x <- x[, 1] 68 | } else { 69 | stop("y is not provided while x is not a two-column matrix") 70 | } 71 | } 72 | cpp_dbpois(x, y, a, b, c, log[1L]) 73 | } 74 | 75 | 76 | #' @rdname BivPoiss 77 | #' @export 78 | 79 | rbvpois <- function(n, a, b, c) { 80 | if (length(n) > 1) n <- length(n) 81 | cpp_rbpois(n, a, b, c) 82 | } 83 | 84 | -------------------------------------------------------------------------------- /R/dirichlet-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Dirichlet distribution 4 | #' 5 | #' Density function, cumulative distribution function and random generation 6 | #' for the Dirichlet distribution. 7 | #' 8 | #' @param x \eqn{k}-column matrix of quantiles. 9 | #' @param n number of observations. If \code{length(n) > 1}, 10 | #' the length is taken to be the number required. 11 | #' @param alpha \eqn{k}-values vector or \eqn{k}-column matrix; 12 | #' concentration parameter. Must be positive. 13 | #' @param log logical; if TRUE, probabilities p are given as log(p). 14 | #' 15 | #' @details 16 | #' 17 | #' Probability density function 18 | #' \deqn{ 19 | #' f(x) = \frac{\Gamma(\sum_k \alpha_k)}{\prod_k \Gamma(\alpha_k)} \prod_k x_k^{k-1} 20 | #' }{ 21 | #' f(x) = \Gamma(sum(\alpha[k])) / prod(\Gamma(\alpha[k])) * prod(x[k]^{k-1}) 22 | #' } 23 | #' 24 | #' @references 25 | #' Devroye, L. (1986). Non-Uniform Random Variate Generation. Springer-Verlag. 26 | #' 27 | #' @examples 28 | #' 29 | #' # Generating 10 random draws from Dirichlet distribution 30 | #' # parametrized using a vector 31 | #' 32 | #' rdirichlet(10, c(1, 1, 1, 1)) 33 | #' 34 | #' # or parametrized using a matrix where each row 35 | #' # is a vector of parameters 36 | #' 37 | #' alpha <- matrix(c(1, 1, 1, 1:3, 7:9), ncol = 3, byrow = TRUE) 38 | #' rdirichlet(10, alpha) 39 | #' 40 | #' @name Dirichlet 41 | #' @aliases Dirichlet 42 | #' @aliases ddirichlet 43 | #' 44 | #' @keywords distribution 45 | #' @concept Multivariate 46 | #' @concept Continuous 47 | #' 48 | #' @export 49 | 50 | ddirichlet <- function(x, alpha, log = FALSE) { 51 | if (is.vector(alpha)) 52 | alpha <- matrix(alpha, nrow = 1) 53 | else if (!is.matrix(alpha)) 54 | alpha <- as.matrix(alpha) 55 | if (is.data.frame(x)) 56 | x <- as.matrix(x) 57 | else if (is.vector(x)) 58 | x <- matrix(x, byrow = TRUE, nrow = 1) 59 | cpp_ddirichlet(x, alpha, log[1L]) 60 | } 61 | 62 | 63 | #' @rdname Dirichlet 64 | #' @export 65 | 66 | rdirichlet <- function (n, alpha) { 67 | if (length(n) > 1) n <- length(n) 68 | if (is.vector(alpha)) 69 | alpha <- matrix(alpha, nrow = 1) 70 | else if (!is.matrix(alpha)) 71 | alpha <- as.matrix(alpha) 72 | cpp_rdirichlet(n, alpha) 73 | } 74 | 75 | -------------------------------------------------------------------------------- /R/dirichlet-multinomial-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Dirichlet-multinomial (multivariate Polya) distribution 4 | #' 5 | #' Density function, cumulative distribution function and random generation 6 | #' for the Dirichlet-multinomial (multivariate Polya) distribution. 7 | #' 8 | #' @param x \eqn{k}-column matrix of quantiles. 9 | #' @param n number of observations. If \code{length(n) > 1}, 10 | #' the length is taken to be the number required. 11 | #' @param size numeric vector; number of trials (zero or more). 12 | #' @param alpha \eqn{k}-values vector or \eqn{k}-column matrix; 13 | #' concentration parameter. Must be positive. 14 | #' @param log logical; if TRUE, probabilities p are given as log(p). 15 | #' 16 | #' @details 17 | #' 18 | #' If \eqn{(p_1,\dots,p_k) \sim \mathrm{Dirichlet}(\alpha_1,\dots,\alpha_k)}{(p[1],\dots,p[k]) ~ Dirichlet(\alpha[1],\dots,\alpha[k])} and 19 | #' \eqn{(x_1,\dots,x_k) \sim \mathrm{Multinomial}(n, p_1,\dots,p_k)}{(x[1],\dots,x[k]) ~ Multinomial(n, p[1],\dots,p[k])}, then 20 | #' \eqn{(x_1,\dots,x_k) \sim \mathrm{DirichletMultinomial(n, \alpha_1,\dots,\alpha_k)}}{(x[1],\dots,x[k]) ~ DirichletMultinomial(n, \alpha[1],\dots,\alpha[k])}. 21 | #' 22 | #' Probability density function 23 | #' \deqn{ 24 | #' f(x) = \frac{\left(n!\right)\Gamma\left(\sum \alpha_k\right)}{\Gamma\left(n+\sum \alpha_k\right)}\prod_{k=1}^K\frac{\Gamma(x_{k}+\alpha_{k})}{\left(x_{k}!\right)\Gamma(\alpha_{k})} 25 | #' }{ 26 | #' f(x) = (n! * \Gamma(sum(\alpha[k]))) / (\Gamma(n + sum(\alpha[k]))) * prod((\Gamma(x[k] + \alpha[k])) / (x[k]! * \Gamma(\alpha[k])) 27 | #' } 28 | #' 29 | #' @seealso \code{\link{Dirichlet}}, \code{\link{Multinomial}} 30 | #' 31 | #' @references 32 | #' Gentle, J.E. (2006). Random number generation and Monte Carlo methods. Springer. 33 | #' 34 | #' @references 35 | #' Kvam, P. and Day, D. (2001) The multivariate Polya distribution in combat modeling. 36 | #' Naval Research Logistics, 48, 1-17. 37 | #' 38 | #' @name DirMnom 39 | #' @aliases DirMnom 40 | #' @aliases ddirmnom 41 | #' 42 | #' @keywords distribution 43 | #' @concept Multivariate 44 | #' @concept Discrete 45 | #' 46 | #' @export 47 | 48 | ddirmnom <- function(x, size, alpha, log = FALSE) { 49 | if (is.vector(alpha)) 50 | alpha <- matrix(alpha, nrow = 1) 51 | else if (!is.matrix(alpha)) 52 | alpha <- as.matrix(alpha) 53 | if (is.data.frame(x)) 54 | x <- as.matrix(x) 55 | else if (is.vector(x)) 56 | x <- matrix(x, byrow = TRUE, nrow = 1) 57 | cpp_ddirmnom(x, size, alpha, log[1L]) 58 | } 59 | 60 | 61 | #' @rdname DirMnom 62 | #' @export 63 | 64 | rdirmnom <- function (n, size, alpha) { 65 | if (length(n) > 1) n <- length(n) 66 | if (is.vector(alpha)) 67 | alpha <- matrix(alpha, nrow = 1) 68 | else if (!is.matrix(alpha)) 69 | alpha <- as.matrix(alpha) 70 | cpp_rdirmnom(n, size, alpha) 71 | } 72 | 73 | -------------------------------------------------------------------------------- /R/discrete-gamma-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Discrete gamma distribution 4 | #' 5 | #' Probability mass function, distribution function and random generation 6 | #' for discrete gamma distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param n number of observations. If \code{length(n) > 1}, 10 | #' the length is taken to be the number required. 11 | #' @param rate an alternative way to specify the scale. 12 | #' @param shape,scale shape and scale parameters. Must be positive, scale strictly. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' 19 | #' Probability mass function of discrete gamma distribution \eqn{f_Y(y)}{f} 20 | #' is defined by discretization of continuous gamma distribution 21 | #' \eqn{f_Y(y) = S_X(y) - S_X(y+1)}{f(y) = S(x) - S(x+1)} 22 | #' where \eqn{S_X}{S} is a survival function of continuous gamma distribution. 23 | #' 24 | #' @references 25 | #' Chakraborty, S. and Chakravarty, D. (2012). 26 | #' Discrete Gamma distributions: Properties and parameter estimations. 27 | #' Communications in Statistics-Theory and Methods, 41(18), 3301-3324. 28 | #' 29 | #' @seealso \code{\link[stats]{GammaDist}}, \code{\link{DiscreteNormal}} 30 | #' 31 | #' @examples 32 | #' 33 | #' x <- rdgamma(1e5, 9, 1) 34 | #' xx <- 0:50 35 | #' plot(prop.table(table(x))) 36 | #' lines(xx, ddgamma(xx, 9, 1), col = "red") 37 | #' hist(pdgamma(x, 9, 1)) 38 | #' plot(ecdf(x)) 39 | #' xx <- seq(0, 50, 0.1) 40 | #' lines(xx, pdgamma(xx, 9, 1), col = "red", lwd = 2, type = "s") 41 | #' 42 | #' @name DiscreteGamma 43 | #' @aliases DiscreteGamma 44 | #' @aliases ddgamma 45 | #' 46 | #' @keywords distribution 47 | #' @concept Univariate 48 | #' @concept Discrete 49 | #' 50 | #' @export 51 | 52 | ddgamma <- function(x, shape, rate = 1, scale = 1/rate, log = FALSE) { 53 | if (!missing(rate) && !missing(scale)) { 54 | if (abs(rate * scale - 1) < 1e-15) 55 | warning("specify 'rate' or 'scale' but not both") 56 | else stop("specify 'rate' or 'scale' but not both") 57 | } 58 | cpp_ddgamma(x, shape, scale, log[1L]) 59 | } 60 | 61 | 62 | #' @rdname DiscreteGamma 63 | #' @export 64 | 65 | pdgamma <- function(q, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE) { 66 | pgamma(floor(q)+1, shape, scale = scale, lower.tail = lower.tail[1L], log.p = log.p[1L]) 67 | } 68 | 69 | 70 | #' @rdname DiscreteGamma 71 | #' @export 72 | 73 | rdgamma <- function(n, shape, rate = 1, scale = 1/rate) { 74 | floor(rgamma(n, shape, scale = scale)) 75 | } 76 | 77 | -------------------------------------------------------------------------------- /R/discrete-normal-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Discrete normal distribution 4 | #' 5 | #' Probability mass function, distribution function and random generation 6 | #' for discrete normal distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param n number of observations. If \code{length(n) > 1}, 10 | #' the length is taken to be the number required. 11 | #' @param mean vector of means. 12 | #' @param sd vector of standard deviations. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' @details 17 | #' 18 | #' Probability mass function 19 | #' 20 | #' \deqn{ 21 | #' f(x) = \Phi\left(\frac{x-\mu+1}{\sigma}\right) - \Phi\left(\frac{x-\mu}{\sigma}\right) 22 | #' }{ 23 | #' f(x) = \Phi((x-\mu+1)/\sigma) - \Phi((x-\mu)/\sigma) 24 | #' } 25 | #' 26 | #' Cumulative distribution function 27 | #' 28 | #' \deqn{ 29 | #' F(x) = \Phi\left(\frac{\lfloor x \rfloor + 1 - \mu}{\sigma}\right) 30 | #' }{ 31 | #' F(x) = \Phi((floor(x)+1-\mu)/\sigma) 32 | #' } 33 | #' 34 | #' @references 35 | #' Roy, D. (2003). The discrete normal distribution. 36 | #' Communications in Statistics-Theory and Methods, 32, 1871-1883. 37 | #' 38 | #' @seealso \code{\link[stats]{Normal}} 39 | #' 40 | #' @examples 41 | #' 42 | #' x <- rdnorm(1e5, 0, 3) 43 | #' xx <- -15:15 44 | #' plot(prop.table(table(x))) 45 | #' lines(xx, ddnorm(xx, 0, 3), col = "red") 46 | #' hist(pdnorm(x, 0, 3)) 47 | #' plot(ecdf(x)) 48 | #' xx <- seq(-15, 15, 0.1) 49 | #' lines(xx, pdnorm(xx, 0, 3), col = "red", lwd = 2, type = "s") 50 | #' 51 | #' @name DiscreteNormal 52 | #' @aliases DiscreteNormal 53 | #' @aliases ddnorm 54 | #' 55 | #' @keywords distribution 56 | #' @concept Univariate 57 | #' @concept Discrete 58 | #' 59 | #' @export 60 | 61 | ddnorm <- function(x, mean = 0, sd = 1, log = FALSE) { 62 | cpp_ddnorm(x, mean, sd, log[1L]) 63 | } 64 | 65 | 66 | #' @rdname DiscreteNormal 67 | #' @export 68 | 69 | pdnorm <- function(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) { 70 | pnorm(floor(q)+1, mean, sd, lower.tail[1L], log.p[1L]) 71 | } 72 | 73 | 74 | #' @rdname DiscreteNormal 75 | #' @export 76 | 77 | rdnorm <- function(n, mean = 0, sd = 1) { 78 | floor(rnorm(n, mean, sd)) 79 | } 80 | 81 | -------------------------------------------------------------------------------- /R/discrete-uniform-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Discrete uniform distribution 4 | #' 5 | #' Probability mass function, distribution function, quantile function and random generation 6 | #' for the discrete uniform distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param min,max lower and upper limits of the distribution. Must be finite. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' 19 | #' If \code{min == max}, then discrete uniform distribution is a degenerate distribution. 20 | #' 21 | #' @examples 22 | #' 23 | #' x <- rdunif(1e5, 1, 10) 24 | #' xx <- -1:11 25 | #' plot(prop.table(table(x)), type = "h") 26 | #' lines(xx, ddunif(xx, 1, 10), col = "red") 27 | #' hist(pdunif(x, 1, 10)) 28 | #' xx <- seq(-1, 11, by = 0.01) 29 | #' plot(ecdf(x)) 30 | #' lines(xx, pdunif(xx, 1, 10), col = "red") 31 | #' 32 | #' @name DiscreteUniform 33 | #' @aliases DiscreteUniform 34 | #' @aliases ddunif 35 | #' 36 | #' @keywords distribution 37 | #' @concept Univariate 38 | #' @concept Discrete 39 | #' 40 | #' @export 41 | 42 | ddunif <- function(x, min, max, log = FALSE) { 43 | cpp_ddunif(x, min, max, log[1L]) 44 | } 45 | 46 | 47 | #' @rdname DiscreteUniform 48 | #' @export 49 | 50 | pdunif <- function(q, min, max, lower.tail = TRUE, log.p = FALSE) { 51 | cpp_pdunif(q, min, max, lower.tail[1L], log.p[1L]) 52 | } 53 | 54 | 55 | #' @rdname DiscreteUniform 56 | #' @export 57 | 58 | qdunif <- function(p, min, max, lower.tail = TRUE, log.p = FALSE) { 59 | cpp_qdunif(p, min, max, lower.tail[1L], log.p[1L]) 60 | } 61 | 62 | 63 | #' @rdname DiscreteUniform 64 | #' @export 65 | 66 | rdunif <- function(n, min, max) { 67 | if (length(n) > 1) n <- length(n) 68 | cpp_rdunif(n, min, max) 69 | } 70 | 71 | -------------------------------------------------------------------------------- /R/extraDistr-deprecated.R: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/twolodzko/extraDistr/3e7b962fa257c1e323f691f2b9f17cf88dc9e0c5/R/extraDistr-deprecated.R -------------------------------------------------------------------------------- /R/extraDistr-package.R: -------------------------------------------------------------------------------- 1 | #' @keywords internal 2 | "_PACKAGE" 3 | 4 | #' Additional univariate and multivariate distributions 5 | #' 6 | #' Density, distribution function, quantile function and random 7 | #' generation for a number of univariate and multivariate distributions. 8 | #' 9 | #' @details 10 | #' 11 | #' This package follows naming convention that is consistent with base R, 12 | #' where density (or probability mass) functions, distribution functions, 13 | #' quantile functions and random generation functions names are followed by 14 | #' \code{d}*, \code{p}*, \code{q}*, and \code{r}* prefixes. 15 | #' 16 | #' Behaviour of the functions is consistent with base R, where for 17 | #' not valid parameters values \code{NaN}'s are returned, while 18 | #' for values beyond function support \code{0}'s are returned 19 | #' (e.g. for non-integers in discrete distributions, or for 20 | #' negative values in functions with non-negative support). 21 | #' 22 | #' All the functions vectorized and coded in C++ using \pkg{Rcpp}. 23 | #' 24 | #' @docType package 25 | #' @name extraDistr-package 26 | #' 27 | #' @useDynLib extraDistr, .registration = TRUE 28 | #' @importFrom Rcpp sourceCpp 29 | #' 30 | #' @importFrom stats pgamma qgamma rgamma 31 | #' @importFrom stats pnorm qnorm rnorm 32 | #' @importFrom stats rpois 33 | #' @importFrom stats qchisq rchisq 34 | NULL 35 | -------------------------------------------------------------------------------- /R/frechet-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Frechet distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the Frechet distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param lambda,sigma,mu shape, scale, and location parameters. 13 | #' Scale and shape must be positive. 14 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 15 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 16 | #' otherwise, \eqn{P[X > x]}. 17 | #' 18 | #' @details 19 | #' 20 | #' Probability density function 21 | #' \deqn{ 22 | #' f(x) = \frac{\lambda}{\sigma} \left(\frac{x-\mu}{\sigma}\right)^{-1-\lambda} \exp\left(-\left(\frac{x-\mu}{\sigma}\right)^{-\lambda}\right) 23 | #' }{ 24 | #' f(x) = \lambda/\sigma * ((x-\mu)/\sigma)^(-1-\lambda) * exp(-((x-\mu)/\sigma)^-\lambda) 25 | #' } 26 | #' 27 | #' Cumulative distribution function 28 | #' \deqn{ 29 | #' F(x) = \exp\left(-\left(\frac{x-\mu}{\sigma}\right)^{-\lambda}\right) 30 | #' }{ 31 | #' F(x) = exp(-((x-\mu)/\sigma)^-\lambda) 32 | #' } 33 | #' 34 | #' Quantile function 35 | #' \deqn{ 36 | #' F^{-1}(p) = \mu + \sigma -\log(p)^{-1/\lambda} 37 | #' }{ 38 | #' F^-1(p) = \mu + \sigma * -log(p)^{-1/\lambda} 39 | #' } 40 | #' 41 | #' @references 42 | #' Bury, K. (1999). Statistical Distributions in Engineering. 43 | #' Cambridge University Press. 44 | #' 45 | #' @examples 46 | #' 47 | #' x <- rfrechet(1e5, 5, 2, 1.5) 48 | #' xx <- seq(0, 1000, by = 0.1) 49 | #' hist(x, 200, freq = FALSE) 50 | #' lines(xx, dfrechet(xx, 5, 2, 1.5), col = "red") 51 | #' hist(pfrechet(x, 5, 2, 1.5)) 52 | #' plot(ecdf(x)) 53 | #' lines(xx, pfrechet(xx, 5, 2, 1.5), col = "red", lwd = 2) 54 | #' 55 | #' @name Frechet 56 | #' @aliases Frechet 57 | #' @aliases dfrechet 58 | #' 59 | #' @keywords distribution 60 | #' @concept Univariate 61 | #' @concept Continuous 62 | #' 63 | #' @export 64 | 65 | dfrechet <- function(x, lambda = 1, mu = 0, sigma = 1, log = FALSE) { 66 | cpp_dfrechet(x, lambda, mu, sigma, log[1L]) 67 | } 68 | 69 | 70 | #' @rdname Frechet 71 | #' @export 72 | 73 | pfrechet <- function(q, lambda = 1, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 74 | cpp_pfrechet(q, lambda, mu, sigma, lower.tail[1L], log.p[1L]) 75 | } 76 | 77 | 78 | #' @rdname Frechet 79 | #' @export 80 | 81 | qfrechet <- function(p, lambda = 1, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 82 | cpp_qfrechet(p, lambda, mu, sigma, lower.tail[1L], log.p[1L]) 83 | } 84 | 85 | 86 | #' @rdname Frechet 87 | #' @export 88 | 89 | rfrechet <- function(n, lambda = 1, mu = 0, sigma = 1) { 90 | if (length(n) > 1) n <- length(n) 91 | cpp_rfrechet(n, lambda, mu, sigma) 92 | } 93 | 94 | -------------------------------------------------------------------------------- /R/gompertz-distribution.R: -------------------------------------------------------------------------------- 1 | #' Gompertz distribution 2 | #' 3 | #' Density, distribution function, quantile function and random generation 4 | #' for the Gompertz distribution. 5 | #' 6 | #' @param x,q vector of quantiles. 7 | #' @param p vector of probabilities. 8 | #' @param n number of observations. If \code{length(n) > 1}, 9 | #' the length is taken to be the number required. 10 | #' @param a,b positive valued scale and location parameters. 11 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 12 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 13 | #' otherwise, \eqn{P[X > x]}. 14 | #' 15 | #' @details 16 | #' 17 | #' Probability density function 18 | #' \deqn{ 19 | #' f(x) = a \exp\left(bx - \frac{a}{b} (\exp(bx)-1)\right) 20 | #' }{ 21 | #' f(x) = a*exp(b*x - a/b * (exp(b*x)-1)) 22 | #' } 23 | #' 24 | #' Cumulative distribution function 25 | #' \deqn{ 26 | #' F(x) = 1-\exp\left(-\frac{a}{b} (\exp(bx)-1)\right) 27 | #' }{ 28 | #' F(x) = 1-exp(-a/b * (exp(b*x)-1)) 29 | #' } 30 | #' 31 | #' Quantile function 32 | #' \deqn{ 33 | #' F^{-1}(p) = \frac{1}{b} \log\left(1-\frac{b}{a}\log(1-p)\right) 34 | #' }{ 35 | #' F^-1(p) = 1/b * log(1 - b/a * log(1-p)) 36 | #' } 37 | #' 38 | #' @references 39 | #' Lenart, A. (2012). The Gompertz distribution and Maximum Likelihood Estimation 40 | #' of its parameters - a revision. MPIDR WORKING PAPER WP 2012-008. 41 | #' \url{https://www.demogr.mpg.de/papers/working/wp-2012-008.pdf} 42 | #' 43 | #' @examples 44 | #' 45 | #' x <- rgompertz(1e5, 5, 2) 46 | #' hist(x, 100, freq = FALSE) 47 | #' curve(dgompertz(x, 5, 2), 0, 1, col = "red", add = TRUE) 48 | #' hist(pgompertz(x, 5, 2)) 49 | #' plot(ecdf(x)) 50 | #' curve(pgompertz(x, 5, 2), 0, 1, col = "red", lwd = 2, add = TRUE) 51 | #' 52 | #' @name Gompertz 53 | #' @aliases Gompertz 54 | #' @aliases dgompertz 55 | #' 56 | #' @keywords distribution 57 | #' @concept Univariate 58 | #' @concept Continuous 59 | #' 60 | #' @export 61 | 62 | dgompertz <- function(x, a = 1, b = 1, log = FALSE) { 63 | cpp_dgompertz(x, a, b, log[1L]) 64 | } 65 | 66 | 67 | #' @rdname Gompertz 68 | #' @export 69 | 70 | pgompertz <- function(q, a = 1, b = 1, lower.tail = TRUE, log.p = FALSE) { 71 | cpp_pgompertz(q, a, b, lower.tail[1L], log.p[1L]) 72 | } 73 | 74 | 75 | #' @rdname Gompertz 76 | #' @export 77 | 78 | qgompertz <- function(p, a = 1, b = 1, lower.tail = TRUE, log.p = FALSE) { 79 | cpp_qgompertz(p, a, b, lower.tail[1L], log.p[1L]) 80 | } 81 | 82 | 83 | #' @rdname Gompertz 84 | #' @export 85 | 86 | rgompertz <- function(n, a = 1, b = 1) { 87 | if (length(n) > 1) n <- length(n) 88 | cpp_rgompertz(n, a, b) 89 | } 90 | -------------------------------------------------------------------------------- /R/gumbel-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Gumbel distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the Gumbel distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param mu,sigma location and scale parameters. Scale must be positive. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' 19 | #' Probability density function 20 | #' \deqn{ 21 | #' f(x) = \frac{1}{\sigma} \exp\left(-\left(\frac{x-\mu}{\sigma} + \exp\left(-\frac{x-\mu}{\sigma}\right)\right)\right) 22 | #' }{ 23 | #' f(x) = 1/\sigma * exp(-((x-\mu)/\sigma + exp(-(x-\mu)/\sigma))) 24 | #' } 25 | #' 26 | #' Cumulative distribution function 27 | #' \deqn{ 28 | #' F(x) = \exp\left(-\exp\left(-\frac{x-\mu}{\sigma}\right)\right) 29 | #' }{ 30 | #' F(x) = exp(-exp(-(x-\mu)/\sigma)) 31 | #' } 32 | #' 33 | #' Quantile function 34 | #' \deqn{ 35 | #' F^{-1}(p) = \mu - \sigma \log(-\log(p)) 36 | #' }{ 37 | #' F^-1(p) = \mu - \sigma * log(-log(p)) 38 | #' } 39 | #' 40 | #' @references 41 | #' Bury, K. (1999). Statistical Distributions in Engineering. 42 | #' Cambridge University Press. 43 | #' 44 | #' @examples 45 | #' 46 | #' x <- rgumbel(1e5, 5, 2) 47 | #' hist(x, 100, freq = FALSE) 48 | #' curve(dgumbel(x, 5, 2), 0, 25, col = "red", add = TRUE) 49 | #' hist(pgumbel(x, 5, 2)) 50 | #' plot(ecdf(x)) 51 | #' curve(pgumbel(x, 5, 2), 0, 25, col = "red", lwd = 2, add = TRUE) 52 | #' 53 | #' @name Gumbel 54 | #' @aliases Gumbel 55 | #' @aliases dgumbel 56 | #' 57 | #' @keywords distribution 58 | #' @concept Univariate 59 | #' @concept Continuous 60 | #' 61 | #' @export 62 | 63 | dgumbel <- function(x, mu = 0, sigma = 1, log = FALSE) { 64 | cpp_dgumbel(x, mu, sigma, log[1L]) 65 | } 66 | 67 | 68 | #' @rdname Gumbel 69 | #' @export 70 | 71 | pgumbel <- function(q, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 72 | cpp_pgumbel(q, mu, sigma, lower.tail[1L], log.p[1L]) 73 | } 74 | 75 | 76 | #' @rdname Gumbel 77 | #' @export 78 | 79 | qgumbel <- function(p, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 80 | cpp_qgumbel(p, mu, sigma, lower.tail[1L], log.p[1L]) 81 | } 82 | 83 | 84 | #' @rdname Gumbel 85 | #' @export 86 | 87 | rgumbel <- function(n, mu = 0, sigma = 1) { 88 | if (length(n) > 1) n <- length(n) 89 | cpp_rgumbel(n, mu, sigma) 90 | } 91 | 92 | -------------------------------------------------------------------------------- /R/half-cauchy-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Half-Cauchy distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the half-Cauchy distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param sigma positive valued scale parameter. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' If \eqn{X} follows Cauchy centered at 0 and parametrized by scale \eqn{\sigma}, 19 | #' then \eqn{|X|} follows half-Cauchy distribution parametrized by 20 | #' scale \eqn{\sigma}. Half-Cauchy distribution is a special case of half-t 21 | #' distribution with \eqn{\nu=1} degrees of freedom. 22 | #' 23 | #' @references 24 | #' Gelman, A. (2006). Prior distributions for variance parameters in hierarchical 25 | #' models (comment on article by Browne and Draper). 26 | #' Bayesian analysis, 1(3), 515-534. 27 | #' 28 | #' @references 29 | #' Jacob, E. and Jayakumar, K. (2012). 30 | #' On Half-Cauchy Distribution and Process. 31 | #' International Journal of Statistika and Mathematika, 3(2), 77-81. 32 | #' 33 | #' @seealso \code{\link{HalfT}} 34 | #' 35 | #' @examples 36 | #' 37 | #' x <- rhcauchy(1e5, 2) 38 | #' hist(x, 2e5, freq = FALSE, xlim = c(0, 100)) 39 | #' curve(dhcauchy(x, 2), 0, 100, col = "red", add = TRUE) 40 | #' hist(phcauchy(x, 2)) 41 | #' plot(ecdf(x), xlim = c(0, 100)) 42 | #' curve(phcauchy(x, 2), col = "red", lwd = 2, add = TRUE) 43 | #' 44 | #' @name HalfCauchy 45 | #' @aliases HalfCauchy 46 | #' @aliases dhcauchy 47 | #' 48 | #' @keywords distribution 49 | #' @concept Univariate 50 | #' @concept Continuous 51 | #' 52 | #' @export 53 | 54 | dhcauchy <- function(x, sigma = 1, log = FALSE) { 55 | cpp_dhcauchy(x, sigma, log[1L]) 56 | } 57 | 58 | 59 | #' @rdname HalfCauchy 60 | #' @export 61 | 62 | phcauchy <- function(q, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 63 | cpp_phcauchy(q, sigma, lower.tail[1L], log.p[1L]) 64 | } 65 | 66 | 67 | #' @rdname HalfCauchy 68 | #' @export 69 | 70 | qhcauchy <- function(p, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 71 | cpp_qhcauchy(p, sigma, lower.tail[1L], log.p[1L]) 72 | } 73 | 74 | 75 | #' @rdname HalfCauchy 76 | #' @export 77 | 78 | rhcauchy <- function(n, sigma = 1) { 79 | if (length(n) > 1) n <- length(n) 80 | cpp_rhcauchy(n, sigma) 81 | } 82 | 83 | -------------------------------------------------------------------------------- /R/half-normal-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Half-normal distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the half-normal distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param sigma positive valued scale parameter. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' If \eqn{X} follows normal distribution centered at 0 and parametrized 19 | #' by scale \eqn{\sigma}, then \eqn{|X|} follows half-normal distribution 20 | #' parametrized by scale \eqn{\sigma}. Half-t distribution with \eqn{\nu=\infty} 21 | #' degrees of freedom converges to half-normal distribution. 22 | #' 23 | #' @references 24 | #' Gelman, A. (2006). Prior distributions for variance parameters in hierarchical 25 | #' models (comment on article by Browne and Draper). 26 | #' Bayesian analysis, 1(3), 515-534. 27 | #' 28 | #' @references 29 | #' Jacob, E. and Jayakumar, K. (2012). 30 | #' On Half-Cauchy Distribution and Process. 31 | #' International Journal of Statistika and Mathematika, 3(2), 77-81. 32 | #' 33 | #' @seealso \code{\link{HalfT}} 34 | #' 35 | #' @examples 36 | #' 37 | #' x <- rhnorm(1e5, 2) 38 | #' hist(x, 100, freq = FALSE) 39 | #' curve(dhnorm(x, 2), 0, 8, col = "red", add = TRUE) 40 | #' hist(phnorm(x, 2)) 41 | #' plot(ecdf(x)) 42 | #' curve(phnorm(x, 2), 0, 8, col = "red", lwd = 2, add = TRUE) 43 | #' 44 | #' @name HalfNormal 45 | #' @aliases HalfNormal 46 | #' @aliases dhnorm 47 | #' 48 | #' @keywords distribution 49 | #' @concept Univariate 50 | #' @concept Continuous 51 | #' 52 | #' @export 53 | 54 | dhnorm <- function(x, sigma = 1, log = FALSE) { 55 | cpp_dhnorm(x, sigma, log[1L]) 56 | } 57 | 58 | 59 | #' @rdname HalfNormal 60 | #' @export 61 | 62 | phnorm <- function(q, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 63 | cpp_phnorm(q, sigma, lower.tail[1L], log.p[1L]) 64 | } 65 | 66 | 67 | #' @rdname HalfNormal 68 | #' @export 69 | 70 | qhnorm <- function(p, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 71 | cpp_qhnorm(p, sigma, lower.tail[1L], log.p[1L]) 72 | } 73 | 74 | 75 | #' @rdname HalfNormal 76 | #' @export 77 | 78 | rhnorm <- function(n, sigma = 1) { 79 | if (length(n) > 1) n <- length(n) 80 | cpp_rhnorm(n, sigma) 81 | } 82 | 83 | -------------------------------------------------------------------------------- /R/half-t-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Half-t distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the half-t distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param nu,sigma positive valued degrees of freedom and scale parameters. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' If \eqn{X} follows t distribution parametrized by degrees of freedom \eqn{\nu} 19 | #' and scale \eqn{\sigma}, then \eqn{|X|} follows half-t distribution parametrized 20 | #' by degrees of freedom \eqn{\nu} and scale \eqn{\sigma}. 21 | #' 22 | #' @references 23 | #' Gelman, A. (2006). Prior distributions for variance parameters in hierarchical 24 | #' models (comment on article by Browne and Draper). 25 | #' Bayesian analysis, 1(3), 515-534. 26 | #' 27 | #' @references 28 | #' Jacob, E. and Jayakumar, K. (2012). 29 | #' On Half-Cauchy Distribution and Process. 30 | #' International Journal of Statistika and Mathematika, 3(2), 77-81. 31 | #' 32 | #' @seealso \code{\link{HalfNormal}}, \code{\link{HalfCauchy}} 33 | #' 34 | #' @examples 35 | #' 36 | #' x <- rht(1e5, 2, 2) 37 | #' hist(x, 500, freq = FALSE, xlim = c(0, 100)) 38 | #' curve(dht(x, 2, 2), 0, 100, col = "red", add = TRUE) 39 | #' hist(pht(x, 2, 2)) 40 | #' plot(ecdf(x), xlim = c(0, 100)) 41 | #' curve(pht(x, 2, 2), 0, 100, col = "red", lwd = 2, add = TRUE) 42 | #' 43 | #' @name HalfT 44 | #' @aliases HalfT 45 | #' @aliases dht 46 | #' 47 | #' @keywords distribution 48 | #' @concept Univariate 49 | #' @concept Continuous 50 | #' 51 | #' @export 52 | 53 | dht <- function(x, nu, sigma = 1, log = FALSE) { 54 | cpp_dht(x, nu, sigma, log[1L]) 55 | } 56 | 57 | 58 | #' @rdname HalfT 59 | #' @export 60 | 61 | pht <- function(q, nu, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 62 | cpp_pht(q, nu, sigma, lower.tail[1L], log.p[1L]) 63 | } 64 | 65 | 66 | #' @rdname HalfT 67 | #' @export 68 | 69 | qht <- function(p, nu, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 70 | cpp_qht(p, nu, sigma, lower.tail[1L], log.p[1L]) 71 | } 72 | 73 | 74 | #' @rdname HalfT 75 | #' @export 76 | 77 | rht <- function(n, nu, sigma = 1) { 78 | if (length(n) > 1) n <- length(n) 79 | cpp_rht(n, nu, sigma) 80 | } 81 | 82 | -------------------------------------------------------------------------------- /R/inverse-gamma-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Inverse-gamma distribution 4 | #' 5 | #' Density, distribution function and random generation 6 | #' for the inverse-gamma distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param alpha,beta positive valued shape and scale parameters. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' 19 | #' Probability mass function 20 | #' \deqn{ 21 | #' f(x) = \frac{\beta^\alpha x^{-\alpha-1} \exp(-\frac{\beta}{x})}{\Gamma(\alpha)} 22 | #' }{ 23 | #' f(x) = (\beta^\alpha * x^(-\alpha-1) * exp(-\beta/x)) / \Gamma(\alpha) 24 | #' } 25 | #' 26 | #' Cumulative distribution function 27 | #' \deqn{ 28 | #' F(x) = \frac{\gamma(\alpha, \frac{\beta}{x})}{\Gamma(\alpha)} 29 | #' }{ 30 | #' F(x) = \gamma(\alpha, \beta/x) / \Gamma(\alpha) 31 | #' } 32 | #' 33 | #' @references 34 | #' Witkovsky, V. (2001). Computing the distribution of a linear 35 | #' combination of inverted gamma variables. Kybernetika 37(1), 79-90. 36 | #' 37 | #' @references 38 | #' Leemis, L.M. and McQueston, L.T. (2008). Univariate Distribution 39 | #' Relationships. American Statistician 62(1): 45-53. 40 | #' 41 | #' @seealso \code{\link[stats]{GammaDist}} 42 | #' 43 | #' @examples 44 | #' 45 | #' x <- rinvgamma(1e5, 20, 3) 46 | #' hist(x, 100, freq = FALSE) 47 | #' curve(dinvgamma(x, 20, 3), 0, 1, col = "red", add = TRUE, n = 5000) 48 | #' hist(pinvgamma(x, 20, 3)) 49 | #' plot(ecdf(x)) 50 | #' curve(pinvgamma(x, 20, 3), 0, 1, col = "red", lwd = 2, add = TRUE, n = 5000) 51 | #' 52 | #' @name InvGamma 53 | #' @aliases InvGamma 54 | #' @aliases dinvgamma 55 | #' 56 | #' @keywords distribution 57 | #' @concept Univariate 58 | #' @concept Continuous 59 | #' 60 | #' @export 61 | 62 | dinvgamma <- function(x, alpha, beta = 1, log = FALSE) { 63 | cpp_dinvgamma(x, alpha, 1/beta, log[1L]) 64 | } 65 | 66 | 67 | #' @rdname InvGamma 68 | #' @export 69 | 70 | pinvgamma <- function(q, alpha, beta = 1, lower.tail = TRUE, log.p = FALSE) { 71 | cpp_pinvgamma(q, alpha, beta, lower.tail, log.p[1L]) 72 | } 73 | 74 | 75 | #' @rdname InvGamma 76 | #' @export 77 | 78 | qinvgamma <- function(p, alpha, beta = 1, lower.tail = TRUE, log.p = FALSE) { 79 | 1/qgamma(p, alpha, beta, lower.tail = !lower.tail[1L], log.p = log.p[1L]) 80 | } 81 | 82 | 83 | #' @rdname InvGamma 84 | #' @export 85 | 86 | rinvgamma <- function(n, alpha, beta = 1) { 87 | 1/rgamma(n, alpha, beta) 88 | } 89 | 90 | -------------------------------------------------------------------------------- /R/kumaraswamy-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Kumaraswamy distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the Kumaraswamy distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param a,b positive valued parameters. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' 19 | #' Probability density function 20 | #' \deqn{ 21 | #' f(x) = abx^{a-1} (1-x^a)^{b-1} 22 | #' }{ 23 | #' f(x) = a*b*x^(a-1)*(1-x^a)^(b-1) 24 | #' } 25 | #' 26 | #' Cumulative distribution function 27 | #' \deqn{ 28 | #' F(x) = 1-(1-x^a)^b 29 | #' }{ 30 | #' F(x) = 1-(1-x^a)^b 31 | #' } 32 | #' 33 | #' Quantile function 34 | #' \deqn{ 35 | #' F^{-1}(p) = 1-(1-p^{1/b})^{1/a} 36 | #' }{ 37 | #' F^-1(p) = 1-(1-p^(1/b))^(1/a) 38 | #' } 39 | #' 40 | #' @references 41 | #' Jones, M. C. (2009). Kumaraswamy's distribution: A beta-type distribution with 42 | #' some tractability advantages. Statistical Methodology, 6, 70-81. 43 | #' 44 | #' @references 45 | #' Cordeiro, G.M. and de Castro, M. (2009). A new family of generalized distributions. 46 | #' Journal of Statistical Computation & Simulation, 1-17. 47 | #' 48 | #' @examples 49 | #' 50 | #' x <- rkumar(1e5, 5, 16) 51 | #' hist(x, 100, freq = FALSE) 52 | #' curve(dkumar(x, 5, 16), 0, 1, col = "red", add = TRUE) 53 | #' hist(pkumar(x, 5, 16)) 54 | #' plot(ecdf(x)) 55 | #' curve(pkumar(x, 5, 16), 0, 1, col = "red", lwd = 2, add = TRUE) 56 | #' 57 | #' @name Kumaraswamy 58 | #' @aliases Kumaraswamy 59 | #' @aliases dkumar 60 | #' 61 | #' @keywords distribution 62 | #' @concept Univariate 63 | #' @concept Continuous 64 | #' 65 | #' @export 66 | 67 | dkumar <- function(x, a = 1, b = 1, log = FALSE) { 68 | cpp_dkumar(x, a, b, log[1L]) 69 | } 70 | 71 | 72 | #' @rdname Kumaraswamy 73 | #' @export 74 | 75 | pkumar <- function(q, a = 1, b = 1, lower.tail = TRUE, log.p = FALSE) { 76 | cpp_pkumar(q, a, b, lower.tail[1L], log.p[1L]) 77 | } 78 | 79 | 80 | #' @rdname Kumaraswamy 81 | #' @export 82 | 83 | qkumar <- function(p, a = 1, b = 1, lower.tail = TRUE, log.p = FALSE) { 84 | cpp_qkumar(p, a, b, lower.tail[1L], log.p[1L]) 85 | } 86 | 87 | 88 | #' @rdname Kumaraswamy 89 | #' @export 90 | 91 | rkumar <- function(n, a = 1, b = 1) { 92 | if (length(n) > 1) n <- length(n) 93 | cpp_rkumar(n, a, b) 94 | } 95 | 96 | -------------------------------------------------------------------------------- /R/location-scale-t-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Location-scale version of the t-distribution 4 | #' 5 | #' Probability mass function, distribution function and random generation 6 | #' for location-scale version of the t-distribution. Location-scale version 7 | #' of the t-distribution besides degrees of freedom \eqn{\nu}, is parametrized 8 | #' using additional parameters \eqn{\mu} for location and \eqn{\sigma} for 9 | #' scale (\eqn{\mu = 0} and \eqn{\sigma = 1} for standard t-distribution). 10 | #' 11 | #' @param x,q vector of quantiles. 12 | #' @param p vector of probabilities. 13 | #' @param n number of observations. If \code{length(n) > 1}, 14 | #' the length is taken to be the number required. 15 | #' @param mu vector of locations 16 | #' @param sigma vector of positive valued scale parameters. 17 | #' @param df degrees of freedom (> 0, maybe non-integer). \code{df = Inf} is allowed. 18 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 19 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 20 | #' otherwise, \eqn{P[X > x]}. 21 | #' 22 | #' @seealso \code{\link[stats]{TDist}} 23 | #' 24 | #' @examples 25 | #' 26 | #' x <- rlst(1e5, 1000, 5, 13) 27 | #' hist(x, 100, freq = FALSE) 28 | #' curve(dlst(x, 1000, 5, 13), -60, 60, col = "red", add = TRUE) 29 | #' hist(plst(x, 1000, 5, 13)) 30 | #' plot(ecdf(x)) 31 | #' curve(plst(x, 1000, 5, 13), -60, 60, col = "red", lwd = 2, add = TRUE) 32 | #' 33 | #' @name LocationScaleT 34 | #' @aliases LocationScaleT 35 | #' @aliases dlst 36 | #' 37 | #' @keywords distribution 38 | #' @concept Univariate 39 | #' @concept Continuous 40 | #' 41 | #' @export 42 | 43 | dlst <- function(x, df, mu = 0, sigma = 1, log = FALSE) { 44 | cpp_dlst(x, df, mu, sigma, log[1L]) 45 | } 46 | 47 | 48 | #' @rdname LocationScaleT 49 | #' @export 50 | 51 | plst <- function(q, df, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 52 | cpp_plst(q, df, mu, sigma, lower.tail[1L], log.p[1L]) 53 | } 54 | 55 | 56 | #' @rdname LocationScaleT 57 | #' @export 58 | 59 | qlst <- function(p, df, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 60 | cpp_qlst(p, df, mu, sigma, lower.tail[1L], log.p[1L]) 61 | } 62 | 63 | 64 | #' @rdname LocationScaleT 65 | #' @export 66 | 67 | rlst <- function(n, df, mu = 0, sigma = 1) { 68 | if (length(n) > 1) n <- length(n) 69 | cpp_rlst(n, df, mu, sigma) 70 | } 71 | 72 | -------------------------------------------------------------------------------- /R/logarithmic-series-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Logarithmic series distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the logarithmic series distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param theta vector; concentration parameter; (\code{0 < theta < 1}). 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' 19 | #' Probability mass function 20 | #' \deqn{ 21 | #' f(x) = \frac{-1}{\log(1-\theta)} \frac{\theta^x}{x} 22 | #' }{ 23 | #' f(x) = (-1/log(1-\theta)*\theta^x) / x 24 | #' } 25 | #' 26 | #' Cumulative distribution function 27 | #' \deqn{ 28 | #' F(x) = \frac{-1}{\log(1-\theta)} \sum_{k=1}^x \frac{\theta^x}{x} 29 | #' }{ 30 | #' F(x) = -1/log(1-\theta) * sum((\theta^x)/x) 31 | #' } 32 | #' 33 | #' Quantile function and random generation are computed using 34 | #' algorithm described in Krishnamoorthy (2006). 35 | #' 36 | #' @references 37 | #' Krishnamoorthy, K. (2006). Handbook of Statistical Distributions 38 | #' with Applications. Chapman & Hall/CRC 39 | #' 40 | #' @references 41 | #' Forbes, C., Evans, M. Hastings, N., & Peacock, B. (2011). 42 | #' Statistical Distributions. John Wiley & Sons. 43 | #' 44 | #' @examples 45 | #' 46 | #' x <- rlgser(1e5, 0.66) 47 | #' xx <- seq(0, 100, by = 1) 48 | #' plot(prop.table(table(x)), type = "h") 49 | #' lines(xx, dlgser(xx, 0.66), col = "red") 50 | #' 51 | #' # Notice: distribution of F(X) is far from uniform: 52 | #' hist(plgser(x, 0.66), 50) 53 | #' 54 | #' xx <- seq(0, 100, by = 0.01) 55 | #' plot(ecdf(x)) 56 | #' lines(xx, plgser(xx, 0.66), col = "red", lwd = 2) 57 | #' 58 | #' @name LogSeries 59 | #' @aliases LogSeries 60 | #' @aliases dlgser 61 | #' 62 | #' @keywords distribution 63 | #' @concept Univariate 64 | #' @concept Discrete 65 | #' 66 | #' @export 67 | 68 | dlgser <- function(x, theta, log = FALSE) { 69 | cpp_dlgser(x, theta, log[1L]) 70 | } 71 | 72 | 73 | #' @rdname LogSeries 74 | #' @export 75 | 76 | plgser <- function(q, theta, lower.tail = TRUE, log.p = FALSE) { 77 | cpp_plgser(q, theta, lower.tail[1L], log.p[1L]) 78 | } 79 | 80 | 81 | #' @rdname LogSeries 82 | #' @export 83 | 84 | qlgser <- function(p, theta, lower.tail = TRUE, log.p = FALSE) { 85 | cpp_qlgser(p, theta, lower.tail[1L], log.p[1L]) 86 | } 87 | 88 | 89 | #' @rdname LogSeries 90 | #' @export 91 | 92 | rlgser <- function (n, theta) { 93 | if (length(n) > 1) n <- length(n) 94 | cpp_rlgser(n, theta) 95 | } 96 | 97 | -------------------------------------------------------------------------------- /R/lomax-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Lomax distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the Lomax distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param lambda,kappa positive valued parameters. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' 19 | #' Probability density function 20 | #' \deqn{ 21 | #' f(x) = \frac{\lambda \kappa}{(1+\lambda x)^{\kappa+1}} 22 | #' }{ 23 | #' f(x) = \lambda*\kappa / (1+\lambda*x)^(\kappa+1) 24 | #' } 25 | #' 26 | #' Cumulative distribution function 27 | #' \deqn{ 28 | #' F(x) = 1-(1+\lambda x)^{-\kappa} 29 | #' }{ 30 | #' F(x) = 1-(1+\lambda*x)^-\kappa 31 | #' } 32 | #' 33 | #' Quantile function 34 | #' \deqn{ 35 | #' F^{-1}(p) = \frac{(1-p)^{-1/\kappa} -1}{\lambda} 36 | #' }{ 37 | #' F^-1(p) = ((1-p)^(-1/\kappa)-1) / \lambda 38 | #' } 39 | #' 40 | #' @examples 41 | #' 42 | #' x <- rlomax(1e5, 5, 16) 43 | #' hist(x, 100, freq = FALSE) 44 | #' curve(dlomax(x, 5, 16), 0, 1, col = "red", add = TRUE, n = 5000) 45 | #' hist(plomax(x, 5, 16)) 46 | #' plot(ecdf(x)) 47 | #' curve(plomax(x, 5, 16), 0, 1, col = "red", lwd = 2, add = TRUE) 48 | #' 49 | #' @name Lomax 50 | #' @aliases Lomax 51 | #' @aliases dlomax 52 | #' 53 | #' @keywords distribution 54 | #' @concept Univariate 55 | #' @concept Continuous 56 | #' 57 | #' @export 58 | 59 | dlomax <- function(x, lambda, kappa, log = FALSE) { 60 | cpp_dlomax(x, lambda, kappa, log[1L]) 61 | } 62 | 63 | 64 | #' @rdname Lomax 65 | #' @export 66 | 67 | plomax <- function(q, lambda, kappa, lower.tail = TRUE, log.p = FALSE) { 68 | cpp_plomax(q, lambda, kappa, lower.tail[1L], log.p[1L]) 69 | } 70 | 71 | 72 | #' @rdname Lomax 73 | #' @export 74 | 75 | qlomax <- function(p, lambda, kappa, lower.tail = TRUE, log.p = FALSE) { 76 | cpp_qlomax(p, lambda, kappa, lower.tail[1L], log.p[1L]) 77 | } 78 | 79 | 80 | #' @rdname Lomax 81 | #' @export 82 | 83 | rlomax <- function(n, lambda, kappa) { 84 | if (length(n) > 1) n <- length(n) 85 | cpp_rlomax(n, lambda, kappa) 86 | } 87 | 88 | -------------------------------------------------------------------------------- /R/multinomial-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Multinomial distribution 4 | #' 5 | #' Probability mass function and random generation 6 | #' for the multinomial distribution. 7 | #' 8 | #' @param x \eqn{k}-column matrix of quantiles. 9 | #' @param n number of observations. If \code{length(n) > 1}, 10 | #' the length is taken to be the number required. 11 | #' @param size numeric vector; number of trials (zero or more). 12 | #' @param prob \eqn{k}-column numeric matrix; probability of success on each trial. 13 | #' @param log logical; if TRUE, probabilities p are given as log(p). 14 | #' 15 | #' @details 16 | #' 17 | #' Probability mass function 18 | #' \deqn{ 19 | #' f(x) = \frac{n!}{\prod_{i=1}^k x_i} \prod_{i=1}^k p_i^{x_i} 20 | #' }{ 21 | #' f(x) = n!/prod(x[i]!) * prod(p[i]^x[i]) 22 | #' } 23 | #' 24 | #' @references 25 | #' Gentle, J.E. (2006). Random number generation and Monte Carlo methods. Springer. 26 | #' 27 | #' @seealso \code{\link[stats]{Binomial}}, \code{\link[stats]{Multinomial}} 28 | #' 29 | #' @examples 30 | #' 31 | #' # Generating 10 random draws from multinomial distribution 32 | #' # parametrized using a vector 33 | #' 34 | #' (x <- rmnom(10, 3, c(1/3, 1/3, 1/3))) 35 | #' 36 | #' # Results are consistent with dmultinom() from stats: 37 | #' 38 | #' all.equal(dmultinom(x[1,], 3, c(1/3, 1/3, 1/3)), 39 | #' dmnom(x[1, , drop = FALSE], 3, c(1/3, 1/3, 1/3))) 40 | #' 41 | #' @name Multinomial 42 | #' @aliases Multinomial 43 | #' @aliases dmnom 44 | #' 45 | #' @keywords distribution 46 | #' @concept Multivariate 47 | #' @concept Discrete 48 | #' 49 | #' @export 50 | 51 | dmnom <- function(x, size, prob, log = FALSE) { 52 | if (is.vector(prob)) 53 | prob <- matrix(prob, nrow = 1) 54 | else if (!is.matrix(prob)) 55 | prob <- as.matrix(prob) 56 | 57 | if (is.vector(x)) 58 | x <- matrix(x, nrow = 1) 59 | else if (!is.matrix(x)) 60 | x <- as.matrix(x) 61 | 62 | cpp_dmnom(x, size, prob, log[1L]) 63 | } 64 | 65 | 66 | #' @rdname Multinomial 67 | #' @export 68 | 69 | rmnom <- function(n, size, prob) { 70 | if (length(n) > 1) 71 | n <- length(n) 72 | 73 | if (is.vector(prob)) 74 | prob <- matrix(prob, nrow = 1) 75 | else if (!is.matrix(prob)) 76 | prob <- as.matrix(prob) 77 | 78 | cpp_rmnom(n, size, prob) 79 | } 80 | 81 | -------------------------------------------------------------------------------- /R/multivariate-hypergeometric-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Multivariate hypergeometric distribution 4 | #' 5 | #' Probability mass function and random generation 6 | #' for the multivariate hypergeometric distribution. 7 | #' 8 | #' @param x \eqn{m}-column matrix of quantiles. 9 | #' @param nn number of observations. If \code{length(n) > 1}, 10 | #' the length is taken to be the number required. 11 | #' @param n \eqn{m}-length vector or \eqn{m}-column matrix 12 | #' of numbers of balls in \eqn{m} colors. 13 | #' @param k the number of balls drawn from the urn. 14 | #' @param log logical; if TRUE, probabilities p are given as log(p). 15 | #' 16 | #' @details 17 | #' 18 | #' Probability mass function 19 | #' \deqn{ 20 | #' f(x) = \frac{\prod_{i=1}^m {n_i \choose x_i}}{{N \choose k}} 21 | #' }{ 22 | #' f(x) = prod(choose(n, x)) / choose(N, k) 23 | #' } 24 | #' 25 | #' @details 26 | #' The multivariate hypergeometric distribution is generalization of 27 | #' hypergeometric distribution. It is used for sampling \emph{without} replacement 28 | #' \eqn{k} out of \eqn{N} marbles in \eqn{m} colors, where each of the colors appears 29 | #' \eqn{n_i}{n[i]} times. Where \eqn{k=\sum_{i=1}^m x_i}{k=sum(x)}, 30 | #' \eqn{N=\sum_{i=1}^m n_i}{N=sum(n)} and \eqn{k \le N}{k<=N}. 31 | #' 32 | #' @references 33 | #' Gentle, J.E. (2006). Random number generation and Monte Carlo methods. Springer. 34 | #' 35 | #' @seealso \code{\link[stats]{Hypergeometric}} 36 | #' 37 | #' @examples 38 | #' 39 | #' # Generating 10 random draws from multivariate hypergeometric 40 | #' # distribution parametrized using a vector 41 | #' 42 | #' rmvhyper(10, c(10, 12, 5, 8, 11), 33) 43 | #' 44 | #' @name MultiHypergeometric 45 | #' @aliases MultiHypergeometric 46 | #' @aliases dmvhyper 47 | #' 48 | #' @keywords distribution 49 | #' @concept Multivariate 50 | #' @concept Discrete 51 | #' 52 | #' @export 53 | 54 | dmvhyper <- function(x, n, k, log = FALSE) { 55 | if (is.vector(n)) 56 | n <- matrix(n, nrow = 1) 57 | else if (!is.matrix(n)) 58 | n <- as.matrix(n) 59 | 60 | if (is.vector(x)) 61 | x <- matrix(x, nrow = 1) 62 | else if (!is.matrix(x)) 63 | x <- as.matrix(x) 64 | 65 | cpp_dmvhyper(x, n, k, log[1L]) 66 | } 67 | 68 | 69 | #' @rdname MultiHypergeometric 70 | #' @export 71 | 72 | rmvhyper <- function(nn, n, k) { 73 | if (length(nn) > 1) 74 | nn <- length(nn) 75 | 76 | if (is.vector(n) && length(k) == 1) { 77 | if (anyNA(n) || is.na(k)) { 78 | warning("NAs produced") 79 | return(matrix(rep(NA, nn), nrow = nn, byrow = TRUE)) 80 | } 81 | if (sum(n) == k) 82 | return(matrix(rep(n, nn), nrow = nn, byrow = TRUE)) 83 | n <- matrix(n, nrow = 1) 84 | } else if (!is.matrix(n)) { 85 | n <- as.matrix(n) 86 | } 87 | 88 | cpp_rmvhyper(nn, n, k) 89 | } 90 | 91 | -------------------------------------------------------------------------------- /R/non-standard-beta-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | #' Non-standard beta distribution 3 | #' 4 | #' Non-standard form of beta distribution with lower and upper bounds 5 | #' denoted as \code{min} and \code{max}. By default \code{min=0} 6 | #' and \code{max=1} what leads to standard beta distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param shape1,shape2 non-negative parameters of the Beta distribution. 13 | #' @param min,max lower and upper bounds. 14 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 15 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \leq x]}, 16 | #' otherwise, \eqn{P[X > x]}. 17 | #' 18 | #' @seealso \code{\link[stats]{Beta}} 19 | #' 20 | #' @examples 21 | #' 22 | #' x <- rnsbeta(1e5, 5, 13, -4, 8) 23 | #' hist(x, 100, freq = FALSE) 24 | #' curve(dnsbeta(x, 5, 13, -4, 8), -4, 6, col = "red", add = TRUE) 25 | #' hist(pnsbeta(x, 5, 13, -4, 8)) 26 | #' plot(ecdf(x)) 27 | #' curve(pnsbeta(x, 5, 13, -4, 8), -4, 6, col = "red", lwd = 2, add = TRUE) 28 | #' 29 | #' @name NSBeta 30 | #' @aliases NSBeta 31 | #' @aliases dgbeta 32 | #' 33 | #' @keywords distribution 34 | #' @concept Univariate 35 | #' @concept Continuous 36 | #' 37 | #' @export 38 | 39 | dnsbeta <- function(x, shape1, shape2, min = 0, max = 1, log = FALSE) { 40 | cpp_dnsbeta(x, shape1, shape2, min, max, log[1L]) 41 | } 42 | 43 | 44 | #' @rdname NSBeta 45 | #' @export 46 | 47 | pnsbeta <- function(q, shape1, shape2, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE) { 48 | cpp_pnsbeta(q, shape1, shape2, min, max, lower.tail[1L], log.p[1L]) 49 | } 50 | 51 | 52 | #' @rdname NSBeta 53 | #' @export 54 | 55 | qnsbeta <- function(p, shape1, shape2, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE) { 56 | cpp_qnsbeta(p, shape1, shape2, min, max, lower.tail[1L], log.p[1L]) 57 | } 58 | 59 | 60 | #' @rdname NSBeta 61 | #' @export 62 | 63 | rnsbeta <- function(n, shape1, shape2, min = 0, max = 1) { 64 | if (length(n) > 1) n <- length(n) 65 | cpp_rnsbeta(n, shape1, shape2, min, max) 66 | } 67 | 68 | -------------------------------------------------------------------------------- /R/pareto-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Pareto distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the Pareto distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param a,b positive valued scale and location parameters. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' 19 | #' Probability density function 20 | #' \deqn{ 21 | #' f(x) = \frac{ab^a}{x^{a+1}} 22 | #' }{ 23 | #' f(x) = (a*b^a) / x^(a+1) 24 | #' } 25 | #' 26 | #' Cumulative distribution function 27 | #' \deqn{ 28 | #' F(x) = 1 - \left(\frac{b}{x}\right)^a 29 | #' }{ 30 | #' F(x) = 1 - (b/x)^a 31 | #' } 32 | #' 33 | #' Quantile function 34 | #' \deqn{ 35 | #' F^{-1}(p) = \frac{b}{(1-p)^{1-a}} 36 | #' }{ 37 | #' F^-1(p) = b/(1-p)^(1-a) 38 | #' } 39 | #' 40 | #' @references 41 | #' Krishnamoorthy, K. (2006). Handbook of Statistical Distributions 42 | #' with Applications. Chapman & Hall/CRC 43 | #' 44 | #' @examples 45 | #' 46 | #' x <- rpareto(1e5, 5, 16) 47 | #' hist(x, 100, freq = FALSE) 48 | #' curve(dpareto(x, 5, 16), 0, 200, col = "red", add = TRUE) 49 | #' hist(ppareto(x, 5, 16)) 50 | #' plot(ecdf(x)) 51 | #' curve(ppareto(x, 5, 16), 0, 200, col = "red", lwd = 2, add = TRUE) 52 | #' 53 | #' @name Pareto 54 | #' @aliases Pareto 55 | #' @aliases dpareto 56 | #' 57 | #' @keywords distribution 58 | #' @concept Univariate 59 | #' @concept Continuous 60 | #' 61 | #' @export 62 | 63 | dpareto <- function(x, a = 1, b = 1, log = FALSE) { 64 | cpp_dpareto(x, a, b, log[1L]) 65 | } 66 | 67 | 68 | #' @rdname Pareto 69 | #' @export 70 | 71 | ppareto <- function(q, a = 1, b = 1, lower.tail = TRUE, log.p = FALSE) { 72 | cpp_ppareto(q, a, b, lower.tail[1L], log.p[1L]) 73 | } 74 | 75 | 76 | #' @rdname Pareto 77 | #' @export 78 | 79 | qpareto <- function(p, a = 1, b = 1, lower.tail = TRUE, log.p = FALSE) { 80 | cpp_qpareto(p, a, b, lower.tail[1L], log.p[1L]) 81 | } 82 | 83 | 84 | #' @rdname Pareto 85 | #' @export 86 | 87 | rpareto <- function(n, a = 1, b = 1) { 88 | if (length(n) > 1) n <- length(n) 89 | cpp_rpareto(n, a, b) 90 | } 91 | 92 | -------------------------------------------------------------------------------- /R/power-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Power distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the power distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param alpha,beta parameters. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' 19 | #' Probability density function 20 | #' \deqn{ 21 | #' f(x) = \frac{\beta x^{\beta-1}}{\alpha^\beta} 22 | #' }{ 23 | #' f(x) = (\beta*x^(\beta-1)) / (\alpha^\beta) 24 | #' } 25 | #' 26 | #' Cumulative distribution function 27 | #' \deqn{ 28 | #' F(x) = \frac{x^\beta}{\alpha^\beta} 29 | #' }{ 30 | #' F(x) = x^\beta / \alpha^\beta 31 | #' } 32 | #' 33 | #' Quantile function 34 | #' \deqn{ 35 | #' F^{-1}(p) = \alpha p^{1/\beta} 36 | #' }{ 37 | #' F^-1(p) = \alpha * p^(1/\beta) 38 | #' } 39 | #' 40 | #' @examples 41 | #' 42 | #' x <- rpower(1e5, 5, 16) 43 | #' hist(x, 100, freq = FALSE) 44 | #' curve(dpower(x, 5, 16), 2, 6, col = "red", add = TRUE, n = 5000) 45 | #' hist(ppower(x, 5, 16)) 46 | #' plot(ecdf(x)) 47 | #' curve(ppower(x, 5, 16), 2, 6, col = "red", lwd = 2, add = TRUE) 48 | #' 49 | #' @name PowerDist 50 | #' @aliases PowerDist 51 | #' @aliases dpower 52 | #' 53 | #' @keywords distribution 54 | #' @concept Univariate 55 | #' @concept Continuous 56 | #' 57 | #' @export 58 | 59 | dpower <- function(x, alpha, beta, log = FALSE) { 60 | cpp_dpower(x, alpha, beta, log[1L]) 61 | } 62 | 63 | 64 | #' @rdname PowerDist 65 | #' @export 66 | 67 | ppower <- function(q, alpha, beta, lower.tail = TRUE, log.p = FALSE) { 68 | cpp_ppower(q, alpha, beta, lower.tail[1L], log.p[1L]) 69 | } 70 | 71 | 72 | #' @rdname PowerDist 73 | #' @export 74 | 75 | qpower <- function(p, alpha, beta, lower.tail = TRUE, log.p = FALSE) { 76 | cpp_qpower(p, alpha, beta, lower.tail[1L], log.p[1L]) 77 | } 78 | 79 | 80 | #' @rdname PowerDist 81 | #' @export 82 | 83 | rpower <- function(n, alpha, beta) { 84 | if (length(n) > 1) n <- length(n) 85 | cpp_rpower(n, alpha, beta) 86 | } 87 | 88 | -------------------------------------------------------------------------------- /R/rademacher-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Random generation from Rademacher distribution 4 | #' 5 | #' Random generation for the Rademacher distribution (values -1 and +1 6 | #' with equal probability). 7 | #' 8 | #' @param n number of observations. If \code{length(n) > 1}, 9 | #' the length is taken to be the number required. 10 | #' 11 | #' @name Rademacher 12 | #' @aliases Rademacher 13 | #' @aliases rsign 14 | #' 15 | #' @keywords distribution 16 | #' 17 | #' @export 18 | 19 | rsign <- function(n) { 20 | if (length(n) > 1) n <- length(n) 21 | cpp_rsign(n) 22 | } 23 | 24 | -------------------------------------------------------------------------------- /R/rayleigh-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Rayleigh distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the Rayleigh distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param sigma positive valued parameter. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' 19 | #' Probability density function 20 | #' \deqn{ 21 | #' f(x) = \frac{x}{\sigma^2} \exp\left(-\frac{x^2}{2\sigma^2}\right) 22 | #' }{ 23 | #' f(x) = x/\sigma^2 * exp(-(x^2 / 2*\sigma^2)) 24 | #' } 25 | #' 26 | #' Cumulative distribution function 27 | #' \deqn{ 28 | #' F(x) = 1 - \exp\left(-\frac{x^2}{2\sigma^2}\right) 29 | #' }{ 30 | #' F(x) = 1 - exp(-x^2 / 2*\sigma^2) 31 | #' } 32 | #' 33 | #' Quantile function 34 | #' \deqn{ 35 | #' F^{-1}(p) = \sqrt{-2\sigma^2 \log(1-p)} 36 | #' }{ 37 | #' F^-1(p) = sqrt(-2*\sigma^2 * log(1-p)) 38 | #' } 39 | #' 40 | #' @references 41 | #' Krishnamoorthy, K. (2006). Handbook of Statistical Distributions 42 | #' with Applications. Chapman & Hall/CRC. 43 | #' 44 | #' @references 45 | #' Forbes, C., Evans, M. Hastings, N., & Peacock, B. (2011). 46 | #' Statistical Distributions. John Wiley & Sons. 47 | #' 48 | #' @examples 49 | #' 50 | #' x <- rrayleigh(1e5, 13) 51 | #' hist(x, 100, freq = FALSE) 52 | #' curve(drayleigh(x, 13), 0, 60, col = "red", add = TRUE) 53 | #' hist(prayleigh(x, 13)) 54 | #' plot(ecdf(x)) 55 | #' curve(prayleigh(x, 13), 0, 60, col = "red", lwd = 2, add = TRUE) 56 | #' 57 | #' @name Rayleigh 58 | #' @aliases Rayleigh 59 | #' @aliases drayleigh 60 | #' 61 | #' @keywords distribution 62 | #' @concept Univariate 63 | #' @concept Continuous 64 | #' 65 | #' @export 66 | 67 | drayleigh <- function(x, sigma = 1, log = FALSE) { 68 | cpp_drayleigh(x, sigma, log[1L]) 69 | } 70 | 71 | 72 | #' @rdname Rayleigh 73 | #' @export 74 | 75 | prayleigh <- function(q, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 76 | cpp_prayleigh(q, sigma, lower.tail[1L], log.p[1L]) 77 | } 78 | 79 | 80 | #' @rdname Rayleigh 81 | #' @export 82 | 83 | qrayleigh <- function(p, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 84 | cpp_qrayleigh(p, sigma, lower.tail[1L], log.p[1L]) 85 | } 86 | 87 | 88 | #' @rdname Rayleigh 89 | #' @export 90 | 91 | rrayleigh <- function(n, sigma = 1) { 92 | if (length(n) > 1) n <- length(n) 93 | cpp_rrayleigh(n, sigma) 94 | } 95 | -------------------------------------------------------------------------------- /R/skellam-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Skellam distribution 4 | #' 5 | #' Probability mass function and random generation 6 | #' for the Skellam distribution. 7 | #' 8 | #' @param x vector of quantiles. 9 | #' @param n number of observations. If \code{length(n) > 1}, 10 | #' the length is taken to be the number required. 11 | #' @param mu1,mu2 positive valued parameters. 12 | #' @param log logical; if TRUE, probabilities p are given as log(p). 13 | #' 14 | #' @details 15 | #' 16 | #' If \eqn{X} and \eqn{Y} follow Poisson distributions with means 17 | #' \eqn{\mu_1}{\mu[1]} and \eqn{\mu_2}{\mu[2]}, than \eqn{X-Y} follows 18 | #' Skellam distribution parametrized by \eqn{\mu_1}{\mu[1]} and \eqn{\mu_2}{\mu[2]}. 19 | #' 20 | #' Probability mass function 21 | #' \deqn{ 22 | #' f(x) = e^{-(\mu_1\!+\!\mu_2)} \left(\frac{\mu_1}{\mu_2}\right)^{k/2}\!\!I_{k}(2\sqrt{\mu_1\mu_2}) 23 | #' }{ 24 | #' f(x) = exp(-(\mu1+\mu2)) * (\mu1/\mu2)^(x/2) * besselI(2*sqrt(\mu1*\mu2), x) 25 | #' } 26 | #' 27 | #' @references 28 | #' Karlis, D., & Ntzoufras, I. (2006). Bayesian analysis of the differences of count data. 29 | #' Statistics in medicine, 25(11), 1885-1905. 30 | #' 31 | #' @references 32 | #' Skellam, J.G. (1946). The frequency distribution of the difference between 33 | #' two Poisson variates belonging to different populations. 34 | #' Journal of the Royal Statistical Society, series A, 109(3), 26. 35 | #' 36 | #' @examples 37 | #' 38 | #' x <- rskellam(1e5, 5, 13) 39 | #' xx <- -40:40 40 | #' plot(prop.table(table(x)), type = "h") 41 | #' lines(xx, dskellam(xx, 5, 13), col = "red") 42 | #' 43 | #' @name Skellam 44 | #' @aliases Skellam 45 | #' @aliases dskellam 46 | #' 47 | #' @keywords distribution 48 | #' @concept Univariate 49 | #' @concept Discrete 50 | #' 51 | #' @export 52 | 53 | dskellam <- function(x, mu1, mu2, log = FALSE) { 54 | cpp_dskellam(x, mu1, mu2, log[1L]) 55 | } 56 | 57 | 58 | #' @rdname Skellam 59 | #' @export 60 | 61 | rskellam <- function(n, mu1, mu2) { 62 | if (length(n) > 1) n <- length(n) 63 | cpp_rskellam(n, mu1, mu2) 64 | } 65 | 66 | -------------------------------------------------------------------------------- /R/slash-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Slash distribution 4 | #' 5 | #' Probability mass function, distribution function and random generation 6 | #' for slash distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param n number of observations. If \code{length(n) > 1}, 10 | #' the length is taken to be the number required. 11 | #' @param mu vector of locations 12 | #' @param sigma vector of positive valued scale parameters. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' 19 | #' If \eqn{Z \sim \mathrm{Normal}(0, 1)}{Z ~ Normal(0, 1)} and \eqn{U \sim \mathrm{Uniform}(0, 1)}{U ~ Uniform(0, 1)}, 20 | #' then \eqn{Z/U} follows slash distribution. 21 | #' 22 | #' Probability density function 23 | #' 24 | #' \deqn{ 25 | #' f(x) = \left\{\begin{array}{ll} 26 | #' \frac{\phi(0) - \phi(x)}{x^2} & x \ne 0 \\ 27 | #' \frac{1}{2\sqrt{2\pi}} & x = 0 28 | #' \end{array}\right. 29 | #' }{ 30 | #' f(x) = [if x != 0:] (\phi(0)-\phi(x))/x^2 [else:] 1/(2*sqrt(2*\pi)) 31 | #' } 32 | #' 33 | #' Cumulative distribution function 34 | #' 35 | #' \deqn{ 36 | #' F(x) = \left\{\begin{array}{ll} 37 | #' \Phi(x) - \frac{\phi(0)-\phi(x)}{x} & x \neq 0 \\ 38 | #' \frac{1}{2} & x = 0 39 | #' \end{array}\right. 40 | #' }{ 41 | #' F(x) = [if x != 0:] \Phi(x) - [\phi(0)-\phi(x)]/x [else:] 1/2 42 | #' } 43 | #' 44 | #' @examples 45 | #' 46 | #' x <- rslash(1e5, 5, 3) 47 | #' hist(x, 1e5, freq = FALSE, xlim = c(-100, 100)) 48 | #' curve(dslash(x, 5, 3), -100, 100, col = "red", n = 500, add = TRUE) 49 | #' hist(pslash(x, 5, 3)) 50 | #' plot(ecdf(x), xlim = c(-100, 100)) 51 | #' curve(pslash(x, 5, 3), -100, 100, col = "red", lwd = 2, n = 500, add = TRUE) 52 | #' 53 | #' @name Slash 54 | #' @aliases Slash 55 | #' @aliases dslash 56 | #' 57 | #' @keywords distribution 58 | #' @concept Univariate 59 | #' @concept Continuous 60 | #' 61 | #' @export 62 | 63 | dslash <- function(x, mu = 0, sigma = 1, log = FALSE) { 64 | cpp_dslash(x, mu, sigma, log[1L]) 65 | } 66 | 67 | 68 | #' @rdname Slash 69 | #' @export 70 | 71 | pslash <- function(q, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) { 72 | cpp_pslash(q, mu, sigma, lower.tail[1L], log.p[1L]) 73 | } 74 | 75 | 76 | #' @rdname Slash 77 | #' @export 78 | 79 | rslash <- function(n, mu = 0, sigma = 1) { 80 | if (length(n) > 1) n <- length(n) 81 | cpp_rslash(n, mu, sigma) 82 | } 83 | 84 | -------------------------------------------------------------------------------- /R/truncated-binomial-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Truncated binomial distribution 4 | #' 5 | #' Density, distribution function, quantile function and random generation 6 | #' for the truncated binomial distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param size number of trials (zero or more). 13 | #' @param prob probability of success on each trial. 14 | #' @param a,b lower and upper truncation points (\code{a < x <= b}). 15 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 16 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 17 | #' otherwise, \eqn{P[X > x]}. 18 | #' 19 | #' @examples 20 | #' 21 | #' x <- rtbinom(1e5, 100, 0.83, 76, 86) 22 | #' xx <- seq(0, 100) 23 | #' plot(prop.table(table(x))) 24 | #' lines(xx, dtbinom(xx, 100, 0.83, 76, 86), col = "red") 25 | #' hist(ptbinom(x, 100, 0.83, 76, 86)) 26 | #' 27 | #' xx <- seq(0, 100, by = 0.01) 28 | #' plot(ecdf(x)) 29 | #' lines(xx, ptbinom(xx, 100, 0.83, 76, 86), col = "red", lwd = 2) 30 | #' uu <- seq(0, 1, by = 0.001) 31 | #' lines(qtbinom(uu, 100, 0.83, 76, 86), uu, col = "blue", lty = 2) 32 | #' 33 | #' @name TruncBinom 34 | #' @aliases TruncBinom 35 | #' @aliases dtbinom 36 | #' 37 | #' @keywords distribution 38 | #' @concept Univariate 39 | #' @concept Discrete 40 | #' 41 | #' @export 42 | 43 | dtbinom <- function(x, size, prob, a = -Inf, b = Inf, log = FALSE) { 44 | cpp_dtbinom(x, size, prob, a, b, log[1L]) 45 | } 46 | 47 | 48 | #' @rdname TruncBinom 49 | #' @export 50 | 51 | ptbinom <- function(q, size, prob, a = -Inf, b = Inf, lower.tail = TRUE, log.p = FALSE) { 52 | cpp_ptbinom(q, size, prob, a, b, lower.tail[1L], log.p[1L]) 53 | } 54 | 55 | 56 | #' @rdname TruncBinom 57 | #' @export 58 | 59 | qtbinom <- function(p, size, prob, a = -Inf, b = Inf, lower.tail = TRUE, log.p = FALSE) { 60 | cpp_qtbinom(p, size, prob, a, b, lower.tail[1L], log.p[1L]) 61 | } 62 | 63 | 64 | #' @rdname TruncBinom 65 | #' @export 66 | 67 | rtbinom <- function(n, size, prob, a = -Inf, b = Inf) { 68 | if (length(n) > 1) n <- length(n) 69 | cpp_rtbinom(n, size, prob, a, b) 70 | } 71 | 72 | -------------------------------------------------------------------------------- /R/wald-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Wald (inverse Gaussian) distribution 4 | #' 5 | #' Density, distribution function and random generation 6 | #' for the Wald distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param mu,lambda location and shape parameters. Scale must be positive. 13 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 14 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 15 | #' otherwise, \eqn{P[X > x]}. 16 | #' 17 | #' @details 18 | #' 19 | #' Probability density function 20 | #' \deqn{ 21 | #' f(x) = \sqrt{\frac{\lambda}{2\pi x^3}} \exp\left( \frac{-\lambda(x-\mu)^2}{2\mu^2 x} \right) 22 | #' }{ 23 | #' f(x) = sqrt(\lambda/(2*\pi*x^3)) * exp((-\lambda*(x-\mu)^2)/(2*\mu^2*x)) 24 | #' } 25 | #' 26 | #' Cumulative distribution function 27 | #' \deqn{ 28 | #' F(x) = \Phi\left(\sqrt{\frac{\lambda}{x}} \left(\frac{x}{\mu}-1 \right) \right) + 29 | #' \exp\left(\frac{2\lambda}{\mu} \right) \Phi\left(\sqrt{\frac{\lambda}{x}} 30 | #' \left(\frac{x}{\mu}+1 \right) \right) 31 | #' }{ 32 | #' F(x) = \Phi(sqrt(\lambda/\mu)*(x/\mu-1)) - exp((2*\lambda)/\mu) * 33 | #' \Phi(sqrt(\lambda/\mu)*(x/\mu+1)) 34 | #' } 35 | #' 36 | #' Random generation is done using the algorithm described by Michael, Schucany and Haas (1976). 37 | #' 38 | #' @references 39 | #' 40 | #' Michael, J.R., Schucany, W.R., and Haas, R.W. (1976). 41 | #' Generating Random Variates Using Transformations with Multiple Roots. 42 | #' The American Statistician, 30(2): 88-90. 43 | #' 44 | #' @examples 45 | #' 46 | #' x <- rwald(1e5, 5, 16) 47 | #' hist(x, 100, freq = FALSE) 48 | #' curve(dwald(x, 5, 16), 0, 50, col = "red", add = TRUE) 49 | #' hist(pwald(x, 5, 16)) 50 | #' plot(ecdf(x)) 51 | #' curve(pwald(x, 5, 16), 0, 50, col = "red", lwd = 2, add = TRUE) 52 | #' 53 | #' @name Wald 54 | #' @aliases Wald 55 | #' @aliases dwald 56 | #' 57 | #' @keywords distribution 58 | #' @concept Univariate 59 | #' @concept Continuous 60 | #' 61 | #' @export 62 | 63 | dwald <- function(x, mu, lambda, log = FALSE) { 64 | cpp_dwald(x, mu, lambda, log[1L]) 65 | } 66 | 67 | 68 | #' @rdname Wald 69 | #' @export 70 | 71 | pwald <- function(q, mu, lambda, lower.tail = TRUE, log.p = FALSE) { 72 | cpp_pwald(q, mu, lambda, lower.tail[1L], log.p[1L]) 73 | } 74 | 75 | 76 | #' @rdname Wald 77 | #' @export 78 | 79 | rwald <- function(n, mu, lambda) { 80 | if (length(n) > 1) n <- length(n) 81 | cpp_rwald(n, mu, lambda) 82 | } 83 | 84 | -------------------------------------------------------------------------------- /R/zero-inflated-binomial-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Zero-inflated binomial distribution 4 | #' 5 | #' Probability mass function and random generation 6 | #' for the zero-inflated binomial distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param size number of trials (zero or more). 13 | #' @param prob probability of success in each trial. \code{0 < prob <= 1}. 14 | #' @param pi probability of extra zeros. 15 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 16 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 17 | #' otherwise, \eqn{P[X > x]}. 18 | #' 19 | #' @details 20 | #' 21 | #' Probability density function 22 | #' \deqn{ 23 | #' f(x) = \left\{\begin{array}{ll} 24 | #' \pi + (1 - \pi) (1-p)^n & x = 0 \\ 25 | #' (1 - \pi) {n \choose x} p^x (1-p)^{n-x} & x > 0 \\ 26 | #' \end{array}\right. 27 | #' }{ 28 | #' f(x) = [if x = 0:] (1-\pi)+\pi * p^r [else:] (1-\pi) * dnbinom(x, size, prob) 29 | #' } 30 | #' 31 | #' @seealso \code{\link[stats]{Binomial}} 32 | #' 33 | #' @examples 34 | #' 35 | #' x <- rzib(1e5, 10, 0.6, 0.33) 36 | #' xx <- -2:20 37 | #' plot(prop.table(table(x)), type = "h") 38 | #' lines(xx, dzib(xx, 10, 0.6, 0.33), col = "red") 39 | #' 40 | #' xx <- seq(0, 20, by = 0.01) 41 | #' plot(ecdf(x)) 42 | #' lines(xx, pzib(xx, 10, 0.6, 0.33), col = "red") 43 | #' 44 | #' @name ZIB 45 | #' @aliases ZIB 46 | #' @aliases dzib 47 | #' 48 | #' @keywords distribution 49 | #' @concept Univariate 50 | #' @concept Discrete 51 | #' 52 | #' @export 53 | 54 | dzib <- function(x, size, prob, pi, log = FALSE) { 55 | cpp_dzib(x, size, prob, pi, log[1L]) 56 | } 57 | 58 | 59 | #' @rdname ZIB 60 | #' @export 61 | 62 | pzib <- function(q, size, prob, pi, lower.tail = TRUE, log.p = FALSE) { 63 | cpp_pzib(q, size, prob, pi, lower.tail[1L], log.p[1L]) 64 | } 65 | 66 | 67 | #' @rdname ZIB 68 | #' @export 69 | 70 | qzib <- function(p, size, prob, pi, lower.tail = TRUE, log.p = FALSE) { 71 | cpp_qzib(p, size, prob, pi, lower.tail[1L], log.p[1L]) 72 | } 73 | 74 | 75 | #' @rdname ZIB 76 | #' @export 77 | 78 | rzib <- function(n, size, prob, pi) { 79 | if (length(n) > 1) n <- length(n) 80 | cpp_rzib(n, size, prob, pi) 81 | } 82 | 83 | -------------------------------------------------------------------------------- /R/zero-inflated-negative-binomial-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Zero-inflated negative binomial distribution 4 | #' 5 | #' Probability mass function and random generation 6 | #' for the zero-inflated negative binomial distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param size target for number of successful trials, or dispersion 13 | #' parameter (the shape parameter of the gamma mixing 14 | #' distribution). Must be strictly positive, need not be 15 | #' integer. 16 | #' @param prob probability of success in each trial. \code{0 < prob <= 1}. 17 | #' @param pi probability of extra zeros. 18 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 19 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 20 | #' otherwise, \eqn{P[X > x]}. 21 | #' 22 | #' @details 23 | #' 24 | #' Probability density function 25 | #' \deqn{ 26 | #' f(x) = \left\{\begin{array}{ll} 27 | #' \pi + (1 - \pi) p^r & x = 0 \\ 28 | #' (1 - \pi) {x+r-1 \choose x} p^r (1-p)^x & x > 0 \\ 29 | #' \end{array}\right. 30 | #' }{ 31 | #' f(x) = [if x = 0:] (1-\pi)+\pi * p^r [else:] (1-\pi) * dnbinom(x, size, prob) 32 | #' } 33 | #' 34 | #' @seealso \code{\link[stats]{NegBinomial}} 35 | #' 36 | #' @examples 37 | #' 38 | #' x <- rzinb(1e5, 100, 0.6, 0.33) 39 | #' xx <- -2:200 40 | #' plot(prop.table(table(x)), type = "h") 41 | #' lines(xx, dzinb(xx, 100, 0.6, 0.33), col = "red") 42 | #' 43 | #' xx <- seq(0, 200, by = 0.01) 44 | #' plot(ecdf(x)) 45 | #' lines(xx, pzinb(xx, 100, 0.6, 0.33), col = "red") 46 | #' 47 | #' @name ZINB 48 | #' @aliases ZINB 49 | #' @aliases dzinb 50 | #' 51 | #' @keywords distribution 52 | #' @concept Univariate 53 | #' @concept Discrete 54 | #' 55 | #' @export 56 | 57 | dzinb <- function(x, size, prob, pi, log = FALSE) { 58 | cpp_dzinb(x, size, prob, pi, log[1L]) 59 | } 60 | 61 | 62 | #' @rdname ZINB 63 | #' @export 64 | 65 | pzinb <- function(q, size, prob, pi, lower.tail = TRUE, log.p = FALSE) { 66 | cpp_pzinb(q, size, prob, pi, lower.tail[1L], log.p[1L]) 67 | } 68 | 69 | 70 | #' @rdname ZINB 71 | #' @export 72 | 73 | qzinb <- function(p, size, prob, pi, lower.tail = TRUE, log.p = FALSE) { 74 | cpp_qzinb(p, size, prob, pi, lower.tail[1L], log.p[1L]) 75 | } 76 | 77 | 78 | #' @rdname ZINB 79 | #' @export 80 | 81 | rzinb <- function(n, size, prob, pi) { 82 | if (length(n) > 1) n <- length(n) 83 | cpp_rzinb(n, size, prob, pi) 84 | } 85 | 86 | -------------------------------------------------------------------------------- /R/zero-inflated-poisson-distribution.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Zero-inflated Poisson distribution 4 | #' 5 | #' Probability mass function and random generation 6 | #' for the zero-inflated Poisson distribution. 7 | #' 8 | #' @param x,q vector of quantiles. 9 | #' @param p vector of probabilities. 10 | #' @param n number of observations. If \code{length(n) > 1}, 11 | #' the length is taken to be the number required. 12 | #' @param lambda vector of (non-negative) means. 13 | #' @param pi probability of extra zeros. 14 | #' @param log,log.p logical; if TRUE, probabilities p are given as log(p). 15 | #' @param lower.tail logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 16 | #' otherwise, \eqn{P[X > x]}. 17 | #' 18 | #' @details 19 | #' 20 | #' Probability density function 21 | #' \deqn{ 22 | #' f(x) = \left\{\begin{array}{ll} 23 | #' \pi + (1 - \pi) e^{-\lambda} & x = 0 \\ 24 | #' (1 - \pi) \frac{\lambda^{x} e^{-\lambda}} {x!} & x > 0 \\ 25 | #' \end{array}\right. 26 | #' }{ 27 | #' f(x) = [if x = 0:] \pi + (1-\pi) * exp(-\lambda) [else:] (1-\pi) * dpois(x, lambda) 28 | #' } 29 | #' 30 | #' @seealso \code{\link[stats]{Poisson}} 31 | #' 32 | #' @examples 33 | #' 34 | #' x <- rzip(1e5, 6, 0.33) 35 | #' xx <- -2:20 36 | #' plot(prop.table(table(x)), type = "h") 37 | #' lines(xx, dzip(xx, 6, 0.33), col = "red") 38 | #' 39 | #' xx <- seq(0, 20, by = 0.01) 40 | #' plot(ecdf(x)) 41 | #' lines(xx, pzip(xx, 6, 0.33), col = "red") 42 | #' 43 | #' @name ZIP 44 | #' @aliases ZIP 45 | #' @aliases dzip 46 | #' 47 | #' @keywords distribution 48 | #' @concept Univariate 49 | #' @concept Discrete 50 | #' 51 | #' @export 52 | 53 | dzip <- function(x, lambda, pi, log = FALSE) { 54 | cpp_dzip(x, lambda, pi, log[1L]) 55 | } 56 | 57 | 58 | #' @rdname ZIP 59 | #' @export 60 | 61 | pzip <- function(q, lambda, pi, lower.tail = TRUE, log.p = FALSE) { 62 | cpp_pzip(q, lambda, pi, lower.tail[1L], log.p[1L]) 63 | } 64 | 65 | 66 | #' @rdname ZIP 67 | #' @export 68 | 69 | qzip <- function(p, lambda, pi, lower.tail = TRUE, log.p = FALSE) { 70 | cpp_qzip(p, lambda, pi, lower.tail[1L], log.p[1L]) 71 | } 72 | 73 | 74 | #' @rdname ZIP 75 | #' @export 76 | 77 | rzip <- function(n, lambda, pi) { 78 | if (length(n) > 1) n <- length(n) 79 | cpp_rzip(n, lambda, pi) 80 | } 81 | 82 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | 2 | .onUnload <- function (libpath) { 3 | library.dynam.unload("extraDistr", libpath) 4 | } 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Warning: this repository is not activelly maintained anymore 3 | 4 | If you have a feature request or bug report, feel free to send a pull request. 5 | 6 | --- 7 | 8 | Density, distribution function, quantile function and random 9 | generation for a number of univariate and multivariate distributions. 10 | 11 | This package follows naming convention that is consistent with base R, 12 | where density (or probability mass) functions, distribution functions, 13 | quantile functions and random generation functions names are followed by 14 | `d`\*, `p`\*, `q`\*, and `r`\* prefixes. 15 | 16 | Behaviour of the functions mimics the base R, where for 17 | invalid parameters `NaN`'s are returned, while 18 | for values beyond function support 0's are returned 19 | (e.g. for non-integers in discrete distributions, or for 20 | negative values in functions with non-negative support). 21 | 22 | All the functions vectorized and coded in C++11 using [Rcpp](https://www.rcpp.org/). 23 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # DO NOT CHANGE the "init" and "install" sections below 2 | 3 | # Download script file from GitHub 4 | init: 5 | ps: | 6 | $ErrorActionPreference = "Stop" 7 | Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1" 8 | Import-Module '..\appveyor-tool.ps1' 9 | 10 | install: 11 | ps: Bootstrap 12 | 13 | # Adapt as necessary starting from here 14 | 15 | build_script: 16 | - travis-tool.sh install_deps 17 | 18 | test_script: 19 | - travis-tool.sh run_tests 20 | 21 | on_failure: 22 | - 7z a failure.zip *.Rcheck\* 23 | - appveyor PushArtifact failure.zip 24 | 25 | artifacts: 26 | - path: '*.Rcheck\**\*.log' 27 | name: Logs 28 | 29 | - path: '*.Rcheck\**\*.out' 30 | name: Logs 31 | 32 | - path: '*.Rcheck\**\*.fail' 33 | name: Logs 34 | 35 | - path: '*.Rcheck\**\*.Rout' 36 | name: Logs 37 | 38 | - path: '\*_*.tar.gz' 39 | name: Bits 40 | 41 | - path: '\*_*.zip' 42 | name: Bits 43 | 44 | - path: '*.Rcheck\**\*.pdf' 45 | name: Pdfs 46 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | ## Test environments 2 | 3 | * Windows, Ubuntu, Mac OS on GitHub Actions 4 | 5 | ## Comments 6 | 7 | This is a bugfix. 8 | -------------------------------------------------------------------------------- /extraDistr.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | LineEndingConversion: Posix 16 | 17 | BuildType: Package 18 | PackageUseDevtools: Yes 19 | PackageInstallArgs: --no-multiarch --with-keep.source 20 | PackageRoxygenize: rd,collate,namespace 21 | -------------------------------------------------------------------------------- /inst/include/extraDistr.h: -------------------------------------------------------------------------------- 1 | // Generated by using Rcpp::compileAttributes() -> do not edit by hand 2 | // Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 3 | 4 | #ifndef RCPP_extraDistr_H_GEN_ 5 | #define RCPP_extraDistr_H_GEN_ 6 | 7 | #include "extraDistr_RcppExports.h" 8 | 9 | #endif // RCPP_extraDistr_H_GEN_ 10 | -------------------------------------------------------------------------------- /man/Bernoulli.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bernoulli-distribution.R 3 | \name{Bernoulli} 4 | \alias{Bernoulli} 5 | \alias{dbern} 6 | \alias{pbern} 7 | \alias{qbern} 8 | \alias{rbern} 9 | \title{Bernoulli distribution} 10 | \usage{ 11 | dbern(x, prob = 0.5, log = FALSE) 12 | 13 | pbern(q, prob = 0.5, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qbern(p, prob = 0.5, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rbern(n, prob = 0.5) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{prob}{probability of success; (\code{0 < prob < 1}).} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Probability mass function, distribution function, quantile function and random generation 36 | for the Bernoulli distribution. 37 | } 38 | \examples{ 39 | 40 | prop.table(table(rbern(1e5, 0.5))) 41 | 42 | } 43 | \seealso{ 44 | \code{\link[stats]{Binomial}} 45 | } 46 | \concept{Discrete} 47 | \concept{Univariate} 48 | \keyword{distribution} 49 | -------------------------------------------------------------------------------- /man/BetaPrime.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/beta-prime-distribution.R 3 | \name{BetaPrime} 4 | \alias{BetaPrime} 5 | \alias{dbetapr} 6 | \alias{pbetapr} 7 | \alias{qbetapr} 8 | \alias{rbetapr} 9 | \title{Beta prime distribution} 10 | \usage{ 11 | dbetapr(x, shape1, shape2, scale = 1, log = FALSE) 12 | 13 | pbetapr(q, shape1, shape2, scale = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qbetapr(p, shape1, shape2, scale = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rbetapr(n, shape1, shape2, scale = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{shape1, shape2}{non-negative parameters.} 23 | 24 | \item{scale}{positive valued scale parameter.} 25 | 26 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 27 | 28 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 29 | otherwise, \eqn{P[X > x]}.} 30 | 31 | \item{p}{vector of probabilities.} 32 | 33 | \item{n}{number of observations. If \code{length(n) > 1}, 34 | the length is taken to be the number required.} 35 | } 36 | \description{ 37 | Density, distribution function, quantile function and random generation 38 | for the beta prime distribution. 39 | } 40 | \details{ 41 | If \eqn{X \sim \mathrm{Beta}(\alpha, \beta)}{X ~ Beta(\alpha, \beta)}, then 42 | \eqn{\frac{X}{1-X} \sim \mathrm{BetaPrime}(\alpha, \beta)}{X/(1-X) ~ BetaPrime(\alpha, \beta)}. 43 | 44 | Probability density function 45 | 46 | \deqn{ 47 | f(x) = \frac{(x/\sigma)^{\alpha-1} (1+x/\sigma)^{-\alpha -\beta}}{\mathrm{B}(\alpha,\beta)\sigma} 48 | }{ 49 | f(x) = ((x/\sigma)^(\alpha-1) * (1 + x/\sigma)^(-\alpha-\beta)) / (B(\alpha,\beta) * \sigma) 50 | } 51 | 52 | Cumulative distribution function 53 | 54 | \deqn{ 55 | F(x) = I_{\frac{x/\sigma}{1+x/\sigma}}(\alpha, \beta) 56 | }{ 57 | F(x) = pbeta((x/\sigma)/(1+(x/\sigma)), \alpha, \beta) 58 | } 59 | } 60 | \examples{ 61 | 62 | x <- rbetapr(1e5, 5, 3, 2) 63 | hist(x, 350, freq = FALSE, xlim = c(0, 100)) 64 | curve(dbetapr(x, 5, 3, 2), 0, 100, col = "red", add = TRUE, n = 500) 65 | hist(pbetapr(x, 5, 3, 2)) 66 | plot(ecdf(x), xlim = c(0, 100)) 67 | curve(pbetapr(x, 5, 3, 2), 0, 100, col = "red", add = TRUE, n = 500) 68 | 69 | } 70 | \seealso{ 71 | \code{\link[stats]{Beta}} 72 | } 73 | \concept{Continuous} 74 | \concept{Univariate} 75 | \keyword{distribution} 76 | -------------------------------------------------------------------------------- /man/Bhattacharjee.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bhattacharjee-distribution.R 3 | \name{Bhattacharjee} 4 | \alias{Bhattacharjee} 5 | \alias{dbhatt} 6 | \alias{pbhatt} 7 | \alias{rbhatt} 8 | \title{Bhattacharjee distribution} 9 | \usage{ 10 | dbhatt(x, mu = 0, sigma = 1, a = sigma, log = FALSE) 11 | 12 | pbhatt(q, mu = 0, sigma = 1, a = sigma, lower.tail = TRUE, log.p = FALSE) 13 | 14 | rbhatt(n, mu = 0, sigma = 1, a = sigma) 15 | } 16 | \arguments{ 17 | \item{x, q}{vector of quantiles.} 18 | 19 | \item{mu, sigma, a}{location, scale and shape parameters. 20 | Scale and shape must be positive.} 21 | 22 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 23 | 24 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 25 | otherwise, \eqn{P[X > x]}.} 26 | 27 | \item{n}{number of observations. If \code{length(n) > 1}, 28 | the length is taken to be the number required.} 29 | } 30 | \description{ 31 | Density, distribution function, and random generation for the Bhattacharjee 32 | distribution. 33 | } 34 | \details{ 35 | If \eqn{Z \sim \mathrm{Normal}(0, 1)}{Z ~ Normal(0, 1)} and 36 | \eqn{U \sim \mathrm{Uniform}(0, 1)}{U ~ Uniform(0, 1)}, then 37 | \eqn{Z+U} follows Bhattacharjee distribution. 38 | 39 | Probability density function 40 | 41 | \deqn{ 42 | f(z) = \frac{1}{2a} \left[\Phi\left(\frac{x-\mu+a}{\sigma}\right) - \Phi\left(\frac{x-\mu-a}{\sigma}\right)\right] 43 | }{ 44 | f(z) = 1/(2*a) * (\Phi((x-\mu+a)/\sigma) - \Phi((x-\mu+a)/\sigma)) 45 | } 46 | 47 | Cumulative distribution function 48 | 49 | \deqn{ 50 | F(z) = \frac{\sigma}{2a} \left[(x-\mu)\Phi\left(\frac{x-\mu+a}{\sigma}\right) - 51 | (x-\mu)\Phi\left(\frac{x-\mu-a}{\sigma}\right) + 52 | \phi\left(\frac{x-\mu+a}{\sigma}\right) - 53 | \phi\left(\frac{x-\mu-a}{\sigma}\right)\right] 54 | }{ 55 | F(z) = \sigma/(2*a) * ((x-\mu)*\Phi((x-\mu+a)/\sigma) - (x-\mu)*\Phi((x-\mu-a)/\sigma) + 56 | \phi((x-\mu+a)/\sigma) - \phi((x-\mu-a)/\sigma)) 57 | } 58 | } 59 | \examples{ 60 | 61 | x <- rbhatt(1e5, 5, 3, 5) 62 | hist(x, 100, freq = FALSE) 63 | curve(dbhatt(x, 5, 3, 5), -20, 20, col = "red", add = TRUE) 64 | hist(pbhatt(x, 5, 3, 5)) 65 | plot(ecdf(x)) 66 | curve(pbhatt(x, 5, 3, 5), -20, 20, col = "red", lwd = 2, add = TRUE) 67 | 68 | } 69 | \references{ 70 | Bhattacharjee, G.P., Pandit, S.N.N., and Mohan, R. (1963). 71 | Dimensional chains involving rectangular and normal error-distributions. 72 | Technometrics, 5, 404-406. 73 | } 74 | \concept{Continuous} 75 | \concept{Univariate} 76 | \keyword{distribution} 77 | -------------------------------------------------------------------------------- /man/BivNormal.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bivariate-normal-distribution.R 3 | \name{BivNormal} 4 | \alias{BivNormal} 5 | \alias{dbvnorm} 6 | \alias{rbvnorm} 7 | \title{Bivariate normal distribution} 8 | \usage{ 9 | dbvnorm( 10 | x, 11 | y = NULL, 12 | mean1 = 0, 13 | mean2 = mean1, 14 | sd1 = 1, 15 | sd2 = sd1, 16 | cor = 0, 17 | log = FALSE 18 | ) 19 | 20 | rbvnorm(n, mean1 = 0, mean2 = mean1, sd1 = 1, sd2 = sd1, cor = 0) 21 | } 22 | \arguments{ 23 | \item{x, y}{vectors of quantiles; alternatively x may be a two-column 24 | matrix (or data.frame) and y may be omitted.} 25 | 26 | \item{mean1, mean2}{vectors of means.} 27 | 28 | \item{sd1, sd2}{vectors of standard deviations.} 29 | 30 | \item{cor}{vector of correlations (\code{-1 < cor < 1}).} 31 | 32 | \item{log}{logical; if TRUE, probabilities p are given as log(p).} 33 | 34 | \item{n}{number of observations. If \code{length(n) > 1}, 35 | the length is taken to be the number required.} 36 | } 37 | \description{ 38 | Density, distribution function and random generation 39 | for the bivariate normal distribution. 40 | } 41 | \details{ 42 | Probability density function 43 | \deqn{ 44 | f(x) = \frac{1}{2\pi\sqrt{1-\rho^2}\sigma_1\sigma_2} 45 | \exp\left\{-\frac{1}{2(1-\rho^2)} \left[\left(\frac{x_1 - \mu_1}{\sigma_1}\right)^2 - 46 | 2\rho \left(\frac{x_1 - \mu_1}{\sigma_1}\right) \left(\frac{x_2 - \mu_2}{\sigma_2}\right) + 47 | \left(\frac{x_2 - \mu_2}{\sigma_2}\right)^2\right]\right\} 48 | }{ 49 | f(x) = 1/(2*\pi*sqrt(1-\rho^2)*\sigma1*\sigma2) * exp(-(1/(2*(1-\rho^2)* 50 | (((x1-\mu1)/\sigma1)^2 - 2*\rho*((x1-\mu1)/\sigma2)*((x2-\mu2)/\sigma2) * 51 | ((x2-\mu2)/\sigma2)^2)))) 52 | } 53 | } 54 | \examples{ 55 | 56 | y <- x <- seq(-4, 4, by = 0.25) 57 | z <- outer(x, y, function(x, y) dbvnorm(x, y, cor = -0.75)) 58 | persp(x, y, z) 59 | 60 | y <- x <- seq(-4, 4, by = 0.25) 61 | z <- outer(x, y, function(x, y) dbvnorm(x, y, cor = -0.25)) 62 | persp(x, y, z) 63 | 64 | } 65 | \references{ 66 | Krishnamoorthy, K. (2006). Handbook of Statistical Distributions 67 | with Applications. Chapman & Hall/CRC 68 | 69 | Mukhopadhyay, N. (2000). Probability and statistical inference. 70 | Chapman & Hall/CRC 71 | } 72 | \seealso{ 73 | \code{\link[stats]{Normal}} 74 | } 75 | \concept{Continuous} 76 | \concept{Multivariate} 77 | \keyword{distribution} 78 | -------------------------------------------------------------------------------- /man/BivPoiss.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bivariate-poisson-distribution.R 3 | \name{BivPoiss} 4 | \alias{BivPoiss} 5 | \alias{dbvpois} 6 | \alias{rbvpois} 7 | \title{Bivariate Poisson distribution} 8 | \usage{ 9 | dbvpois(x, y = NULL, a, b, c, log = FALSE) 10 | 11 | rbvpois(n, a, b, c) 12 | } 13 | \arguments{ 14 | \item{x, y}{vectors of quantiles; alternatively x may be a two-column 15 | matrix (or data.frame) and y may be omitted.} 16 | 17 | \item{a, b, c}{positive valued parameters.} 18 | 19 | \item{log}{logical; if TRUE, probabilities p are given as log(p).} 20 | 21 | \item{n}{number of observations. If \code{length(n) > 1}, 22 | the length is taken to be the number required.} 23 | } 24 | \description{ 25 | Probability mass function and random generation for the bivariate Poisson distribution. 26 | } 27 | \details{ 28 | Probability mass function 29 | \deqn{ 30 | f(x,y) = \exp \{-(a+b+c)\} \frac{a^x}{x!} \frac{b^y}{y!} \sum_{k=0}^{\min(x,y)} 31 | {x \choose k} {y \choose k} k! \left( \frac{c}{ab} \right)^k 32 | }{ 33 | f(x,y) = exp(-(a+b+c)) * (a^x)/x! * (b^y)/y! * 34 | sum(choose(x,k)*choose(y,k)*k!*(c/(a*b))^k) 35 | } 36 | } 37 | \examples{ 38 | 39 | x <- rbvpois(5000, 7, 8, 5) 40 | image(prop.table(table(x[,1], x[,2]))) 41 | colMeans(x) 42 | 43 | } 44 | \references{ 45 | Karlis, D. and Ntzoufras, I. (2003). Analysis of sports data by using bivariate Poisson models. 46 | Journal of the Royal Statistical Society: Series D (The Statistician), 52(3), 381-393. 47 | 48 | Kocherlakota, S. and Kocherlakota, K. (1992) Bivariate Discrete Distributions. 49 | New York: Dekker. 50 | 51 | Johnson, N., Kotz, S. and Balakrishnan, N. (1997). Discrete Multivariate Distributions. 52 | New York: Wiley. 53 | 54 | Holgate, P. (1964). Estimation for the bivariate Poisson distribution. 55 | Biometrika, 51(1-2), 241-287. 56 | 57 | Kawamura, K. (1984). Direct calculation of maximum likelihood estimator for the bivariate 58 | Poisson distribution. Kodai mathematical journal, 7(2), 211-221. 59 | } 60 | \seealso{ 61 | \code{\link[stats]{Poisson}} 62 | } 63 | \concept{Discrete} 64 | \concept{Multivariate} 65 | \keyword{distribution} 66 | -------------------------------------------------------------------------------- /man/DirMnom.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dirichlet-multinomial-distribution.R 3 | \name{DirMnom} 4 | \alias{DirMnom} 5 | \alias{ddirmnom} 6 | \alias{rdirmnom} 7 | \title{Dirichlet-multinomial (multivariate Polya) distribution} 8 | \usage{ 9 | ddirmnom(x, size, alpha, log = FALSE) 10 | 11 | rdirmnom(n, size, alpha) 12 | } 13 | \arguments{ 14 | \item{x}{\eqn{k}-column matrix of quantiles.} 15 | 16 | \item{size}{numeric vector; number of trials (zero or more).} 17 | 18 | \item{alpha}{\eqn{k}-values vector or \eqn{k}-column matrix; 19 | concentration parameter. Must be positive.} 20 | 21 | \item{log}{logical; if TRUE, probabilities p are given as log(p).} 22 | 23 | \item{n}{number of observations. If \code{length(n) > 1}, 24 | the length is taken to be the number required.} 25 | } 26 | \description{ 27 | Density function, cumulative distribution function and random generation 28 | for the Dirichlet-multinomial (multivariate Polya) distribution. 29 | } 30 | \details{ 31 | If \eqn{(p_1,\dots,p_k) \sim \mathrm{Dirichlet}(\alpha_1,\dots,\alpha_k)}{(p[1],\dots,p[k]) ~ Dirichlet(\alpha[1],\dots,\alpha[k])} and 32 | \eqn{(x_1,\dots,x_k) \sim \mathrm{Multinomial}(n, p_1,\dots,p_k)}{(x[1],\dots,x[k]) ~ Multinomial(n, p[1],\dots,p[k])}, then 33 | \eqn{(x_1,\dots,x_k) \sim \mathrm{DirichletMultinomial(n, \alpha_1,\dots,\alpha_k)}}{(x[1],\dots,x[k]) ~ DirichletMultinomial(n, \alpha[1],\dots,\alpha[k])}. 34 | 35 | Probability density function 36 | \deqn{ 37 | f(x) = \frac{\left(n!\right)\Gamma\left(\sum \alpha_k\right)}{\Gamma\left(n+\sum \alpha_k\right)}\prod_{k=1}^K\frac{\Gamma(x_{k}+\alpha_{k})}{\left(x_{k}!\right)\Gamma(\alpha_{k})} 38 | }{ 39 | f(x) = (n! * \Gamma(sum(\alpha[k]))) / (\Gamma(n + sum(\alpha[k]))) * prod((\Gamma(x[k] + \alpha[k])) / (x[k]! * \Gamma(\alpha[k])) 40 | } 41 | } 42 | \references{ 43 | Gentle, J.E. (2006). Random number generation and Monte Carlo methods. Springer. 44 | 45 | Kvam, P. and Day, D. (2001) The multivariate Polya distribution in combat modeling. 46 | Naval Research Logistics, 48, 1-17. 47 | } 48 | \seealso{ 49 | \code{\link{Dirichlet}}, \code{\link{Multinomial}} 50 | } 51 | \concept{Discrete} 52 | \concept{Multivariate} 53 | \keyword{distribution} 54 | -------------------------------------------------------------------------------- /man/Dirichlet.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dirichlet-distribution.R 3 | \name{Dirichlet} 4 | \alias{Dirichlet} 5 | \alias{ddirichlet} 6 | \alias{rdirichlet} 7 | \title{Dirichlet distribution} 8 | \usage{ 9 | ddirichlet(x, alpha, log = FALSE) 10 | 11 | rdirichlet(n, alpha) 12 | } 13 | \arguments{ 14 | \item{x}{\eqn{k}-column matrix of quantiles.} 15 | 16 | \item{alpha}{\eqn{k}-values vector or \eqn{k}-column matrix; 17 | concentration parameter. Must be positive.} 18 | 19 | \item{log}{logical; if TRUE, probabilities p are given as log(p).} 20 | 21 | \item{n}{number of observations. If \code{length(n) > 1}, 22 | the length is taken to be the number required.} 23 | } 24 | \description{ 25 | Density function, cumulative distribution function and random generation 26 | for the Dirichlet distribution. 27 | } 28 | \details{ 29 | Probability density function 30 | \deqn{ 31 | f(x) = \frac{\Gamma(\sum_k \alpha_k)}{\prod_k \Gamma(\alpha_k)} \prod_k x_k^{k-1} 32 | }{ 33 | f(x) = \Gamma(sum(\alpha[k])) / prod(\Gamma(\alpha[k])) * prod(x[k]^{k-1}) 34 | } 35 | } 36 | \examples{ 37 | 38 | # Generating 10 random draws from Dirichlet distribution 39 | # parametrized using a vector 40 | 41 | rdirichlet(10, c(1, 1, 1, 1)) 42 | 43 | # or parametrized using a matrix where each row 44 | # is a vector of parameters 45 | 46 | alpha <- matrix(c(1, 1, 1, 1:3, 7:9), ncol = 3, byrow = TRUE) 47 | rdirichlet(10, alpha) 48 | 49 | } 50 | \references{ 51 | Devroye, L. (1986). Non-Uniform Random Variate Generation. Springer-Verlag. 52 | } 53 | \concept{Continuous} 54 | \concept{Multivariate} 55 | \keyword{distribution} 56 | -------------------------------------------------------------------------------- /man/DiscreteGamma.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/discrete-gamma-distribution.R 3 | \name{DiscreteGamma} 4 | \alias{DiscreteGamma} 5 | \alias{ddgamma} 6 | \alias{pdgamma} 7 | \alias{rdgamma} 8 | \title{Discrete gamma distribution} 9 | \usage{ 10 | ddgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE) 11 | 12 | pdgamma(q, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE) 13 | 14 | rdgamma(n, shape, rate = 1, scale = 1/rate) 15 | } 16 | \arguments{ 17 | \item{x, q}{vector of quantiles.} 18 | 19 | \item{shape, scale}{shape and scale parameters. Must be positive, scale strictly.} 20 | 21 | \item{rate}{an alternative way to specify the scale.} 22 | 23 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 24 | 25 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 26 | otherwise, \eqn{P[X > x]}.} 27 | 28 | \item{n}{number of observations. If \code{length(n) > 1}, 29 | the length is taken to be the number required.} 30 | } 31 | \description{ 32 | Probability mass function, distribution function and random generation 33 | for discrete gamma distribution. 34 | } 35 | \details{ 36 | Probability mass function of discrete gamma distribution \eqn{f_Y(y)}{f} 37 | is defined by discretization of continuous gamma distribution 38 | \eqn{f_Y(y) = S_X(y) - S_X(y+1)}{f(y) = S(x) - S(x+1)} 39 | where \eqn{S_X}{S} is a survival function of continuous gamma distribution. 40 | } 41 | \examples{ 42 | 43 | x <- rdgamma(1e5, 9, 1) 44 | xx <- 0:50 45 | plot(prop.table(table(x))) 46 | lines(xx, ddgamma(xx, 9, 1), col = "red") 47 | hist(pdgamma(x, 9, 1)) 48 | plot(ecdf(x)) 49 | xx <- seq(0, 50, 0.1) 50 | lines(xx, pdgamma(xx, 9, 1), col = "red", lwd = 2, type = "s") 51 | 52 | } 53 | \references{ 54 | Chakraborty, S. and Chakravarty, D. (2012). 55 | Discrete Gamma distributions: Properties and parameter estimations. 56 | Communications in Statistics-Theory and Methods, 41(18), 3301-3324. 57 | } 58 | \seealso{ 59 | \code{\link[stats]{GammaDist}}, \code{\link{DiscreteNormal}} 60 | } 61 | \concept{Discrete} 62 | \concept{Univariate} 63 | \keyword{distribution} 64 | -------------------------------------------------------------------------------- /man/DiscreteLaplace.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/discrete-laplace-distribution.R 3 | \name{DiscreteLaplace} 4 | \alias{DiscreteLaplace} 5 | \alias{ddlaplace} 6 | \alias{pdlaplace} 7 | \alias{rdlaplace} 8 | \title{Discrete Laplace distribution} 9 | \usage{ 10 | ddlaplace(x, location, scale, log = FALSE) 11 | 12 | pdlaplace(q, location, scale, lower.tail = TRUE, log.p = FALSE) 13 | 14 | rdlaplace(n, location, scale) 15 | } 16 | \arguments{ 17 | \item{x, q}{vector of quantiles.} 18 | 19 | \item{location}{location parameter.} 20 | 21 | \item{scale}{scale parameter; \code{0 < scale < 1}.} 22 | 23 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 24 | 25 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 26 | otherwise, \eqn{P[X > x]}.} 27 | 28 | \item{n}{number of observations. If \code{length(n) > 1}, 29 | the length is taken to be the number required.} 30 | } 31 | \description{ 32 | Probability mass, distribution function and random generation 33 | for the discrete Laplace distribution parametrized by location and scale. 34 | } 35 | \details{ 36 | If \eqn{U \sim \mathrm{Geometric}(1-p)}{U ~ Geometric(1-p)} and 37 | \eqn{V \sim \mathrm{Geometric}(1-p)}{V ~ Geometric(1-p)}, 38 | then \eqn{U-V \sim \mathrm{DiscreteLaplace}(p)}{U-V ~ DiscreteLaplace(p)}, 39 | where geometric distribution is related to discrete Laplace distribution 40 | in similar way as exponential distribution is related to Laplace distribution. 41 | 42 | Probability mass function 43 | 44 | \deqn{ 45 | f(x) = \frac{1-p}{1+p} p^{|x-\mu|} 46 | }{ 47 | f(x) = (1-p)/(1+p) * p^(|x-\mu|) 48 | } 49 | 50 | Cumulative distribution function 51 | 52 | \deqn{ 53 | F(x) = \left\{\begin{array}{ll} 54 | \frac{p^{-|x-\mu|}}{1+p} & x < 0 \\ 55 | 1 - \frac{p^{|x-\mu|+1}}{1+p} & x \ge 0 56 | \end{array}\right. 57 | }{ 58 | F(x) = [if x < 0:] p^-floor(x-\mu))/(1+p) [else:] 1-(p^(floor(x-\mu)+1))/(1+p) 59 | } 60 | } 61 | \examples{ 62 | 63 | p <- 0.45 64 | x <- rdlaplace(1e5, 0, p) 65 | xx <- seq(-200, 200, by = 1) 66 | plot(prop.table(table(x))) 67 | lines(xx, ddlaplace(xx, 0, p), col = "red") 68 | hist(pdlaplace(x, 0, p)) 69 | plot(ecdf(x)) 70 | lines(xx, pdlaplace(xx, 0, p), col = "red", type = "s") 71 | 72 | } 73 | \references{ 74 | Inusah, S., & Kozubowski, T.J. (2006). A discrete analogue of the Laplace distribution. 75 | Journal of statistical planning and inference, 136(3), 1090-1102. 76 | 77 | Kotz, S., Kozubowski, T., & Podgorski, K. (2012). 78 | The Laplace distribution and generalizations: a revisit with applications 79 | to communications, economics, engineering, and finance. 80 | Springer Science & Business Media. 81 | } 82 | \concept{Discrete} 83 | \concept{Univariate} 84 | \keyword{distribution} 85 | -------------------------------------------------------------------------------- /man/DiscreteNormal.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/discrete-normal-distribution.R 3 | \name{DiscreteNormal} 4 | \alias{DiscreteNormal} 5 | \alias{ddnorm} 6 | \alias{pdnorm} 7 | \alias{rdnorm} 8 | \title{Discrete normal distribution} 9 | \usage{ 10 | ddnorm(x, mean = 0, sd = 1, log = FALSE) 11 | 12 | pdnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) 13 | 14 | rdnorm(n, mean = 0, sd = 1) 15 | } 16 | \arguments{ 17 | \item{x, q}{vector of quantiles.} 18 | 19 | \item{mean}{vector of means.} 20 | 21 | \item{sd}{vector of standard deviations.} 22 | 23 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 24 | 25 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 26 | otherwise, \eqn{P[X > x]}.} 27 | 28 | \item{n}{number of observations. If \code{length(n) > 1}, 29 | the length is taken to be the number required.} 30 | } 31 | \description{ 32 | Probability mass function, distribution function and random generation 33 | for discrete normal distribution. 34 | } 35 | \details{ 36 | Probability mass function 37 | 38 | \deqn{ 39 | f(x) = \Phi\left(\frac{x-\mu+1}{\sigma}\right) - \Phi\left(\frac{x-\mu}{\sigma}\right) 40 | }{ 41 | f(x) = \Phi((x-\mu+1)/\sigma) - \Phi((x-\mu)/\sigma) 42 | } 43 | 44 | Cumulative distribution function 45 | 46 | \deqn{ 47 | F(x) = \Phi\left(\frac{\lfloor x \rfloor + 1 - \mu}{\sigma}\right) 48 | }{ 49 | F(x) = \Phi((floor(x)+1-\mu)/\sigma) 50 | } 51 | } 52 | \examples{ 53 | 54 | x <- rdnorm(1e5, 0, 3) 55 | xx <- -15:15 56 | plot(prop.table(table(x))) 57 | lines(xx, ddnorm(xx, 0, 3), col = "red") 58 | hist(pdnorm(x, 0, 3)) 59 | plot(ecdf(x)) 60 | xx <- seq(-15, 15, 0.1) 61 | lines(xx, pdnorm(xx, 0, 3), col = "red", lwd = 2, type = "s") 62 | 63 | } 64 | \references{ 65 | Roy, D. (2003). The discrete normal distribution. 66 | Communications in Statistics-Theory and Methods, 32, 1871-1883. 67 | } 68 | \seealso{ 69 | \code{\link[stats]{Normal}} 70 | } 71 | \concept{Discrete} 72 | \concept{Univariate} 73 | \keyword{distribution} 74 | -------------------------------------------------------------------------------- /man/DiscreteUniform.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/discrete-uniform-distribution.R 3 | \name{DiscreteUniform} 4 | \alias{DiscreteUniform} 5 | \alias{ddunif} 6 | \alias{pdunif} 7 | \alias{qdunif} 8 | \alias{rdunif} 9 | \title{Discrete uniform distribution} 10 | \usage{ 11 | ddunif(x, min, max, log = FALSE) 12 | 13 | pdunif(q, min, max, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qdunif(p, min, max, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rdunif(n, min, max) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{min, max}{lower and upper limits of the distribution. Must be finite.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Probability mass function, distribution function, quantile function and random generation 36 | for the discrete uniform distribution. 37 | } 38 | \details{ 39 | If \code{min == max}, then discrete uniform distribution is a degenerate distribution. 40 | } 41 | \examples{ 42 | 43 | x <- rdunif(1e5, 1, 10) 44 | xx <- -1:11 45 | plot(prop.table(table(x)), type = "h") 46 | lines(xx, ddunif(xx, 1, 10), col = "red") 47 | hist(pdunif(x, 1, 10)) 48 | xx <- seq(-1, 11, by = 0.01) 49 | plot(ecdf(x)) 50 | lines(xx, pdunif(xx, 1, 10), col = "red") 51 | 52 | } 53 | \concept{Discrete} 54 | \concept{Univariate} 55 | \keyword{distribution} 56 | -------------------------------------------------------------------------------- /man/DiscreteWeibull.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/discrete-weibull-distribution.R 3 | \name{DiscreteWeibull} 4 | \alias{DiscreteWeibull} 5 | \alias{ddweibull} 6 | \alias{pdweibull} 7 | \alias{qdweibull} 8 | \alias{rdweibull} 9 | \title{Discrete Weibull distribution (type I)} 10 | \usage{ 11 | ddweibull(x, shape1, shape2, log = FALSE) 12 | 13 | pdweibull(q, shape1, shape2, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qdweibull(p, shape1, shape2, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rdweibull(n, shape1, shape2) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{shape1, shape2}{parameters (named q, \eqn{\beta}). Values of \code{shape2} 23 | need to be positive and \code{0 < shape1 < 1}.} 24 | 25 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 26 | 27 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 28 | otherwise, \eqn{P[X > x]}.} 29 | 30 | \item{p}{vector of probabilities.} 31 | 32 | \item{n}{number of observations. If \code{length(n) > 1}, 33 | the length is taken to be the number required.} 34 | } 35 | \description{ 36 | Density, distribution function, quantile function and random generation 37 | for the discrete Weibull (type I) distribution. 38 | } 39 | \details{ 40 | Probability mass function 41 | \deqn{ 42 | f(x) = q^{x^\beta} - q^{(x+1)^\beta} 43 | }{ 44 | f(x) = q^x^\beta - q^(x+1)^\beta 45 | } 46 | 47 | Cumulative distribution function 48 | \deqn{ 49 | F(x) = 1-q^{(x+1)^\beta} 50 | }{ 51 | F(x) = 1-q^(x+1)^\beta 52 | } 53 | 54 | Quantile function 55 | \deqn{ 56 | F^{-1}(p) = \left \lceil{\left(\frac{\log(1-p)}{\log(q)}\right)^{1/\beta} - 1}\right \rceil 57 | }{ 58 | F^-1(p) = ceiling((log(1-p)/log(q))^(1/\beta) - 1) 59 | } 60 | } 61 | \examples{ 62 | 63 | x <- rdweibull(1e5, 0.32, 1) 64 | xx <- seq(-2, 100, by = 1) 65 | plot(prop.table(table(x)), type = "h") 66 | lines(xx, ddweibull(xx, .32, 1), col = "red") 67 | 68 | # Notice: distribution of F(X) is far from uniform: 69 | hist(pdweibull(x, .32, 1), 50) 70 | 71 | plot(ecdf(x)) 72 | lines(xx, pdweibull(xx, .32, 1), col = "red", lwd = 2, type = "s") 73 | 74 | } 75 | \references{ 76 | Nakagawa, T. and Osaki, S. (1975). The Discrete Weibull Distribution. 77 | IEEE Transactions on Reliability, R-24, 300-301. 78 | 79 | Kulasekera, K.B. (1994). 80 | Approximate MLE's of the parameters of a discrete Weibull distribution 81 | with type I censored data. 82 | Microelectronics Reliability, 34(7), 1185-1188. 83 | 84 | Khan, M.A., Khalique, A. and Abouammoh, A.M. (1989). 85 | On estimating parameters in a discrete Weibull distribution. 86 | IEEE Transactions on Reliability, 38(3), 348-350. 87 | } 88 | \seealso{ 89 | \code{\link[stats]{Weibull}} 90 | } 91 | \concept{Discrete} 92 | \concept{Univariate} 93 | \keyword{distribution} 94 | -------------------------------------------------------------------------------- /man/Frechet.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/frechet-distribution.R 3 | \name{Frechet} 4 | \alias{Frechet} 5 | \alias{dfrechet} 6 | \alias{pfrechet} 7 | \alias{qfrechet} 8 | \alias{rfrechet} 9 | \title{Frechet distribution} 10 | \usage{ 11 | dfrechet(x, lambda = 1, mu = 0, sigma = 1, log = FALSE) 12 | 13 | pfrechet(q, lambda = 1, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qfrechet(p, lambda = 1, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rfrechet(n, lambda = 1, mu = 0, sigma = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{lambda, sigma, mu}{shape, scale, and location parameters. 23 | Scale and shape must be positive.} 24 | 25 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 26 | 27 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 28 | otherwise, \eqn{P[X > x]}.} 29 | 30 | \item{p}{vector of probabilities.} 31 | 32 | \item{n}{number of observations. If \code{length(n) > 1}, 33 | the length is taken to be the number required.} 34 | } 35 | \description{ 36 | Density, distribution function, quantile function and random generation 37 | for the Frechet distribution. 38 | } 39 | \details{ 40 | Probability density function 41 | \deqn{ 42 | f(x) = \frac{\lambda}{\sigma} \left(\frac{x-\mu}{\sigma}\right)^{-1-\lambda} \exp\left(-\left(\frac{x-\mu}{\sigma}\right)^{-\lambda}\right) 43 | }{ 44 | f(x) = \lambda/\sigma * ((x-\mu)/\sigma)^(-1-\lambda) * exp(-((x-\mu)/\sigma)^-\lambda) 45 | } 46 | 47 | Cumulative distribution function 48 | \deqn{ 49 | F(x) = \exp\left(-\left(\frac{x-\mu}{\sigma}\right)^{-\lambda}\right) 50 | }{ 51 | F(x) = exp(-((x-\mu)/\sigma)^-\lambda) 52 | } 53 | 54 | Quantile function 55 | \deqn{ 56 | F^{-1}(p) = \mu + \sigma -\log(p)^{-1/\lambda} 57 | }{ 58 | F^-1(p) = \mu + \sigma * -log(p)^{-1/\lambda} 59 | } 60 | } 61 | \examples{ 62 | 63 | x <- rfrechet(1e5, 5, 2, 1.5) 64 | xx <- seq(0, 1000, by = 0.1) 65 | hist(x, 200, freq = FALSE) 66 | lines(xx, dfrechet(xx, 5, 2, 1.5), col = "red") 67 | hist(pfrechet(x, 5, 2, 1.5)) 68 | plot(ecdf(x)) 69 | lines(xx, pfrechet(xx, 5, 2, 1.5), col = "red", lwd = 2) 70 | 71 | } 72 | \references{ 73 | Bury, K. (1999). Statistical Distributions in Engineering. 74 | Cambridge University Press. 75 | } 76 | \concept{Continuous} 77 | \concept{Univariate} 78 | \keyword{distribution} 79 | -------------------------------------------------------------------------------- /man/GPD.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/gpd-distribution.R 3 | \name{GPD} 4 | \alias{GPD} 5 | \alias{dgpd} 6 | \alias{pgpd} 7 | \alias{qgpd} 8 | \alias{rgpd} 9 | \title{Generalized Pareto distribution} 10 | \usage{ 11 | dgpd(x, mu = 0, sigma = 1, xi = 0, log = FALSE) 12 | 13 | pgpd(q, mu = 0, sigma = 1, xi = 0, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qgpd(p, mu = 0, sigma = 1, xi = 0, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rgpd(n, mu = 0, sigma = 1, xi = 0) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{mu, sigma, xi}{location, scale, and shape parameters. Scale must be positive.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the generalized Pareto distribution. 37 | } 38 | \details{ 39 | Probability density function 40 | \deqn{ f(x) = \left\{\begin{array}{ll} 41 | \frac{1}{\sigma} \left(1+\xi \frac{x-\mu}{\sigma}\right)^{-(\xi+1)/\xi} & \xi \neq 0 \\ 42 | \frac{1}{\sigma} \exp\left(-\frac{x-\mu}{\sigma}\right) & \xi = 0 43 | \end{array}\right. 44 | }{ 45 | f(x) = [if \xi != 0:] (1+\xi*(x-\mu)/\sigma)^{-(\xi+1)/\xi}/\sigma 46 | [else:] exp(-(x-\mu)/\sigma)/\sigma 47 | } 48 | 49 | Cumulative distribution function 50 | \deqn{ F(x) = \left\{\begin{array}{ll} 51 | 1-\left(1+\xi \frac{x-\mu}{\sigma}\right)^{-1/\xi} & \xi \neq 0 \\ 52 | 1-\exp\left(-\frac{x-\mu}{\sigma}\right) & \xi = 0 53 | \end{array}\right. 54 | }{ 55 | F(x) = [if \xi != 0:] 1-(1+\xi*(x-\mu)/\sigma)^{-1/\xi} 56 | [else:] 1-exp(-(x-\mu)/\sigma) 57 | } 58 | 59 | Quantile function 60 | \deqn{ F^{-1}(x) = \left\{\begin{array}{ll} 61 | \mu + \sigma \frac{(1-p)^{-\xi}-1}{\xi} & \xi \neq 0 \\ 62 | \mu - \sigma \log(1-p) & \xi = 0 63 | \end{array}\right. 64 | }{ 65 | F^-1(x) = [if \xi != 0:] \mu + \sigma * ((1-p)^{-\xi}-1)/\xi 66 | [else:] \mu - \sigma * log(1-p) 67 | } 68 | } 69 | \examples{ 70 | 71 | x <- rgpd(1e5, 5, 2, .1) 72 | hist(x, 100, freq = FALSE, xlim = c(0, 50)) 73 | curve(dgpd(x, 5, 2, .1), 0, 50, col = "red", add = TRUE, n = 5000) 74 | hist(pgpd(x, 5, 2, .1)) 75 | plot(ecdf(x)) 76 | curve(pgpd(x, 5, 2, .1), 0, 50, col = "red", lwd = 2, add = TRUE) 77 | 78 | } 79 | \references{ 80 | Coles, S. (2001). An Introduction to Statistical Modeling of Extreme Values. 81 | Springer. 82 | } 83 | \concept{Continuous} 84 | \concept{Univariate} 85 | \keyword{distribution} 86 | -------------------------------------------------------------------------------- /man/Gompertz.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/gompertz-distribution.R 3 | \name{Gompertz} 4 | \alias{Gompertz} 5 | \alias{dgompertz} 6 | \alias{pgompertz} 7 | \alias{qgompertz} 8 | \alias{rgompertz} 9 | \title{Gompertz distribution} 10 | \usage{ 11 | dgompertz(x, a = 1, b = 1, log = FALSE) 12 | 13 | pgompertz(q, a = 1, b = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qgompertz(p, a = 1, b = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rgompertz(n, a = 1, b = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{a, b}{positive valued scale and location parameters.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the Gompertz distribution. 37 | } 38 | \details{ 39 | Probability density function 40 | \deqn{ 41 | f(x) = a \exp\left(bx - \frac{a}{b} (\exp(bx)-1)\right) 42 | }{ 43 | f(x) = a*exp(b*x - a/b * (exp(b*x)-1)) 44 | } 45 | 46 | Cumulative distribution function 47 | \deqn{ 48 | F(x) = 1-\exp\left(-\frac{a}{b} (\exp(bx)-1)\right) 49 | }{ 50 | F(x) = 1-exp(-a/b * (exp(b*x)-1)) 51 | } 52 | 53 | Quantile function 54 | \deqn{ 55 | F^{-1}(p) = \frac{1}{b} \log\left(1-\frac{b}{a}\log(1-p)\right) 56 | }{ 57 | F^-1(p) = 1/b * log(1 - b/a * log(1-p)) 58 | } 59 | } 60 | \examples{ 61 | 62 | x <- rgompertz(1e5, 5, 2) 63 | hist(x, 100, freq = FALSE) 64 | curve(dgompertz(x, 5, 2), 0, 1, col = "red", add = TRUE) 65 | hist(pgompertz(x, 5, 2)) 66 | plot(ecdf(x)) 67 | curve(pgompertz(x, 5, 2), 0, 1, col = "red", lwd = 2, add = TRUE) 68 | 69 | } 70 | \references{ 71 | Lenart, A. (2012). The Gompertz distribution and Maximum Likelihood Estimation 72 | of its parameters - a revision. MPIDR WORKING PAPER WP 2012-008. 73 | \url{https://www.demogr.mpg.de/papers/working/wp-2012-008.pdf} 74 | } 75 | \concept{Continuous} 76 | \concept{Univariate} 77 | \keyword{distribution} 78 | -------------------------------------------------------------------------------- /man/Gumbel.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/gumbel-distribution.R 3 | \name{Gumbel} 4 | \alias{Gumbel} 5 | \alias{dgumbel} 6 | \alias{pgumbel} 7 | \alias{qgumbel} 8 | \alias{rgumbel} 9 | \title{Gumbel distribution} 10 | \usage{ 11 | dgumbel(x, mu = 0, sigma = 1, log = FALSE) 12 | 13 | pgumbel(q, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qgumbel(p, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rgumbel(n, mu = 0, sigma = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{mu, sigma}{location and scale parameters. Scale must be positive.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the Gumbel distribution. 37 | } 38 | \details{ 39 | Probability density function 40 | \deqn{ 41 | f(x) = \frac{1}{\sigma} \exp\left(-\left(\frac{x-\mu}{\sigma} + \exp\left(-\frac{x-\mu}{\sigma}\right)\right)\right) 42 | }{ 43 | f(x) = 1/\sigma * exp(-((x-\mu)/\sigma + exp(-(x-\mu)/\sigma))) 44 | } 45 | 46 | Cumulative distribution function 47 | \deqn{ 48 | F(x) = \exp\left(-\exp\left(-\frac{x-\mu}{\sigma}\right)\right) 49 | }{ 50 | F(x) = exp(-exp(-(x-\mu)/\sigma)) 51 | } 52 | 53 | Quantile function 54 | \deqn{ 55 | F^{-1}(p) = \mu - \sigma \log(-\log(p)) 56 | }{ 57 | F^-1(p) = \mu - \sigma * log(-log(p)) 58 | } 59 | } 60 | \examples{ 61 | 62 | x <- rgumbel(1e5, 5, 2) 63 | hist(x, 100, freq = FALSE) 64 | curve(dgumbel(x, 5, 2), 0, 25, col = "red", add = TRUE) 65 | hist(pgumbel(x, 5, 2)) 66 | plot(ecdf(x)) 67 | curve(pgumbel(x, 5, 2), 0, 25, col = "red", lwd = 2, add = TRUE) 68 | 69 | } 70 | \references{ 71 | Bury, K. (1999). Statistical Distributions in Engineering. 72 | Cambridge University Press. 73 | } 74 | \concept{Continuous} 75 | \concept{Univariate} 76 | \keyword{distribution} 77 | -------------------------------------------------------------------------------- /man/HalfCauchy.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/half-cauchy-distribution.R 3 | \name{HalfCauchy} 4 | \alias{HalfCauchy} 5 | \alias{dhcauchy} 6 | \alias{phcauchy} 7 | \alias{qhcauchy} 8 | \alias{rhcauchy} 9 | \title{Half-Cauchy distribution} 10 | \usage{ 11 | dhcauchy(x, sigma = 1, log = FALSE) 12 | 13 | phcauchy(q, sigma = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qhcauchy(p, sigma = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rhcauchy(n, sigma = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{sigma}{positive valued scale parameter.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the half-Cauchy distribution. 37 | } 38 | \details{ 39 | If \eqn{X} follows Cauchy centered at 0 and parametrized by scale \eqn{\sigma}, 40 | then \eqn{|X|} follows half-Cauchy distribution parametrized by 41 | scale \eqn{\sigma}. Half-Cauchy distribution is a special case of half-t 42 | distribution with \eqn{\nu=1} degrees of freedom. 43 | } 44 | \examples{ 45 | 46 | x <- rhcauchy(1e5, 2) 47 | hist(x, 2e5, freq = FALSE, xlim = c(0, 100)) 48 | curve(dhcauchy(x, 2), 0, 100, col = "red", add = TRUE) 49 | hist(phcauchy(x, 2)) 50 | plot(ecdf(x), xlim = c(0, 100)) 51 | curve(phcauchy(x, 2), col = "red", lwd = 2, add = TRUE) 52 | 53 | } 54 | \references{ 55 | Gelman, A. (2006). Prior distributions for variance parameters in hierarchical 56 | models (comment on article by Browne and Draper). 57 | Bayesian analysis, 1(3), 515-534. 58 | 59 | Jacob, E. and Jayakumar, K. (2012). 60 | On Half-Cauchy Distribution and Process. 61 | International Journal of Statistika and Mathematika, 3(2), 77-81. 62 | } 63 | \seealso{ 64 | \code{\link{HalfT}} 65 | } 66 | \concept{Continuous} 67 | \concept{Univariate} 68 | \keyword{distribution} 69 | -------------------------------------------------------------------------------- /man/HalfNormal.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/half-normal-distribution.R 3 | \name{HalfNormal} 4 | \alias{HalfNormal} 5 | \alias{dhnorm} 6 | \alias{phnorm} 7 | \alias{qhnorm} 8 | \alias{rhnorm} 9 | \title{Half-normal distribution} 10 | \usage{ 11 | dhnorm(x, sigma = 1, log = FALSE) 12 | 13 | phnorm(q, sigma = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qhnorm(p, sigma = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rhnorm(n, sigma = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{sigma}{positive valued scale parameter.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the half-normal distribution. 37 | } 38 | \details{ 39 | If \eqn{X} follows normal distribution centered at 0 and parametrized 40 | by scale \eqn{\sigma}, then \eqn{|X|} follows half-normal distribution 41 | parametrized by scale \eqn{\sigma}. Half-t distribution with \eqn{\nu=\infty} 42 | degrees of freedom converges to half-normal distribution. 43 | } 44 | \examples{ 45 | 46 | x <- rhnorm(1e5, 2) 47 | hist(x, 100, freq = FALSE) 48 | curve(dhnorm(x, 2), 0, 8, col = "red", add = TRUE) 49 | hist(phnorm(x, 2)) 50 | plot(ecdf(x)) 51 | curve(phnorm(x, 2), 0, 8, col = "red", lwd = 2, add = TRUE) 52 | 53 | } 54 | \references{ 55 | Gelman, A. (2006). Prior distributions for variance parameters in hierarchical 56 | models (comment on article by Browne and Draper). 57 | Bayesian analysis, 1(3), 515-534. 58 | 59 | Jacob, E. and Jayakumar, K. (2012). 60 | On Half-Cauchy Distribution and Process. 61 | International Journal of Statistika and Mathematika, 3(2), 77-81. 62 | } 63 | \seealso{ 64 | \code{\link{HalfT}} 65 | } 66 | \concept{Continuous} 67 | \concept{Univariate} 68 | \keyword{distribution} 69 | -------------------------------------------------------------------------------- /man/HalfT.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/half-t-distribution.R 3 | \name{HalfT} 4 | \alias{HalfT} 5 | \alias{dht} 6 | \alias{pht} 7 | \alias{qht} 8 | \alias{rht} 9 | \title{Half-t distribution} 10 | \usage{ 11 | dht(x, nu, sigma = 1, log = FALSE) 12 | 13 | pht(q, nu, sigma = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qht(p, nu, sigma = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rht(n, nu, sigma = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{nu, sigma}{positive valued degrees of freedom and scale parameters.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the half-t distribution. 37 | } 38 | \details{ 39 | If \eqn{X} follows t distribution parametrized by degrees of freedom \eqn{\nu} 40 | and scale \eqn{\sigma}, then \eqn{|X|} follows half-t distribution parametrized 41 | by degrees of freedom \eqn{\nu} and scale \eqn{\sigma}. 42 | } 43 | \examples{ 44 | 45 | x <- rht(1e5, 2, 2) 46 | hist(x, 500, freq = FALSE, xlim = c(0, 100)) 47 | curve(dht(x, 2, 2), 0, 100, col = "red", add = TRUE) 48 | hist(pht(x, 2, 2)) 49 | plot(ecdf(x), xlim = c(0, 100)) 50 | curve(pht(x, 2, 2), 0, 100, col = "red", lwd = 2, add = TRUE) 51 | 52 | } 53 | \references{ 54 | Gelman, A. (2006). Prior distributions for variance parameters in hierarchical 55 | models (comment on article by Browne and Draper). 56 | Bayesian analysis, 1(3), 515-534. 57 | 58 | Jacob, E. and Jayakumar, K. (2012). 59 | On Half-Cauchy Distribution and Process. 60 | International Journal of Statistika and Mathematika, 3(2), 77-81. 61 | } 62 | \seealso{ 63 | \code{\link{HalfNormal}}, \code{\link{HalfCauchy}} 64 | } 65 | \concept{Continuous} 66 | \concept{Univariate} 67 | \keyword{distribution} 68 | -------------------------------------------------------------------------------- /man/Huber.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/huber-distribution.R 3 | \name{Huber} 4 | \alias{Huber} 5 | \alias{dhuber} 6 | \alias{phuber} 7 | \alias{qhuber} 8 | \alias{rhuber} 9 | \title{"Huber density" distribution} 10 | \usage{ 11 | dhuber(x, mu = 0, sigma = 1, epsilon = 1.345, log = FALSE) 12 | 13 | phuber(q, mu = 0, sigma = 1, epsilon = 1.345, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qhuber(p, mu = 0, sigma = 1, epsilon = 1.345, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rhuber(n, mu = 0, sigma = 1, epsilon = 1.345) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{mu, sigma, epsilon}{location, and scale, and shape parameters. 23 | Scale and shape must be positive.} 24 | 25 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 26 | 27 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 28 | otherwise, \eqn{P[X > x]}.} 29 | 30 | \item{p}{vector of probabilities.} 31 | 32 | \item{n}{number of observations. If \code{length(n) > 1}, 33 | the length is taken to be the number required.} 34 | } 35 | \description{ 36 | Density, distribution function, quantile function and random generation 37 | for the "Huber density" distribution. 38 | } 39 | \details{ 40 | Huber density is connected to Huber loss and can be defined as: 41 | 42 | \deqn{ 43 | f(x) = \frac{1}{2 \sqrt{2\pi} \left( \Phi(k) + \phi(k)/k - \frac{1}{2} \right)} e^{-\rho_k(x)} 44 | }{ 45 | f(x) = 1/(2 * sqrt(2\pi) * (\Phi(k) + \phi(k)/k - 1/2)) * exp(-\rho(x, k)) 46 | } 47 | 48 | where 49 | 50 | \deqn{ 51 | \rho_k(x) = 52 | \left\{\begin{array}{ll} 53 | \frac{1}{2} x^2 & |x|\le k \\ 54 | k|x|- \frac{1}{2} k^2 & |x|>k 55 | \end{array}\right. 56 | }{ 57 | \rho(x, k) = [if abs(x) <= k:] (x^2)/2 [else:] k*abs(x) - (k^2)/2 58 | } 59 | } 60 | \examples{ 61 | 62 | x <- rhuber(1e5, 5, 2, 3) 63 | hist(x, 100, freq = FALSE) 64 | curve(dhuber(x, 5, 2, 3), -20, 20, col = "red", add = TRUE, n = 5000) 65 | hist(phuber(x, 5, 2, 3)) 66 | plot(ecdf(x)) 67 | curve(phuber(x, 5, 2, 3), -20, 20, col = "red", lwd = 2, add = TRUE) 68 | 69 | } 70 | \references{ 71 | Huber, P.J. (1964). Robust Estimation of a Location Parameter. 72 | Annals of Statistics, 53(1), 73-101. 73 | 74 | Huber, P.J. (1981). Robust Statistics. Wiley. 75 | 76 | Schumann, D. (2009). Robust Variable Selection. ProQuest. 77 | } 78 | \concept{Continuous} 79 | \concept{Univariate} 80 | \keyword{distribution} 81 | -------------------------------------------------------------------------------- /man/InvChiSq.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/inverse-chi-squared-distribution.R 3 | \name{InvChiSq} 4 | \alias{InvChiSq} 5 | \alias{dinvchisq} 6 | \alias{pinvchisq} 7 | \alias{qinvchisq} 8 | \alias{rinvchisq} 9 | \title{Inverse chi-squared and scaled chi-squared distributions} 10 | \usage{ 11 | dinvchisq(x, nu, tau, log = FALSE) 12 | 13 | pinvchisq(q, nu, tau, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qinvchisq(p, nu, tau, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rinvchisq(n, nu, tau) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{nu}{positive valued shape parameter.} 23 | 24 | \item{tau}{positive valued scaling parameter; if provided it 25 | returns values for scaled chi-squared distributions.} 26 | 27 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 28 | 29 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 30 | otherwise, \eqn{P[X > x]}.} 31 | 32 | \item{p}{vector of probabilities.} 33 | 34 | \item{n}{number of observations. If \code{length(n) > 1}, 35 | the length is taken to be the number required.} 36 | } 37 | \description{ 38 | Density, distribution function and random generation 39 | for the inverse chi-squared distribution and scaled chi-squared distribution. 40 | } 41 | \details{ 42 | If \eqn{X} follows \eqn{\chi^2 (\nu)} distribution, then \eqn{1/X} follows inverse 43 | chi-squared distribution parametrized by \eqn{\nu}. Inverse chi-squared distribution 44 | is a special case of inverse gamma distribution with parameters 45 | \eqn{\alpha=\frac{\nu}{2}}{\alpha=\nu/2} and \eqn{\beta=\frac{1}{2}}{\beta=1/2}; 46 | or \eqn{\alpha=\frac{\nu}{2}}{\alpha=\nu/2} and 47 | \eqn{\beta=\frac{\nu\tau^2}{2}}{\beta=(\nu\tau^2)/2} for scaled inverse 48 | chi-squared distribution. 49 | } 50 | \examples{ 51 | 52 | x <- rinvchisq(1e5, 20) 53 | hist(x, 100, freq = FALSE) 54 | curve(dinvchisq(x, 20), 0, 1, n = 501, col = "red", add = TRUE) 55 | hist(pinvchisq(x, 20)) 56 | plot(ecdf(x)) 57 | curve(pinvchisq(x, 20), 0, 1, n = 501, col = "red", lwd = 2, add = TRUE) 58 | 59 | # scaled 60 | 61 | x <- rinvchisq(1e5, 10, 5) 62 | hist(x, 100, freq = FALSE) 63 | curve(dinvchisq(x, 10, 5), 0, 150, n = 501, col = "red", add = TRUE) 64 | hist(pinvchisq(x, 10, 5)) 65 | plot(ecdf(x)) 66 | curve(pinvchisq(x, 10, 5), 0, 150, n = 501, col = "red", lwd = 2, add = TRUE) 67 | 68 | } 69 | \seealso{ 70 | \code{\link[stats]{Chisquare}}, \code{\link[stats]{GammaDist}} 71 | } 72 | \concept{Continuous} 73 | \concept{Univariate} 74 | \keyword{distribution} 75 | -------------------------------------------------------------------------------- /man/InvGamma.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/inverse-gamma-distribution.R 3 | \name{InvGamma} 4 | \alias{InvGamma} 5 | \alias{dinvgamma} 6 | \alias{pinvgamma} 7 | \alias{qinvgamma} 8 | \alias{rinvgamma} 9 | \title{Inverse-gamma distribution} 10 | \usage{ 11 | dinvgamma(x, alpha, beta = 1, log = FALSE) 12 | 13 | pinvgamma(q, alpha, beta = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qinvgamma(p, alpha, beta = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rinvgamma(n, alpha, beta = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{alpha, beta}{positive valued shape and scale parameters.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function and random generation 36 | for the inverse-gamma distribution. 37 | } 38 | \details{ 39 | Probability mass function 40 | \deqn{ 41 | f(x) = \frac{\beta^\alpha x^{-\alpha-1} \exp(-\frac{\beta}{x})}{\Gamma(\alpha)} 42 | }{ 43 | f(x) = (\beta^\alpha * x^(-\alpha-1) * exp(-\beta/x)) / \Gamma(\alpha) 44 | } 45 | 46 | Cumulative distribution function 47 | \deqn{ 48 | F(x) = \frac{\gamma(\alpha, \frac{\beta}{x})}{\Gamma(\alpha)} 49 | }{ 50 | F(x) = \gamma(\alpha, \beta/x) / \Gamma(\alpha) 51 | } 52 | } 53 | \examples{ 54 | 55 | x <- rinvgamma(1e5, 20, 3) 56 | hist(x, 100, freq = FALSE) 57 | curve(dinvgamma(x, 20, 3), 0, 1, col = "red", add = TRUE, n = 5000) 58 | hist(pinvgamma(x, 20, 3)) 59 | plot(ecdf(x)) 60 | curve(pinvgamma(x, 20, 3), 0, 1, col = "red", lwd = 2, add = TRUE, n = 5000) 61 | 62 | } 63 | \references{ 64 | Witkovsky, V. (2001). Computing the distribution of a linear 65 | combination of inverted gamma variables. Kybernetika 37(1), 79-90. 66 | 67 | Leemis, L.M. and McQueston, L.T. (2008). Univariate Distribution 68 | Relationships. American Statistician 62(1): 45-53. 69 | } 70 | \seealso{ 71 | \code{\link[stats]{GammaDist}} 72 | } 73 | \concept{Continuous} 74 | \concept{Univariate} 75 | \keyword{distribution} 76 | -------------------------------------------------------------------------------- /man/Kumaraswamy.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/kumaraswamy-distribution.R 3 | \name{Kumaraswamy} 4 | \alias{Kumaraswamy} 5 | \alias{dkumar} 6 | \alias{pkumar} 7 | \alias{qkumar} 8 | \alias{rkumar} 9 | \title{Kumaraswamy distribution} 10 | \usage{ 11 | dkumar(x, a = 1, b = 1, log = FALSE) 12 | 13 | pkumar(q, a = 1, b = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qkumar(p, a = 1, b = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rkumar(n, a = 1, b = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{a, b}{positive valued parameters.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the Kumaraswamy distribution. 37 | } 38 | \details{ 39 | Probability density function 40 | \deqn{ 41 | f(x) = abx^{a-1} (1-x^a)^{b-1} 42 | }{ 43 | f(x) = a*b*x^(a-1)*(1-x^a)^(b-1) 44 | } 45 | 46 | Cumulative distribution function 47 | \deqn{ 48 | F(x) = 1-(1-x^a)^b 49 | }{ 50 | F(x) = 1-(1-x^a)^b 51 | } 52 | 53 | Quantile function 54 | \deqn{ 55 | F^{-1}(p) = 1-(1-p^{1/b})^{1/a} 56 | }{ 57 | F^-1(p) = 1-(1-p^(1/b))^(1/a) 58 | } 59 | } 60 | \examples{ 61 | 62 | x <- rkumar(1e5, 5, 16) 63 | hist(x, 100, freq = FALSE) 64 | curve(dkumar(x, 5, 16), 0, 1, col = "red", add = TRUE) 65 | hist(pkumar(x, 5, 16)) 66 | plot(ecdf(x)) 67 | curve(pkumar(x, 5, 16), 0, 1, col = "red", lwd = 2, add = TRUE) 68 | 69 | } 70 | \references{ 71 | Jones, M. C. (2009). Kumaraswamy's distribution: A beta-type distribution with 72 | some tractability advantages. Statistical Methodology, 6, 70-81. 73 | 74 | Cordeiro, G.M. and de Castro, M. (2009). A new family of generalized distributions. 75 | Journal of Statistical Computation & Simulation, 1-17. 76 | } 77 | \concept{Continuous} 78 | \concept{Univariate} 79 | \keyword{distribution} 80 | -------------------------------------------------------------------------------- /man/Laplace.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/laplace-distribution.R 3 | \name{Laplace} 4 | \alias{Laplace} 5 | \alias{dlaplace} 6 | \alias{plaplace} 7 | \alias{qlaplace} 8 | \alias{rlaplace} 9 | \title{Laplace distribution} 10 | \usage{ 11 | dlaplace(x, mu = 0, sigma = 1, log = FALSE) 12 | 13 | plaplace(q, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qlaplace(p, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rlaplace(n, mu = 0, sigma = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{mu, sigma}{location and scale parameters. Scale must be positive.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the Laplace distribution. 37 | } 38 | \details{ 39 | Probability density function 40 | \deqn{ 41 | f(x) = \frac{1}{2\sigma} \exp\left(-\left|\frac{x-\mu}{\sigma}\right|\right) 42 | }{ 43 | f(x) = 1/(2*\sigma) * exp(-|(x-\mu)/\sigma|) 44 | } 45 | 46 | Cumulative distribution function 47 | \deqn{ F(x) = \left\{\begin{array}{ll} 48 | \frac{1}{2} \exp\left(\frac{x-\mu}{\sigma}\right) & x < \mu \\ 49 | 1 - \frac{1}{2} \exp\left(\frac{x-\mu}{\sigma}\right) & x \geq \mu 50 | \end{array}\right. 51 | }{ 52 | F(x) = [if x < mu:] 1/2 * exp((x-\mu)/\sigma) 53 | [else:] 1 - 1/2 * exp((x-\mu)/\sigma) 54 | } 55 | 56 | Quantile function 57 | \deqn{ F^{-1}(p) = \left\{\begin{array}{ll} 58 | \mu + \sigma \log(2p) & p < 0.5 \\ 59 | \mu - \sigma \log(2(1-p)) & p \geq 0.5 60 | \end{array}\right. 61 | }{ 62 | F^-1(p) = [if p < 0.5:] \mu + \sigma * log(2*p) 63 | [else:] \mu - \sigma * log(2*(1-p)) 64 | } 65 | } 66 | \examples{ 67 | 68 | x <- rlaplace(1e5, 5, 16) 69 | hist(x, 100, freq = FALSE) 70 | curve(dlaplace(x, 5, 16), -200, 200, n = 500, col = "red", add = TRUE) 71 | hist(plaplace(x, 5, 16)) 72 | plot(ecdf(x)) 73 | curve(plaplace(x, 5, 16), -200, 200, n = 500, col = "red", lwd = 2, add = TRUE) 74 | 75 | } 76 | \references{ 77 | Krishnamoorthy, K. (2006). Handbook of Statistical Distributions 78 | with Applications. Chapman & Hall/CRC 79 | 80 | Forbes, C., Evans, M. Hastings, N., & Peacock, B. (2011). 81 | Statistical Distributions. John Wiley & Sons. 82 | } 83 | \concept{Continuous} 84 | \concept{Univariate} 85 | \keyword{distribution} 86 | -------------------------------------------------------------------------------- /man/LocationScaleT.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/location-scale-t-distribution.R 3 | \name{LocationScaleT} 4 | \alias{LocationScaleT} 5 | \alias{dlst} 6 | \alias{plst} 7 | \alias{qlst} 8 | \alias{rlst} 9 | \title{Location-scale version of the t-distribution} 10 | \usage{ 11 | dlst(x, df, mu = 0, sigma = 1, log = FALSE) 12 | 13 | plst(q, df, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qlst(p, df, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rlst(n, df, mu = 0, sigma = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{df}{degrees of freedom (> 0, maybe non-integer). \code{df = Inf} is allowed.} 23 | 24 | \item{mu}{vector of locations} 25 | 26 | \item{sigma}{vector of positive valued scale parameters.} 27 | 28 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 29 | 30 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 31 | otherwise, \eqn{P[X > x]}.} 32 | 33 | \item{p}{vector of probabilities.} 34 | 35 | \item{n}{number of observations. If \code{length(n) > 1}, 36 | the length is taken to be the number required.} 37 | } 38 | \description{ 39 | Probability mass function, distribution function and random generation 40 | for location-scale version of the t-distribution. Location-scale version 41 | of the t-distribution besides degrees of freedom \eqn{\nu}, is parametrized 42 | using additional parameters \eqn{\mu} for location and \eqn{\sigma} for 43 | scale (\eqn{\mu = 0} and \eqn{\sigma = 1} for standard t-distribution). 44 | } 45 | \examples{ 46 | 47 | x <- rlst(1e5, 1000, 5, 13) 48 | hist(x, 100, freq = FALSE) 49 | curve(dlst(x, 1000, 5, 13), -60, 60, col = "red", add = TRUE) 50 | hist(plst(x, 1000, 5, 13)) 51 | plot(ecdf(x)) 52 | curve(plst(x, 1000, 5, 13), -60, 60, col = "red", lwd = 2, add = TRUE) 53 | 54 | } 55 | \seealso{ 56 | \code{\link[stats]{TDist}} 57 | } 58 | \concept{Continuous} 59 | \concept{Univariate} 60 | \keyword{distribution} 61 | -------------------------------------------------------------------------------- /man/LogSeries.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/logarithmic-series-distribution.R 3 | \name{LogSeries} 4 | \alias{LogSeries} 5 | \alias{dlgser} 6 | \alias{plgser} 7 | \alias{qlgser} 8 | \alias{rlgser} 9 | \title{Logarithmic series distribution} 10 | \usage{ 11 | dlgser(x, theta, log = FALSE) 12 | 13 | plgser(q, theta, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qlgser(p, theta, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rlgser(n, theta) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{theta}{vector; concentration parameter; (\code{0 < theta < 1}).} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the logarithmic series distribution. 37 | } 38 | \details{ 39 | Probability mass function 40 | \deqn{ 41 | f(x) = \frac{-1}{\log(1-\theta)} \frac{\theta^x}{x} 42 | }{ 43 | f(x) = (-1/log(1-\theta)*\theta^x) / x 44 | } 45 | 46 | Cumulative distribution function 47 | \deqn{ 48 | F(x) = \frac{-1}{\log(1-\theta)} \sum_{k=1}^x \frac{\theta^x}{x} 49 | }{ 50 | F(x) = -1/log(1-\theta) * sum((\theta^x)/x) 51 | } 52 | 53 | Quantile function and random generation are computed using 54 | algorithm described in Krishnamoorthy (2006). 55 | } 56 | \examples{ 57 | 58 | x <- rlgser(1e5, 0.66) 59 | xx <- seq(0, 100, by = 1) 60 | plot(prop.table(table(x)), type = "h") 61 | lines(xx, dlgser(xx, 0.66), col = "red") 62 | 63 | # Notice: distribution of F(X) is far from uniform: 64 | hist(plgser(x, 0.66), 50) 65 | 66 | xx <- seq(0, 100, by = 0.01) 67 | plot(ecdf(x)) 68 | lines(xx, plgser(xx, 0.66), col = "red", lwd = 2) 69 | 70 | } 71 | \references{ 72 | Krishnamoorthy, K. (2006). Handbook of Statistical Distributions 73 | with Applications. Chapman & Hall/CRC 74 | 75 | Forbes, C., Evans, M. Hastings, N., & Peacock, B. (2011). 76 | Statistical Distributions. John Wiley & Sons. 77 | } 78 | \concept{Discrete} 79 | \concept{Univariate} 80 | \keyword{distribution} 81 | -------------------------------------------------------------------------------- /man/Lomax.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lomax-distribution.R 3 | \name{Lomax} 4 | \alias{Lomax} 5 | \alias{dlomax} 6 | \alias{plomax} 7 | \alias{qlomax} 8 | \alias{rlomax} 9 | \title{Lomax distribution} 10 | \usage{ 11 | dlomax(x, lambda, kappa, log = FALSE) 12 | 13 | plomax(q, lambda, kappa, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qlomax(p, lambda, kappa, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rlomax(n, lambda, kappa) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{lambda, kappa}{positive valued parameters.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the Lomax distribution. 37 | } 38 | \details{ 39 | Probability density function 40 | \deqn{ 41 | f(x) = \frac{\lambda \kappa}{(1+\lambda x)^{\kappa+1}} 42 | }{ 43 | f(x) = \lambda*\kappa / (1+\lambda*x)^(\kappa+1) 44 | } 45 | 46 | Cumulative distribution function 47 | \deqn{ 48 | F(x) = 1-(1+\lambda x)^{-\kappa} 49 | }{ 50 | F(x) = 1-(1+\lambda*x)^-\kappa 51 | } 52 | 53 | Quantile function 54 | \deqn{ 55 | F^{-1}(p) = \frac{(1-p)^{-1/\kappa} -1}{\lambda} 56 | }{ 57 | F^-1(p) = ((1-p)^(-1/\kappa)-1) / \lambda 58 | } 59 | } 60 | \examples{ 61 | 62 | x <- rlomax(1e5, 5, 16) 63 | hist(x, 100, freq = FALSE) 64 | curve(dlomax(x, 5, 16), 0, 1, col = "red", add = TRUE, n = 5000) 65 | hist(plomax(x, 5, 16)) 66 | plot(ecdf(x)) 67 | curve(plomax(x, 5, 16), 0, 1, col = "red", lwd = 2, add = TRUE) 68 | 69 | } 70 | \concept{Continuous} 71 | \concept{Univariate} 72 | \keyword{distribution} 73 | -------------------------------------------------------------------------------- /man/MultiHypergeometric.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/multivariate-hypergeometric-distribution.R 3 | \name{MultiHypergeometric} 4 | \alias{MultiHypergeometric} 5 | \alias{dmvhyper} 6 | \alias{rmvhyper} 7 | \title{Multivariate hypergeometric distribution} 8 | \usage{ 9 | dmvhyper(x, n, k, log = FALSE) 10 | 11 | rmvhyper(nn, n, k) 12 | } 13 | \arguments{ 14 | \item{x}{\eqn{m}-column matrix of quantiles.} 15 | 16 | \item{n}{\eqn{m}-length vector or \eqn{m}-column matrix 17 | of numbers of balls in \eqn{m} colors.} 18 | 19 | \item{k}{the number of balls drawn from the urn.} 20 | 21 | \item{log}{logical; if TRUE, probabilities p are given as log(p).} 22 | 23 | \item{nn}{number of observations. If \code{length(n) > 1}, 24 | the length is taken to be the number required.} 25 | } 26 | \description{ 27 | Probability mass function and random generation 28 | for the multivariate hypergeometric distribution. 29 | } 30 | \details{ 31 | Probability mass function 32 | \deqn{ 33 | f(x) = \frac{\prod_{i=1}^m {n_i \choose x_i}}{{N \choose k}} 34 | }{ 35 | f(x) = prod(choose(n, x)) / choose(N, k) 36 | } 37 | 38 | The multivariate hypergeometric distribution is generalization of 39 | hypergeometric distribution. It is used for sampling \emph{without} replacement 40 | \eqn{k} out of \eqn{N} marbles in \eqn{m} colors, where each of the colors appears 41 | \eqn{n_i}{n[i]} times. Where \eqn{k=\sum_{i=1}^m x_i}{k=sum(x)}, 42 | \eqn{N=\sum_{i=1}^m n_i}{N=sum(n)} and \eqn{k \le N}{k<=N}. 43 | } 44 | \examples{ 45 | 46 | # Generating 10 random draws from multivariate hypergeometric 47 | # distribution parametrized using a vector 48 | 49 | rmvhyper(10, c(10, 12, 5, 8, 11), 33) 50 | 51 | } 52 | \references{ 53 | Gentle, J.E. (2006). Random number generation and Monte Carlo methods. Springer. 54 | } 55 | \seealso{ 56 | \code{\link[stats]{Hypergeometric}} 57 | } 58 | \concept{Discrete} 59 | \concept{Multivariate} 60 | \keyword{distribution} 61 | -------------------------------------------------------------------------------- /man/Multinomial.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/multinomial-distribution.R 3 | \name{Multinomial} 4 | \alias{Multinomial} 5 | \alias{dmnom} 6 | \alias{rmnom} 7 | \title{Multinomial distribution} 8 | \usage{ 9 | dmnom(x, size, prob, log = FALSE) 10 | 11 | rmnom(n, size, prob) 12 | } 13 | \arguments{ 14 | \item{x}{\eqn{k}-column matrix of quantiles.} 15 | 16 | \item{size}{numeric vector; number of trials (zero or more).} 17 | 18 | \item{prob}{\eqn{k}-column numeric matrix; probability of success on each trial.} 19 | 20 | \item{log}{logical; if TRUE, probabilities p are given as log(p).} 21 | 22 | \item{n}{number of observations. If \code{length(n) > 1}, 23 | the length is taken to be the number required.} 24 | } 25 | \description{ 26 | Probability mass function and random generation 27 | for the multinomial distribution. 28 | } 29 | \details{ 30 | Probability mass function 31 | \deqn{ 32 | f(x) = \frac{n!}{\prod_{i=1}^k x_i} \prod_{i=1}^k p_i^{x_i} 33 | }{ 34 | f(x) = n!/prod(x[i]!) * prod(p[i]^x[i]) 35 | } 36 | } 37 | \examples{ 38 | 39 | # Generating 10 random draws from multinomial distribution 40 | # parametrized using a vector 41 | 42 | (x <- rmnom(10, 3, c(1/3, 1/3, 1/3))) 43 | 44 | # Results are consistent with dmultinom() from stats: 45 | 46 | all.equal(dmultinom(x[1,], 3, c(1/3, 1/3, 1/3)), 47 | dmnom(x[1, , drop = FALSE], 3, c(1/3, 1/3, 1/3))) 48 | 49 | } 50 | \references{ 51 | Gentle, J.E. (2006). Random number generation and Monte Carlo methods. Springer. 52 | } 53 | \seealso{ 54 | \code{\link[stats]{Binomial}}, \code{\link[stats]{Multinomial}} 55 | } 56 | \concept{Discrete} 57 | \concept{Multivariate} 58 | \keyword{distribution} 59 | -------------------------------------------------------------------------------- /man/NSBeta.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/non-standard-beta-distribution.R 3 | \name{NSBeta} 4 | \alias{NSBeta} 5 | \alias{dnsbeta} 6 | \alias{dgbeta} 7 | \alias{pnsbeta} 8 | \alias{qnsbeta} 9 | \alias{rnsbeta} 10 | \title{Non-standard beta distribution} 11 | \usage{ 12 | dnsbeta(x, shape1, shape2, min = 0, max = 1, log = FALSE) 13 | 14 | pnsbeta(q, shape1, shape2, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE) 15 | 16 | qnsbeta(p, shape1, shape2, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE) 17 | 18 | rnsbeta(n, shape1, shape2, min = 0, max = 1) 19 | } 20 | \arguments{ 21 | \item{x, q}{vector of quantiles.} 22 | 23 | \item{shape1, shape2}{non-negative parameters of the Beta distribution.} 24 | 25 | \item{min, max}{lower and upper bounds.} 26 | 27 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 28 | 29 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \leq x]}, 30 | otherwise, \eqn{P[X > x]}.} 31 | 32 | \item{p}{vector of probabilities.} 33 | 34 | \item{n}{number of observations. If \code{length(n) > 1}, 35 | the length is taken to be the number required.} 36 | } 37 | \description{ 38 | Non-standard form of beta distribution with lower and upper bounds 39 | denoted as \code{min} and \code{max}. By default \code{min=0} 40 | and \code{max=1} what leads to standard beta distribution. 41 | } 42 | \examples{ 43 | 44 | x <- rnsbeta(1e5, 5, 13, -4, 8) 45 | hist(x, 100, freq = FALSE) 46 | curve(dnsbeta(x, 5, 13, -4, 8), -4, 6, col = "red", add = TRUE) 47 | hist(pnsbeta(x, 5, 13, -4, 8)) 48 | plot(ecdf(x)) 49 | curve(pnsbeta(x, 5, 13, -4, 8), -4, 6, col = "red", lwd = 2, add = TRUE) 50 | 51 | } 52 | \seealso{ 53 | \code{\link[stats]{Beta}} 54 | } 55 | \concept{Continuous} 56 | \concept{Univariate} 57 | \keyword{distribution} 58 | -------------------------------------------------------------------------------- /man/NormalMix.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mixture-of-normal-distributions.R 3 | \name{NormalMix} 4 | \alias{NormalMix} 5 | \alias{dmixnorm} 6 | \alias{pmixnorm} 7 | \alias{rmixnorm} 8 | \title{Mixture of normal distributions} 9 | \usage{ 10 | dmixnorm(x, mean, sd, alpha, log = FALSE) 11 | 12 | pmixnorm(q, mean, sd, alpha, lower.tail = TRUE, log.p = FALSE) 13 | 14 | rmixnorm(n, mean, sd, alpha) 15 | } 16 | \arguments{ 17 | \item{x, q}{vector of quantiles.} 18 | 19 | \item{mean}{matrix (or vector) of means.} 20 | 21 | \item{sd}{matrix (or vector) of standard deviations.} 22 | 23 | \item{alpha}{matrix (or vector) of mixing proportions; 24 | mixing proportions need to sum up to 1.} 25 | 26 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 27 | 28 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 29 | otherwise, \eqn{P[X > x]}.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | 34 | \item{p}{vector of probabilities.} 35 | } 36 | \description{ 37 | Density, distribution function and random generation 38 | for the mixture of normal distributions. 39 | } 40 | \details{ 41 | Probability density function 42 | \deqn{ 43 | f(x) = \alpha_1 f_1(x; \mu_1, \sigma_1) + \dots + \alpha_k f_k(x; \mu_k, \sigma_k) 44 | }{ 45 | f(x) = \alpha[1] * f1(x; \mu[1], \sigma[1]) + \dots + \alpha[k] * fk(x; \mu[k], \sigma[k]) 46 | } 47 | 48 | Cumulative distribution function 49 | \deqn{ 50 | F(x) = \alpha_1 F_1(x; \mu_1, \sigma_1) + \dots + \alpha_k F_k(x; \mu_k, \sigma_k) 51 | }{ 52 | F(x) = \alpha[1] * F1(x; \mu[1], \sigma[1]) + \dots + \alpha[k] * Fk(x; \mu[k], \sigma[k]) 53 | } 54 | 55 | where \eqn{\sum_i \alpha_i = 1}{sum(\alpha[i]) == 1}. 56 | } 57 | \examples{ 58 | 59 | x <- rmixnorm(1e5, c(0.5, 3, 6), c(3, 1, 1), c(1/3, 1/3, 1/3)) 60 | hist(x, 100, freq = FALSE) 61 | curve(dmixnorm(x, c(0.5, 3, 6), c(3, 1, 1), c(1/3, 1/3, 1/3)), 62 | -20, 20, n = 500, col = "red", add = TRUE) 63 | hist(pmixnorm(x, c(0.5, 3, 6), c(3, 1, 1), c(1/3, 1/3, 1/3))) 64 | plot(ecdf(x)) 65 | curve(pmixnorm(x, c(0.5, 3, 6), c(3, 1, 1), c(1/3, 1/3, 1/3)), 66 | -20, 20, n = 500, col = "red", lwd = 2, add = TRUE) 67 | 68 | } 69 | \concept{Continuous} 70 | \concept{Univariate} 71 | \keyword{distribution} 72 | -------------------------------------------------------------------------------- /man/Pareto.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/pareto-distribution.R 3 | \name{Pareto} 4 | \alias{Pareto} 5 | \alias{dpareto} 6 | \alias{ppareto} 7 | \alias{qpareto} 8 | \alias{rpareto} 9 | \title{Pareto distribution} 10 | \usage{ 11 | dpareto(x, a = 1, b = 1, log = FALSE) 12 | 13 | ppareto(q, a = 1, b = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qpareto(p, a = 1, b = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rpareto(n, a = 1, b = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{a, b}{positive valued scale and location parameters.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the Pareto distribution. 37 | } 38 | \details{ 39 | Probability density function 40 | \deqn{ 41 | f(x) = \frac{ab^a}{x^{a+1}} 42 | }{ 43 | f(x) = (a*b^a) / x^(a+1) 44 | } 45 | 46 | Cumulative distribution function 47 | \deqn{ 48 | F(x) = 1 - \left(\frac{b}{x}\right)^a 49 | }{ 50 | F(x) = 1 - (b/x)^a 51 | } 52 | 53 | Quantile function 54 | \deqn{ 55 | F^{-1}(p) = \frac{b}{(1-p)^{1-a}} 56 | }{ 57 | F^-1(p) = b/(1-p)^(1-a) 58 | } 59 | } 60 | \examples{ 61 | 62 | x <- rpareto(1e5, 5, 16) 63 | hist(x, 100, freq = FALSE) 64 | curve(dpareto(x, 5, 16), 0, 200, col = "red", add = TRUE) 65 | hist(ppareto(x, 5, 16)) 66 | plot(ecdf(x)) 67 | curve(ppareto(x, 5, 16), 0, 200, col = "red", lwd = 2, add = TRUE) 68 | 69 | } 70 | \references{ 71 | Krishnamoorthy, K. (2006). Handbook of Statistical Distributions 72 | with Applications. Chapman & Hall/CRC 73 | } 74 | \concept{Continuous} 75 | \concept{Univariate} 76 | \keyword{distribution} 77 | -------------------------------------------------------------------------------- /man/PoissonMix.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mixture-of-poisson-distributions.R 3 | \name{PoissonMix} 4 | \alias{PoissonMix} 5 | \alias{dmixpois} 6 | \alias{pmixpois} 7 | \alias{rmixpois} 8 | \title{Mixture of Poisson distributions} 9 | \usage{ 10 | dmixpois(x, lambda, alpha, log = FALSE) 11 | 12 | pmixpois(q, lambda, alpha, lower.tail = TRUE, log.p = FALSE) 13 | 14 | rmixpois(n, lambda, alpha) 15 | } 16 | \arguments{ 17 | \item{x, q}{vector of quantiles.} 18 | 19 | \item{lambda}{matrix (or vector) of (non-negative) means.} 20 | 21 | \item{alpha}{matrix (or vector) of mixing proportions; 22 | mixing proportions need to sum up to 1.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{n}{number of observations. If \code{length(n) > 1}, 30 | the length is taken to be the number required.} 31 | 32 | \item{p}{vector of probabilities.} 33 | } 34 | \description{ 35 | Density, distribution function and random generation 36 | for the mixture of Poisson distributions. 37 | } 38 | \details{ 39 | Probability density function 40 | \deqn{ 41 | f(x) = \alpha_1 f_1(x; \lambda_1) + \dots + \alpha_k f_k(x; \lambda_k) 42 | }{ 43 | f(x) = \alpha[1] * f1(x; \lambda[1]) + \dots + \alpha[k] * fk(x; \lambda[k]) 44 | } 45 | 46 | Cumulative distribution function 47 | \deqn{ 48 | F(x) = \alpha_1 F_1(x; \lambda_1) + \dots + \alpha_k F_k(x; \lambda_k) 49 | }{ 50 | F(x) = \alpha[1] * F1(x; \lambda[1]) + \dots + \alpha[k] * Fk(x; \lambda[k]) 51 | } 52 | 53 | where \eqn{\sum_i \alpha_i = 1}{sum(\alpha[i]) == 1}. 54 | } 55 | \examples{ 56 | 57 | x <- rmixpois(1e5, c(5, 12, 19), c(1/3, 1/3, 1/3)) 58 | xx <- seq(-1, 50) 59 | plot(prop.table(table(x))) 60 | lines(xx, dmixpois(xx, c(5, 12, 19), c(1/3, 1/3, 1/3)), col = "red") 61 | hist(pmixpois(x, c(5, 12, 19), c(1/3, 1/3, 1/3))) 62 | 63 | xx <- seq(0, 50, by = 0.01) 64 | plot(ecdf(x)) 65 | lines(xx, pmixpois(xx, c(5, 12, 19), c(1/3, 1/3, 1/3)), col = "red", lwd = 2) 66 | 67 | } 68 | \concept{Discrete} 69 | \concept{Univariate} 70 | \keyword{distribution} 71 | -------------------------------------------------------------------------------- /man/PowerDist.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/power-distribution.R 3 | \name{PowerDist} 4 | \alias{PowerDist} 5 | \alias{dpower} 6 | \alias{ppower} 7 | \alias{qpower} 8 | \alias{rpower} 9 | \title{Power distribution} 10 | \usage{ 11 | dpower(x, alpha, beta, log = FALSE) 12 | 13 | ppower(q, alpha, beta, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qpower(p, alpha, beta, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rpower(n, alpha, beta) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{alpha, beta}{parameters.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the power distribution. 37 | } 38 | \details{ 39 | Probability density function 40 | \deqn{ 41 | f(x) = \frac{\beta x^{\beta-1}}{\alpha^\beta} 42 | }{ 43 | f(x) = (\beta*x^(\beta-1)) / (\alpha^\beta) 44 | } 45 | 46 | Cumulative distribution function 47 | \deqn{ 48 | F(x) = \frac{x^\beta}{\alpha^\beta} 49 | }{ 50 | F(x) = x^\beta / \alpha^\beta 51 | } 52 | 53 | Quantile function 54 | \deqn{ 55 | F^{-1}(p) = \alpha p^{1/\beta} 56 | }{ 57 | F^-1(p) = \alpha * p^(1/\beta) 58 | } 59 | } 60 | \examples{ 61 | 62 | x <- rpower(1e5, 5, 16) 63 | hist(x, 100, freq = FALSE) 64 | curve(dpower(x, 5, 16), 2, 6, col = "red", add = TRUE, n = 5000) 65 | hist(ppower(x, 5, 16)) 66 | plot(ecdf(x)) 67 | curve(ppower(x, 5, 16), 2, 6, col = "red", lwd = 2, add = TRUE) 68 | 69 | } 70 | \concept{Continuous} 71 | \concept{Univariate} 72 | \keyword{distribution} 73 | -------------------------------------------------------------------------------- /man/Rademacher.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rademacher-distribution.R 3 | \name{Rademacher} 4 | \alias{Rademacher} 5 | \alias{rsign} 6 | \title{Random generation from Rademacher distribution} 7 | \usage{ 8 | rsign(n) 9 | } 10 | \arguments{ 11 | \item{n}{number of observations. If \code{length(n) > 1}, 12 | the length is taken to be the number required.} 13 | } 14 | \description{ 15 | Random generation for the Rademacher distribution (values -1 and +1 16 | with equal probability). 17 | } 18 | \keyword{distribution} 19 | -------------------------------------------------------------------------------- /man/Rayleigh.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rayleigh-distribution.R 3 | \name{Rayleigh} 4 | \alias{Rayleigh} 5 | \alias{drayleigh} 6 | \alias{prayleigh} 7 | \alias{qrayleigh} 8 | \alias{rrayleigh} 9 | \title{Rayleigh distribution} 10 | \usage{ 11 | drayleigh(x, sigma = 1, log = FALSE) 12 | 13 | prayleigh(q, sigma = 1, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qrayleigh(p, sigma = 1, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rrayleigh(n, sigma = 1) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{sigma}{positive valued parameter.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the Rayleigh distribution. 37 | } 38 | \details{ 39 | Probability density function 40 | \deqn{ 41 | f(x) = \frac{x}{\sigma^2} \exp\left(-\frac{x^2}{2\sigma^2}\right) 42 | }{ 43 | f(x) = x/\sigma^2 * exp(-(x^2 / 2*\sigma^2)) 44 | } 45 | 46 | Cumulative distribution function 47 | \deqn{ 48 | F(x) = 1 - \exp\left(-\frac{x^2}{2\sigma^2}\right) 49 | }{ 50 | F(x) = 1 - exp(-x^2 / 2*\sigma^2) 51 | } 52 | 53 | Quantile function 54 | \deqn{ 55 | F^{-1}(p) = \sqrt{-2\sigma^2 \log(1-p)} 56 | }{ 57 | F^-1(p) = sqrt(-2*\sigma^2 * log(1-p)) 58 | } 59 | } 60 | \examples{ 61 | 62 | x <- rrayleigh(1e5, 13) 63 | hist(x, 100, freq = FALSE) 64 | curve(drayleigh(x, 13), 0, 60, col = "red", add = TRUE) 65 | hist(prayleigh(x, 13)) 66 | plot(ecdf(x)) 67 | curve(prayleigh(x, 13), 0, 60, col = "red", lwd = 2, add = TRUE) 68 | 69 | } 70 | \references{ 71 | Krishnamoorthy, K. (2006). Handbook of Statistical Distributions 72 | with Applications. Chapman & Hall/CRC. 73 | 74 | Forbes, C., Evans, M. Hastings, N., & Peacock, B. (2011). 75 | Statistical Distributions. John Wiley & Sons. 76 | } 77 | \concept{Continuous} 78 | \concept{Univariate} 79 | \keyword{distribution} 80 | -------------------------------------------------------------------------------- /man/ShiftGomp.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/shifted-gompertz-distribution.R 3 | \name{ShiftGomp} 4 | \alias{ShiftGomp} 5 | \alias{dsgomp} 6 | \alias{psgomp} 7 | \alias{rsgomp} 8 | \title{Shifted Gompertz distribution} 9 | \usage{ 10 | dsgomp(x, b, eta, log = FALSE) 11 | 12 | psgomp(q, b, eta, lower.tail = TRUE, log.p = FALSE) 13 | 14 | rsgomp(n, b, eta) 15 | } 16 | \arguments{ 17 | \item{x, q}{vector of quantiles.} 18 | 19 | \item{b, eta}{positive valued scale and shape parameters; 20 | both need to be positive.} 21 | 22 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 23 | 24 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 25 | otherwise, \eqn{P[X > x]}.} 26 | 27 | \item{n}{number of observations. If \code{length(n) > 1}, 28 | the length is taken to be the number required.} 29 | } 30 | \description{ 31 | Density, distribution function, and random generation 32 | for the shifted Gompertz distribution. 33 | } 34 | \details{ 35 | If \eqn{X} follows exponential distribution parametrized by scale \eqn{b} and 36 | \eqn{Y} follows reparametrized Gumbel distribution with cumulative distribution function 37 | \eqn{F(x) = \exp(-\eta e^{-bx})}{F(x) = exp(-\eta*exp(-b*x))} parametrized by 38 | scale \eqn{b} and shape \eqn{\eta}, then \eqn{\max(X,Y)}{max(X,Y)} follows shifted 39 | Gompertz distribution parametrized by scale \eqn{b>0} and shape \eqn{\eta>0}. 40 | The above relation is used by \code{rsgomp} function for random generation from 41 | shifted Gompertz distribution. 42 | 43 | Probability density function 44 | \deqn{ 45 | f(x) = b e^{-bx} \exp(-\eta e^{-bx}) \left[1 + \eta(1 - e^{-bx})\right] 46 | }{ 47 | f(x) = b*exp(-b*x) * exp(-\eta*exp(-b*x)) * (1 + \eta*(1 - exp(-b*x))) 48 | } 49 | 50 | Cumulative distribution function 51 | \deqn{ 52 | F(x) = (1-e^{-bx}) \exp(-\eta e^{-bx}) 53 | }{ 54 | F(x) = (1-exp(-b*x)) * exp(-\eta*exp(-b*x)) 55 | } 56 | } 57 | \examples{ 58 | 59 | x <- rsgomp(1e5, 0.4, 1) 60 | hist(x, 50, freq = FALSE) 61 | curve(dsgomp(x, 0.4, 1), 0, 30, col = "red", add = TRUE) 62 | hist(psgomp(x, 0.4, 1)) 63 | plot(ecdf(x)) 64 | curve(psgomp(x, 0.4, 1), 0, 30, col = "red", lwd = 2, add = TRUE) 65 | 66 | } 67 | \references{ 68 | Bemmaor, A.C. (1994). 69 | Modeling the Diffusion of New Durable Goods: Word-of-Mouth Effect Versus Consumer Heterogeneity. 70 | [In:] G. Laurent, G.L. Lilien & B. Pras. Research Traditions in Marketing. 71 | Boston: Kluwer Academic Publishers. pp. 201-223. 72 | 73 | Jimenez, T.F. and Jodra, P. (2009). 74 | A Note on the Moments and Computer Generation of the Shifted Gompertz Distribution. 75 | Communications in Statistics - Theory and Methods, 38(1), 78-89. 76 | 77 | Jimenez T.F. (2014). 78 | Estimation of the Parameters of the Shifted Gompertz Distribution, 79 | Using Least Squares, Maximum Likelihood and Moments Methods. 80 | Journal of Computational and Applied Mathematics, 255(1), 867-877. 81 | } 82 | \concept{Continuous} 83 | \concept{Univariate} 84 | \keyword{distribution} 85 | -------------------------------------------------------------------------------- /man/Skellam.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/skellam-distribution.R 3 | \name{Skellam} 4 | \alias{Skellam} 5 | \alias{dskellam} 6 | \alias{rskellam} 7 | \title{Skellam distribution} 8 | \usage{ 9 | dskellam(x, mu1, mu2, log = FALSE) 10 | 11 | rskellam(n, mu1, mu2) 12 | } 13 | \arguments{ 14 | \item{x}{vector of quantiles.} 15 | 16 | \item{mu1, mu2}{positive valued parameters.} 17 | 18 | \item{log}{logical; if TRUE, probabilities p are given as log(p).} 19 | 20 | \item{n}{number of observations. If \code{length(n) > 1}, 21 | the length is taken to be the number required.} 22 | } 23 | \description{ 24 | Probability mass function and random generation 25 | for the Skellam distribution. 26 | } 27 | \details{ 28 | If \eqn{X} and \eqn{Y} follow Poisson distributions with means 29 | \eqn{\mu_1}{\mu[1]} and \eqn{\mu_2}{\mu[2]}, than \eqn{X-Y} follows 30 | Skellam distribution parametrized by \eqn{\mu_1}{\mu[1]} and \eqn{\mu_2}{\mu[2]}. 31 | 32 | Probability mass function 33 | \deqn{ 34 | f(x) = e^{-(\mu_1\!+\!\mu_2)} \left(\frac{\mu_1}{\mu_2}\right)^{k/2}\!\!I_{k}(2\sqrt{\mu_1\mu_2}) 35 | }{ 36 | f(x) = exp(-(\mu1+\mu2)) * (\mu1/\mu2)^(x/2) * besselI(2*sqrt(\mu1*\mu2), x) 37 | } 38 | } 39 | \examples{ 40 | 41 | x <- rskellam(1e5, 5, 13) 42 | xx <- -40:40 43 | plot(prop.table(table(x)), type = "h") 44 | lines(xx, dskellam(xx, 5, 13), col = "red") 45 | 46 | } 47 | \references{ 48 | Karlis, D., & Ntzoufras, I. (2006). Bayesian analysis of the differences of count data. 49 | Statistics in medicine, 25(11), 1885-1905. 50 | 51 | Skellam, J.G. (1946). The frequency distribution of the difference between 52 | two Poisson variates belonging to different populations. 53 | Journal of the Royal Statistical Society, series A, 109(3), 26. 54 | } 55 | \concept{Discrete} 56 | \concept{Univariate} 57 | \keyword{distribution} 58 | -------------------------------------------------------------------------------- /man/Slash.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/slash-distribution.R 3 | \name{Slash} 4 | \alias{Slash} 5 | \alias{dslash} 6 | \alias{pslash} 7 | \alias{rslash} 8 | \title{Slash distribution} 9 | \usage{ 10 | dslash(x, mu = 0, sigma = 1, log = FALSE) 11 | 12 | pslash(q, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE) 13 | 14 | rslash(n, mu = 0, sigma = 1) 15 | } 16 | \arguments{ 17 | \item{x, q}{vector of quantiles.} 18 | 19 | \item{mu}{vector of locations} 20 | 21 | \item{sigma}{vector of positive valued scale parameters.} 22 | 23 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 24 | 25 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 26 | otherwise, \eqn{P[X > x]}.} 27 | 28 | \item{n}{number of observations. If \code{length(n) > 1}, 29 | the length is taken to be the number required.} 30 | } 31 | \description{ 32 | Probability mass function, distribution function and random generation 33 | for slash distribution. 34 | } 35 | \details{ 36 | If \eqn{Z \sim \mathrm{Normal}(0, 1)}{Z ~ Normal(0, 1)} and \eqn{U \sim \mathrm{Uniform}(0, 1)}{U ~ Uniform(0, 1)}, 37 | then \eqn{Z/U} follows slash distribution. 38 | 39 | Probability density function 40 | 41 | \deqn{ 42 | f(x) = \left\{\begin{array}{ll} 43 | \frac{\phi(0) - \phi(x)}{x^2} & x \ne 0 \\ 44 | \frac{1}{2\sqrt{2\pi}} & x = 0 45 | \end{array}\right. 46 | }{ 47 | f(x) = [if x != 0:] (\phi(0)-\phi(x))/x^2 [else:] 1/(2*sqrt(2*\pi)) 48 | } 49 | 50 | Cumulative distribution function 51 | 52 | \deqn{ 53 | F(x) = \left\{\begin{array}{ll} 54 | \Phi(x) - \frac{\phi(0)-\phi(x)}{x} & x \neq 0 \\ 55 | \frac{1}{2} & x = 0 56 | \end{array}\right. 57 | }{ 58 | F(x) = [if x != 0:] \Phi(x) - [\phi(0)-\phi(x)]/x [else:] 1/2 59 | } 60 | } 61 | \examples{ 62 | 63 | x <- rslash(1e5, 5, 3) 64 | hist(x, 1e5, freq = FALSE, xlim = c(-100, 100)) 65 | curve(dslash(x, 5, 3), -100, 100, col = "red", n = 500, add = TRUE) 66 | hist(pslash(x, 5, 3)) 67 | plot(ecdf(x), xlim = c(-100, 100)) 68 | curve(pslash(x, 5, 3), -100, 100, col = "red", lwd = 2, n = 500, add = TRUE) 69 | 70 | } 71 | \concept{Continuous} 72 | \concept{Univariate} 73 | \keyword{distribution} 74 | -------------------------------------------------------------------------------- /man/Triangular.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/triangular-distribution.R 3 | \name{Triangular} 4 | \alias{Triangular} 5 | \alias{dtriang} 6 | \alias{ptriang} 7 | \alias{qtriang} 8 | \alias{rtriang} 9 | \title{Triangular distribution} 10 | \usage{ 11 | dtriang(x, a = -1, b = 1, c = (a + b)/2, log = FALSE) 12 | 13 | ptriang(q, a = -1, b = 1, c = (a + b)/2, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qtriang(p, a = -1, b = 1, c = (a + b)/2, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rtriang(n, a = -1, b = 1, c = (a + b)/2) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{a, b, c}{minimum, maximum and mode of the distribution.} 23 | 24 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 25 | 26 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 27 | otherwise, \eqn{P[X > x]}.} 28 | 29 | \item{p}{vector of probabilities.} 30 | 31 | \item{n}{number of observations. If \code{length(n) > 1}, 32 | the length is taken to be the number required.} 33 | } 34 | \description{ 35 | Density, distribution function, quantile function and random generation 36 | for the triangular distribution. 37 | } 38 | \details{ 39 | Probability density function 40 | \deqn{ 41 | f(x) = \left\{\begin{array}{ll} 42 | \frac{2(x-a)}{(b-a)(c-a)} & x < c \\ 43 | \frac{2}{b-a} & x = c \\ 44 | \frac{2(b-x)}{(b-a)(b-c)} & x > c 45 | \end{array}\right. 46 | }{ 47 | f(x) = [if x < c:] (2*(x-a)) / ((b-a)*(c-a)) 48 | [if x = c:] 2/(b-a) 49 | [if x >= c:] (2*(b-x)) / ((b-a)*(b-c)) 50 | } 51 | 52 | Cumulative distribution function 53 | \deqn{ 54 | F(x) = \left\{\begin{array}{ll} 55 | \frac{(x-a)^2}{(b-a)(c-a)} & x \leq c \\ 56 | 1 - \frac{(b-x)^2}{(b-a)(b-c)} & x > c 57 | \end{array}\right. 58 | }{ 59 | F(x) = [if x <= c:] (x-a)^2 / ((b-a)*(c-a)) 60 | [if x > c:] 1 - ((b-x)^2 / ((b-a)*(b-c))) 61 | } 62 | 63 | Quantile function 64 | \deqn{ 65 | F^{-1}(p) = \left\{\begin{array}{ll} 66 | a + \sqrt{p \times (b-a)(c-a)} & p \leq \frac{c-a}{b-a} \\ 67 | b - \sqrt{(1-p)(b-a)(b-c)} & p > \frac{c-a}{b-a} 68 | \end{array}\right. 69 | }{ 70 | F^-1(p) = [if p < (c-a)/(b-a):] a + sqrt(p*(b-a)*(c-a)) 71 | [else:] b - sqrt((1-p)*(b-a)*(b-c)) 72 | } 73 | 74 | For random generation MINMAX method described by 75 | Stein and Keblis (2009) is used. 76 | } 77 | \examples{ 78 | 79 | x <- rtriang(1e5, 5, 7, 6) 80 | hist(x, 100, freq = FALSE) 81 | curve(dtriang(x, 5, 7, 6), 3, 10, n = 500, col = "red", add = TRUE) 82 | hist(ptriang(x, 5, 7, 6)) 83 | plot(ecdf(x)) 84 | curve(ptriang(x, 5, 7, 6), 3, 10, n = 500, col = "red", lwd = 2, add = TRUE) 85 | 86 | } 87 | \references{ 88 | Forbes, C., Evans, M. Hastings, N., & Peacock, B. (2011). 89 | Statistical Distributions. John Wiley & Sons. 90 | 91 | Stein, W. E., & Keblis, M. F. (2009). 92 | A new method to simulate the triangular distribution. 93 | Mathematical and computer modelling, 49(5), 1143-1147. 94 | } 95 | \concept{Continuous} 96 | \concept{Univariate} 97 | \keyword{distribution} 98 | -------------------------------------------------------------------------------- /man/TruncBinom.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/truncated-binomial-distribution.R 3 | \name{TruncBinom} 4 | \alias{TruncBinom} 5 | \alias{dtbinom} 6 | \alias{ptbinom} 7 | \alias{qtbinom} 8 | \alias{rtbinom} 9 | \title{Truncated binomial distribution} 10 | \usage{ 11 | dtbinom(x, size, prob, a = -Inf, b = Inf, log = FALSE) 12 | 13 | ptbinom(q, size, prob, a = -Inf, b = Inf, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qtbinom(p, size, prob, a = -Inf, b = Inf, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rtbinom(n, size, prob, a = -Inf, b = Inf) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{size}{number of trials (zero or more).} 23 | 24 | \item{prob}{probability of success on each trial.} 25 | 26 | \item{a, b}{lower and upper truncation points (\code{a < x <= b}).} 27 | 28 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 29 | 30 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 31 | otherwise, \eqn{P[X > x]}.} 32 | 33 | \item{p}{vector of probabilities.} 34 | 35 | \item{n}{number of observations. If \code{length(n) > 1}, 36 | the length is taken to be the number required.} 37 | } 38 | \description{ 39 | Density, distribution function, quantile function and random generation 40 | for the truncated binomial distribution. 41 | } 42 | \examples{ 43 | 44 | x <- rtbinom(1e5, 100, 0.83, 76, 86) 45 | xx <- seq(0, 100) 46 | plot(prop.table(table(x))) 47 | lines(xx, dtbinom(xx, 100, 0.83, 76, 86), col = "red") 48 | hist(ptbinom(x, 100, 0.83, 76, 86)) 49 | 50 | xx <- seq(0, 100, by = 0.01) 51 | plot(ecdf(x)) 52 | lines(xx, ptbinom(xx, 100, 0.83, 76, 86), col = "red", lwd = 2) 53 | uu <- seq(0, 1, by = 0.001) 54 | lines(qtbinom(uu, 100, 0.83, 76, 86), uu, col = "blue", lty = 2) 55 | 56 | } 57 | \concept{Discrete} 58 | \concept{Univariate} 59 | \keyword{distribution} 60 | -------------------------------------------------------------------------------- /man/TruncPoisson.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/truncated-poisson-distributtion.R 3 | \name{TruncPoisson} 4 | \alias{TruncPoisson} 5 | \alias{dtpois} 6 | \alias{ptpois} 7 | \alias{qtpois} 8 | \alias{rtpois} 9 | \title{Truncated Poisson distribution} 10 | \usage{ 11 | dtpois(x, lambda, a = -Inf, b = Inf, log = FALSE) 12 | 13 | ptpois(q, lambda, a = -Inf, b = Inf, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qtpois(p, lambda, a = -Inf, b = Inf, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rtpois(n, lambda, a = -Inf, b = Inf) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{lambda}{vector of (non-negative) means.} 23 | 24 | \item{a, b}{lower and upper truncation points (\code{a < x <= b}).} 25 | 26 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 27 | 28 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 29 | otherwise, \eqn{P[X > x]}.} 30 | 31 | \item{p}{vector of probabilities.} 32 | 33 | \item{n}{number of observations. If \code{length(n) > 1}, 34 | the length is taken to be the number required.} 35 | } 36 | \description{ 37 | Density, distribution function, quantile function and random generation 38 | for the truncated Poisson distribution. 39 | } 40 | \examples{ 41 | 42 | x <- rtpois(1e5, 14, 16) 43 | xx <- seq(-1, 50) 44 | plot(prop.table(table(x))) 45 | lines(xx, dtpois(xx, 14, 16), col = "red") 46 | hist(ptpois(x, 14, 16)) 47 | 48 | xx <- seq(0, 50, by = 0.01) 49 | plot(ecdf(x)) 50 | lines(xx, ptpois(xx, 14, 16), col = "red", lwd = 2) 51 | 52 | uu <- seq(0, 1, by = 0.001) 53 | lines(qtpois(uu, 14, 16), uu, col = "blue", lty = 2) 54 | 55 | # Zero-truncated Poisson 56 | 57 | x <- rtpois(1e5, 5, 0) 58 | xx <- seq(-1, 50) 59 | plot(prop.table(table(x))) 60 | lines(xx, dtpois(xx, 5, 0), col = "red") 61 | hist(ptpois(x, 5, 0)) 62 | 63 | xx <- seq(0, 50, by = 0.01) 64 | plot(ecdf(x)) 65 | lines(xx, ptpois(xx, 5, 0), col = "red", lwd = 2) 66 | lines(qtpois(uu, 5, 0), uu, col = "blue", lty = 2) 67 | 68 | } 69 | \references{ 70 | Plackett, R.L. (1953). The truncated Poisson distribution. 71 | Biometrics, 9(4), 485-488. 72 | 73 | Singh, J. (1978). A characterization of positive Poisson distribution and 74 | its statistical application. 75 | SIAM Journal on Applied Mathematics, 34(3), 545-548. 76 | 77 | Dalgaard, P. (May 1, 2005). [R] simulate zero-truncated Poisson distribution. 78 | R-help mailing list. 79 | \url{https://stat.ethz.ch/pipermail/r-help/2005-May/070680.html} 80 | } 81 | \concept{Discrete} 82 | \concept{Univariate} 83 | \keyword{distribution} 84 | -------------------------------------------------------------------------------- /man/TukeyLambda.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tukey-lambda-distribution.R 3 | \name{TukeyLambda} 4 | \alias{TukeyLambda} 5 | \alias{qtlambda} 6 | \alias{rtlambda} 7 | \title{Tukey lambda distribution} 8 | \usage{ 9 | qtlambda(p, lambda, lower.tail = TRUE, log.p = FALSE) 10 | 11 | rtlambda(n, lambda) 12 | } 13 | \arguments{ 14 | \item{p}{vector of probabilities.} 15 | 16 | \item{lambda}{shape parameter.} 17 | 18 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 19 | otherwise, \eqn{P[X > x]}.} 20 | 21 | \item{log.p}{logical; if TRUE, probabilities p are given as log(p).} 22 | 23 | \item{n}{number of observations. If \code{length(n) > 1}, 24 | the length is taken to be the number required.} 25 | } 26 | \description{ 27 | Quantile function, and random generation for the Tukey lambda 28 | distribution. 29 | } 30 | \details{ 31 | Tukey lambda distribution is a continuous probability distribution defined in terms 32 | of its quantile function. It is typically used to identify other distributions. 33 | 34 | Quantile function: 35 | 36 | \deqn{F^{-1}(p) = \left\{\begin{array}{ll} 37 | \frac{1}{\lambda} [p^\lambda - (1-p)^\lambda] & \lambda \ne 0 \\ 38 | \log(\frac{p}{1-p}) & \lambda = 0 39 | \end{array}\right. 40 | }{ 41 | F^-1(p) = [if \lambda != 0:] (p^\lambda - (1-p)^\lambda)/\lambda 42 | [if \lambda = 0:] log(p/(1-p)) 43 | } 44 | } 45 | \examples{ 46 | 47 | pp = seq(0, 1, by = 0.001) 48 | partmp <- par(mfrow = c(2,3)) 49 | plot(qtlambda(pp, -1), pp, type = "l", main = "lambda = -1 (Cauchy)") 50 | plot(qtlambda(pp, 0), pp, type = "l", main = "lambda = 0 (logistic)") 51 | plot(qtlambda(pp, 0.14), pp, type = "l", main = "lambda = 0.14 (normal)") 52 | plot(qtlambda(pp, 0.5), pp, type = "l", main = "lambda = 0.5 (concave)") 53 | plot(qtlambda(pp, 1), pp, type = "l", main = "lambda = 1 (uniform)") 54 | plot(qtlambda(pp, 2), pp, type = "l", main = "lambda = 2 (uniform)") 55 | 56 | hist(rtlambda(1e5, -1), freq = FALSE, main = "lambda = -1 (Cauchy)") 57 | hist(rtlambda(1e5, 0), freq = FALSE, main = "lambda = 0 (logistic)") 58 | hist(rtlambda(1e5, 0.14), freq = FALSE, main = "lambda = 0.14 (normal)") 59 | hist(rtlambda(1e5, 0.5), freq = FALSE, main = "lambda = 0.5 (concave)") 60 | hist(rtlambda(1e5, 1), freq = FALSE, main = "lambda = 1 (uniform)") 61 | hist(rtlambda(1e5, 2), freq = FALSE, main = "lambda = 2 (uniform)") 62 | par(partmp) 63 | 64 | } 65 | \references{ 66 | Joiner, B.L., & Rosenblatt, J.R. (1971). 67 | Some properties of the range in samples from Tukey's symmetric lambda distributions. 68 | Journal of the American Statistical Association, 66(334), 394-399. 69 | 70 | Hastings Jr, C., Mosteller, F., Tukey, J.W., & Winsor, C.P. (1947). 71 | Low moments for small samples: a comparative study of order statistics. 72 | The Annals of Mathematical Statistics, 413-426. 73 | } 74 | \concept{Continuous} 75 | \concept{Univariate} 76 | \keyword{distribution} 77 | -------------------------------------------------------------------------------- /man/Wald.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/wald-distribution.R 3 | \name{Wald} 4 | \alias{Wald} 5 | \alias{dwald} 6 | \alias{pwald} 7 | \alias{rwald} 8 | \title{Wald (inverse Gaussian) distribution} 9 | \usage{ 10 | dwald(x, mu, lambda, log = FALSE) 11 | 12 | pwald(q, mu, lambda, lower.tail = TRUE, log.p = FALSE) 13 | 14 | rwald(n, mu, lambda) 15 | } 16 | \arguments{ 17 | \item{x, q}{vector of quantiles.} 18 | 19 | \item{mu, lambda}{location and shape parameters. Scale must be positive.} 20 | 21 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 22 | 23 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 24 | otherwise, \eqn{P[X > x]}.} 25 | 26 | \item{n}{number of observations. If \code{length(n) > 1}, 27 | the length is taken to be the number required.} 28 | 29 | \item{p}{vector of probabilities.} 30 | } 31 | \description{ 32 | Density, distribution function and random generation 33 | for the Wald distribution. 34 | } 35 | \details{ 36 | Probability density function 37 | \deqn{ 38 | f(x) = \sqrt{\frac{\lambda}{2\pi x^3}} \exp\left( \frac{-\lambda(x-\mu)^2}{2\mu^2 x} \right) 39 | }{ 40 | f(x) = sqrt(\lambda/(2*\pi*x^3)) * exp((-\lambda*(x-\mu)^2)/(2*\mu^2*x)) 41 | } 42 | 43 | Cumulative distribution function 44 | \deqn{ 45 | F(x) = \Phi\left(\sqrt{\frac{\lambda}{x}} \left(\frac{x}{\mu}-1 \right) \right) + 46 | \exp\left(\frac{2\lambda}{\mu} \right) \Phi\left(\sqrt{\frac{\lambda}{x}} 47 | \left(\frac{x}{\mu}+1 \right) \right) 48 | }{ 49 | F(x) = \Phi(sqrt(\lambda/\mu)*(x/\mu-1)) - exp((2*\lambda)/\mu) * 50 | \Phi(sqrt(\lambda/\mu)*(x/\mu+1)) 51 | } 52 | 53 | Random generation is done using the algorithm described by Michael, Schucany and Haas (1976). 54 | } 55 | \examples{ 56 | 57 | x <- rwald(1e5, 5, 16) 58 | hist(x, 100, freq = FALSE) 59 | curve(dwald(x, 5, 16), 0, 50, col = "red", add = TRUE) 60 | hist(pwald(x, 5, 16)) 61 | plot(ecdf(x)) 62 | curve(pwald(x, 5, 16), 0, 50, col = "red", lwd = 2, add = TRUE) 63 | 64 | } 65 | \references{ 66 | Michael, J.R., Schucany, W.R., and Haas, R.W. (1976). 67 | Generating Random Variates Using Transformations with Multiple Roots. 68 | The American Statistician, 30(2): 88-90. 69 | } 70 | \concept{Continuous} 71 | \concept{Univariate} 72 | \keyword{distribution} 73 | -------------------------------------------------------------------------------- /man/ZIB.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/zero-inflated-binomial-distribution.R 3 | \name{ZIB} 4 | \alias{ZIB} 5 | \alias{dzib} 6 | \alias{pzib} 7 | \alias{qzib} 8 | \alias{rzib} 9 | \title{Zero-inflated binomial distribution} 10 | \usage{ 11 | dzib(x, size, prob, pi, log = FALSE) 12 | 13 | pzib(q, size, prob, pi, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qzib(p, size, prob, pi, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rzib(n, size, prob, pi) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{size}{number of trials (zero or more).} 23 | 24 | \item{prob}{probability of success in each trial. \code{0 < prob <= 1}.} 25 | 26 | \item{pi}{probability of extra zeros.} 27 | 28 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 29 | 30 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 31 | otherwise, \eqn{P[X > x]}.} 32 | 33 | \item{p}{vector of probabilities.} 34 | 35 | \item{n}{number of observations. If \code{length(n) > 1}, 36 | the length is taken to be the number required.} 37 | } 38 | \description{ 39 | Probability mass function and random generation 40 | for the zero-inflated binomial distribution. 41 | } 42 | \details{ 43 | Probability density function 44 | \deqn{ 45 | f(x) = \left\{\begin{array}{ll} 46 | \pi + (1 - \pi) (1-p)^n & x = 0 \\ 47 | (1 - \pi) {n \choose x} p^x (1-p)^{n-x} & x > 0 \\ 48 | \end{array}\right. 49 | }{ 50 | f(x) = [if x = 0:] (1-\pi)+\pi * p^r [else:] (1-\pi) * dnbinom(x, size, prob) 51 | } 52 | } 53 | \examples{ 54 | 55 | x <- rzib(1e5, 10, 0.6, 0.33) 56 | xx <- -2:20 57 | plot(prop.table(table(x)), type = "h") 58 | lines(xx, dzib(xx, 10, 0.6, 0.33), col = "red") 59 | 60 | xx <- seq(0, 20, by = 0.01) 61 | plot(ecdf(x)) 62 | lines(xx, pzib(xx, 10, 0.6, 0.33), col = "red") 63 | 64 | } 65 | \seealso{ 66 | \code{\link[stats]{Binomial}} 67 | } 68 | \concept{Discrete} 69 | \concept{Univariate} 70 | \keyword{distribution} 71 | -------------------------------------------------------------------------------- /man/ZINB.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/zero-inflated-negative-binomial-distribution.R 3 | \name{ZINB} 4 | \alias{ZINB} 5 | \alias{dzinb} 6 | \alias{pzinb} 7 | \alias{qzinb} 8 | \alias{rzinb} 9 | \title{Zero-inflated negative binomial distribution} 10 | \usage{ 11 | dzinb(x, size, prob, pi, log = FALSE) 12 | 13 | pzinb(q, size, prob, pi, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qzinb(p, size, prob, pi, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rzinb(n, size, prob, pi) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{size}{target for number of successful trials, or dispersion 23 | parameter (the shape parameter of the gamma mixing 24 | distribution). Must be strictly positive, need not be 25 | integer.} 26 | 27 | \item{prob}{probability of success in each trial. \code{0 < prob <= 1}.} 28 | 29 | \item{pi}{probability of extra zeros.} 30 | 31 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 32 | 33 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 34 | otherwise, \eqn{P[X > x]}.} 35 | 36 | \item{p}{vector of probabilities.} 37 | 38 | \item{n}{number of observations. If \code{length(n) > 1}, 39 | the length is taken to be the number required.} 40 | } 41 | \description{ 42 | Probability mass function and random generation 43 | for the zero-inflated negative binomial distribution. 44 | } 45 | \details{ 46 | Probability density function 47 | \deqn{ 48 | f(x) = \left\{\begin{array}{ll} 49 | \pi + (1 - \pi) p^r & x = 0 \\ 50 | (1 - \pi) {x+r-1 \choose x} p^r (1-p)^x & x > 0 \\ 51 | \end{array}\right. 52 | }{ 53 | f(x) = [if x = 0:] (1-\pi)+\pi * p^r [else:] (1-\pi) * dnbinom(x, size, prob) 54 | } 55 | } 56 | \examples{ 57 | 58 | x <- rzinb(1e5, 100, 0.6, 0.33) 59 | xx <- -2:200 60 | plot(prop.table(table(x)), type = "h") 61 | lines(xx, dzinb(xx, 100, 0.6, 0.33), col = "red") 62 | 63 | xx <- seq(0, 200, by = 0.01) 64 | plot(ecdf(x)) 65 | lines(xx, pzinb(xx, 100, 0.6, 0.33), col = "red") 66 | 67 | } 68 | \seealso{ 69 | \code{\link[stats]{NegBinomial}} 70 | } 71 | \concept{Discrete} 72 | \concept{Univariate} 73 | \keyword{distribution} 74 | -------------------------------------------------------------------------------- /man/ZIP.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/zero-inflated-poisson-distribution.R 3 | \name{ZIP} 4 | \alias{ZIP} 5 | \alias{dzip} 6 | \alias{pzip} 7 | \alias{qzip} 8 | \alias{rzip} 9 | \title{Zero-inflated Poisson distribution} 10 | \usage{ 11 | dzip(x, lambda, pi, log = FALSE) 12 | 13 | pzip(q, lambda, pi, lower.tail = TRUE, log.p = FALSE) 14 | 15 | qzip(p, lambda, pi, lower.tail = TRUE, log.p = FALSE) 16 | 17 | rzip(n, lambda, pi) 18 | } 19 | \arguments{ 20 | \item{x, q}{vector of quantiles.} 21 | 22 | \item{lambda}{vector of (non-negative) means.} 23 | 24 | \item{pi}{probability of extra zeros.} 25 | 26 | \item{log, log.p}{logical; if TRUE, probabilities p are given as log(p).} 27 | 28 | \item{lower.tail}{logical; if TRUE (default), probabilities are \eqn{P[X \le x]} 29 | otherwise, \eqn{P[X > x]}.} 30 | 31 | \item{p}{vector of probabilities.} 32 | 33 | \item{n}{number of observations. If \code{length(n) > 1}, 34 | the length is taken to be the number required.} 35 | } 36 | \description{ 37 | Probability mass function and random generation 38 | for the zero-inflated Poisson distribution. 39 | } 40 | \details{ 41 | Probability density function 42 | \deqn{ 43 | f(x) = \left\{\begin{array}{ll} 44 | \pi + (1 - \pi) e^{-\lambda} & x = 0 \\ 45 | (1 - \pi) \frac{\lambda^{x} e^{-\lambda}} {x!} & x > 0 \\ 46 | \end{array}\right. 47 | }{ 48 | f(x) = [if x = 0:] \pi + (1-\pi) * exp(-\lambda) [else:] (1-\pi) * dpois(x, lambda) 49 | } 50 | } 51 | \examples{ 52 | 53 | x <- rzip(1e5, 6, 0.33) 54 | xx <- -2:20 55 | plot(prop.table(table(x)), type = "h") 56 | lines(xx, dzip(xx, 6, 0.33), col = "red") 57 | 58 | xx <- seq(0, 20, by = 0.01) 59 | plot(ecdf(x)) 60 | lines(xx, pzip(xx, 6, 0.33), col = "red") 61 | 62 | } 63 | \seealso{ 64 | \code{\link[stats]{Poisson}} 65 | } 66 | \concept{Discrete} 67 | \concept{Univariate} 68 | \keyword{distribution} 69 | -------------------------------------------------------------------------------- /man/extraDistr-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extraDistr-package.R 3 | \docType{package} 4 | \name{extraDistr-package} 5 | \alias{extraDistr} 6 | \alias{extraDistr-package} 7 | \title{extraDistr: Additional Univariate and Multivariate Distributions} 8 | \description{ 9 | Density, distribution function, quantile function and random generation for a number of univariate and multivariate distributions. This package implements the following distributions: Bernoulli, beta-binomial, beta-negative binomial, beta prime, Bhattacharjee, Birnbaum-Saunders, bivariate normal, bivariate Poisson, categorical, Dirichlet, Dirichlet-multinomial, discrete gamma, discrete Laplace, discrete normal, discrete uniform, discrete Weibull, Frechet, gamma-Poisson, generalized extreme value, Gompertz, generalized Pareto, Gumbel, half-Cauchy, half-normal, half-t, Huber density, inverse chi-squared, inverse-gamma, Kumaraswamy, Laplace, location-scale t, logarithmic, Lomax, multivariate hypergeometric, multinomial, negative hypergeometric, non-standard beta, normal mixture, Poisson mixture, Pareto, power, reparametrized beta, Rayleigh, shifted Gompertz, Skellam, slash, triangular, truncated binomial, truncated normal, truncated Poisson, Tukey lambda, Wald, zero-inflated binomial, zero-inflated negative binomial, zero-inflated Poisson. 10 | 11 | Density, distribution function, quantile function and random 12 | generation for a number of univariate and multivariate distributions. 13 | } 14 | \details{ 15 | This package follows naming convention that is consistent with base R, 16 | where density (or probability mass) functions, distribution functions, 17 | quantile functions and random generation functions names are followed by 18 | \code{d}*, \code{p}*, \code{q}*, and \code{r}* prefixes. 19 | 20 | Behaviour of the functions is consistent with base R, where for 21 | not valid parameters values \code{NaN}'s are returned, while 22 | for values beyond function support \code{0}'s are returned 23 | (e.g. for non-integers in discrete distributions, or for 24 | negative values in functions with non-negative support). 25 | 26 | All the functions vectorized and coded in C++ using \pkg{Rcpp}. 27 | } 28 | \seealso{ 29 | Useful links: 30 | \itemize{ 31 | \item \url{https://github.com/twolodzko/extraDistr} 32 | \item Report bugs at \url{https://github.com/twolodzko/extraDistr/issues} 33 | } 34 | 35 | } 36 | \keyword{internal} 37 | -------------------------------------------------------------------------------- /src/categorical-distribution-lp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "shared.h" 3 | // [[Rcpp::interfaces(r, cpp)]] 4 | // [[Rcpp::plugins(cpp11)]] 5 | 6 | using std::log; 7 | using Rcpp::NumericVector; 8 | using Rcpp::NumericMatrix; 9 | 10 | 11 | /* 12 | * 13 | * 14 | * Random generation from categorical distribution using Gumbel-max trick 15 | * 16 | * Random generation from categorical distribution given log-probabilities. 17 | * 18 | * Given vector of log-probabilities gamma[1], ..., gamma[k], where each 19 | * gamma[i] = log(prob[i]) + constant, then it is possible to taking 20 | * 21 | * x = argmax[i]( gamma[i] + g[i] ) 22 | * 23 | * where g[1], ..., g[k] is a sample from standard Gumbel distribution. 24 | * 25 | * 26 | * References: 27 | * 28 | * Maddison, C. J., Tarlow, D., & Minka, T. (2014). A* sampling. 29 | * [In:] Advances in Neural Information Processing Systems (pp. 3086-3094). 30 | * 31 | * 32 | */ 33 | 34 | 35 | // [[Rcpp::export]] 36 | NumericVector cpp_rcatlp( 37 | const int& n, 38 | const NumericMatrix& log_prob 39 | ) { 40 | 41 | if (log_prob.length() < 1) { 42 | Rcpp::warning("NAs produced"); 43 | return NumericVector(n, NA_REAL); 44 | } 45 | 46 | NumericVector x(n); 47 | int k = log_prob.ncol(); 48 | double u, glp, max_val; 49 | int jj; 50 | 51 | bool throw_warning = false; 52 | bool wrong_prob = false; 53 | 54 | for (int i = 0; i < n; i++) { 55 | 56 | max_val = -INFINITY; 57 | jj = 0; 58 | 59 | for (int j = 0; j < k; j++) { 60 | 61 | wrong_prob = false; 62 | 63 | if (ISNAN(GETM(log_prob, i, j))) { 64 | throw_warning = wrong_prob = true; 65 | break; 66 | } 67 | 68 | u = R::exp_rand(); // -log(rng_unif()) 69 | glp = -log(u) + GETM(log_prob, i, j); 70 | if (glp > max_val) { 71 | max_val = glp; 72 | jj = j; 73 | } 74 | 75 | } 76 | 77 | if (wrong_prob) { 78 | x[i] = NA_REAL; 79 | } else { 80 | x[i] = static_cast(jj); 81 | } 82 | } 83 | 84 | if (throw_warning) 85 | Rcpp::warning("NAs produced"); 86 | 87 | return x; 88 | } 89 | 90 | -------------------------------------------------------------------------------- /src/discrete-gamma-distribution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "shared.h" 3 | // [[Rcpp::interfaces(r, cpp)]] 4 | // [[Rcpp::plugins(cpp11)]] 5 | 6 | using std::pow; 7 | using std::sqrt; 8 | using std::abs; 9 | using std::exp; 10 | using std::log; 11 | using std::floor; 12 | using std::ceil; 13 | using Rcpp::NumericVector; 14 | 15 | 16 | /* 17 | * Discrete normal distribution 18 | * 19 | * Values: 20 | * x 21 | * 22 | * Parameters 23 | * mu 24 | * sigma > 0 25 | * 26 | */ 27 | 28 | 29 | inline double pmf_dgamma(double x, double shape, double scale, 30 | bool& throw_warning) { 31 | #ifdef IEEE_754 32 | if (ISNAN(x) || ISNAN(shape) || ISNAN(scale)) 33 | return x+shape+scale; 34 | #endif 35 | if (shape <= 0.0 || scale <= 0) { 36 | throw_warning = true; 37 | return NAN; 38 | } 39 | if (x < 0.0 || !isInteger(x)) 40 | return 0.0; 41 | return R::pgamma(x+1.0, shape, scale, true, false) - 42 | R::pgamma(x, shape, scale, true, false); 43 | } 44 | 45 | 46 | // [[Rcpp::export]] 47 | NumericVector cpp_ddgamma( 48 | const NumericVector& x, 49 | const NumericVector& shape, 50 | const NumericVector& scale, 51 | const bool& log_prob = false 52 | ) { 53 | 54 | if (std::min({x.length(), shape.length(), scale.length()}) < 1) { 55 | return NumericVector(0); 56 | } 57 | 58 | int Nmax = std::max({ 59 | x.length(), 60 | shape.length(), 61 | scale.length() 62 | }); 63 | NumericVector p(Nmax); 64 | 65 | bool throw_warning = false; 66 | 67 | for (int i = 0; i < Nmax; i++) 68 | p[i] = pmf_dgamma(GETV(x, i), GETV(shape, i), 69 | GETV(scale, i), throw_warning); 70 | 71 | if (log_prob) 72 | p = Rcpp::log(p); 73 | 74 | if (throw_warning) 75 | Rcpp::warning("NaNs produced"); 76 | 77 | return p; 78 | } 79 | 80 | -------------------------------------------------------------------------------- /src/discrete-normal-distribution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "shared.h" 3 | // [[Rcpp::interfaces(r, cpp)]] 4 | // [[Rcpp::plugins(cpp11)]] 5 | 6 | using std::pow; 7 | using std::sqrt; 8 | using std::abs; 9 | using std::exp; 10 | using std::log; 11 | using std::floor; 12 | using std::ceil; 13 | using Rcpp::NumericVector; 14 | 15 | 16 | /* 17 | * Discrete normal distribution 18 | * 19 | * Values: 20 | * x 21 | * 22 | * Parameters 23 | * mu 24 | * sigma > 0 25 | * 26 | */ 27 | 28 | 29 | inline double pmf_dnorm(double x, double mu, double sigma, 30 | bool& throw_warning) { 31 | #ifdef IEEE_754 32 | if (ISNAN(x) || ISNAN(mu) || ISNAN(sigma)) 33 | return x+mu+sigma; 34 | #endif 35 | if (sigma <= 0.0) { 36 | throw_warning = true; 37 | return NAN; 38 | } 39 | if (!isInteger(x)) 40 | return 0.0; 41 | return R::pnorm(x+1.0, mu, sigma, true, false) - 42 | R::pnorm(x, mu, sigma, true, false); 43 | } 44 | 45 | 46 | // [[Rcpp::export]] 47 | NumericVector cpp_ddnorm( 48 | const NumericVector& x, 49 | const NumericVector& mu, 50 | const NumericVector& sigma, 51 | const bool& log_prob = false 52 | ) { 53 | 54 | if (std::min({x.length(), mu.length(), sigma.length()}) < 1) { 55 | return NumericVector(0); 56 | } 57 | 58 | int Nmax = std::max({ 59 | x.length(), 60 | mu.length(), 61 | sigma.length() 62 | }); 63 | NumericVector p(Nmax); 64 | 65 | bool throw_warning = false; 66 | 67 | for (int i = 0; i < Nmax; i++) 68 | p[i] = pmf_dnorm(GETV(x, i), GETV(mu, i), 69 | GETV(sigma, i), throw_warning); 70 | 71 | if (log_prob) 72 | p = Rcpp::log(p); 73 | 74 | if (throw_warning) 75 | Rcpp::warning("NaNs produced"); 76 | 77 | return p; 78 | } 79 | 80 | -------------------------------------------------------------------------------- /src/rademacher-distribution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "shared.h" 3 | // [[Rcpp::interfaces(r, cpp)]] 4 | // [[Rcpp::plugins(cpp11)]] 5 | 6 | using Rcpp::NumericVector; 7 | 8 | 9 | // [[Rcpp::export]] 10 | NumericVector cpp_rsign( 11 | const int& n 12 | ) { 13 | 14 | NumericVector x(n); 15 | 16 | for (int i = 0; i < n; i++) 17 | x[i] = rng_sign(); 18 | 19 | return x; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /src/shared.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "shared.h" 3 | 4 | bool isInteger(double x, bool warn) { 5 | if (ISNAN(x)) 6 | return false; 7 | if (((x < 0.0) ? std::ceil(x) : std::floor(x)) != x) { 8 | if (warn) { 9 | char msg[55]; 10 | std::snprintf(msg, sizeof(msg), "non-integer: %f", x); 11 | Rcpp::warning(msg); 12 | } 13 | return false; 14 | } 15 | return true; 16 | } 17 | 18 | double finite_max_int(const Rcpp::NumericVector& x) { 19 | double max_x = 0.0; 20 | int n = x.length(); 21 | int i = 0; 22 | do { 23 | if (x[i] > 0.0 && !is_large_int(x[i])) { 24 | max_x = x[i]; 25 | break; 26 | } 27 | i++; 28 | } while (i < n); 29 | while (i < n) { 30 | if (x[i] > max_x && !is_large_int(x[i])) { 31 | max_x = x[i]; 32 | } 33 | i++; 34 | } 35 | return max_x; 36 | } 37 | 38 | double rng_unif() { 39 | double u; 40 | // same as in base R 41 | do { 42 | u = R::unif_rand(); 43 | } while (u <= 0.0 || u >= 1.0); 44 | return u; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /src/shared.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef EDCPP_SHARED_H 3 | #define EDCPP_SHARED_H 4 | 5 | #define STRICT_R_HEADERS 6 | #include 7 | 8 | // Constants 9 | 10 | static const double SQRT_2_PI = 2.506628274631000241612; // sqrt(2*pi) 11 | static const double PHI_0 = 0.3989422804014327028632; // dnorm(0) 12 | static const double LOG_2F = 0.6931471805599452862268; // log(2) 13 | 14 | static const double MIN_DIFF_EPS = 1e-8; 15 | 16 | // MACROS 17 | 18 | #define GETV(x, i) x[i % x.length()] // wrapped indexing of vector 19 | #define GETM(x, i, j) x(i % x.nrow(), j) // wrapped indexing of matrix 20 | #define VALID_PROB(p) ((p >= 0.0) && (p <= 1.0)) 21 | 22 | // functions 23 | 24 | bool isInteger(double x, bool warn = true); 25 | double finite_max_int(const Rcpp::NumericVector& x); 26 | double rng_unif(); // standard uniform 27 | 28 | // inline functions 29 | 30 | inline bool tol_equal(double x, double y); 31 | inline double phi(double x); 32 | inline double lphi(double x); 33 | inline double Phi(double x); 34 | inline double InvPhi(double x); 35 | inline double factorial(double x); 36 | inline double lfactorial(double x); 37 | inline double rng_sign(); 38 | inline bool is_large_int(double x); 39 | inline double to_dbl(int x); 40 | inline int to_pos_int(double x); 41 | inline double trunc_p(double x); 42 | 43 | #include "shared_inline.h" 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/shared_inline.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef EDCPP_INLINEFUNS_H 3 | #define EDCPP_INLINEFUNS_H 4 | 5 | #include "shared.h" 6 | #include 7 | 8 | 9 | inline bool tol_equal(double x, double y) { 10 | return std::abs(x - y) < MIN_DIFF_EPS; 11 | } 12 | 13 | inline double phi(double x) { 14 | return R::dnorm(x, 0.0, 1.0, false); 15 | } 16 | 17 | inline double lphi(double x) { 18 | return R::dnorm(x, 0.0, 1.0, true); 19 | } 20 | 21 | inline double Phi(double x) { 22 | return R::pnorm(x, 0.0, 1.0, true, false); 23 | } 24 | 25 | inline double InvPhi(double x) { 26 | return R::qnorm(x, 0.0, 1.0, true, false); 27 | } 28 | 29 | inline double factorial(double x) { 30 | return R::gammafn(x + 1.0); 31 | } 32 | 33 | inline double lfactorial(double x) { 34 | return R::lgammafn(x + 1.0); 35 | } 36 | 37 | inline double rng_sign() { 38 | double u = rng_unif(); 39 | return (u > 0.5) ? 1.0 : -1.0; 40 | } 41 | 42 | inline bool is_large_int(double x) { 43 | if (x > std::numeric_limits::max()) 44 | return true; 45 | return false; 46 | } 47 | 48 | inline double to_dbl(int x) { 49 | return static_cast(x); 50 | } 51 | 52 | inline int to_pos_int(double x) { 53 | if (x < 0.0 || ISNAN(x)) 54 | Rcpp::stop("value cannot be coerced to integer"); 55 | if (is_large_int(x)) 56 | Rcpp::stop("value out of integer range"); 57 | return static_cast(x); 58 | } 59 | 60 | inline double trunc_p(double x) { 61 | return x < 0.0 ? 0.0 : (x > 1.0 ? 1.0 : x); 62 | } 63 | 64 | 65 | #endif 66 | 67 | -------------------------------------------------------------------------------- /src/skellam-distribution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "shared.h" 3 | // [[Rcpp::interfaces(r, cpp)]] 4 | // [[Rcpp::plugins(cpp11)]] 5 | 6 | using std::pow; 7 | using std::sqrt; 8 | using std::abs; 9 | using std::exp; 10 | using std::log; 11 | using std::floor; 12 | using std::ceil; 13 | using Rcpp::NumericVector; 14 | 15 | 16 | /* 17 | * Skellam distribution 18 | * 19 | * mu1 >= 0 20 | * mu2 >= 0 21 | * 22 | */ 23 | 24 | inline double pmf_skellam(double x, double mu1, double mu2, 25 | bool& throw_warning) { 26 | #ifdef IEEE_754 27 | if (ISNAN(x) || ISNAN(mu1) || ISNAN(mu2)) 28 | return x+mu1+mu2; 29 | #endif 30 | if (mu1 < 0.0 || mu2 < 0.0) { 31 | throw_warning = true; 32 | return NAN; 33 | } 34 | if (!isInteger(x) || !R_FINITE(x)) 35 | return 0.0; 36 | return exp(-(mu1+mu2)) * pow(mu1/mu2, x/2.0) * 37 | R::bessel_i(2.0*sqrt(mu1*mu2), x, 1.0); 38 | } 39 | 40 | inline double rng_skellam(double mu1, double mu2, 41 | bool& throw_warning) { 42 | if (ISNAN(mu1) || ISNAN(mu2) || mu1 < 0.0 || mu2 < 0.0) { 43 | throw_warning = true; 44 | return NA_REAL; 45 | } 46 | return R::rpois(mu1) - R::rpois(mu2); 47 | } 48 | 49 | 50 | 51 | // [[Rcpp::export]] 52 | NumericVector cpp_dskellam( 53 | const NumericVector& x, 54 | const NumericVector& mu1, 55 | const NumericVector& mu2, 56 | const bool& log_prob = false 57 | ) { 58 | 59 | if (std::min({x.length(), mu1.length(), mu2.length()}) < 1) { 60 | return NumericVector(0); 61 | } 62 | 63 | int Nmax = std::max({ 64 | x.length(), 65 | mu1.length(), 66 | mu2.length() 67 | }); 68 | NumericVector p(Nmax); 69 | 70 | bool throw_warning = false; 71 | 72 | for (int i = 0; i < Nmax; i++) 73 | p[i] = pmf_skellam(GETV(x, i), GETV(mu1, i), 74 | GETV(mu2, i), throw_warning); 75 | 76 | if (log_prob) 77 | p = Rcpp::log(p); 78 | 79 | if (throw_warning) 80 | Rcpp::warning("NaNs produced"); 81 | 82 | return p; 83 | } 84 | 85 | 86 | // [[Rcpp::export]] 87 | NumericVector cpp_rskellam( 88 | const int& n, 89 | const NumericVector& mu1, 90 | const NumericVector& mu2 91 | ) { 92 | 93 | if (std::min({mu1.length(), mu2.length()}) < 1) { 94 | Rcpp::warning("NAs produced"); 95 | return NumericVector(n, NA_REAL); 96 | } 97 | 98 | NumericVector x(n); 99 | 100 | bool throw_warning = false; 101 | 102 | for (int i = 0; i < n; i++) 103 | x[i] = rng_skellam(GETV(mu1, i), GETV(mu2, i), 104 | throw_warning); 105 | 106 | if (throw_warning) 107 | Rcpp::warning("NAs produced"); 108 | 109 | return x; 110 | } 111 | 112 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(extraDistr) 3 | 4 | # use fixed random seed for all tests for reproducibility 5 | set.seed(42) 6 | 7 | test_check("extraDistr") 8 | -------------------------------------------------------------------------------- /tests/testthat/test-misc.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | test_that("other tests", { 4 | 5 | expect_true(all(rsign(1e5) %in% c(-1, 1))) 6 | 7 | expect_silent(qcat(seq(0, 1, by = 0.1), c(1, 1, 1), labels = letters[1:3])) 8 | expect_warning(qcat(seq(0, 1, by = 0.1), c(1, 1, 1), labels = letters[1:2])) 9 | 10 | expect_silent(rcat(10, c(1, 1, 1), labels = letters[1:3])) 11 | expect_warning(rcat(10, c(1, 1, 1), labels = letters[1:2])) 12 | 13 | expect_silent(rcatlp(10, log(c(1, 1, 1)/3), labels = letters[1:3])) 14 | expect_warning(rcatlp(10, log(c(1, 1, 1)/3), labels = letters[1:2])) 15 | 16 | expect_error(dbvpois(1:10, a = 1, b = 1, c= 1)) 17 | 18 | expect_silent(!anyNA(rtlambda(5000, -1))) 19 | expect_silent(!anyNA(rtlambda(5000, 0))) 20 | expect_silent(!anyNA(rtlambda(5000, 1))) 21 | 22 | expect_silent(dbvnorm(mtcars[, 1:2])) 23 | expect_error(dbvnorm(mtcars)) 24 | 25 | xx <- seq(-6, 6, by = 0.01) 26 | expect_identical(dnorm(xx), dtnorm(xx)) 27 | expect_identical(pnorm(xx), ptnorm(xx)) 28 | 29 | pp <- seq(0, 1, by = 0.01) 30 | expect_identical(qnorm(pp), qtnorm(pp)) 31 | 32 | x <- c(-Inf, -100, -10, -5, -1, -0.5, 0, 0.5, 1, 5, 10, 100, Inf) 33 | 34 | expect_equal(pmixnorm(x, c(1,2,3), c(1,2,3), c(1/3,1/3,1/3), lower.tail = TRUE), 35 | 1 - pmixnorm(x, c(1,2,3), c(1,2,3), c(1/3,1/3,1/3), lower.tail = FALSE)) 36 | expect_equal(suppressWarnings(pmixpois(x, c(1,2,3), c(1/3,1/3,1/3), lower.tail = TRUE)), 37 | 1 - suppressWarnings(pmixpois(x, c(1,2,3), c(1/3,1/3,1/3), lower.tail = FALSE))) 38 | 39 | }) 40 | -------------------------------------------------------------------------------- /tests/testthat/test-multivariate-distributions.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | # extraDistr::ddirichlet was compared to MCMCpack::ddirichlet 4 | 5 | test_that("Testing multivariate distributions", { 6 | 7 | skip_on_cran() 8 | 9 | expect_true(all.equal(rowSums(rmnom(5000, 50, c(2/10, 5/10, 3/10))), rep(50, 5000))) 10 | expect_true(all.equal(rowSums(rdirichlet(5000, c(2/10, 5/10, 3/10))), rep(1.0, 5000))) 11 | 12 | xx <- expand.grid(0:20, 0:20, 0:20) 13 | expect_equal(sum(ddirmnom(xx[rowSums(xx) == 20,], 20, c(2, 5, 3))), 1) 14 | expect_equal(sum(dmnom(xx[rowSums(xx) == 20,], 20, c(2/10, 5/10, 3/10))), 1) 15 | expect_equal(sum(dmvhyper(xx[rowSums(xx) == 35,], c(20, 20, 20), 35)), 1) 16 | 17 | p <- c(4, 5, 1, 6, 2) 18 | 19 | expect_equal(prop.table(colSums(rmnom(1e5, 100, p/sum(p)))), 20 | p/sum(p), 21 | tolerance = 1e-2) 22 | 23 | expect_equal(as.numeric(prop.table(table(rcat(1e5, p/sum(p))))), 24 | p/sum(p), 25 | tolerance = 1e-2) 26 | 27 | expect_equal(prop.table(colSums(rdirichlet(1e5, p))), 28 | p/sum(p), 29 | tolerance = 1e-2) 30 | 31 | expect_equal(prop.table(colSums(rdirmnom(1e5, 100, p))), 32 | p/sum(p), 33 | tolerance = 1e-2) 34 | 35 | n <- c(11, 24, 43, 7, 56) 36 | 37 | expect_equal(prop.table(colSums(rmvhyper(1e5, n, 100))), 38 | n/sum(n), 39 | tolerance = 1e-2) 40 | 41 | }) 42 | 43 | 44 | test_that("Evaluate wrong parameters first", { 45 | 46 | expect_warning(expect_true(is.nan(dbvpois(-1, -1, -1, 1, 1)))) 47 | expect_warning(expect_true(is.nan(ddirichlet(c(2, 2), c(-1, 0.5))))) 48 | expect_warning(expect_true(is.nan(ddirmnom(c(-1, 1, 1), 1.5, c(1, 1, 1))))) 49 | expect_warning(expect_true(is.nan(dmnom(c(-1, 1, 1), 1.5, c(1/3, 1/3, 1/3))))) 50 | expect_warning(expect_true(is.nan(dmvhyper(c(-1, 2, 2), c(2,3,4), -5)))) 51 | 52 | }) 53 | 54 | 55 | test_that("Check if rmnom and rdirmnom deal with underflow (#3, #7)", { 56 | 57 | skip_on_cran() 58 | 59 | expect_false(anyNA(rmnom(5000, 100, c(0.504115095275327, 2.669522645838e-39, 0, 2.58539638831141, 0)))) 60 | expect_false(anyNA(rdirmnom(5000, 100, c(1.480592e+00, 1.394943e-03, 4.529932e-06, 3.263573e+00, 4.554952e-06)))) 61 | 62 | p <- c(0, 0, 1, 0, 2.77053929981958e-18) 63 | 64 | expect_false(anyNA(rmnom(5000, 100, p))) 65 | expect_false(anyNA(rdirmnom(5000, 100, p + 1e-5))) 66 | 67 | }) 68 | -------------------------------------------------------------------------------- /tests/testthat/test-non-negative.R: -------------------------------------------------------------------------------- 1 | test_that("Zero probabilities for values <0", { 2 | expect_equal(c(0, 0), dbetapr(c(-1, 0), 1, 1, 1)) 3 | expect_equal(0, ddweibull(-1, 0.5, 1)) 4 | expect_equal(0, ddunif(-1, 0, 5)) 5 | expect_equal(0, dcat(-1, c(0.5, 0.5))) 6 | expect_equal(0, dmnom(c(-1, 1), 5, c(0.5, 0.5))) 7 | expect_warning(expect_equal(0, dbern(-1))) 8 | expect_equal(0, dbbinom(-1, 1, 1, 1)) 9 | expect_equal(0, dbnbinom(-1, 1, 1, 1)) 10 | expect_equal(0, dgpois(-1, 1, 1)) 11 | expect_equal(0, dmvhyper(c(1, 1, -1), c(2, 2, 2), 3)) 12 | expect_equal(0, dzip(-1, 1, 0.5)) 13 | expect_equal(0, dzib(-1, 1, 0.5, 0.5)) 14 | expect_equal(0, dzinb(-1, 1, 0.5, 0.5)) 15 | 16 | expect_equal(0, dbvpois(-1, 1, 1, 1, 1)) 17 | expect_equal(0, dbvpois(1, -1, 1, 1, 1)) 18 | 19 | expect_equal(0, dinvchisq(-1, 1)) 20 | expect_equal(0, dinvchisq(-1, 1, 1)) 21 | expect_equal(0, dinvgamma(-1, 1, 1)) 22 | expect_equal(0, dgompertz(-1, 1, 1)) 23 | expect_equal(0, dgpois(-1, 1, 1)) 24 | expect_equal(0, dlomax(-1, 1, 1)) 25 | expect_equal(0, dpower(-1, 1, 0.5)) 26 | expect_equal(0, dsgomp(-1, 0.4, 1)) 27 | expect_equal(0, drayleigh(-1, 1)) 28 | expect_equal(0, dwald(-1, 1, 1)) 29 | 30 | expect_equal(0, dhcauchy(-1, 1)) 31 | expect_equal(0, dhnorm(-1, 1)) 32 | expect_equal(0, dht(-1, 5, 1)) 33 | 34 | expect_equal(0, dmixpois(-1, c(1, 2, 3), c(1 / 3, 1 / 3, 1 / 3))) 35 | expect_equal(0, dtpois(-1, lambda = 5, a = 6)) 36 | 37 | expect_equal(0, dnhyper(-1, 60, 35, 15)) 38 | 39 | expect_equal(0, ddgamma(-1, 9, 1)) 40 | }) 41 | 42 | test_that("Zero probabilities for values x < mean", { 43 | expect_equal(0, dfatigue(-1, 1, 1, 0)) 44 | expect_equal(0, dfrechet(-1, 1, 0, 1)) 45 | expect_equal(0, dgpd(-1, 0, 1, 1)) 46 | }) 47 | 48 | test_that("Zero probabilities for values < 1", { 49 | expect_equal(c(0, 0), dlgser(c(-1, 0), 0.5)) 50 | expect_equal(c(0, 0), dpareto(c(-1, 0), 1, 1)) 51 | expect_equal(c(0, 0), dtpois(c(-1, 0), lambda = 5, a = 0)) 52 | }) 53 | 54 | test_that("No negative samples for distributions with positive support", { 55 | set.seed(42) 56 | expect_true(all(rgpd(1000, xi = 0) > 0)) 57 | expect_true(all(rgpd(1000, xi = 0.1) > 0)) 58 | expect_true(all(rgpd(1000, xi = -0.1) > 0)) 59 | }) 60 | -------------------------------------------------------------------------------- /tests/testthat/test-rtnorm-in-bounds.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | all_in_bounds <- function(object, lower, upper) { 4 | all(object >= lower & object <= upper) 5 | } 6 | 7 | 8 | test_that("Check if values generated using rtnorm stay in bounds", { 9 | 10 | N <- 5000 11 | mu <- 0 12 | sigma <- 1 13 | 14 | a <- -1 15 | b <- 1 16 | X <- rtnorm(N, mu, sigma, a, b) 17 | 18 | expect_true(all_in_bounds(X, a, b)) 19 | 20 | a <- 0 21 | b <- 1 22 | X <- rtnorm(N, mu, sigma, a, b) 23 | 24 | expect_true(all_in_bounds(X, a, b)) 25 | 26 | a <- -1 27 | b <- 0 28 | X <- rtnorm(N, mu, sigma, a, b) 29 | 30 | expect_true(all_in_bounds(X, a, b)) 31 | 32 | a <- 4 33 | b <- 5 34 | X <- rtnorm(N, mu, sigma, a, b) 35 | 36 | expect_true(all_in_bounds(X, a, b)) 37 | 38 | a <- -5 39 | b <- -4 40 | X <- rtnorm(N, mu, sigma, a, b) 41 | 42 | expect_true(all_in_bounds(X, a, b)) 43 | 44 | a <- -Inf 45 | b <- 5 46 | X <- rtnorm(N, mu, sigma, a, b) 47 | 48 | expect_true(all_in_bounds(X, a, b)) 49 | 50 | a <- -5 51 | b <- Inf 52 | X <- rtnorm(N, mu, sigma, a, b) 53 | 54 | expect_true(all_in_bounds(X, a, b)) 55 | 56 | a <- -Inf 57 | b <- -4 58 | X <- rtnorm(N, mu, sigma, a, b) 59 | 60 | expect_true(all_in_bounds(X, a, b)) 61 | 62 | a <- 4 63 | b <- Inf 64 | X <- rtnorm(N, mu, sigma, a, b) 65 | 66 | expect_true(all_in_bounds(X, a, b)) 67 | 68 | }) 69 | 70 | -------------------------------------------------------------------------------- /tests/testthat/test_vectorization.R: -------------------------------------------------------------------------------- 1 | 2 | test_that("Fix for #16 works", { 3 | 4 | test_dat <- data.frame( 5 | q = c(1, 10, 10, 100), 6 | size = c(10, 100, 1000, 1000) 7 | ) 8 | 9 | # This failed in #16 10 | testthat::expect_visible(with( 11 | test_dat, 12 | pbbinom(q, size, 1, 1) 13 | )) 14 | 15 | testthat::expect_visible(with( 16 | test_dat, 17 | pbnbinom(q, size, 1, 1) 18 | )) 19 | 20 | }) 21 | --------------------------------------------------------------------------------