rm(list = ls()) # Clear env
gc()            # Clear unused memory
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 508471 27.2    1130651 60.4   644245 34.5
## Vcells 899328  6.9    8388608 64.0  1635533 12.5
cat("\f")       # Clear console

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

require(utils)
# Create Dice
d1 <- 1:6
d2 <- 1:6
d3 <- 1:6

grid <- expand.grid(d1,d2,d3)
#print(grid)
t <- rowSums(grid)
n <- sum(t == 12)
d <- nrow(grid)
p <- round(n/d,3)
p
## [1] 0.116

The probability is 0.116

Q02 - What is the probability that a customer is male and lives in ‘Other’ or is female and lives in ‘Other’?

#Create table
tb <- data.frame(residence = c('Apartment','Dorm','W/ Parents','GreekLife House','Other'),
                 male = c(200, 200, 100, 200, 200),
                 female = c(300, 100, 200, 100, 100))
tb
##         residence male female
## 1       Apartment  200    300
## 2            Dorm  200    100
## 3      W/ Parents  100    200
## 4 GreekLife House  200    100
## 5           Other  200    100
# Marginal Total
total <- sum(tb[2:3])
total
## [1] 1700
# Other Male
om <- with(tb, sum(male[residence == 'Other']))
om
## [1] 200
# Other Female
of <- with(tb, sum(female[residence == 'Other']))
of
## [1] 100
# Probability OM or OF
P <- round((om+of)/total,3)
P
## [1] 0.176

The probability is 0.176

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

deck <- 52 # num cards in deck
dmd <- 13  # num diamonds in a deck
# Remove one diamond from the deck
P = round((dmd-1)/(deck-1),3)
P
## [1] 0.235

The probability is 0.235

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

Making the assumption that we’re playing ten different songs because otherwise this is going to be a terrible event. Therefore order does matter

ts <- 20 # total songs
ss <- 10 # selected songs
lineups <- factorial(20)/factorial(10)
lineups
## [1] 670442572800

There are 670442572800 lineups possible

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

tv <- 20
ss <- 20
dp <- 18
ht <- 20*20*18
ht
## [1] 7200

There are 7200 different home theater systems possible

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

library(gtools)
df <- 1:10
p <- nrow(permutations(n=10,r=10,v=df))
p
## [1] 3628800
f <- factorial(10) # double check
f
## [1] 3628800

There are 3628800 possible ways to do the rounds

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

library(gtools)
coin <- c('heads','tails')
dice <- 1:6
deck <- 52
cards <- 4

Pcoin <- nrow(permutations(n=2,r=7,v=coin,repeats.allowed=T)) #Permutation w/ rep
Pdice <- nrow(permutations(n=6,r=3,v=dice,repeats.allowed=T)) #Permutation w/ rep
Pdeck <- (factorial(deck))/(factorial(deck-cards)) # Combination w/o rep
P = Pcoin * Pdice * Pdeck
P 
## [1] 179640115200

There are 179640115200 possible outcomes

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

# Seat the 4 women, there are 3! ways they can sit not directly next to each other
women <- factorial(3)

# Now the men have 4! ways they can sit
men <- factorial(4)

# Seating arrangements
table <- women * men 
table
## [1] 144

There are 144 ways to arrange the seating at the table

Q09 - 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|+)?

# Define variables
# Probability of a
a <- 0.03
# Probability (b | a) / Sensitivity
bGivena <- 0.95
# Probability (¬b | ¬a) / Specificity
notbGivenNota <- 0.99

# Calculate the rest of the values based upon the 3 variables above
notA            <-    1 - a
notbGivena      <-    1 - bGivena
bGivenNota      <-    1 - notbGivenNota

# Joint Probabilities of a and B, a and notb, nota and b, nota and notb
aANDb          <-   a    *  bGivena
aANDnotb       <-   a    *  notbGivena
notaANDb       <-   notA *  bGivenNota
notaANDnotb    <-   notA *  notbGivenNota

# Probability of B
b <- aANDb + notaANDb
notB <-   1 - b

# Bayes theorem - probability of A | B
# Prob (a | b) = Prob (a AND b) / Prob (b)
aGivenb <- aANDb / b
round(aGivenb,3)
## [1] 0.746

The probability that person is actually a user after testing positive is 0.746

Q10 - You have a hat in which there are three pancakes. One is golden on both sides (A), one is brown on both sides (B), 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.

I think this answer depends on your perspective. I believe the answer is 0.5, because the question is what is the probability the other side is brown - at this point in the problem one pancake has been eliminated from the population set. There are only two options - it was the brown-golden pancake or the brown-brown. I started to calculate it with bayes and a rough tree diagram but I don’t believe that perspective is fully answer the question as it starts from scenario of 3 pancakes, when based on the phrasing of the question one possibility has been removed and is no relevant.

sessionInfo()
## R version 4.2.2 (2022-10-31 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19044)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.utf8 
## [2] LC_CTYPE=English_United States.utf8   
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.utf8    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] gtools_3.9.4
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.31   R6_2.5.1        jsonlite_1.8.4  evaluate_0.20  
##  [5] cachem_1.0.6    rlang_1.0.6     cli_3.6.0       rstudioapi_0.14
##  [9] jquerylib_0.1.4 bslib_0.4.2     rmarkdown_2.20  tools_4.2.2    
## [13] xfun_0.36       yaml_2.3.7      fastmap_1.1.0   compiler_4.2.2 
## [17] htmltools_0.5.4 knitr_1.42      sass_0.4.5