CUNY 605 Homework Assignment Week 6

  1. A box contains 54 red marbles, 9 white marbles, and 75 blue marbles. If a marble is randomly selected from the box, what is the probability that it is red or blue? Express your answer as a fraction or a decimal number rounded to four decimal places.
# Count the total marbles in the box
reds <- 54
whites <- 9
blues <- 75
total <- reds + whites + blues

# Probability that its red or blue
prob_red_or_blue <- (reds + blues) / total
print(paste0("Probability that a marble is red or blue: ", round(prob_red_or_blue, 4)))
## [1] "Probability that a marble is red or blue: 0.9348"
  1. You are going to play mini golf. A ball machine that contains 19 green golf balls, 20 red golf balls, 24 blue golf balls, and 17 yellow golf balls, randomly gives you your ball. What is the probability that you end up with a red golf ball? Express your answer as a simplified fraction or a decimal rounded to four decimal places.
# Calculate the total amount of balls
green <- 19
red <- 20
blue <- 24
yellow <- 17
totals <- green + red + blue + yellow

# Calculate the probaiblity that I will end up with a red golf ball
prob_red <- red / totals
print(paste0("Probability that I will get a red golf ball: ", round(prob_red,4)))
## [1] "Probability that I will get a red golf ball: 0.25"
  1. A pizza delivery company classifies its customers by gender and location of residence. The research department has gathered data from a random sample of 1399 customers. The data is summarized in the table below.

What is the probability that a customer is not male or does not live with parents? Write your answer as a fraction or a decimal number rounded to four decimal places.

The probability of union of two events is given by: \(P(A \cup B) = P(A) + P(B) - P(A \cap B)\)

# Find total amount
total2 <- 81 + 228 + 116 + 79 + 215 + 252 + 130 + 97 + 129 + 72

# Find probability that a customer is not male (or in other words, are female)
prob_not_male <- (228 + 79 + 252 + 97 + 72) / total2

# Find the probability that a customer does not live with parents
prob_not_parents <- (81 + 228 + 116 + 79 + 130 + 97 + 129 + 72) / total2

# Find the intersection between not_male and not_parents
prob_not_male_not_parents <- (228 + 79 + 97 + 72)/ total2

# Using the formula above to solve the problem
prob_final <- prob_not_male + prob_not_parents - prob_not_male_not_parents

print(paste0("Probability that a customer is not male or does not live with parents: ", round(prob_final, 4)))
## [1] "Probability that a customer is not male or does not live with parents: 0.8463"
  1. Determine if the following events are independent.

Going to the gym. Losing weight.

Answer: The events are likely dependent as going to the gym will likely cause you to loose weight.

  1. A veggie wrap at City Subs is composed of 3 different vegetables and 3 different condiments wrapped up in a tortilla. If there are 8 vegetables, 7 condiments, and 3 types of tortilla available, how many different veggie wraps can be made?
# Given that we can only use 3 different vegetables and 3 different condiments and 1 tortilla, we'll need to find all the different amount of combinations
veg_combo <- choose(8, 3)
condiment_combo <- choose(7, 3)
tortilla_combo <- choose(3, 1)

# How many different vegie wraps can be made?
wrap_combo <- veg_combo * condiment_combo * tortilla_combo

print(paste0("How many different veggie wraps can be made? ", wrap_combo))
## [1] "How many different veggie wraps can be made? 5880"
  1. Determine if the following events are independent.

Jeff runs out of gas on the way to work. Liz watches the evening news.

Answer: These are seemingly independent events as Liz watching the news is very unlikely to influence Jeff’s gas tank on his way to work.

  1. The newly elected president needs to decide the remaining 8 spots available in the cabinet he/she is appointing. If there are 14 eligible candidates for these positions (where rank matters), how many different ways can the members of the cabinet be appointed?
# Given that rank matters, we will be using permutation
candidates <- factorial(14)/(factorial(14 - 8))
print(paste0("How many different ways a candidate can be appointed: ", candidates))
## [1] "How many different ways a candidate can be appointed: 121080960"
  1. A bag contains 9 red, 4 orange, and 9 green jellybeans. What is the probability of reaching into the bag and randomly withdrawing 4 jellybeans such that the number of red ones is 0, the number of orange ones is 1, and the number of green ones is 3? Write your answer as a fraction or a decimal number rounded to four decimal places.

Assume that choosing the jellybeans are independent events.

red <- 9
orange <- 4
green <- 9
total_beans <- red + orange + green

# How many different combinations exist for drawing random 4 jellybeans from the bag
# Using the choose function
jellybean_combo <- choose(total_beans, 4)

# How many ways can we get 0 reds, 1 orange and 3 greens
red_combo <- choose(red, 0)
orange_combo <- choose(orange, 1)
green_combo <- choose(green, 3)

# Calculate the probability
jellybean <- (red_combo * orange_combo * green_combo) / jellybean_combo

print(paste0("Probability drawing 4 jellybeans such that there are 0 reds, 1 orange, and 3 greens: ", round(jellybean, 4)))
## [1] "Probability drawing 4 jellybeans such that there are 0 reds, 1 orange, and 3 greens: 0.0459"
  1. Evaluate the following expression.

\[11!/7!\]

Let’s simplify this factorial down:

\[ 11! = 11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 \] \[ 7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 \] \(11!/7!\) can be reduced into \(11 * 10 * 9 * 8\) which equals to \(7920\).

# Let's double check our answer.
answer <- factorial(11)/factorial(7)
print(paste0("11!/7! = ", answer))
## [1] "11!/7! = 7920"
  1. Describe the complement of the given event.

67% of subscribers to a fitness magazine are over the age of 34.

Complement of the event: 33% of subscribers to a fitness matazines are 34 years of age or younger.

  1. If you throw exactly three heads in four tosses of a coin you win $97. If not, you pay me $30.

Step 1. Find the expected value of the proposition. Round your answer to two decimal places.

# First find the probability distribution with four tosses.
total_tosses <- 2^4
no_head <- choose(4, 0)/total_tosses
one_head <- choose(4,1)/total_tosses
two_head <- choose(4,2)/total_tosses
three_head <- choose(4, 3)/total_tosses
four_head <- choose(4, 4)/total_tosses

head_prob <- c(no_head, one_head, two_head, three_head, four_head)
head_num <- 0:4

head_df <- data.frame(head_num, head_prob)

# Plot the probability distribution with the four tosses with a histogram
plot(head_df,
     main = "Probability Distribution with Four Tosses (Heads)",
     type = "l",
     col = "blue")

# Calculate the expected value
# If I throw exactly 3 heads, I win $97
# Anything else, I lose $30
money <- c(-30,-30,-30,97, -30)

exp_value <- sum(money * head_df[,2])
print(paste0("Expected Value: ", exp_value))
## [1] "Expected Value: 1.75"

So overall, if I play this game, I would win $1.75 in the long term.

Step 2. If you played this game 559 times how much would you expect to win or lose? (Losses must be entered as negative.)

exp_win <- 559 * 1.75
print(paste0("On average, I would expect to win: $", exp_win))
## [1] "On average, I would expect to win: $978.25"
  1. Flip a coin 9 times. If you get 4 tails or less, I will pay you $23. Otherwise you pay me $26.

There are \(2^9\) different possibilities. To get the probability of flipping 4 tails or less, we need to take the sum of all probabilities when tails = 4, tails = 3, tails = 2 … tails = 0. To obtain each probability for each scenario, we need to use the combination formula, which is \((n!) / (k! * (n-k)!)\).

Therefore, for example, if tails is 4. There will be \((9!)/(4! * 5!) = 126\). Therefore, there is a probability of 165/(2^11). Likewise, we would peform this for tails = 3, tails = 2, etc. and take the sum.

Step 1. Find the expected value of the proposition. Round your answer to two decimal places.

n <- 9
k <- 4
temp <- 0

while (k >= 0){
  temp <- temp + choose(n,k)
  k <- k - 1
}

# Take the sum of all combinations and divide it by all possible combinations 2^9
total_prob_four_tails_or_less <- temp/(2^9)
print(paste0("Probability of 4 tails or less with 9 tosses: ", total_prob_four_tails_or_less))
## [1] "Probability of 4 tails or less with 9 tosses: 0.5"
# Another way to show this is through the pbinom function
Winning <- pbinom(4, size=9, prob=1/2)
print(paste0(Winning))
## [1] "0.5"
# Now to calculate the expected value(gain) from playing this game.
# Again, 4 tails or less, I win $23, otherwise, I lose $26
exp_wins <- 23*total_prob_four_tails_or_less + -26*(1 - total_prob_four_tails_or_less)
print(paste0("On average, expected value: ", exp_wins))
## [1] "On average, expected value: -1.5"

Step 2. If you played this game 994 times how much would you expect to win or lose? (Losses must be entered as negative.)

# If played 994 times
win_or_lose <- 994 * exp_wins
print(paste0("After playing 994 games, I would expect to win: $", win_or_lose))
## [1] "After playing 994 games, I would expect to win: $-1491"
  1. The sensitivity and specificity of the polygraph has been a subject of study and debate for years. A 2001 study of the use of polygraph for screening purposes suggested that the probability of detecting a liar was .59 (sensitivity) and that the probability of detecting a “truth teller” was .90 (specificity). We estimate that about 20% of individuals selected for the screening polygraph will lie.
  1. What is the probability that an individual is actually a liar given that the polygraph detected him/her as such? (Show me the table or the formulaic solution or both.)
# Let's assume that we have 10000 subjects
subjects <- 10000

# From prior testing, 20% of individuals will lie on the screening polygraph
liars <- subjects * .20
truth_tellers <- subjects * .80

# If the sujbect is a liar, how many will the test pick up? and how many will it NOT pick up?
# And remember, sensitivity detects the probability of a liar, or in other words, the test will be positive
liars_pos_test <- 0.59 * liars
liars_neg_test <- liars - liars_pos_test

# And likewise, specificity is the probability of detecting a "truth teller" if he/she was indeed telling the truth, or in other words, the test will be negative.
truth_neg_test <- 0.90 * truth_tellers
truth_pos_test <- truth_tellers - truth_neg_test

# P(liars | POSITIVE TEST)?
prob_liars_pos <- liars_pos_test / (liars_pos_test + truth_pos_test)
print(paste0("Given that the polygraph detects as a liar (POSITIVE TEST), what is the probability that an individiual is actually a liar? ", round(prob_liars_pos, 4) * 100, "%"))
## [1] "Given that the polygraph detects as a liar (POSITIVE TEST), what is the probability that an individiual is actually a liar? 59.6%"
  1. What is the probability that an individual is actually a truth-teller given that the polygraph detected him/her as such? (Show me the table or the formulaic solution or both.)
# P(truth_teller | NEGATIVE TEST)
prob_truth_neg <- truth_neg_test / (truth_neg_test + liars_neg_test)
print(paste0("Given that the polygraph is NEGATIVE, what is the probability that an individiual is actually a truth teller? ", round(prob_truth_neg, 4) * 100, "%"))
## [1] "Given that the polygraph is NEGATIVE, what is the probability that an individiual is actually a truth teller? 89.78%"
  1. What is the probability that a randomly selected individual is either a liar or was identified as a liar by the polygraph? Be sure to write the probability statement.

Recalling the formula above: \(P(A \cup B) = P(A) + P(B) - P(A \cap B)\)

# P(liar or identified_as_liar)
prob_liar_or_POS <- liars/subjects + (liars_pos_test + truth_pos_test)/subjects - (liars_pos_test) / subjects
print(paste0("What is the probability that a randomly selected individual is either a liar or was identified as a liar by the polygraph? ", round(prob_liar_or_POS, 4) * 100, "%"))
## [1] "What is the probability that a randomly selected individual is either a liar or was identified as a liar by the polygraph? 28%"

\[P(liar \cup POS) = P(liar) + P(POS) - P(liar \cap POS) = .28\]