Week 2, HW 2

Q1 What is the probability of rolling a sum of 12 on three rolls of six-sided dice?

sides <- 6
sum1 <- 12

all_prob <- expand.grid(rep(list(1:sides), 3))

rowSums(all_prob)
##   [1]  3  4  5  6  7  8  4  5  6  7  8  9  5  6  7  8  9 10  6  7  8  9 10 11  7
##  [26]  8  9 10 11 12  8  9 10 11 12 13  4  5  6  7  8  9  5  6  7  8  9 10  6  7
##  [51]  8  9 10 11  7  8  9 10 11 12  8  9 10 11 12 13  9 10 11 12 13 14  5  6  7
##  [76]  8  9 10  6  7  8  9 10 11  7  8  9 10 11 12  8  9 10 11 12 13  9 10 11 12
## [101] 13 14 10 11 12 13 14 15  6  7  8  9 10 11  7  8  9 10 11 12  8  9 10 11 12
## [126] 13  9 10 11 12 13 14 10 11 12 13 14 15 11 12 13 14 15 16  7  8  9 10 11 12
## [151]  8  9 10 11 12 13  9 10 11 12 13 14 10 11 12 13 14 15 11 12 13 14 15 16 12
## [176] 13 14 15 16 17  8  9 10 11 12 13  9 10 11 12 13 14 10 11 12 13 14 15 11 12
## [201] 13 14 15 16 12 13 14 15 16 17 13 14 15 16 17 18
sum(rowSums(all_prob) == 12)
## [1] 25
length(rowSums(all_prob))
## [1] 216
finalprob <- (25/216)
print(finalprob, digits = 4)
## [1] 0.1157

Q2 A newspaper company classifies its customers by gender and location of residence. The research department has gathered data from a random sample of customers. What is the probability that a customer is male and lives in ‘Other’ or is female and lives in ‘Other’?

data <- data.frame(males = c(200, 200, 100, 200, 200),
                   females = c(300, 100, 200, 100, 100)
                   )
data
##   males females
## 1   200     300
## 2   200     100
## 3   100     200
## 4   200     100
## 5   200     100
colsums <- colSums(data)
total_residence <- 900 + 800
total_residence
## [1] 1700
rowSums(data)
## [1] 500 300 300 300 300
other_total <- 300
prob_mf_other <- other_total / total_residence 
print(prob_mf_other, digits = 4)
## [1] 0.1765

Q3 Two cards are drawn without replacement from a standard deck of 52 playing cards. What is the probability of choosing a diamond for the second card drawn, if the first card, drawn without replacement, was a diamond?

possible_events_cards <- 52
possible_events_diamonds <- 13
possible_events_2nd_draw_cards <- 51
possible_events_2nd_draw_diamonds <- 12
prob_diamonds_2nd_draw <- possible_events_2nd_draw_diamonds / possible_events_2nd_draw_cards
print(prob_diamonds_2nd_draw, digit = 4)
## [1] 0.2353

Q4 A coordinator will select 10 songs from a list of 20 songs to compose an event’s musical entertainment lineup. How many different lineups are possible?

perm_without_replacement <- function(n,r){
  return(factorial(n)/factorial(n-r))
}
perm_without_replacement(20,10)
## [1] 670442572800

Q5 You are ordering a new home theater system that consists of a TV, surround sound system, and DVD player. You can choose from 20 different TVs, 20 types of surround sound systems, and 18 types of DVD players. How many different home theater systems can you build?

comb_with_replacement <- function(n, r){
  return(factorial(n + r - 1) / (factorial(r) * factorial(n-1)))
}
comb_with_replacement(58, 3)
## [1] 34220

Q6 A doctor visits her patients during morning rounds. In how many ways can the doctor visit 10 patients during the morning rounds?

factorial(10)
## [1] 3628800

Q7 If a coin is tossed 7 times, and then a standard six-sided die is rolled 3 times, and finally a group of four cards are drawn from a standard deck of 52 cards without replacement, how many different outcomes are possible?

cointoss <- 2^7
diceroll <- 6^3
cardsdrawn <- choose(n=52,k=4)
total_outcomes <- cointoss*diceroll*cardsdrawn
print(total_outcomes)
## [1] 7485004800

Q8 In how many ways may a party of four women and four men be seated at a round table if the women and men are to occupy alternate seats?

combo_men <- factorial(4)
combo_women <- factorial(4)
combo_pairs_mw <- factorial (4)
num_ways_arrangement <- combo_men * combo_women * combo_pairs_mw
print(num_ways_arrangement)
## [1] 13824

Q9 An opioid urinalysis test is 95% sensitive for a 30-day period, meaning that if a person has actually used opioids within 30 days, they will test positive 95% of the time P( + | User) =.95. The same test is 99% specific, meaning that if they did not use opioids within 30 days, they will test negative P( - | Not User) = .99. Assume that 3% of the population are users. Then what is the probability that a person who tests positive is actually a user P(User | +)?

\[ P(user \cap positive) = {P(positive \mid user) \times P(user)} \]

p_user <- .03
p_posGivenuser <- .95
p_user_and_pos <- p_user * p_posGivenuser
p_user_and_pos
## [1] 0.0285

\[ P(positive) = {P(positive \cap user) + P(positive \cap nonuser)} \]

p_nonuser <- .97
p_posGivenNonuser <- 1 - .99
p_pos_and_nonuser <- p_nonuser * p_posGivenNonuser
p_pos <- p_user_and_pos + p_pos_and_nonuser
p_pos
## [1] 0.0382

\[ P(user \mid positive) = \frac{P(positive \mid user) \times P(user)}{P(positive)} \]

p_userGivenpos <- p_user_and_pos / p_pos
p_userGivenpos
## [1] 0.7460733

Q10 You have a hat in which there are three pancakes. One is golden on both sides, one is brown on both sides, and one is golden on one side and brown on the other. You withdraw one pancake and see that one side is brown. What is the probability that the other side is brown?

\[ P(other side unseen brown \mid pancake drawn is brown) = \frac{P(other side unseen brown \cap pancake drawn is brown)}{P(pancake drawn is brown)} \]

We need to find the probability that the other unseen side is brown, given that the pancake we already drew is brown. We do this by finding the probability the other side is brown and the initially drawn pancake’s side is brown. Because at least one side has to be brown, we can ignore the pancake with two golden sides. The probability the other side is brown and the initially drawn pancake’s side is brown is 1/3, because we know one side is already brown, so there is a 1/3 chance for choosing the remaining pancake. There are two pancakes with at least one brown side, so the probability of drawing a pancake with a brown side is 2/3.

p_otherside_brown_givenfirstside_brown <- (1/3) / (2/3)
p_otherside_brown_givenfirstside_brown
## [1] 0.5