Question Description
In this projects we will draw plots for different discrete probability distributions that we have studied in class.
# 1. Binomial distribution.
We will plot the density function for the binomial distribution Bin(n, p).
Note:
1) The values for this random variable are 0, 1, 2, …, n.
2) The density plot will have a bar of height P(X=k), at the point ‘k’ on the x-axis.
3) In the plot include a vertical line at the expected value of Bin(n,p).
Write a function plot_binom, that takes input values: n and p, and returns the density plot of Bin(n,p).
“`{r}
# plot_binom <- function(n,p){
# x <- 0:n
# bin <- dbinom( _____ ) # input appropriate parameters
# mu_X <- ____ #input appropriate value for expected value
# plot(x, bin, type = “h”, lwd = 5,
# main = paste(“Binom density: n = “, n ,”and p = “, p),
# xlab = “x”, ylab = “P(X=x)”)
# abline(v = mu_X, col = ‘red’, lwd = 4)
# }
“`
Fix n = 40. Compute plots for the following values of p: 0.05, 0.1, 0.4, 0.6, 0.9, 0.95.
Use the command “par(mfrow=c(3,2))” to have all the plots on the same frame.
“`{r}
# n <- 40
# prob_vals <- c(_______) #input appropriate values for probability
# par(mfrow=c(3,2))
# for (p in prob_vals){
# plot_binom(____) #input the appropriate values
#}
“`
Write at least two observations that you can note from these plots. Consider skewness and symmetry.
# 2. Poisson Distribution.
We will plot the density function for the Poison distribution Pois(mu).
Note:
1) The values for this random variable are: 0, 1, 2, 3, ….
2) The density plot will have a bar of height P(X=k), at the point ‘k’ on the x-axis.
3) Since most of the densities will be concentrated at lower values of k, we will fix a large enough value of n, say n = 100, when drawing the density plots.
3) In the plot include a vertical line at the expected value of Pois(mu).
Write a function plot_pois, that takes input values: mu, and returns the density plot of Pois(mu).
“`{r}
# plot_pois <- function(mu){
# n <- 100
# x <- 0:n
# pois <- dpois(x, lambda = ___ ) # input appropriate values
# mu_X <- ____ #input appropriate expected value
# plot(x, pois, type = “h”, lwd = 5,
# main = paste(“Poisson density: mu = “, mu),
# xlab = “x”, ylab = “P(X=x)”)
# abline(v = mu_X, col = ‘red’, lwd = 4)
# }
“`
For the following values of mu compute the plots for the Poisson Density:
mu: 0.5, 1, 5, 8, 20, 50
“`{r}
# mu_vals <- c(_______) #input appropriate values
# par(mfrow=c(3,2))
# for (mu in mu_vals){
# plot_ ______ #call the appropriate function
#}
“`
What observations can you make about the density plots of the Poisson distribution for large values of mu?
Now use your plot functions to evaluate the following two plots on the same frame (one below the other):
plot_binom(100, 0.05)
plot_pois(5)
“`{r}
# par(mfrow = c(2,1))
# plot_binom( ___ , ___)
# plot_pois(___)
“`
What observations can you make?
# 3. Hypergeometric Distribution.
Recall from lectures that the hypergeometric distribution has parameters: n, M, N.
The inbuilt function “dhyper” takes input x (values of rv), m (number of successes), n (number of failures), and k (size of the sample).
We will plot the density function for the hypergeometric distribution Hyper(n, m, k).
Note:
1) The values for this random variable are: 0, 1, 2, 3, …., k
2) The density plot will have a bar of height P(X=x), at the point ‘x’ on the x-axis.
3) In the plot include a vertical line at the expected value of Hyper(n, m, k).
“`{r}
# plot_hyper <- function(n, m, k){
# x <- 0:k
# hyper <- dhyper(x, n, m, k)
# mu_X <- _____ #compute the relevant expected value
# plot(x, hyper, type = “h”, lwd = 5,
# main = paste(“Hypergeo: n = “, n ,”, m = “, m, “, k = “, k),
# xlab = “x”, ylab = “P(X=x)”)
# abline(v = mu_X, col = ‘red’, lwd = 4)
# }
“`
Now use your plot functions to evaluate the following two plots on the same frame (one below the other):
plot_hyper(60, 40, 20)
plot_bin(20, 0.6)
“`{r}
# par(mfrow = c(2,1))
# plot_hyper(60, 40, 20)
# plot_binom(20, .6 )
“`
Also, compute the plots
plot_hyper(60, 40, 80)
plot_bin(80, 0.6)
“`{r}
# par(mfrow = c(2,1))
# plot_hyper(60, 40, 80)
# plot_binom(80, .6 )
“`
What do you observe in these four plots?
# 4. Negative Binomial Distribution.
Recall from lectures that the negative binomial distribution has parameters: r, p.
The inbuilt function “dnbinom” takes input x (values of rv), size (number successes), and prob (probability of a success).
We will plot the density function for the hypergeometric distribution Neg_Bin(r,p)
Note:
1) The values for this random variable are: 0, 1, 2, 3, ….
2) The density plot will have a bar of height P(X=x), at the point ‘x’ on the x-axis.
3) Since for large values of x, P(X=x) will be very small, we will fix n = 100, for our plots.
4) In the plot include a vertical line at the expected value of neg_bin(r, p).
“`{r}
# plot_neg_bin <- function(r, p){
# n <- 200
# x <- 0:n
# neg_bin <- dnbinom(x, size = _____ , prob = _____) #input the relevant values
# mu_X <- ______ #input the relevant values
# plot(x, neg_bin, type = “h”, lwd = 5,
# main = paste(“Neg Binom: r = “, r ,”, p = “, p),
# xlab = “x”, ylab = “P(X=x)”)
# abline(v = mu_X, col = ‘red’, lwd = 4)
# }
“`
Use this function to compute the following plots:
“`{r}
# par(mfrow = c(3,3))
# plot_neg_bin(1, 0.05)
# plot_neg_bin(1, 0.1)
# plot_neg_bin(1, 0.5)
# plot_neg_bin(3, 0.05)
# plot_neg_bin(3, 0.1)
# plot_neg_bin(3, 0.5)
“`
EXTRA CREDIT
Rolling two dice.
Experiment: Given two die which are biased to land on 3 with probability .5 and equally likely to land on all other numbers with probability .1.
We roll these die.
Define a random variable “sum_dice” that calculates the sum of the numbers that show up on the two dice.
“`{r}
“`
Run a simulation of 100000 rolls. Use this simulation to compute the empirical probability distribution of this random variable.
Compare this with the theoretical probability distribution.
“`{r}
“`
Compute the expected value and the variation of this random variable.
“`{r}
“`