2023-11-22

Counting posibilities

Probability of 2 blues and 1 white considering in a bag with 3 whites and 1 blue

Results <- list()

for (i in 1:1e+05) {
    Results[[i]] <- sum(sample(c("b", "w"), prob = c(0.25, 0.75), 3, replace = T) ==
        "b")
}

Prob_n_Blue <- purrr::reduce(Results, c) |>
    table() |>
    prop.table() |>
    as.data.frame() |>
    magrittr::set_colnames(c("Number_of_Blues", "Probability")) |>
    dplyr::mutate(CumProb = cumsum(Probability)) |>
    mutate(Number_of_Blues = as.numeric(as.character(Number_of_Blues)))

Counting posibilities cont()

Probabilities per number of blues

N_Blue <- c(0:4)
All_Probs <- list()

for (x in 1:length(N_Blue)) {
    Results <- list()
    for (i in 1:1e+05) {
        Results[[i]] <- sum(sample(c("b", "w"), prob = c(N_Blue[x]/4, 1 - (N_Blue[x]/4)),
            3, replace = T) == "b")
    }

    All_Probs[[x]] <- purrr::reduce(Results, c) |>
        table() |>
        prop.table() |>
        as.data.frame() |>
        magrittr::set_colnames(c("Number_of_Blues", "Probability")) |>
        dplyr::mutate(CumProb = cumsum(Probability)) |>
        mutate(Number_of_Blues = as.numeric(as.character(Number_of_Blues)), N_Blue = N_Blue[x])
}


All_Probs <- All_Probs |>
    purrr::reduce(bind_rows) |>
    dplyr::filter(Number_of_Blues == 2)

Probabilities per number of blues (cont)

Combining other information

N_Blue <- c(0:4)
All_Probs <- list()

for (x in 1:length(N_Blue)) {
    Results <- list()
    for (i in 1:1e+05) {
        Results[[i]] <- sum(sample(c("b", "w"), prob = c(N_Blue[x]/4, 1 - (N_Blue[x]/4)),
            4, replace = T) == "b")
    }

    All_Probs[[x]] <- purrr::reduce(Results, c) |>
        table() |>
        prop.table() |>
        as.data.frame() |>
        magrittr::set_colnames(c("Number_of_Blues", "Probability")) |>
        dplyr::mutate(CumProb = cumsum(Probability)) |>
        mutate(Number_of_Blues = as.numeric(as.character(Number_of_Blues)), N_Blue = N_Blue[x])
}


All_Probs <- All_Probs |>
    purrr::reduce(bind_rows) |>
    dplyr::filter(Number_of_Blues == 3)

Combining other information (cont)

Adding the probability of each combination

All_Probs |>
    mutate(Probability = case_when(N_Blue == 1 ~ Probability * (3/6), N_Blue == 2 ~
        Probability * (2/6), N_Blue == 3 ~ Probability * (1/6)))
##   Number_of_Blues Probability CumProb N_Blue
## 1               3  0.02353000 0.99604      1
## 2               3  0.08356000 0.93861      2
## 3               3  0.07032667 0.68274      3

Adding the probability of each combination (cont.)

Building a model

Water in the world

Prob <- c("W", "L", "W", "W", "W", "L", "W", "L", "W") |>
    table() |>
    prop.table() |>
    as.data.frame() |>
    magrittr::set_names(c("Value", "Prob"))
Value Prob
L 0.3333333
W 0.6666667

Likelihood

Sample <- c("W", "L", "W", "W", "W", "L", "W", "L", "W")
Likelihoods <- list()
for (i in 1:length(Sample)) {
    Temp <- data.frame(Proportion = seq(0.01, 0.99, by = 0.01), N = i, W = sum(Sample[1:i] ==
        "W"))
    Temp$Plausivility <- dbinom(x = unique(Temp$W), size = i, prob = Temp$Proportion)
    Likelihoods[[i]] <- Temp

}

Likelihood (cont.)

Questions

Easy 2E1.

Which of the expressions below correspond to the statement: the probability of rain on Monday?

  • Pr(rain): Probability that is raining
  • Pr(rain|Monday): Probability that it is raining given that is monday
  • Pr(Monday|rain): Probability that is monday, given that is raining
  • Pr(rain, Monday)/ Pr(Monday) the probability that is raining and a Monday, divided by the probability of it being a Monday” which is the same as “the probability of rain, given that it is Monday.

2E2.

Which of the following statements corresponds to the expression: Pr(Monday|rain)?

  • The probability of rain on Monday. pr(rain| Monday)
  • The probability of rain, given that it is Monday. pr(rain|Monday)
  • The probability that it is Monday, given that it is raining. Pr(Monday|rain)
  • The probability that it is Monday and that it is raining. pr(rain, Monday)

2E3.

Which of the expressions below correspond to the statement: the probability that it is Monday, given that it is raining?

  • Pr(Monday|rain)
  • Pr(rain|Monday)
  • Pr(rain|Monday) Pr(Monday)
  • Pr(rain|Monday) Pr(Monday)/ Pr(rain)
  • Pr(Monday|rain) Pr(rain)/ Pr(Monday)

pr(Monday|rain)

2M1.

Recall the globe tossing model from the chapter. Compute and plot the grid approximate posterior distribution for each of the following sets of observations. In each case, assume a uniform prior for p.

  1. W, W, W
  2. W, W, W, L
  3. L, W, W, L, W, W, W

2M1.1

Sample <- c("W", "W", "W")
Likelihoods <- data.frame(Proportion = seq(0, 1, by = 0.01), W = sum(Sample == "W"))
Likelihoods$Plausivility <- dbinom(x = unique(Likelihoods$W), size = length(Sample),
    prob = Likelihoods$Proportion)

2M1.2

Sample <- c("W", "W", "W", "L")
Likelihoods <- data.frame(Proportion = seq(0, 1, by = 0.01), W = sum(Sample == "W"))
Likelihoods$Plausivility <- dbinom(x = unique(Likelihoods$W), size = length(Sample),
    prob = Likelihoods$Proportion)

2M1.3

Sample <- c("L", "W", "W", "L", "W", "W", "W")
Likelihoods <- data.frame(Proportion = seq(0, 1, by = 0.01), W = sum(Sample == "W"))
Likelihoods$Plausivility <- dbinom(x = unique(Likelihoods$W), size = length(Sample),
    prob = Likelihoods$Proportion)

2M2

calculate_posterior <- function(Sample) {
    Likelihoods <- data.frame(Proportion = seq(0, 1, by = 0.01), W = sum(Sample ==
        "W"))

    prior <- ifelse(Likelihoods$Proportion < 0.5, 0, 1)

    Likelihoods$Plausibility <- dbinom(x = unique(Likelihoods$W), size = length(Sample),
        prob = Likelihoods$Proportion)

    unstd.posterior <- Likelihoods$Plausibility * prior

    Likelihoods$posterior <- unstd.posterior/sum(unstd.posterior)

    return(Likelihoods)
}

2M2.1

Likelihoods <- calculate_posterior(Sample = c("W", "W", "W"))

2M2.2

Likelihoods <- calculate_posterior(Sample = c("W", "W", "W", "L"))

2M2.3

Likelihoods <- calculate_posterior(Sample = c("L", "W", "W", "L", "W", "W", "W"))