Professional Documents
Culture Documents
Practical No.1
Practical No.1
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)')
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")