1 Visualizing Discrete Distributions

In this problem we will draw plots for some of the discrete probability distributions that we have studied in class

1.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).

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.

plot_binom <- function(n,p){
x <- 0:n
bin <- dbinom(x,n,p )
mu_X <- 15
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)
}

plot_binom(100,.6)

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. - The larger the probability, the further right the graph skews and the smaller the probability, the further left the graph skews - All of the graphs look symmetrical about the expected value

1.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).

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)
}

For the following values of mu compute the plots for the Poisson Density: mu: 0.5, 1, 5, 8, 20, 50

mu <- c(0.5, 1, 5, 8, 20, 50)
for(i in 1:6){
  plot_pois(mu[i])
}

What observations can you make about the density plots of the Poisson distribution for large values of mu? Answer: The larger the values of mu, the more condensed the plots are

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)

#par(mfrow = c(2,1))
plot_binom(100, 0.05)

plot_pois(5)

What observations can you make? Answer: The poisson distribution and binomial distributions are concentrated at the same areas and appear to have the same solutions (the expected vals Pois(mu) and Bin(n,p))

2 Plots From HW5

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)

2.1 Case 1:

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)).

p1 <- pbinom(2,10,0.10)
p1
## [1] 0.9298092
p<- dbinom(0,10,0.10)
q<- dbinom(1,10,0.10)
r<- dbinom(2,10,0.10)
at_most2<-sum(p,q,r)
at_most2
## [1] 0.9298092

The probability of accepting the batch only if the number of underweight bags in the smaple is 0.9298092.

2.2 Case 2:

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)).

p2<- pbinom(1,10,0.10)
p2
## [1] 0.7360989
p<- dbinom(0,10,0.10)
q<- dbinom(1,10,0.10)
at_most1<-sum(p,q)
at_most1
## [1] 0.7360989

The probability of accepting the batch only if the number of underweight bags in the sample is 0.7360989.

2.3 Case 3:

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)).

p3<-pbinom(2,15,0.10)
p3
## [1] 0.8159389
a<-dbinom(0,15,0.10)
b<-dbinom(1,15,0.10)
c<-dbinom(2,15,0.10)
at_most2_15bags<-sum(a,b,c)
at_most2_15bags
## [1] 0.8159389

The probability of accepting the batch only if the number of underweight bags in the sample is 0.8159.