install.packages("data.tree")
suppressWarnings(suppressMessages(library(knitr)))

HW6 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.

Solution.
The probabilithy that get a red marble or blue marble from the box is:

54/(54+9+75)+75/(54+9+75) =  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.
Solution.
The probability that you end up with a red golf ball is:
20/(19+20+24+17) = 0.2500
  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.

Gender and Residence of Customers Males Females Apartment 81 228 Dorm 116 79 With Parent(s) 215 252 Sorority/Fraternity House 130 97 Other 129 72

v1 <- c("Apartment","Dorm","With Parent(s)","Sorority/Fraternity House","Other")
Males <- c(81,116,215,130,129)
Females <- c(228,79,252,97,72)
df <- data.frame(v1,Males,Females)
colnames(df) <- c("","Males","Females")

kable(df, format = "pandoc",full_width = F,caption = "Gender and Residence of Customers", position = "left")
Gender and Residence of Customers
Males Females
Apartment 81 228
Dorm 116 79
With Parent(s) 215 252
Sorority/Fraternity House 130 97
Other 129 72

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.

Solution.
total attended the surway:
81+116+215+130+129+228+79+252+97+72=1399

The probability that a customer is not male or does not live with parents is:

(228+79+252+97+72)/1399 + (215+252)/1399 = 0.8542

4.Determine if the following events are independent. Going to the gym. Losing weight.

Answer: A) Dependent B) Independent

B)

Discussion.

The events A and B are independent if any one of the following three equivalent conditions hold:

P(A ??? B) = P(A)P(B)
P(A|B) = P(A)       B has no effect on A
P(B|A) = P(B)       A has no effect on B

Let G denote Going to gym, the sample space of whether a person going to gym or not looks like this:
{Y,N}

Let L be the event that the person is losing weight or not. Then event L looks like this:
{y,n}

The sample space of the person going to gym and then losing weight looks like this:
{Yy, Yn,Ny, Nn}

The sample space of the person losing weight and then going to gym looks like this:
{yY, yN, nY, nN}

If each event has the same probability, then

P(G|L) = {yY,nN} = 2/4 = 1/2

P(L|G) = {Yy,Nn} = 2/4 = 1/2


If the probability of the person going to gym P(G) = 1/2 ,then P(G|L) = P(G). Then we know G and L are independent. If
P(G) does not equal to 1/2, then  P(G|L) does not equal to P(G) and G and L are dependent. Or, if each event has different  probability so P(G|L) does not equal to 1/2, then  P(G|L) does not equal to P(G) and G and L are dependent.

If the probability of the person losing weight P(L) = 1/2 ,then P(L|G) = P(L). Then we know G and L are independent. If
P(L) does not equal to 1/2, then  P(L|G) does not equal to P(L) and G and L are dependent. Or, if each event has different  probability so P(L|G) does not equal to 1/2, then  P(L|G) does not equal to P(L) and G and L are dependent.
  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?
# Solution
c8_3 <- factorial(8)/(factorial(5)*factorial(3))
c7_3 <- factorial(7)/(factorial(4)*factorial(3))
var <- c8_3 * c7_3 * 3
cat("Answer: There are",var,"veggie wraps can be made.")
## Answer: There are 5880 veggie wraps can be made.

6.Determine if the following events are independent. Jeff runs out of gas on the way to work. Liz watches the evening news.

Answer: A) Dependent B) Independent

Answer: B)

7.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?

#Solution
# The permutation of 14 people taking 8 at a time is:
p14_8 <- factorial(14)/factorial(6)
cat("Answer: There are",p14_8,"ways that the members of the
cabinet can be appointed.")
## Answer: There are 121080960 ways that the members of the
## cabinet can be appointed.

8.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.

#Solution
c4_1 <- factorial(4)/(factorial(3)*factorial(1))
c9_3 <- factorial(9)/(factorial(6)*factorial(3))

cat("the probability is ",c4_1*c9_3/((9+4+9)^4))
## the probability is  0.001434328

9.Evaluate the following expression. \(\frac{11!}{7!}\)

factorial(11)/factorial(7)
## [1] 7920

10.Describe the complement of the given event. 67% of subscribers to a fitness magazine are over the age of 34.

The complement of the event could be expressed as: 33% of the subscribers to a fitness magazine are not older than 34.
  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.

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

#Solution
#Random Number values are +97 and -30.
#Probabilities to get three head out of 4 tosses is 4C3/2^4 =1/4 and the complement is 1-1/4=3/4.
E <- round(((1/4)*97 - (3/4)*30),2)

print(paste0("I can expect to win $", E," each time I play the game."))
## [1] "I can expect to win $1.75 each time I play the game."
print(paste0("If I played this game 559 times, I expect to win $", E*559, "."))
## [1] "If I played this game 559 times, I 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.

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

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

#Solution
#Random Number values are +23 and -26.
#Probabilities to get 4 tails or less out of 9 tosses is (9C4+9C3+9C2+9C1)/2^9 and the complement is 1-9P4/2^9.

p <- (factorial(9)/(factorial(5)*factorial(4))+factorial(9)/(factorial(6)*factorial(3))+factorial(9)/(factorial(7)*factorial(2))+factorial(9)/(factorial(8)*factorial(1))+factorial(9)/(factorial(9)*factorial(0)))/(2^9)
E <- 23*p -26*(1-p)

print(paste0("I can expect to lose $", abs(E)," each time I play the game."))
## [1] "I can expect to lose $1.5 each time I play the game."
print(paste0("If I played this game 994 times, I expect to lose $", abs(E*994), "."))
## [1] "If I played this game 994 times, I expect to lose $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.
suppressWarnings(suppressMessages(library(data.tree)))

truth <- Node$new("True")
  True <- truth$AddChild("True")
  Lie <- truth$AddChild("Lie")
truth$probability <- '0.8'
True$probability <-  '0.8*0.9'
Lie$probability <- '0.8*(1-0.9)'
  
liar <- Node$new("Lie") 
  True <- liar$AddChild("True")
  Lie <- liar$AddChild("Lie")
truth$probability <- '0.2'
True$probability <-  '0.2*(1-0.59)'
Lie$probability <- '0.2*0.59'    

print(truth,"probability")
##   levelName probability
## 1  True             0.2
## 2   ¦--True     0.8*0.9
## 3   °--Lie  0.8*(1-0.9)
print(liar,"probability")
##   levelName  probability
## 1  Lie                  
## 2   ¦--True 0.2*(1-0.59)
## 3   °--Lie      0.2*0.59
  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.)
# Probability is P (is a ture liar|detected as a liar) 
# bayes theorem: P(A|B) = (P(B|A)P(A))/P(B)

#probability of detected as a liar: P(B)= 0.8*0.1+0.2*0.59 
#probability of actually a liar: P(A)=0.2
#probability of given actually a liar, detected as a liar: P(B|A)= 0.2*0.59

p_ab<-(0.2*0.59)*0.2/(0.8*0.1+0.2*0.59)
cat("P(A|B) =",p_ab)
## P(A|B) = 0.1191919
  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.)
# Probability is P (is a ture truth-teller|detected as a truth-teller) 
# bayes theorem: P(A|B) = (P(B|A)P(A))/P(B)

#probability of detected as a truth-teller: P(B)= 0.8*0.9+0.2*(1-0.59)
#probability of actually a truth-teller: P(A)=0.8
#probability of given actually a truth-teller, detected as a truth-teller: P(B|A)= 0.8*0.9

p_ab<-(0.8*0.9)*0.8/(0.8*0.9+0.2*(1-0.59))
cat("P(A|B) =",p_ab)
## P(A|B) = 0.7182045
  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.
# Probability is P (is a ture truth-teller|detected as a truth-teller) 

#probability of truth teller detected as a liar: P(B)= 0.8*(1-0.9)
#probability of liar detected as a liar: P(A)=0.2*0.59

p_aorb<- (0.8*(1-0.9))+(0.2*0.59)
cat("P(A|B) =",p_aorb)
## P(A|B) = 0.198