2.6 If you roll a pair of fair dice, what is the probability of
(a) getting a sum of 1?
(b) getting a sum of 5?
(c) getting a sum of 12?

#a getting a sum of 1 is zero.  

#b getting a sum of 5 is ((1,4). (2,3), (3,2), (4,1)) i.e. 
4*((1/6)^2) 
## [1] 0.1111111
#c getting a sum of 12 is (6,6) i.e. 
(1/6)^2
## [1] 0.02777778

2.8 The American Community Survey is an ongoing survey that provides data every year to give communities the current information they need to plan investments and services. The 2010 American Community Survey estimates that 14.6% of Americans live below the poverty line, 20.7% speak a language other than English (foreign language) at home, and 4.2% fall into both categories.
(a) Are living below the poverty line and speaking a foreign language at home disjoint?
(b) Draw a Venn diagram summarizing the variables and their associated probabilities.
(c) What percent of Americans live below the poverty line and only speak English at home?
(d) What percent of Americans live below the poverty line or speak a foreign language at home?
(e) What percent of Americans live above the poverty line and only speak English at home?
(f) Is the event that someone lives below the poverty line independent of the event that the person speaks a foreign language at home?

#a No, living below the poverty line and speaking a foreign launguage at home are not disjoint outcomes. They can both occur at the same time. 

#b
library(VennDiagram)
grid.newpage()
draw.pairwise.venn(14.6, 20.7, 4.2, 
                   category = c("Below Poverty Line", "Foreign Language Speaker"),
                   lty = rep("blank", 2), 
                   fill = c("light blue", "pink"), 
                   alpha = rep(0.5, 2),
                   cat.pos = c(0, 0), 
                   cat.dist = rep(0.025, 2)) 

## (polygon[GRID.polygon.1], polygon[GRID.polygon.2], polygon[GRID.polygon.3], polygon[GRID.polygon.4], text[GRID.text.5], text[GRID.text.6], text[GRID.text.7], text[GRID.text.8], text[GRID.text.9])
#c The percent of Americans living below the poverty line and only speaking English at home is 10.4%.
poverty_line <- 14.6
poverty_foreign <- 4.2
poverty_line - poverty_foreign 
## [1] 10.4
#d The percent of Americans living below the poverty line or speaking a foreign language at home is 31.1%.
foreign_language <- 20.7
(poverty_line + foreign_language) - poverty_foreign # P(A or B) = P(A) + P(B) - P(A and B) 
## [1] 31.1
#e The percent of Americans living above the poverty line and only speak English at home is 
((100 - poverty_line) - foreign_language) + poverty_foreign 
## [1] 68.9
#f Yes, the event that someone lives below poverty line is independent of the event that the person speaks a foreign language at home because knowing the American poverty line of a household provides no useful information about the outcome of the foreign language speaker in an American household. 

2.20 Assortative mating is a nonrandom mating pattern where individuals with similar genotypes and/or phenotypes mate with one another more frequently than what would be expected under a random mating pattern. Researchers studying this topic collected data on eye colors of 204 Scandinavian men and their female partners. The table below summarizes the results. For simplicity, we only include heterosexual relationships in this exercise.
(a) What is the probability that a randomly chosen male respondent or his partner has blue eyes?
(b) What is the probability that a randomly chosen male respondent with blue eyes has a partner with blue eyes?
(c) What is the probability that a randomly chosen male respondent with brown eyes has a partner with blue eyes? What about the probability of a randomly chosen male respondent with green eyes having a partner with blue eyes?
(d) Does it appear that the eye colors of male respondents and their partners are independent? Explain your reasoning.

#a The probability that a randomly chosen male respondent or his partner has blue eyes is 71%.
total <- 204
male_blue <- 114
female_blue <- 108
mf_blue <- 78
(male_blue + female_blue - mf_blue) / total
## [1] 0.7058824
#b The probability that a randomly chosen male respondent with blue eyes has a partner with blue eyes is 68%. 
mf_blue / male_blue 
## [1] 0.6842105
#c The probability that a randomly chosen male respondent with brown eyes has a partner with blue eyes is 35%.
mf_brown_blue <- 19
male_brown <- 54
mf_brown_blue / male_brown 
## [1] 0.3518519
# The probability of a randomly chosen male respondent with green eyes having a partner with blue eyes is 31%.
mf_green_blue <- 11
male_green <- 36
mf_green_blue / male_green 
## [1] 0.3055556
#d No, it does not appear that the eye colors of male respondents and their partners are independent because P(male | female) is not equal to P(male), hence male and female are not independent.
mf_blue / total == male_blue / total
## [1] FALSE

2.30 The table below shows the distribution of books on a bookcase based on whether they are nonfiction or fiction and hardcover or paperback.
(a) Find the probability of drawing a hardcover book first then a paperback fiction book second when drawing without replacement.
(b) Determine the probability of drawing a fiction book first and then a hardcover book second, when drawing without replacement.
(c) Calculate the probability of the scenario in part (b), except this time complete the calculations under the scenario where the first book is placed back on the bookcase before randomly drawing the second book.
(d) The final answers to parts (b) and (c) are very similar. Explain why this is the case.

#a The probability of drawing a hardcover book first then a paperback fiction book second when drawing without replacement is 18%. 
hardcover <- 28
paperback_f <- 59
total <- 95
(hardcover / total) * (paperback_f / (total-1)) 
## [1] 0.1849944
#b The probability of drawing a fiction book first and then a hardcover book second, when drawing without replacement is 23%. 
fiction <- 72
(fiction / total) * ((hardcover) / (total-1)) 
## [1] 0.2257559
#c The probability of drawing a fiction book first and then a hardcover book second, when drawing with replacement and randomly is 22%. 
(fiction / total) * (hardcover / total)
## [1] 0.2233795
#d The final answer to parts (b) and (c) are very similar because drawing with replacement only amounted to a very little change due to the sample size. 

2.38 An airline charges the following baggage fees: $25 for the first bag and $35 for the second. Suppose 54% of passengers have no checked luggage, 34% have one piece of checked luggage and 12% have two pieces. We suppose a negligible portion of people check more than two bags.
(a) Build a probability model, compute the average revenue per passenger, and compute the corresponding standard deviation.
(b) About how much revenue should the airline expect for a flight of 120 passengers? With what standard deviation? Note any assumptions you make and if you think they are justified.

#a The average revenue per passenger is $15.7 and the corresponding standard deviation is $19.95.
passenger <- c(0.54, 0.34, 0.12)
bag <- c(0, 1, 2)
bag_fee <- c(0, 25, 60)
airline_bag_fee <- data.frame(passenger, bag, bag_fee)
expected_value <- sum(airline_bag_fee$bag_fee * airline_bag_fee$passenger)
expected_value # average revenue per passenger
## [1] 15.7
var <- sum(((airline_bag_fee$bag_fee - expected_value)^2) * airline_bag_fee$passenger) # variance of the revenue per passenger
sqrt(var) # standard deviation of the revenue per passenger 
## [1] 19.95019
#b The revenue the airline should expect for a flight of 120 passengers is $1,884. The standard deviation is $1,515.
new_passenger <- 120
new_expected_value <- expected_value * new_passenger
new_expected_value
## [1] 1884
new_var <- new_passenger * var
sqrt(new_var)
## [1] 218.5434

2.44 The relative frequency table below displays the distribution of annual total personal income (in 2009 inflation-adjusted dollars) for a representative sample of 96,420,486 Americans. These data come from the American Community Survey for 2005-2009. This sample is comprised of 59% males and 41% females.
(a) Describe the distribution of total personal income.
(b) What is the probability that a randomly chosen US resident makes less than $50,000 per year?
(c) What is the probability that a randomly chosen US resident makes less than $50,000 per year and is female? Note any assumptions you make.
(d) The same data source indicates that 71.8% of females make less than $50,000 per year. Use this value to determine whether or not the assumption you made in part (c) is valid.

#a The distriubtion of total personal income is an unimodal distribution with likely right skew. The mean is larger than median due to the skew.   
library(ggplot2)
income <- c("1 to $9999 or loss",
                  "10,000 to 14,999", "15, 000 to 24,999",
                  "25,000 to 34,999", "35,000 to 49,999", 
                  "50,000 to 64,999", "65,000 to 74,999",
                  "75,000 to 99,999", "100,000 or more")
total <- c(.022, .047, 0.158, 0.183, 0.212, 0.139, 0.058, 0.084, 0.097)
total_income <- data.frame(income, total)
total_income$income <- as.factor(total_income$income)
ggplot(total_income, aes(income, total)) +
  geom_bar(stat = "identity") +
  theme(axis.text.x=element_text(angle=45, hjust=1))  

#b The probability that a randomly chosen US resident makes less than $50,000 per year is 62.2%
sum(total_income$total[1:5]) * 100
## [1] 62.2
#c The probability that a randomly chosen US resident makes less than $50,000 per year and is female is 25.5%.
(sum(total_income$total[1:5]) * 100) * 0.41
## [1] 25.502
#d The same data source indicates that 71.8% of females make less than $50,000 per year. Using this value, the new probablity that a randomly chosen US resident makes less than $50,000 per year and is female is 29.44%. The assumption was not valid as it could be due to race or other outside factors.  
71.8 * 0.41
## [1] 29.438