Skip to main content

Posts

Showing posts from January, 2019

Produce animation in R illustrating uniform and Gaussian density

#Number of simulations and chunk-size sN <- 265 sM <- 10 #Line from 0 to 1 will be divided into 40 bins bin <- seq(0 + (1 / 40) / 2, 1 - (1 / 40) / 2, 1 / 40) bincnt <- rep(0, 40) #Vertical distance between dots icr <- 0.01 printx <- NULL printy <- NULL for (i in 1:sN) { if (i < 15) sMI <- 1 if (i > 14) sMI <- sM sim <- runif(sMI) Sys.sleep(0.1) pointx <- rep(0, sMI) pointy <- rep(0, sMI) for (j in 1:sMI) { idx <- sim[j] %/% (1 / 40) + 1 pointx[j] <- bin[idx] pointy[j] <- bincnt[idx] + 1 pointy[j] <- max(icr, (pointy[j] * icr) %% 1) %% 1 bincnt[idx] <- bincnt[idx] + 1 } printx <- c(printx, pointx) printy <- c(printy, pointy) plot( seq(0, 1, 0.01), dunif(seq(0, 1, 0.01)), type = "l", xlab = "value", ylab = "Uniform density", ylim = c(0, 1.2), col = "red", lwd = 5 ) points(printx, printy, pch = 20) }…