#
# CHAPTER 19: Probability
#
# U(0, 1) distribution
#
runif(4) # four U(0, 1) random numbers
runif(4) # four more U(0, 1) random numbers
set.seed(6) # set the random number stream to 6
runif(4) # four more U(0, 1) random numbers
set.seed(6) # set the random number stream to 6
runif(4) # the same four U(0, 1) random numbers
#
# binomial(n, p) distribution
#
dbinom(3, 5, 1 / 2) # pmf at x = 3 for X ~ binomial(5, 1 / 2)
pbinom(3, 5, 1 / 2) # cdf at x = 3 for X ~ binomial(5, 1 / 2)
rbinom(12, 5, 1 / 2) # 12 random variates from X ~ binomial(5, 1 / 2)
#
# Poisson(lambda) distribution
#
dpois(2, 5) # pmf at x = 2 for X ~ Poisson(5)
ppois(2, 5) # cdf at x = 2 for X ~ Poisson(5)
x = rpois(12, 5) # set x to 12 random variates from Poisson(5)
mean(x) # mean of x
#
# U(a = 0, b = 1) distribution
#
dunif(3, 0, 10) # pdf at x = 3 for X ~ U(0, 10)
punif(3, 0, 10) # cdf at x = 3 for X ~ U(0, 10)
qunif(0.8, 0, 10) # 80th percentile of X ~ U(0, 10)
runif(12, 0, 10) # 12 random variates from X ~ U(0, 10)
runif(12) # 12 random variates from X ~ U(0, 1)
#
# N(mu = 0, sigma = 1) distribution
#
dnorm(1) # pdf at x = 1 for X ~ N(0, 1)
pnorm(-1.2) # cdf at x = -1.2 for X ~ N(0, 1)
qnorm(0.98) # 98th percentile of X ~ N(0, 1)
rnorm(12) # 12 random variates from X ~ N(0, 1)
rnorm(12, 68, 3) # 12 random variates from X ~ N(68, 3)
#
# drawing balls from an urn with the sample function
# syntax: sample(x, size, replace = FALSE, prob = NULL)
#
sample(7) # random permutation of first 7 positive integers
sample(7, 5) # 5 integers from the first 7 positive integers
sample(7, 5, replace = TRUE) # same as previous but with replacement
sample(7, 5, prob = (1:7) / 28) # larger integers more likely
x = c("one", "two", "three", "four", "five", "six")
sample(x, 9, replace = TRUE) # nine rolls of a fair die