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)
plot(x, bin, type='h', lwd=1,
main= paste("Binomial Distribution with probability :", p),
xlab = "x" ,ylab= "P(X=x)")
#We know that the expected value of a binomial distribution is n*p
abline(v=n*p, col="green", lwd=1)
}
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)
plot(x, bin, type='h', lwd=1,
main= paste("Binomial Distribution with probability :", p),
xlab = "x", ylab= "P(X=x)")
abline(v=n*p, col="green", lwd=1)
}
par(mfrow=c(3,2))
plot_binom(40,.05)
plot_binom(40, .1)
plot_binom(40, .4)
plot_binom(40,.6)
plot_binom(40,.9)
plot_binom(40,.95)
Write at least two observations that you can note from these plots. Consider skewness and symmetry.
We noticed that when the probability is around .5, our graphs are roughly symmetrical wheras if we have a smaller probability, our graphs are more rightly skewed.
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
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? 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)
par(mfrow = c(2,1))
plot_binom(100, 0.05)
plot_pois(5)
What observations can you make? Answer: It seems that both distributions look similar in a sense. Both graphs are rightly skewed. # Normal Distibution In this section we will explore the normal distribution.
Set \(\mu = 5\). For values of \(\sigma\) given by \(0.2, 0.4, 0.8, 1, 1.3, 1.8, 2\), plot the densities of \(N(\mu, \sigma)\) in the same plot. It might help if (1) you have the densities of \(N(\mu = 5, \sigma = 0.2)\) and \(N(\mu = 5, \sigma = 2)\) to be blue in color and the rest to be red. (2) choose appropriate limits for the x-axis (use x_lim parameter in the plot funtion) and y-axis (use y_lim).
plot_normal <- function(mu, sigma){
x <- 0:n
normal <- dnorm(x,mean=mu, sd=sigma,log=FALSE)
plot(x, normal, type = "b", lwd = 3,
main = paste("Fixed mean,5"),
xlim = c(0,15), ylim= c(0,1.5),
xlab = "x", ylab = "P(X=x)")
}
x<-seq(-1,15, by= .10)
plot(x,dnorm(x,5,.2),
col="blue")
lines(x,dnorm(x,5,2.0),
col="blue")
lines(x,dnorm(x,5,.4))
lines(x,dnorm(x,5,.8))
lines(x,dnorm(x,5,1.0))
lines(x,dnorm(x,5,1.3))
lines(x,dnorm(x,5,1.8))
#plot(x,dnorm(x,5,.2),
#col="blue")
#plot(x,dnorm(x,5,2.0),
#col="blue")
#plot(x,dnorm(x,5,.4))
#plot(x,dnorm(x,5,.8))
#plot(x,dnorm(x,5,1.0))
#plot(x,dnorm(x,5,1.3))
#plot(x,dnorm(x,5,1.8))
What do you notice about the plot? Comment about how the width changes. The larger the sigma value is, the larger the width becomes showing that the data around the mean is more spread out. ## Varying mean, fixed standard deviation Set \(\sigma = 0.4\). For values of \(\mu\) given by \(-1, -0.5, 0, 0.4, 0.9, 2.5, 4\) plot the densities of \(N(\mu, \sigma)\) in the same plot. You might need to choose appropriate limits for the x-axis.
#x<-seq(-1,25, by= .10)
#plot.new
#window(plot(x,dnorm(x,-1,.4),
#col="blue"),
#lines(x,dnorm(x,-.5,.4),
#col="blue"),
#lines(x,dnorm(x,0,.4)),
#lines(x,dnorm(x,.4,.4)),
#lines(x,dnorm(x,.9,.4)),
#lines(x,dnorm(x,2.5,.4)),
#lines(x,dnorm(x,4,.4)))
#par(mfrow = c(4,2))
x<-seq(-1,25, by=.10)
plot(x,dnorm(x,-1,.4),
col="blue")
plot(x,dnorm(x,-.5,.4),
col="blue")
plot(x,dnorm(x,0,.4))
plot(x,dnorm(x,.4,.4))
plot(x,dnorm(x,.9,.4))
plot(x,dnorm(x,2.5,.4))
plot(x,dnorm(x,4,.4))
We will plot the Gamma distibution for different shapes and scales. You might need to adjust the limits of x and y axes appropriately.
Set \(\alpha = 1\), vary \(\beta\) over \(0.2, 0.6, 1, 1.5, 2\). Plot the densities of \(Gamma(\alpha, \beta)\) in a single plot.
plot_gamma <- function(alpha, beta){
x <- 0:n
gamma <- dgamma(x,shape = alpha, scale= 1/beta, log=FALSE)
plot(x, gamma, type = "h", lwd = 5,
main = paste("gamma distribution = ", alpha, "Beta= ", beta),
xlab = "x", ylab = "P(X=x)")
}
x<-seq(0,10, by=0.10)
windows(plot(x,dgamma(x,shape=1,rate= 0.2,log=FALSE),
col="red"),
lines(x,dgamma(x,shape=1, rate=0.6, log=FALSE),
col="blue"),
lines(x,dgamma(x,shape=1, rate=1, log=FALSE),
col="yellow"),
lines(x, dgamma(x,shape=1, rate= 1.5, log=FALSE),
col="green"),
lines(x, dgamma(x,shape=1, rate=2, log=FALSE)))
## Warning: 'length(width)' and 'length(height)' and 'length(pointsize)' differ between new and previous
## ==> NOT changing 'width' & 'height' & 'pointsize'
Set \(\alpha = 0.6\), vary \(\beta\) over \(0.2, 0.6, 1, 1.5, 2\). Plot the densities of \(Gamma(\alpha, \beta)\) in a single plot.
x<-seq(0,1, by=0.01)
plot(x,dgamma(x,shape=0.6,rate= 0.2,log=FALSE),
col="red")
lines(x,dgamma(x,shape=0.6, rate=0.6, log=FALSE),
col="blue")
lines(x,dgamma(x,shape=0.6, rate=1, log=FALSE),
col="yellow")
lines(x, dgamma(x,shape=0.6, rate= 1.5, log=FALSE),
col="green")
lines(x, dgamma(x,shape=0.6, rate=2, log=FALSE))
Set \(\alpha = 2\), vary \(\beta\) over \(0.2, 0.6, 1, 1.5, 2\). Plot the densities of \(Gamma(\alpha, \beta)\) in a single plot.
#alpha=2
x<-seq(0,50, by=.5)
plot(x,dgamma(x,shape=2,rate= 0.2,log=FALSE),
col="red")
lines(x,dgamma(x,shape=2, rate=0.6, log=FALSE),
col="blue")
lines(x,dgamma(x,shape=2, rate=1, log=FALSE),
col="yellow")
lines(x, dgamma(x,shape=2, rate= 1.5, log=FALSE),
col="green")
lines(x, dgamma(x,shape=2, rate=2, log=FALSE))
Set \(\alpha = 5\), vary \(\beta\) over \(0.2, 0.6, 1, 1.5, 2\). Plot the densities of \(Gamma(\alpha, \beta)\) in a single plot.
x<-seq(0,100, by= 1)
windows(
plot(x,dgamma(x,shape=5,rate= 0.2,log=FALSE),
col="red"),
lines(x,dgamma(x,shape=5, rate=0.6, log=FALSE),
col="blue"),
lines(x,dgamma(x,shape=5, rate=1, log=FALSE),
col="yellow"),
lines(x, dgamma(x,shape=5, rate= 1.5, log=FALSE),
col="green"),
lines(x, dgamma(x,shape=5, rate=2, log=FALSE)))
## Warning: 'length(width)' and 'length(height)' and 'length(pointsize)' differ between new and previous
## ==> NOT changing 'width' & 'height' & 'pointsize'
Plot the probability density function of the Beta distibution in a single plot (use different colors for different densities and have a legend), for each of the following choices of parameters (1) \(\alpha = 1\), \(\beta = 3\) (2) \(\alpha = 5\), \(\beta = 1\) (3) \(\alpha = 0.3\), \(\beta = 0.6\) (4) \(\alpha = 2\), \(\beta = 2\) (5) \(\alpha = 1\), \(\beta = 1\)
x<- seq(0,1, by = 0.01)
plot(x,dbeta(x,1,3),
col="blue")
lines(x,dbeta(x,5,1),
col="red")
lines(x,dbeta(x,2,2),
col="yellow")
lines(x,dbeta(x,1,1),
col="green")
lines(x,dbeta(x,0.3,0.6))
Legend: Blue line=\(\alpha = 1\), \(\beta = 3\) Red line= \(\alpha = 5\), \(\beta = 1\) Black line= \(\alpha = 0.3\), \(\beta = 0.6\) Yellow line=\(\alpha = 2\), \(\beta = 2\) Green Line= \(\alpha = 1\), \(\beta = 1\)