TU Wien:Statistik und Wahrscheinlichkeitstheorie UE (Levajkovic)/Übungen 2023W/HW03.5

Aus VoWi
Zur Navigation springen Zur Suche springen
The Pareto distribution
Generating random numbers

Problem 3c shows that if , then generates a random number from any continuous distribution with the specified cumulative distribution function . This gives us the following algorithm to generate random numbers from a random variable with the given cumulative distribution function .

Algorithm: Inversion method

  1. compute the inverse of
  2. generate independent random numbers from
  3. compute .

Then, are independent random observations of the random variable .

In R, generate 15 observations from a random variable with the Pareto distribution with

(see Problem 4) by applying the inversion method.

Dieses Beispiel ist als solved markiert. Ist dies falsch oder ungenau? Aktualisiere den Lösungsstatus (Details: Vorlage:Beispiel)


Lösungsvorschlag von Lessi[Bearbeiten | Quelltext bearbeiten]

--Lessi 2024-02-07T13:04:11Z

library(EnvStats)


# HW 3 Problem 5
# Generate 15 observations form a random variable X with the Pareto distribution with a = m = 2 by applying the inversion method

F <- function(x) {
  ifelse(x >= 2, 1 - (2 / x)^2, 0)
}

# lets look at the function
x <- seq(0, 20, 0.1)
plot(x, F(x))


F_inv <- function(p) {
  ifelse(p > 0, 2 / sqrt(1 - p), 0)
}

# and we take a look at its inverse
p <- seq(0, 1, 0.01)
plot(p, F_inv(p))


# now for the real algorithm:

# 1) generate 15 samples from standard uniform distribution
p <- runif(15, 0, 1)
p

# 2) map them to values from the pareto distribution
X <- F_inv(p)
X
hist(X)


# just to very: we use EnvStats to see how a histogram of those samples should look like
X_test <- qpareto(p, 2, 2)
hist(X_test)