sessionInfo() library(plot3D) library(ggplot2) #library(magick) library(animation) set.seed(2020) # set 2020th seed; does not mean x_0 = 2020 runif(5) set.seed(2020) # same seed results in same "random" sequence runif(5) # returns a congruential generator with fixed $a$ and $m$ congruential_gen <- function(a, m, seed = 1) { # initial seed for g if (!hasArg(seed)) seed <- (as.numeric(Sys.time()) * 1000) %% m g <- function(n) { u <- vector(length=n) u[1] <- seed for (i in seq_len(n-1)) { u[i+1] <- (a * u[i]) %% m } seed <<- (a * seed) %% m # seed update u / m } g } # IBM System/360 RANDU generator # a = 2^16 + 3 = 65539 # m = 2^31 RANDU <- congruential_gen(65539, 2^31) #, seed=1) RANDU(10) n <- c(10, 50, 100, 500, 1000, 2000, 5000, 10000, 20000) saveGIF({ for (i in 1:length(n)) { u <- RANDU(n[i]) x <- u[1:(n[i]-2)] y <- u[2:(n[i]-1)] z <- u[3:(n[i])] scatter3D(x, y, z, colvar = NULL, pch=20, cex = 0.5, theta=160, main = paste('n = ', n[i])) } }, movie.name = 'RANDU.gif') # Early MATLAB RNG # a = 7^5 # m = 2^31 - 1 MATLABRNG <- congruential_gen(7^5, 2^31 - 1) MATLABRNG(10) saveGIF({ for (i in 1:length(n)) { u <- MATLABRNG(n[i]) x <- u[1:(n[i]-2)] y <- u[2:(n[i]-1)] z <- u[3:(n[i])] scatter3D(x, y, z, colvar = NULL, pch=20, cex = 0.5, theta=160, main = paste('n = ', n[i])) } }, movie.name = 'MATLABRNG.gif') RNGkind() # Exp(1) n <- 1000 u <- runif(n) x <- -log(u) expdata <- data.frame(x) plt <- ggplot(expdata, aes(x=x)) + geom_histogram(binwidth=0.25, fill="white", color="black") + geom_density(aes(y=0.25 * ..count..), color="purple") print(plt) # standard Cauchy (beta=1, mu=0) n <- 1000 u <- runif(n) x <- -1/tan(pi * u) #hist(x, breaks=40) cauchydata <- data.frame(x) plt <- ggplot(cauchydata, aes(x=x)) + geom_histogram(binwidth=10, fill="white", color="black") + geom_density(aes(y=10 * ..count..), color="purple") + xlim(-200, 200) print(plt) k <- 10 n <- 1000 u <- runif(n) x <- ceiling(k * u) table(x) gengeom <- function(p, nsamp=1) { u <- runif(nsamp) y <- log(u) / log(1 - p) ceiling(y) } nsamp <- 1000 p <- 0.3 x <- gengeom(p, nsamp) geomdata <- data.frame(x) plt <- ggplot(geomdata, aes(x=x)) + geom_histogram(binwidth=0.25) print(plt) boxmuller <- function(nsamp) { n <- ceiling(nsamp / 2) u <- runif(n) v <- runif(n) r <- sqrt(-2 * log(u)) theta <- 2 * pi * v x <- r * cos(theta) y <- r * sin(theta) samp <- c(x, y) samp[seq_len(nsamp)] } #hist(c(x, y)) n <- 1000 normdata1 <- data.frame(x = boxmuller(n)) plt <- ggplot(normdata1, aes(x=x)) + geom_histogram(binwidth=0.25, fill="white", color="black") + geom_density(aes(y=0.25 * ..count..), color="purple") print(plt) marsaglia <- function(nsamp) { n <- ceiling(nsamp / 2) it <- 0 x <- numeric(n) y <- numeric(n) while (it < n) { u <- 2 * runif(1) - 1 v <- 2 * runif(1) - 1 tau <- sqrt(u^2 + v^2) if (tau > 1) next x[it] <- sqrt(-4 * log(tau)) * u / tau y[it] <- sqrt(-4 * log(tau)) * v / tau it <- it + 1 } samp <- c(x, y) samp[seq_len(nsamp)] } n <- 1000 normdata2 <- data.frame(x = marsaglia(n)) plt <- ggplot(normdata2, aes(x=x)) + geom_histogram(binwidth=0.25, fill="white", color="black") + geom_density(aes(y=0.25 * ..count..), color="purple") print(plt) ## Binomial random number generation genbin <- function(n, p) { u <- runif(n) x <- sum(u < p) } n <- 10; p <- 0.6 nsamp <- 1000 x <- numeric(nsamp) for (i in seq_len(nsamp)) { x[i] <- genbin(n, p) } bindata <- data.frame(x) plt <- ggplot(bindata, aes(x=x)) + geom_histogram(binwidth=0.25) print(plt) # Negative binomial random number generation gengeom <- function(p, nsamp=1) { u <- runif(nsamp) y <- log(u) / log(1 - p) ceiling(y) } nsamp <- 1000 p <- 0.6 r <- 5 x <- numeric(nsamp) for (i in seq_len(r)) { x <- x + gengeom(p, nsamp) } negbindata <- data.frame(x) plt <- ggplot(negbindata, aes(x=x)) + geom_histogram(binwidth=0.25) print(plt) # Poisson random number generation genpoi <- function(lam, maxiter=1000) { u_cum <- 1.0 k <- 0 while (u_cum > exp(-lam) && k < maxiter ) { u <- runif(1) u_cum <- u_cum * u k <- k + 1 } k - 1 } lam <- 3 # Poisson rate nsamp <- 1000 x <- numeric(nsamp) for (i in seq_len(nsamp)) { x[i] <- genpoi(lam) } poidata <- data.frame(x) plt <- ggplot(poidata, aes(x=x)) + geom_histogram(binwidth=0.25) print(plt) ## chi-square random number generation genchisq1 <- function(nsamp, nu) { z <- matrix(rnorm(nsamp * nu), nrow=nsamp) rowSums(z^2) } nu <- 6 n <- 1000 chisqdata1 <- data.frame(x = genchisq1(n, nu)) plt <- ggplot(chisqdata1, aes(x=x)) + geom_histogram(binwidth=0.25, fill="white", color="black") + geom_density(aes(y=0.25 * ..count..), color="purple") print(plt) ## chi-square random number generation 2 genchisq2 <- function(nsamp, nu) { u <- matrix(runif(nsamp * nu / 2), nrow=nsamp) -2 * log(apply(u, 1, prod) ) } nu <- 6 n <- 1000 chisqdata2 <- data.frame(x = genchisq2(n, nu)) plt <- ggplot(chisqdata2, aes(x=x)) + geom_histogram(binwidth=0.25, fill="white", color="black") + geom_density(aes(y=0.25 * ..count..), color="purple") print(plt) ## Student's t random number generation gent <- function(nsamp, nu) { z <- rnorm(nsamp) chisq <- genchisq1(nsamp, nu) trv <- z / sqrt(chisq / nu) } nu <- 6 n <- 1000 tdata <- data.frame(x = gent(n, nu)) plt <- ggplot(tdata, aes(x=x)) + geom_histogram(binwidth=0.25, fill="white", color="black") + geom_density(aes(y=0.25 * ..count..), color="purple") print(plt) # F random number generation genF <- function(nsamp, nu1, nu2) { chisq1 <- genchisq1(nsamp, nu1) chisq2 <- genchisq1(nsamp, nu2) Frv <- chisq1 / nu1 / chisq2 * nu2 } nu1 <- 10; nu2 <- 6 n <- 1000 Fdata <- data.frame(x = genF(n, nu1, nu2)) plt <- ggplot(Fdata, aes(x=x)) + geom_histogram(binwidth=0.25, fill="white", color="black") + geom_density(aes(y=0.25 * ..count..), color="purple") print(plt) gengamma_ar <- function(nsamp, alph) { # sample X from g v <- runif(nsamp) # unif rv for inverse method idx <- v > 1 / (1 + alph * exp(-1)) x <- numeric(nsamp) x[idx] = -log(1 / alph + exp(-1)) - log(1 - v[idx]) x[!idx] = ((1 + alph * exp(-1)) * v[!idx])^(1 / alph) # test acceptance u <- runif(nsamp) idx2 <- (x > 1) accept <- logical(nsamp) accept[idx2] <- (u[idx2] < x[idx2]^(alph - 1)) accept[!idx2] <- (u[!idx2] < exp(-x[!idx2])) x[accept] } n <- 2000 alph <- 0.5 x <- gengamma_ar(n, alph) length(x) length(x) / n gamma(0.5) / (1 / alph + exp(-1) ) # acceptance ratio gamdata <- data.frame(x = x) plt <- ggplot(gamdata, aes(x=x)) + geom_histogram(binwidth=0.25, fill="white", color="black") + geom_density(aes(y=0.25 * ..count..), color="purple") print(plt)