#Clearing the global environment 
rm(list = ls())
#load library
library(gtools)
  1. What is the probability of rolling a sum of 12?
sample_space = expand.grid(1:6, 1:6, 1:6) #We get 216 obvservations

#Summing the rows and counting the ones adding upto 12
sums = rowSums(sample_space, na.rm = FALSE)
desired = length(which(sums == 12))

# Calculating the total number of possible outcomes
total <- nrow(sample_space) #here, it is 216

# Calculating and rounding off the probability
probability <- desired / total
(round(probability, 4))
## [1] 0.1157
  1. 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’?
#Creating the database
customer_matrix <- matrix(c(200, 300, 200, 100, 100, 200, 200, 100, 200, 100), nrow = 5, byrow = TRUE)
rownames(customer_matrix) <- c("Apartment", "Dorm", "With parent(s)", "Sorority/Fraternity House", "Other")
colnames(customer_matrix) <- c("Males", "Females")
customer_matrix
##                           Males Females
## Apartment                   200     300
## Dorm                        200     100
## With parent(s)              100     200
## Sorority/Fraternity House   200     100
## Other                       200     100
#Calculating the sums
location_m <- rowSums(customer_matrix)
gender_m <- colSums(customer_matrix)

male_other <- customer_matrix["Other", "Males"] / sum(gender_m)

# Calculate the probability that a customer is female and lives in 'Other'
female_other <- customer_matrix["Other", "Females"] / sum(gender_m)

# Calculate the combined probability
prob <- male_other + female_other

# Print the combined probability
(round(prob, 4))
## [1] 0.1765
  1. 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?
# Total number of cards
total_cards = 52

# Total number of diamonds
diamonds = 13

# Probability of drawing a diamond on the first draw without replacement
probability1 <- diamonds / total_cards

# Probability of drawing a diamond on the second draw without replacement
probability2 <- (diamonds - 1) / (total_cards - 1)

(round(probability2, 4))
## [1] 0.2353
  1. 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?
# Permutation without repeats since the question asked for different lineups (arrangement), rather than the ways the songs can be chosen.
total_songs = 20
selected_songs = 10

#Calculating total number of lineups
lineups = factorial(total_songs) / factorial (total_songs - selected_songs)
lineups
## [1] 670442572800
  1. 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?
# Combination without repetition
(ht_sys = 20*20*18)
## [1] 7200
  1. A doctor visits her patients during morning rounds. In how many ways can the doctor
    visit 10 patients during the morning rounds?
# Number of ways to visit his patients (permutation without repeats)
(factorial(10))
## [1] 3628800
  1. 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?
# Number of outcomes for coin tosses 
(coin = 2^7) 
## [1] 128
# Number of outcomes for die rolls 
(die = 6^3)
## [1] 216
# Number of outcomes for card draws (Combination of any four, without replacement)
(card = choose(52, 4))
## [1] 270725
# Number of different outcomes
(total = coin * die * card)
## [1] 7485004800
  1. 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.
# Number of women
num_women = 4

# Number of men
num_men = 4

# Out of 4 men, one can be seated anywhere, however the other 3 need to be placed relatively. So the number of ways they can be seated is calculated by permutation, i.e, 3P3 = 3!
men_prob = factorial(num_men-1)

#Ways women can be seated is 4P4
women_prob = factorial(num_women)

#Total number of ways
(total = men_prob * women_prob)
## [1] 144
  1. 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 | +)?
pos_user = 0.95 #P( + | User)
neg_user = 0.99 #P( - | Not User)
user = 0.03 #P(User)

#Bayesian formula:
#P(User | +) = P(+ | User) * P(User) / P(+)

#To find P(+):
#P(+ | User) * P(User) + P(+ | Not user) * P(Not User)
#where; P(+ | Not user) = P(1 - P( - | Not User))
#Therefore, calculating probability of total users testing positive
pos = (pos_user * user) + ((1-neg_user) * (1-user))

#Applying Bayesian formula:
user_pos = (pos_user * user) / pos
(round(user_pos, 4))
## [1] 0.7461
  1. 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.

    Explanation:

    Out of the three pancakes, only two can have one brown side. As given in the question, the pancake chosen has one side brown, so to find the probability of the other side being brown in color, I have used conditional probability as shown below:

    P (Both sides being brown | One side is brown) = P (Both sides are brown) / P (One side is brown)

    By calculating further, the total number of pancakes cancels out, giving:

    P (Both sides being brown | One side is brown) = N (Both sides are brown) / N (One side is brown)

#Number of pancakes with one brown side
one_brown = 2

#Number of pancakes with two brown sides
two_brown = 1

#Probability of the other side being brown
(probability = two_brown / one_brown)
## [1] 0.5