#Professor wants 4 digit answers.

options(digits = 4)

Question 1

What is the probability of rolling a sum of 12 on three rolls of six-sided dice?
Express your answer as a decimal number only. Show your R code.

#creating vectors representing sample spaces for the indidual die
roll1 <- c(1,2,3,4,5,6) 
roll2 <- c(1,2,3,4,5,6)
roll3 <- c(1,2,3,4,5,6)

#Creating a data frame with all possible outcomes assuming independence- using the multiplication rule of countng 
expanded_grid <- expand.grid(roll1, roll2, roll3) 
expanded_grid
##     Var1 Var2 Var3
## 1      1    1    1
## 2      2    1    1
## 3      3    1    1
## 4      4    1    1
## 5      5    1    1
## 6      6    1    1
## 7      1    2    1
## 8      2    2    1
## 9      3    2    1
## 10     4    2    1
## 11     5    2    1
## 12     6    2    1
## 13     1    3    1
## 14     2    3    1
## 15     3    3    1
## 16     4    3    1
## 17     5    3    1
## 18     6    3    1
## 19     1    4    1
## 20     2    4    1
## 21     3    4    1
## 22     4    4    1
## 23     5    4    1
## 24     6    4    1
## 25     1    5    1
## 26     2    5    1
## 27     3    5    1
## 28     4    5    1
## 29     5    5    1
## 30     6    5    1
## 31     1    6    1
## 32     2    6    1
## 33     3    6    1
## 34     4    6    1
## 35     5    6    1
## 36     6    6    1
## 37     1    1    2
## 38     2    1    2
## 39     3    1    2
## 40     4    1    2
## 41     5    1    2
## 42     6    1    2
## 43     1    2    2
## 44     2    2    2
## 45     3    2    2
## 46     4    2    2
## 47     5    2    2
## 48     6    2    2
## 49     1    3    2
## 50     2    3    2
## 51     3    3    2
## 52     4    3    2
## 53     5    3    2
## 54     6    3    2
## 55     1    4    2
## 56     2    4    2
## 57     3    4    2
## 58     4    4    2
## 59     5    4    2
## 60     6    4    2
## 61     1    5    2
## 62     2    5    2
## 63     3    5    2
## 64     4    5    2
## 65     5    5    2
## 66     6    5    2
## 67     1    6    2
## 68     2    6    2
## 69     3    6    2
## 70     4    6    2
## 71     5    6    2
## 72     6    6    2
## 73     1    1    3
## 74     2    1    3
## 75     3    1    3
## 76     4    1    3
## 77     5    1    3
## 78     6    1    3
## 79     1    2    3
## 80     2    2    3
## 81     3    2    3
## 82     4    2    3
## 83     5    2    3
## 84     6    2    3
## 85     1    3    3
## 86     2    3    3
## 87     3    3    3
## 88     4    3    3
## 89     5    3    3
## 90     6    3    3
## 91     1    4    3
## 92     2    4    3
## 93     3    4    3
## 94     4    4    3
## 95     5    4    3
## 96     6    4    3
## 97     1    5    3
## 98     2    5    3
## 99     3    5    3
## 100    4    5    3
## 101    5    5    3
## 102    6    5    3
## 103    1    6    3
## 104    2    6    3
## 105    3    6    3
## 106    4    6    3
## 107    5    6    3
## 108    6    6    3
## 109    1    1    4
## 110    2    1    4
## 111    3    1    4
## 112    4    1    4
## 113    5    1    4
## 114    6    1    4
## 115    1    2    4
## 116    2    2    4
## 117    3    2    4
## 118    4    2    4
## 119    5    2    4
## 120    6    2    4
## 121    1    3    4
## 122    2    3    4
## 123    3    3    4
## 124    4    3    4
## 125    5    3    4
## 126    6    3    4
## 127    1    4    4
## 128    2    4    4
## 129    3    4    4
## 130    4    4    4
## 131    5    4    4
## 132    6    4    4
## 133    1    5    4
## 134    2    5    4
## 135    3    5    4
## 136    4    5    4
## 137    5    5    4
## 138    6    5    4
## 139    1    6    4
## 140    2    6    4
## 141    3    6    4
## 142    4    6    4
## 143    5    6    4
## 144    6    6    4
## 145    1    1    5
## 146    2    1    5
## 147    3    1    5
## 148    4    1    5
## 149    5    1    5
## 150    6    1    5
## 151    1    2    5
## 152    2    2    5
## 153    3    2    5
## 154    4    2    5
## 155    5    2    5
## 156    6    2    5
## 157    1    3    5
## 158    2    3    5
## 159    3    3    5
## 160    4    3    5
## 161    5    3    5
## 162    6    3    5
## 163    1    4    5
## 164    2    4    5
## 165    3    4    5
## 166    4    4    5
## 167    5    4    5
## 168    6    4    5
## 169    1    5    5
## 170    2    5    5
## 171    3    5    5
## 172    4    5    5
## 173    5    5    5
## 174    6    5    5
## 175    1    6    5
## 176    2    6    5
## 177    3    6    5
## 178    4    6    5
## 179    5    6    5
## 180    6    6    5
## 181    1    1    6
## 182    2    1    6
## 183    3    1    6
## 184    4    1    6
## 185    5    1    6
## 186    6    1    6
## 187    1    2    6
## 188    2    2    6
## 189    3    2    6
## 190    4    2    6
## 191    5    2    6
## 192    6    2    6
## 193    1    3    6
## 194    2    3    6
## 195    3    3    6
## 196    4    3    6
## 197    5    3    6
## 198    6    3    6
## 199    1    4    6
## 200    2    4    6
## 201    3    4    6
## 202    4    4    6
## 203    5    4    6
## 204    6    4    6
## 205    1    5    6
## 206    2    5    6
## 207    3    5    6
## 208    4    5    6
## 209    5    5    6
## 210    6    5    6
## 211    1    6    6
## 212    2    6    6
## 213    3    6    6
## 214    4    6    6
## 215    5    6    6
## 216    6    6    6
Summed_Rows <- rowSums(expanded_grid) #creating a vector of all the sums of each individual row in our expanded grid. In this case, each outcome.

Summed_Rows 
##   [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
counter <- 0 #assinging 0 to a variiable called counter that will count how many times 12 shows up in out row sums in the for loop below.

#this for-loop is telling R to go through all the summed rows and if the sum is equal to 12, add 1 to the counter.
for (i in Summed_Rows) { 
  if (i == 12){
    counter <- counter + 1}
  else {counter <- counter + 0
  }
}
print(counter)
## [1] 25
x <- length(Summed_Rows) #calculating the length of summed rows aka how many combinations do we have

print(x)
## [1] 216
counter/x #this gives the probability of rolling a sum of 12 on three rolls of a six-sided dice
## [1] 0.1157

Rounding to 4 digits we get a .1157 probability or 11.57% chance of rolling a 12 with 3 dice.

Question 2.

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

Express your answer as a decimal number only. Show your R code.


Hint: create the matrix above in R, use rowSums and col.Sums to generate marginal
probabilities.

#create vectors for the varibales
Residence <- c("Apartment", "Dorm", "With Parent(s)", "Sorority/Fraternity House", "Other")

Males <- c(200, 200, 100, 200, 200)

Females <- c(300, 100, 200, 100, 100)

Customer_Matrix <- matrix(data = c(Residence, Males, Females), nrow = 5, ncol = 3) 

colnames(Customer_Matrix) <- c("Residence", "Males", "Females")



print(Customer_Matrix)
##      Residence                   Males Females
## [1,] "Apartment"                 "200" "300"  
## [2,] "Dorm"                      "200" "100"  
## [3,] "With Parent(s)"            "100" "200"  
## [4,] "Sorority/Fraternity House" "200" "100"  
## [5,] "Other"                     "200" "100"
#Get the marginal probabilities/sum the rows and columns

Numeric_Matrix <- matrix(data = c(Males, Females), ncol = 2)
colnames(Numeric_Matrix) <- c("Males", "Females")

colSums(Numeric_Matrix)
##   Males Females 
##     900     800
rowSums(Numeric_Matrix)
## [1] 500 300 300 300 300
#Try to create a table proportions
Proportion_Matrix <- matrix(data = c(Males/1700, Females/1700), ncol =2)
Proportion_Matrix
##         [,1]    [,2]
## [1,] 0.11765 0.17647
## [2,] 0.11765 0.05882
## [3,] 0.05882 0.11765
## [4,] 0.11765 0.05882
## [5,] 0.11765 0.05882
#If we add the bottom row we should get the probability of being male and other or female and other
sum(Proportion_Matrix[5,1], Proportion_Matrix[5,2])
## [1] 0.1765
#We can also solve this by hand.
#P of (male and other) OR (Female and other)

#get the total of males and females
total_M_F<- 900 + 800

#get the total of Other - we know from rowSums that is 300

Total_Other <- 300

#P of (male and other) OR (female and other)

P_Other = Total_Other/total_M_F
print(P_Other)
## [1] 0.1765

The probability someone is Male and lives in other OR is Female and lives in other is .1765, or 17.65%.

Question 3.

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?
Express your answer as a decimal number only. Show your R code

#define some functions
permutations_number <- function(n, k){
  result <- factorial(n)/ (factorial(n-k))
  
  return(result)
}
total_cards <- 52
total_diamonds <- 13

#doing the whole conditional probability formula - P(A and B)/P(B) where B is diamond on the first draw and A is diamond on the second draw.

Prob_drawing_diamond_given_diamond <- ((total_diamonds / total_cards) * ((total_diamonds - 1) / (total_cards - 1))) / (total_diamonds/total_cards)

print(Prob_drawing_diamond_given_diamond)
## [1] 0.2353
#check our answer doing some easier math considering we have a simple data set. If one diamond was already draw... we know that there is 51 total cards now and only 12 diamons in the deck

print(12/51)
## [1] 0.2353
#and we get the same answer.

There is a .2353 probability that you draw a diamond on the second draw, given that a diamond was drawn first without replacement.

Question 4

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?
Show your R code

#no longer dealing with large decimals so we will go back to the default
options(digits = 7)
#import the gtools package
library(gtools)
#assuming order does not matter
lineups <- choose(20,10)
print(lineups)
## [1] 184756

There are 184,756 possible lineup combinations

#assuming order matters
permutations_number(20,10)
## [1] 670442572800

If order of the songs matter, there are 670442572800 different lineups.

Question 5.

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?
Show your R code.

#this is a combination... order does not matter
#we are going to use the multiplication rule. These events are all independent. There are 20 tvs, 20 sound systems, and 18 DVD players. So we can multiply the choices together to get the total number of combinations.
TV_combos <- 20 * 20 * 18
TV_combos
## [1] 7200

There are 7,200 different combinations of TVs, sound systems, and DVD players that can be formed.

Question 6

A doctor visits her patients during morning rounds. In how many ways can the doctor visit 10 patients during the morning rounds?
Show your R code.

#permutation problem, set of 10 and we are picking 10

#doctor_visit_perm <- permutations(n = 10, r = 10, repeats.allowed = F)
#length(doctor_visit_perm)
#I am using my defined function because it works faster than the one above.
print(permutations_number(10,10))
## [1] 3628800

There are 3628800 different ways the doctor can visit the 10 patients. In other words there is that many unique orders to visit the 10 patients.

Question 7

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?
Show your R code

#assuming order does matter here... we need to calculate these things differently and then we can multiply the total outcomes of each scenerio together becuase these are independent event.

#for the coin tosses. We are sampling from a population of 2 items;heads or tails. And we are sampling 7 times
coin <- 2^7

#for the dice we can use a similar model. Here the population is 6 beasue 6 sides to the dice. And the sampling is 3 times. 
dice <- 6^3

#for the cards we need to use the permutation formula because there is no replacement. This scenerio is a little different from the other 2 because the card drawing without replacement is a dependent event. We can use the permutation formula. 
cards <- (factorial(52)) / (factorial(52-4))

coin
## [1] 128
dice
## [1] 216
cards
## [1] 6497400
#Now we can multiply these independent events- dice rolls, coin flip, and card draw permutations to get the total number of outcomes
Q7_outcomes <- coin * dice * cards
print(Q7_outcomes)
## [1] 179640115200

There are 179,640,115,200 different outcomes of this experiment.

Question 8.

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.
Show your R code.

#Order matters here. If we split the men and the women we can find the permuttions for the order of each gender.
men <- permutations_number(4,4)
women <- permutations_number(4,4)
men
## [1] 24
women
## [1] 24

There are 24 permutations for both the men and the womens’ order.

#now we can use the multiplication principle to find the total uniqe ways the men and women can be seated around the table. This is because the unique order of men and women is accounted for. Now we are combining the possible permutations.

party_seat_ways <- men * women
party_seat_ways
## [1] 576

Thre are 576 ways a party of 4 men and women can sit at a roundtable if they occupy alternating seats.

Question 10

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

#This problem is conditional probability. We are asking what the P is of having the other side be brown given that we already picked a pancake with one brown side. We can use the conditional probability formula

#These are the P's of picking a pancake and having one side be either golden or brown

golden_Prob <- 3/6
brown_Prob <- 3/6

Q10 <- (brown_Prob * brown_Prob) / (brown_Prob)
Q10
## [1] 0.5

As we can see the answer is 50%. This is an easy set so we can explain in other terms as well. There are 3 pancakes total, one with both golden, both brown and 1 of each. Upon drawing a pancake with a brown side as side 1, we immediately can eliminate the possibility of this particular pancake being the one with both golden sides because we know that at least one side is brown.

Now we have 2 possible outcomes for what this pancake is. It could either be the one with both brown sides or the one with one brown side and one golden side. This gives us a 50% chance of having the other side be brown.