Using the quantile $p=F(x)$ and inverting the CDF equation gives the quantile function:
$$Q(p) = \frac{k}{(1-p)^{1/\gamma}}
\quad \quad \quad \text{for all } 0 \leqslant p \leqslant 1.$$
The corresponding log-quantile function is:
$$\log Q(p) = \log k - \frac{1}{\gamma} \log (1-p)
\quad \quad \quad \text{for all } 0 \leqslant p \leqslant 1.$$
The probability functions for the Pareto distribution are already available in R
(see e.g., the EnvStats
package). However, it is fairly simple to program this function from scratch if preferred. Here is a vectorised version of the function.
qpareto <- function(p, scale, shape = 1, lower.tail = TRUE, log.p = FALSE) {
#Check input p
if (!is.vector(p)) { stop('Error: p must be a numeric vector') }
if (!is.numeric(p)) { stop('Error: p must be a numeric vector') }
if (min(p) < 0) { stop('Error: p must be between zero and one') }
if (max(p) > 1) { stop('Error: p must be between zero and one') }
n <- length(p)
OUT <- numeric(n)
for (i in 1:n) {
P <- ifelse(lower.tail, 1-p[i], p[i])
OUT[i] <- log(scale) - log(P)/shape) }
if (log.p) OUT else exp(OUT) }
Once you have the quantile function it is simple to generate random variables using inverse-transformation sampling. Again, this is already done in the existing Pareto functions in R
, but if you want to program it from scratch, that is quite simple to do.
rpareto <- function(n, scale, shape = 1) {
#Check input n
if (!is.vector(n)) { stop('Error: n must be a single positive integer') }
if (!is.numeric(n)) { stop('Error: n must be a single positive integer') }
if (length(n) != 1) { stop('Error: n must be a single positive integer') }
if (as.integer(n) != n) { stop('Error: n must be a single positive integer') }
if (n <= 0) { stop('Error: n must be a single positive integer') }
qpareto(runif(n), scale = scale, shape = shape) }