DA_HW2

Pin Lyu

2023-09-25

  • Q1) 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.

    # Number of total possible results 
    results <- expand.grid(dice1 = seq(1,6), dice2 = seq(1,6),dice3 = seq(1,6))
    
    nrow(results)
    ## [1] 216
    # Total number of result that give a sum of 12
    df <- list(rowSums(results))
    table(df)
    ## df
    ##  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 
    ##  1  3  6 10 15 21 25 27 27 25 21 15 10  6  3  1
    # Probability of get a sum of 12 by throwing 3 times
    TTR = 216 #Total number of results
    DR  = 25  # Desired number of results 
    
    print(DR/TTR) 
    ## [1] 0.1157407

    Answer: The probability of getting a sum of 12 by throwing a 6 sided dice 3 times is about 11.57%.

  • Q2) What is the probability that a customer is male and lives in ‘Other’ or is female and lives in ‘Other’?

    # Matrix replicaton
    input <- c(200,300,200,100,100,200,200,100,200,100)
    matrix <- matrix(input,nrow=5,ncol=2,byrow=TRUE)
    
    rownames(matrix) = c("Apartment","Dorm","With Parent(s)", "Sorority/Fraternity House", "Other")
    
    colnames(matrix) = c("males", "females")
    matrix
    ##                           males females
    ## Apartment                   200     300
    ## Dorm                        200     100
    ## With Parent(s)              100     200
    ## Sorority/Fraternity House   200     100
    ## Other                       200     100
    m_other <- matrix[5,1]
    f_other <- matrix[5,2]
    total   <- sum(matrix)
    
    prob <- (m_other/total) + (f_other/total)
    print(prob)
    ## [1] 0.1764706

    Check:

    # Variable values 
    m_other <- 200
    f_other <- 100
    total <- 1700
    
    # Caculation 
    prob <- (m_other / total) + (f_other / total)
    print(prob)
    ## [1] 0.1764706

    Answer: The probability of a customer is male and lives in ‘Other’ or is female and lives in ’Other is about 17.65%.

  • Q3) 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.

    # Total number of cards in a deck 
    total = 52
    
    #Total number of card for 2nd draw (without replacement)
    total = total - 1 
    
    # Total number of diamonds in a deck
    D <- 13
    
    # Total number of diamonds in a deck during 2nd draw
    D <- D - 1
    
    print(D)
    ## [1] 12
    print(total)
    ## [1] 51
    # Probability 
    prob <- (D / total)
    print(prob)
    ## [1] 0.2352941

    Answer: If the first draw is diamond and without replacement, the probability of drawing diamond again in the 2nd time is 23.52%.

  • Q4) 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, order does not matter
    # Building a combination function
    perm_without_replace <- function(n, r){
      return(factorial(n)/factorial(n - r))
    }
    
    perm_without_replace(20,10)
    ## [1] 670442572800

    Answer: The amount of lineups that are possible are 670,442,572,800.

  • Q5) 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, order does not matter
    # Variable values
    tv <- 20
    dvd <- 20
    ss <- 18
    
    # Number of ways to select
    total <- (tv * dvd * ss)
    
    print(total)
    ## [1] 7200

    Answer: The total number of ways to select the home threater system is 670,442,572,800.

  • Q6) 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 10 patients 
    factorial(10)
    ## [1] 3628800

    Answer: There are, in total, 3,628,800 ways to visit the 10 patients.

  • Q7) 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 ways of each event 
    coin <- 2^7
    dice <- 6^3
    
    ## For cards, permutation without repetition 
    card <- (perm_without_replace(52,4))
    
    # Total number of all possible outcomes of three events
    outcome <- (coin * dice * card)
    
    print(outcome)
    ## [1] 179640115200

    Answer: The total possible outcomes for these three events are 179,640,115,200.

  • Q8) 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.

    • Combination, without repetition
    # Number of each sex 
    n = 4
    
    ## Here, we assume a man gets to be seated first 
    # Ways men can be seated
    men <- factorial(n-1)
    
    # Ways women can be seated 
    women <- factorial(n)
    
    # Total ways possible
    total <- (men * women)
    
    print(total)
    ## [1] 144

    Answer: The total number of ways to seat these people alternatively are 144.

  • Q9) 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 | +)?

    # Variable values
    op <- 0.95   # (+|User) Positive positive rate 
    on <- 1 - op   # (+|Non User) false positive rate
    
    Upop  <- 0.03  # Users in population
    NUpop <- 1 - Upop # Non-users in population
    
    
    # Bayesian probability 
    Userispos <- (Upop * op) / ((Upop * op) + (NUpop * on))
    
    print(Userispos)
    ## [1] 0.3701299

    Answer: The probability of having correctly decting a user that is positive is 37.01% based on bayesian probability.

  • Q10) 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: Due to the given information about the color of a pancake on one side, brown, and knowing there are only two possible pancakes have at least one brown side. {brown, brown} and {brown, golden}. Therefore the possibility of the other side is also brown is the following:
    # Probability of select one of the three pancakes
    P <- 1/3
    
    # Probability of selecting a hat that is brown, given the one side is brown and there are two pancakes that have at least one brown side
    
    B <- 1/2
    
    # P{brown, brown}
    Pbb <- (P*B)
    
    print(Pbb)
    ## [1] 0.1666667

    Answer: The total probability of the other side of the hat drawn is brown is 16.67%.