


### UNIFORM DISTRIBUTION ###

# Let's talk about X ~ Unif(a, b). Plug in any values for a and b that you 
# like. Here's a sampling of random values of X.
a <- 5
b <- 7
runif(100, a, b)
hist(runif(100, a, b))

# Here's the PDF f evaluated at three values of x. Make sense?
dunif(4, a, b)
dunif(6, a, b)
dunif(8, a, b)

# Here's the CDF P(X <= x) evaluated at those same three values of x.
punif(4, a, b)
punif(6, a, b)
punif(8, a, b)

# Which x satisfies P(X <= x) = 0.75?
qunif(0.75, a, b)



### NORMAL DISTRIBUTION ###

# Let's talk about X ~ Norm(mu, sigma^2). In R, you specify sigma rather than 
# sigma^2. Here's a random sampling.
mu <- 3
sigma <- 2
rnorm(100, mu, sigma)
hist(rnorm(100, mu, sigma))

# Here's the PDF.
dnorm(4, mu, sigma)
dnorm(5, mu, sigma)
dnorm(6, mu, sigma)

# Here's the CDF.
pnorm(4, mu, sigma)
pnorm(5, mu, sigma)
pnorm(6, mu, sigma)

# Which x satisfies P(X <= x) = 0.975? It should be near mu + 2 * sigma. Why?
qnorm(0.975, mu, sigma)



### POISSON DISTRIBUTION ###

# The Poisson distribution Pois(lambda) is discrete, not continuous, but we 
# learn it in the continuous part of the course because of its tight connection 
# to the exponential distribution.

# Here are some random values. To get a better idea of how the histogram should 
# look, try cranking up the sample size.
lambda <- 2
rpois(100, lambda)
hist(rpois(100, lambda))

# Here's the PDF.
dpois(-1, lambda)
dpois(0, lambda)
dpois(1, lambda)
dpois(2, lambda)
dpois(3, lambda)

# Here's the CDF.
ppois(-1, lambda)
ppois(0, lambda)
ppois(1, lambda)
ppois(2, lambda)
ppois(3, lambda)

# Where's the median?
qpois(0.5, lambda)



### EXPONENTIAL DISTRIBUTION ###

# Here are random values of X ~ Expo(lambda). The histogram should look like 
# exponential decay, when the sample size is large.
lambda <- 2
rexp(100, lambda)
hist(rexp(100, lambda))

# Here's the PDF.
dexp(-1, lambda)
dexp(0, lambda)
dexp(1, lambda)
dexp(2, lambda)

# Here's the CDF.
pexp(-1, lambda)
pexp(0, lambda)
pexp(1, lambda)
pexp(2, lambda)

# Where's the median?
qexp(0.5, lambda)



### GENERATING (A FIXED NUMBER OF) POISSON PROCESS INTER-ARRIVAL TIMES ###

# Here is one way to generate an example of a Poisson process with rate lambda. 
# Select a lambda > 0 and an integer n >= 1.
lambda <- 7
n <- 10

# Randomly pick values x1, ..., xn of independent random variables 
# X1, ..., Xn ~ Expo(lambda). The histogram below should look like exponential 
# decay, when n is large.
xs <- rexp(n, lambda)
xs
hist(xs)

# The jth arrival time Tj is the sum X1 + ... + Xj of the first j inter-
# arrival times. So the realization tj of Tj is x1 + ... + xj.
ts <- sapply(1:n, function(j) sum(xs[1:j]))
ts
plot(x=ts, y=replicate(length(ts), 0))



### GENERATING POISSON PROCESS ARRIVAL TIMES (IN A FIXED INTERVAL) ###

# Here is another way to generate an example of a Poisson process with rate 
# lambda. Time t runs from 0 to tBound. Select lambda > 0 and tBound > 0.
lambda <- 7
tBound <- 1

# The number of arrivals between t = 0 and t = tBound is a random variable 
# N ~ Pois(lambda tBound). Randomly pick a value n of N.
n <- rpois(1, lambda * tBound)
n

# Let T1, ..., Tn ~ Unif(0, tBound) be independent. Randomly pick values 
# t1, ..., tn for them. Then sort those values.
ts <- runif(n, min=0, max=tBound)
ts <- sort(ts)
ts
plot(x=ts, y=replicate(length(ts), 0))

# The inter-arrival times are values x1, ..., xn of independent random 
# variables X1, ..., Xn ~ Expo(lambda). The histogram below should look like 
# exponential decay, when n is large.
xs <- ts - c(0, ts[1:(length(ts) - 1)])
xs
hist(xs)


