Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Q 1) Plot the pmf, cdf, and quantile function for the binomial distribution

with probability of success 0.25 and 39 trials, i.e. X∼Bin(39,0.25). Then


sample 999 random binomials with 39 trials and probability of success
0.25 and plot them on a histogram with the true probability mass
function
n <- 39
p <- 0.25
# pmf
x <- 0:n
plot(x, dbinom(x, size = n, prob = p),
main = "Probability mass function for Bin(39,0.25)")
# cdf
plot(x, pbinom(x, size = n, prob = p), type="s",
main = "Cumulative distribution function for Bin(39,0.25)")
# quantile
p_seq <- seq(from = 0, to = 1, length = 101)
plot(p_seq, qbinom(p_seq, size = n, prob = p),
type="s", main = "Quantile function for Bin(39,0.25)")
#random number
draws <- rbinom(999, size = n, prob = p)
brks <- (0:(n+1)) - 0.5
hist(draws, breaks = brks,
main = "Random draws from Bin(39,0.25)")

Q 2) Plot the pmf, cdf, and quantile function for a Poisson distribution
with rate 23, i.e. X∼Po(23). Then sample 999 Poisson random variables
with rate 23 and plot them on a histogram with the true probability mass
function.

rate <- 23
x <- 0:10 # with no upper limit we need to decide on an upper limit
plot(x, dpois(x, lambda = rate), main = "Probability mass function for Po(23)")
Here is the cdf:
plot(x, ppois(x, lambda = rate), type="s", main = "Cumulative distribution function
for Po(23)")
Here is the quantile function:
plot(p_seq, qpois(p_seq, lambda = rate), type="s", ylim=c(0,10),
main = "Quantile function for Po(23)")
# Change the y limits for comparison purposes
draws <- rpois(999, lambda = rate) hist(draws, breaks = (0:(max(draws)+1)) - 0.5,
probability = TRUE, main = "Random draws from Po(23)")
points(x, dpois(x, lambda = rate), col="red")

Q 3) Plot the pdf, cdf, and quantile function for a uniform distribution on
the interval (13, 65), i.e. X∼Unif(13,65)X∼Unif(13,65). Then sample
999 random uniforms on the interval (13,65) and plot a histogram of
these draws with the probability density function.
a <- 13
b <- 65
# The curve function expects you to give a function of `x`
and then it # (internally) creates a sequence of values from `from`
and to `to` and creates # plots similar to what we had before,
but using a line rather than points.
curve(dunif(x, min = a, max = b), from = -1, to = 2, xlab='y', ylab='f(y)',
main='Probability density function for Unif(13,65)')

curve(punif(x, min = a, max = b), from = -1, to = 2, xlab='y',


ylab='F(y)', main='Cumulative distribution function for Unif(13,65)')
curve(qunif(x, min = a, max = b), from = 0, to = 1, xlab='p',
ylab='F^{-1}(p)', main='Quantile function for Unif(13,65)')

random_uniforms <- runif(999, min = a, max = b)


hist(random_uniforms, probability = TRUE, main = "Random draws from Unif(13,65)")
curve(dunif(x, min = a, max = b), add = TRUE, col="red")

Q 4)Plot the pdf, cdf, and quantile function for a normal distribution with
mean -4 and variance 3, i.e. X∼N(−4,3)X∼N(−4,3). Then sample 999
random N(-4,3) and plot a histogram of these draws with the probability
density function.
mu <- -4
sigma <- 3 # standard deviation
curve(dnorm(x, mean = mu, sd = sigma),
# notice the 3rd argument is the sd from = -4, to = 4, main = "PDF for a standard
normal")

curve(pnorm(x, mean = mu, sd = sigma),


from = -4, to = 4, main = "CDF for a standard normal", ylab = "F(x)")

curve(qnorm(x, mean = mu, sd = sigma),


from = 0, to = 1, main = "Quantile function for a standard normal")

draws <- rnorm(999, mean = mu, sd = sigma)


hist(draws, probability = TRUE)
curve(dnorm(x, mean = mu, sd = sigma), add = TRUE, col = "red")

You might also like