In this problem we will draw plots for some of the discrete probability distributions that we have studied in class
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).
plot_binom <- function(n,p){
x <- 0:n
bin <-dbinom(x,n,p)
mu_x <- n*p
plot(x, bin, type = "h", lwd = 5,
main = paste("density", n,"," ,p),
xlab = "x", ylab = "P(X=x)")
abline(v = mu_x, col ='red', lwd = 4)
}
plot_binom(10,1/6)
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.
n <- 40
prob_vals <- c(0.05, 0.1, 0.4, 0.6, 0.9, 0.95)
par(mfrow = c(3, 2))
for(p in prob_vals){
plot_binom(n, p)
}
Write at least two observations that you can note from these plots. Consider skewness and symmetry.
-From the plots we can see that when p > 0.5 the distrubtion is skewed to the left and when its p < 0.5 its skewed to the right -As the probability increases, the graph mover towards its maximum value (40 in this case)
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).
n <- 100
plot_pois <- function(mu){
x <- 0:n
pois <- dpois(x, lambda = mu)
mu_X <- mu
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)
}
plot_pois(6)
For the following values of mu compute the plots for the Poisson Density: mu: 0.5, 1, 5, 8, 20, 50
n <- 100
plot_pois <- function(mu){
x <- 0:n
pois <- dpois(x, lambda = mu)
mu_X <- mu
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)
}
par(mfrow=c(3,2))
plot_pois(0.5)
plot_pois(1)
plot_pois(5)
plot_pois(8)
plot_pois(20)
plot_pois(50)
What observations can you make about the density plots of the Poisson distribution for large values of mu?
-We can use the normal distribution once the Poisson mean becomes large enough, Answer:
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)
plot_binom <- function(n,p){
x <- 0:n
bin <- dbinom(x, n, p)
mu_X <- n * p
plot(x, bin, type = "h", #lwd = 5,
main = paste("Binomial Distribution: n , p "),
xlab = "x", ylab = "P(X=x)")
#abline(v = mu_X, col = 'red', lwd = 4)
}
plot_pois <- function(mu){
x <- 0:n
pois <- dpois(x, lambda = mu)
mu_X <- mu
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)
}
par(mfrow = c(2,1))
plot_binom(100, 0.05)
plot_pois(5)
What observations can you make? Answer:
Recall problem from HW5. \ Yulia owns a coffee distribution company. A very large batch of bags of coffee beans has arrived. She will accept the batch only if the proportion of underweight bags is at most 0.10. (Hint: Use the Binomial distribution with appropriate parameters for this problem)
Yulia decides to randomly select 10 bags of coffee, and will accept the batch only if the number of underweight bags in the sample is at most 2. \ Let \(p\) denote the actual proportion underweight bags in the batch. Calculate \(P(\text{batch is accepted})\) as a function of \(p\), call this function ‘at_most2’ and plot it (domain is (0,1)).
#n= bags of coffee
#x= underweight bags in the sample
plot_binom <- function(n,p){
x <- 0:n
bin <- dbinom(x, n, p)
mu_X <- n * p
plot(x, bin, type = "h", #lwd = 5,
main = paste("Binomial Distribution: n , p "),
xlab = "x", ylab = "P(X=x)")
abline(v = mu_X, col = 'red', lwd = 4)
}
plot_binom(10,0.1)
bin <- pbinom(2, 10, 0.1)
bin
## [1] 0.9298092
Now suppose Yulia decides to randomly select 10 bags of coffee, and will accept the batch only if the number of underweight bags in the sample is at most 1. \
Let \(p\) denote the actual proportion underweight bags in the batch. Calculate \(P(\text{batch is accepted})\) as a function of \(p\), call this function ‘at_most1’ and plot it (domain is (0,1)).
at_most1 <- function(x,n,p){
bin <- pbinom(x, n, p)
return(bin)
}
plot_binom <- function(n,p){
x <- 0:n
bin <- dbinom(x, n, p)
mu_X <- n * p
plot(x, bin, type = "h", lwd = 5, xlim = c(0,1),
main = paste("Binomial Distribution: n , p "),
xlab = "x", ylab = "P(X=x)")
#abline(v = mu_X, col = 'red', lwd = 4)
}
at_most1(1,10,0.1)
## [1] 0.7360989
plot_binom(10,0.1)
Finally, suppose Yulia decides to randomly select 15 bags of coffee, and will accept the batch only if the number of underweight bags in the sample is at most 2. \
Let \(p\) denote the actual proportion underweight bags in the batch. Calculate \(P(\text{batch is accepted})\) as a function of \(p\), call this function ‘at_most2_15bags’ and plot it (domain is (0,1)).
at_most2_15bags<- function(x,n,p){
bin <- pbinom(x, n, p)
return(bin)
}
plot_binom <- function(n,p){
x <- 0:n
bin <- dbinom(x, n, p)
mu_X <- n * p
plot(x, bin, type = "h", lwd = 5, xlim = c(0,2),
main = paste("Binomial Distribution: n , p "),
xlab = "x", ylab = "P(X=x)")
abline(v = mu_X, col = 'red', lwd = 4)
}
at_most2_15bags(2,15,0.1)
## [1] 0.8159389
plot_binom(15,0.1)