p.289

  1. Consider the following two experiments: the first has outcome X taking on the values 0, 1, and 2 with equal probabilities; the second results in an (independent) outcome Y taking on the value 3 with probability 1/4 and 4 with probability 3/4. Find the distribution of
  1. Y + X. (b) Y-X.

Distribution of X

library(ggplot2)
X <- data.frame(list(num=c(0,1,2)), stringsAsFactors=FALSE)
ggplot(data = X,aes(x=X$num))+geom_histogram(aes(x=X$num,y=..density..),binwidth=1, color='red')

Distribution of Y

Y <- data.frame(list(num=c(3,4,4,4)), stringsAsFactors=FALSE)
ggplot(data = Y,aes(x=Y$num))+geom_histogram(aes(x=Y$num,y=..density..),binwidth=1, color='blue')

Distribution of X+Y

Z <- data.frame()

for (i in X$num){
  for (j in Y$num){
    Z <- rbind(Z, i+j)
  }
}
Z
##    X3
## 1   3
## 2   4
## 3   4
## 4   4
## 5   4
## 6   5
## 7   5
## 8   5
## 9   5
## 10  6
## 11  6
## 12  6
ggplot(data = Z,aes(x=Z))+geom_histogram(aes(x=Z,y=..density..),binwidth=1, color='green')

Distribution of Y- X Distribution of X+Y

Z <- data.frame()
for (i in X$num){
  for (j in Y$num){
    Z <- rbind(Z, j-i)
  }
}
Z
##    X3
## 1   3
## 2   4
## 3   4
## 4   4
## 5   2
## 6   3
## 7   3
## 8   3
## 9   1
## 10  2
## 11  2
## 12  2
ggplot(data = Z,aes(x=Z))+geom_histogram(aes(x=Z,y=..density..),binwidth=1, color='yellow')