Chapter 22 Probability distribution

Rcpp provides all major probability distribution functions in R. Same as R, four functions starting with the character d/p/q/r are defined for each probability distribution.

d/p/q/r functions on probability distribution XXX

  • dXXX: Probability density function
  • pXXX: Cumulative distribution function
  • qXXX: Quantile function
  • rXXX: Random number generation function

22.1 Basic structure of probability distribution function

In Rcpp, probability distribution functions with the same name are defined in two namespaces, R:: and Rcpp::. These differences are that the function defined in Rcpp:: namespace returns a vector, while the function in the R:: namespace returns a scalar. Basically, the probability distribution functions defined in the Rcpp:: namespace has the same functionalities as those in R. So normally you can use a function in the Rcpp:: namespace, but if you want a scalar value, it is better to use that function in R:: namespace because it’s faster.

The basic structures of the probability distribution functions defined in the Rcpp:: namespace are shown below. In fact, the definition of the probability distribution function of the Rcpp :: namespace is not written directly in the source code of Rcpp (because it is written using macros). But you can assume that the function is defined like as below.

NumericVector Rcpp::dXXX( NumericVector x, double par,                    bool log = false )
NumericVector Rcpp::pXXX( NumericVector q, double par, bool lower = true, bool log = false )
NumericVector Rcpp::qXXX( NumericVector p, double par, bool lower = true, bool log = false )
NumericVector Rcpp::rXXX(           int n, double par )

The basic structures of the probability distribution functions defined in the R:: namespace are shown below. It basically has the same functionality as those defined in the Rcpp:: namespace except that it accepts and returns double type. In addition, the arguments of the function do not have default values, so user must give the value explicitly.

double R::dXXX( double x, double par,            int log )
double R::pXXX( double q, double par, int lower, int log )
double R::qXXX( double p, double par, int lower, int log )
double R::rXXX(           double par )

The arguments of the probability distribution function are described below.

  • x, q : random variable
  • p : probability
  • n : number of observation
  • par : Parameter(the number of distribution parameters varies depending on the distribution)
  • lower : true : Calculate the probability of the region where the random variable is less than or equal to x, false : Calculate the probability of the region larger than x
  • log : true : probabilities p are given as log(p)

22.3 Continuous probability distribution

22.3.1 Uniform distribution

These functions provide information about the uniform distribution on the interval from min to max.

Rcpp::dunif( x, min = 0.0, max = 1.0, log = false )
Rcpp::punif( q, min = 0.0, max = 1.0, lower = true, log = false )
Rcpp::qunif( p, min = 0.0, max = 1.0, lower = true, log = false )
Rcpp::runif( n, min = 0.0, max = 1.0 )

R::dunif( x, min, max,        log )
R::punif( q, min, max, lower, log )
R::qunif( p, min, max, lower, log )
R::runif(     min, max )

22.3.2 Normal distribution

These functions provide information about the normal distribution with mean equal to mean and standard deviation equal to sd.

Rcpp::dnorm( x, mean = 0.0, sd = 1.0, log = false )
Rcpp::pnorm( q, mean = 0.0, sd = 1.0, lower = true, log = false )
Rcpp::qnorm( p, mean = 0.0, sd = 1.0, lower = true, log = false )
Rcpp::rnorm( n, mean = 0.0, sd = 1.0 )

R::dnorm( x, mean, sd,        log )
R::pnorm( q, mean, sd, lower, log )
R::qnorm( p, mean, sd, lower, log )
R::rnorm(    mean, sd )

22.3.3 Log-normal distribution

These functions provide information about the log-normal distribution whose logarithm has mean equal to meanlog and standard deviation equal to sdlog.

Rcpp::dlnorm( x, meanlog = 0.0, sdlog = 1.0,               log = false )
Rcpp::plnorm( q, meanlog = 0.0, sdlog = 1.0, lower = true, log = false )
Rcpp::qlnorm( p, meanlog = 0.0, sdlog = 1.0, lower = true, log = false )
Rcpp::rlnorm( n, meanlog = 0.0, sdlog = 1.0 )

R::dlnorm( x, meanlog, sdlog,        log )
R::plnorm( q, meanlog, sdlog, lower, log )
R::qlnorm( p, meanlog, sdlog, lower, log )
R::rlnorm(    meanlog, sdlog )

22.3.4 Gamma distribution

These functions provide information about the Gamma distribution with parameters shape and scale.

Rcpp::dgamma( x, shape, scale = 1.0,               log = false )
Rcpp::pgamma( q, shape, scale = 1.0, lower = true, log = false )
Rcpp::qgamma( p, shape, scale = 1.0, lower = true, log = false )
Rcpp::rgamma( n, shape, scale = 1.0 )

R::dgamma( x, shape, scale, log )
R::pgamma( x, shape, scale, lower, log )
R::qgamma( q, shape, scale, lower, log )
R::rgamma(    shape, scale )

22.3.5 Beta distribution

These functions provide information about the Beta distribution with parameters shape1 and shape2. These functions are equivalent to setting 0 for the noncentrality parameter ncp in the Beta distribution function in R.

Rcpp::dbeta( x, shape1, shape2, log = false )
Rcpp::pbeta( x, shape1, shape2, lower = true, log = false )
Rcpp::qbeta( q, shape1, shape2, lower = true, log = false )
Rcpp::rbeta( n, shape1, shape2)

R::dbeta( x, shape1, shape2,        log )
R::pbeta( x, shape1, shape2, lower, log )
R::qbeta( q, shape1, shape2, lower, log )
R::rbeta(    shape1, shape2 )

22.3.6 Noncentral beta distribution

These functions provide information about the Noncentral beta distribution with parameters shape1 and shape2, noncentrality parameter ncp. These functions are equivalent to setting non 0 value for the noncentrality parameter ncp in the Beta distribution function in R.

Rcpp::dnbeta( x, shape1, shape2, ncp,               log = false );
Rcpp::pnbeta( x, shape1, shape2, ncp, lower = true, log = false );
Rcpp::qnbeta( q, shape1, shape2, ncp, lower = true, log = false );
// Rcpp::rnbeta() does not exist

R::dnbeta( x, shape1, shape2, ncp,        log )
R::pnbeta( x, shape1, shape2, ncp, lower, log )
R::qnbeta( q, shape1, shape2, ncp, lower, log )
R::rnbeta(    shape1, shape2, ncp )

22.3.7 Chi-squared distribution

These functions provide information about the Chi-squared distribution with df degrees of freedom df. These functions are equivalent to setting 0 for the noncentrality parameter ncp in the Beta distribution function in R.

Rcpp::dchisq( x, df,               log = false )
Rcpp::pchisq( x, df, lower = true, log = false )
Rcpp::qchisq( q, df, lower = true, log = false )
Rcpp::rchisq( n, df)

R::dchisq( x, df,        log )
R::pchisq( x, df, lower, log )
R::qchisq( q, df, lower, log )
R::rchisq(    df )

22.3.8 Noncentral chi-squared distribution

These functions provide information about the Noncentral chi-squared distribution with df degrees of freedom df and noncentrality parameter ncp. These functions are equivalent to setting non 0 value for the noncentrality parameter ncp in the Chi-squared distribution function in R.

Rcpp::dnchisq( x, df, ncp,               log = false )
Rcpp::pnchisq( x, df, ncp, lower = true, log = false )
Rcpp::qnchisq( q, df, ncp, lower = true, log = false )
Rcpp::rnchisq( n, df, ncp = 0.0 )

R::dnchisq( x, df, ncp,        log )
R::pnchisq( x, df, ncp, lower, log )
R::qnchisq( q, df, ncp, lower, log )
R::rnchisq(    df, ncp )

22.3.9 t-distribution

These functions provide information about the t-distribution with df degrees of freedom df. These functions are equivalent to setting 0 for the noncentrality parameter ncp in the Beta distribution function in R.

Rcpp::dt( x, df,               log = false )
Rcpp::pt( x, df, lower = true, log = false )
Rcpp::qt( q, df, lower = true, log = false )
Rcpp::rt( n, df )

R::dt( x, df,        log )
R::pt( x, df, lower, log )
R::qt( q, df, lower, log )
R::rt(    df )

22.3.10 Noncentral t-distribution

These functions provide information about the Noncentral t-distribution with df degrees of freedom df and noncentrality parameter ncp. These functions are equivalent to setting non 0 value for the noncentrality parameter ncp in the t-distribution function in R.

Rcpp::dnt( x, df, ncp,               log = false  )
Rcpp::pnt( x, df, ncp, lower = true, log = false  )
Rcpp::qnt( q, df, ncp, lower = true, log = false  )
// Rcpp::rnt() does not exist.

R::dnt( x, df, ncp,        log )
R::pnt( x, df, ncp, lower, log )
R::qnt( q, df, ncp, lower, log )
// R::rnt() does not exist.

22.3.11 F-distribution

These functions provide information about the F-distribution with df degrees of freedom df1 and df2. These functions are equivalent to setting 0 for the noncentrality parameter ncp in the F-distribution function in R.

Rcpp::df( x, df1, df2,               log = false )
Rcpp::pf( x, df1, df2, lower = true, log = false )
Rcpp::qf( q, df1, df2, lower = true, log = false )
Rcpp::rf( n, df1, df1 )

R::df( x, df1, df2,        log )
R::pf( x, df1, df2, lower, log )
R::qf( q, df1, df2, lower, log )
R::rf(    df1, df2 )

22.3.12 Noncentral F-distribution

These functions provide information about the F-distribution with df degrees of freedom df1, df2 and noncentrality parameter ncp. These functions are equivalent to setting non 0 value for the noncentrality parameter ncp in the Noncentral F-distribution function in R.

Rcpp::dnf( x, df1, df2, ncp,               log = false )
Rcpp::pnf( x, df1, df2, ncp, lower = true, log = false )
Rcpp::qnf( q, df1, df2, ncp, lower = true, log = false )
// Rcpp::rnf() does not exist.

R::dnf( x, df1, df2, ncp,        log )
R::pnf( x, df1, df2, ncp, lower, log )
R::qnf( q, df1, df2, ncp, lower, log )
// R::rnf() does not exist.

22.3.13 Cauchy distribution

These functions provide information about the Cauchy distribution with location parameter location and scale parameter scale.

Rcpp::dcauchy( x, location = 0.0, scale = 1.0,               log = false )
Rcpp::pcauchy( x, location = 0.0, scale = 1.0, lower = true, log = false )
Rcpp::qcauchy( q, location = 0.0, scale = 1.0, lower = true, log = false )
Rcpp::rcauchy( n, location = 0.0, scale = 1.0)

R::dcauchy( x, location, scale,        log )
R::pcauchy( x, location, scale, lower, log )
R::qcauchy( q, location, scale, lower, log )
R::rcauchy(    location, scale )

22.3.14 Exponential distribution

These functions provide information about the Exponential distribution with rate rate (The mean of Exponential distribution equals to 1/rate).

Rcpp::dexp( x, rate = 1.0,               log = false )
Rcpp::pexp( x, rate = 1.0, lower = true, log = false )
Rcpp::qexp( q, rate = 1.0, lower = true, log = false )
Rcpp::rexp( n, rate = 1.0)

// The R namespace version are parameterised in terms of the scale (=1/rate)
R::dexp( x, scale,        log )
R::pexp( x, scale, lower, log )
R::qexp( q, scale, lower, log )
R::rexp(    scale )

22.3.15 Logistic distribution

These functions provide information about the Logistic distribution with parameters location and scale.

Rcpp::dlogis( x, location = 0.0, scale = 1.0,               log = false )
Rcpp::plogis( x, location = 0.0, scale = 1.0, lower = true, log = false )
Rcpp::qlogis( q, location = 0.0, scale = 1.0, lower = true, log = false )
Rcpp::rlogis( n, location = 0.0, scale = 1.0 )

R::dlogis( x, location, scale,        log )
R::plogis( x, location, scale, lower, log )
R::qlogis( q, location, scale, lower, log )
R::rlogis(    location, scale )

22.3.16 Weibull distribution

These functions provide information about the Weibull distribution with parameters shape and scale.

Rcpp::dweibull( x, shape, scale = 1.0,               log = false  )
Rcpp::pweibull( x, shape, scale = 1.0, lower = true, log = false  )
Rcpp::qweibull( q, shape, scale = 1.0, lower = true, log = false  )
Rcpp::rweibull( n, shape, scale = 1.0 )

R::dweibull( x, shape, scale,        log )
R::pweibull( x, shape, scale, lower, log )
R::qweibull( q, shape, scale, lower, log )
R::rweibull(    shape, scale )

22.4 Discrete probability distribution

22.4.1 Binomial distribution

These functions provide information about the Binomial distribution with number of trials size and success probability prob.

Rcpp::dbinom( x, size, prob,               log = false )
Rcpp::pbinom( x, size, prob, lower = true, log = false )
Rcpp::qbinom( q, size, prob, lower = true, log = false )
Rcpp::rbinom( n, size, prob )

R::dbinom( x, size, prob,        log )
R::pbinom( x, size, prob, lower, log )
R::qbinom( q, size, prob, lower, log )
R::rbinom(    size, prob )

22.4.2 Negative binomial distribution (with success probability as parameter)

These functions provide information about the Negative binomial distribution with number of success size and success probability prob.

Rcpp::dnbinom( x, size, prob,               log = false )
Rcpp::pnbinom( x, size, prob, lower = true, log = false )
Rcpp::qnbinom( q, size, prob, lower = true, log = false )
Rcpp::rnbinom( n, size, prob )

R::dnbinom( x, size, prob,        log )
R::pnbinom( x, size, prob, lower, log )
R::qnbinom( q, size, prob, lower, log )
R::rnbinom(    size, prob )

22.4.3 Negative binomial distribution (with mean as parameter)

These functions provide information about the Negative binomial distribution with number of success size and mean mu.

Rcpp::dnbinom_mu( x, size, mu,               log = false )
Rcpp::pnbinom_mu( x, size, mu, lower = true, log = false )
Rcpp::qnbinom_mu( q, size, mu, lower = true, log = false )
Rcpp::rnbinom_mu( n, size, mu )

R::dnbinom_mu( x, size, mu,        log )
R::pnbinom_mu( x, size, mu, lower, log )
R::qnbinom_mu( q, size, mu, lower, log )
R::rnbinom_mu(    size, mu )

22.4.4 Poisson distribution

These functions provide information about the Poisson distribution with mean and variance are equal to lambda.

Rcpp::dpois( x, lambda,               log = false )
Rcpp::ppois( x, lambda, lower = true, log = false )
Rcpp::qpois( q, lambda, lower = true, log = false )
Rcpp::rpois( n, lambda )

R::dpois( x, lambda, log )
R::ppois( x, lambda, lower, log )
R::qpois( q, lambda, lower, log )
R::rpois(    lambda )

22.4.5 Geometric distribution

These functions provide information about the Geometric distribution with success probability prob.

Rcpp::dgeom( x, prob,               log = false )
Rcpp::pgeom( x, prob, lower = true, log = false )
Rcpp::qgeom( q, prob, lower = true, log = false )
Rcpp::rgeom( n, prob )

R::dgeom( x, prob, log )
R::pgeom( x, prob, lower, log )
R::qgeom( q, prob, lower, log )
R::rgeom(    prob )

22.4.6 Hypergeometric distribution

These functions provide information about the Hypergeometric distribution with number of success in the population m , number of failure in the population n, number of sample from the population k.

Rcpp::dhyper( x, m, n, k,               log = false )
Rcpp::phyper( x, m, n, k, lower = true, log = false )
Rcpp::qhyper( q, m, n, k, lower = true, log = false )
Rcpp::rhyper(nn, m, n, k )

R::dhyper( x, m, n, k,        log )
R::phyper( x, m, n, k, lower, log )
R::qhyper( q, m, n, k, lower, log )
R::rhyper(    m, n, k )

22.4.7 Distribution of Wilcoxon rank-sum test statistic

These functions provide information about the distribution of test statistic when Wilcoxon rank-sum test (Mann–Whitney U test) is performed on two specimens with number of samples m and n respectively.

// Rcpp::dwilcox() does not exist.
// Rcpp::pwilcox() does not exist.
// Rcpp::qwilcox() does not exist.
Rcpp::rwilcox( nn, m, n );

R::dwilcox( x, m, n,        log )
R::pwilcox( x, m, n, lower, log )
R::qwilcox( q, m, n, lower, log )
R::rwilcox(    m, n )

22.4.8 Distribution of Wilcoxon signed-rank test statistic

These functions provide information about the distribution of test statistic when Wilcoxon signed-rank test is performed with number of samples n.

// Rcpp::dsignrank() does not exist.
// Rcpp::psignrank() does not exist.
// Rcpp::qsignrank() does not exist.
Rcpp::rsignrank( nn, n )

R::dsignrank( x, n,        log )
R::psignrank( x, n, lower, log )
R::qsignrank( q, n, lower, log )
R::rsignrank(    n )