* The probability of a car passing a certain intersection in a 20 minute windows is 0.9. What is the probability of a car passing the intersection in a 5 minute window? (Assuming a constant probability throughout)
a= 1-0.9 ## for probabiliyt within 20 min no car passed by
# 1-(1-b)^4 = a
# b is the probability of within 5 min no car pass by
# x^4 = 0.1
x = 0.1^(1/4)
1-x
## [1] 0.4376587
* I have three colored cards. Two of the cards are painted green on each side, and one is painted orange on one side and green on the other. Suppose you are shown a single face of a card and that face is green. Find the probability that the other side is orange.
this question uses p(A) = p(A)* p(B)/p(B)
* You roll two dice. Determine if the following events are independent: A = "the sum of two dice is odd" and B = "the second die is a 3."
This question to prove A and B are indepent uses p(A ^ B) = p(A)*p(B)
* Let X be a uniformly distributed Random Variable on [1, 2]. What is the E[X2]
#you can use calculus to solve this
# generate pdf of this
all = rep(runif(1, 1,2), 100)
sum(all^2)/100
## [1] 1.605883
* You have 52 playing cards (26 red, 26 black). You draw cards one by one. A red card pays you a dollar. A black one fines you a dollar. You can stop any time you want. Cards are not returned to the deck after being drawn. What is the optimal stopping rule in terms of maximizing expected payoff? Also, what is the expected payoff following this optimal rule?
## this is one simulartion
card = c(rep(1, 26), rep(-1, 26))
total = 0
draw = rep(0, 52)
current = c()
for (i in draw) {
i = round(runif(1, 1, length(card)))
total = total + card[i]
print(i)
print(card[i])
print(paste('current', total))
card = card[-i]
current = c(current, total)
}
## [1] 46
## [1] -1
## [1] "current -1"
## [1] 44
## [1] -1
## [1] "current -2"
## [1] 42
## [1] -1
## [1] "current -3"
## [1] 47
## [1] -1
## [1] "current -4"
## [1] 31
## [1] -1
## [1] "current -5"
## [1] 10
## [1] 1
## [1] "current -4"
## [1] 20
## [1] 1
## [1] "current -3"
## [1] 14
## [1] 1
## [1] "current -2"
## [1] 27
## [1] -1
## [1] "current -3"
## [1] 8
## [1] 1
## [1] "current -2"
## [1] 20
## [1] 1
## [1] "current -1"
## [1] 17
## [1] 1
## [1] "current 0"
## [1] 5
## [1] 1
## [1] "current 1"
## [1] 23
## [1] -1
## [1] "current 0"
## [1] 29
## [1] -1
## [1] "current -1"
## [1] 4
## [1] 1
## [1] "current 0"
## [1] 3
## [1] 1
## [1] "current 1"
## [1] 10
## [1] 1
## [1] "current 2"
## [1] 11
## [1] 1
## [1] "current 3"
## [1] 15
## [1] 1
## [1] "current 4"
## [1] 5
## [1] 1
## [1] "current 5"
## [1] 18
## [1] -1
## [1] "current 4"
## [1] 28
## [1] -1
## [1] "current 3"
## [1] 15
## [1] -1
## [1] "current 2"
## [1] 5
## [1] 1
## [1] "current 3"
## [1] 3
## [1] 1
## [1] "current 4"
## [1] 25
## [1] -1
## [1] "current 3"
## [1] 11
## [1] 1
## [1] "current 4"
## [1] 23
## [1] -1
## [1] "current 3"
## [1] 7
## [1] 1
## [1] "current 4"
## [1] 17
## [1] -1
## [1] "current 3"
## [1] 6
## [1] 1
## [1] "current 4"
## [1] 6
## [1] 1
## [1] "current 5"
## [1] 14
## [1] -1
## [1] "current 4"
## [1] 13
## [1] -1
## [1] "current 3"
## [1] 6
## [1] 1
## [1] "current 4"
## [1] 10
## [1] -1
## [1] "current 3"
## [1] 11
## [1] -1
## [1] "current 2"
## [1] 8
## [1] -1
## [1] "current 1"
## [1] 8
## [1] -1
## [1] "current 0"
## [1] 3
## [1] 1
## [1] "current 1"
## [1] 8
## [1] -1
## [1] "current 0"
## [1] 5
## [1] 1
## [1] "current 1"
## [1] 2
## [1] 1
## [1] "current 2"
## [1] 4
## [1] -1
## [1] "current 1"
## [1] 5
## [1] -1
## [1] "current 0"
## [1] 5
## [1] -1
## [1] "current -1"
## [1] 3
## [1] 1
## [1] "current 0"
## [1] 3
## [1] -1
## [1] "current -1"
## [1] 2
## [1] 1
## [1] "current 0"
## [1] 1
## [1] 1
## [1] "current 1"
## [1] 1
## [1] -1
## [1] "current 0"
* You have two dice. There are two players. You each pick a number in turn and the second person cannot pick the same number as the first. The person who picks the number closest or equal to the sum of the roll of the dice is the winner. What are each player's strategies? Who has a greater chance of winning?
a = 1:6
b = 1:6
result = c()
for (i in a) {
for (j in b) {
result = c(result, i+j)
}
}
hist(result)
# the highest frequency is 7
* Person A has a 30-sided die (numbered 1 - 30) and person B has a 20-sided die (numbered 1 - 20). Both players role and the person with the highest role wins (on a draw B wins). The loser pays the winner the value of the winner's die. A. Calculate expected value of this game for player A.
* You are give 12 identical-looking balls. One of them is fake (could be heavier or lighter than the rest of the 11; all the others weigh exactly the same). You are provided with a simple mechanical balance and you are restricted to only 3 uses. Find the fake ball.
# weigh 4 4 and left 4
# pick 4 and weight 2 * 2
# weight one and one
* Bayesian basics
library(LearnBayes)
##### not sure what is doing here
## we estimate the proportion of people who like chocolate are 0.85, but this prior doesn not smmaller than 0.6 and not bigger than 0.95
quantile1 = list(p = 0.5, x = 0.85)
quantile2 = list(p = 99.99, x = 0.95)
quantile3 = list(p = 0.001, x= 0.60)
## find the beta
findBeta <- function(quantile1, quantile2, quantile3){
# find the quantiles specified by quantile1 and quantile2 and quantile3
quantile1_p <- quantile1[[1]]; quantile1_q <- quantile1[[2]]
quantile2_p <- quantile2[[1]]; quantile2_q <- quantile2[[2]]
quantile3_p <- quantile3[[1]]; quantile3_q <- quantile3[[2]]
# find the beta prior using quantile1 and quantile2
priorA <- beta.select(quantile1,quantile2)
priorA_a <- priorA[1]; priorA_b <- priorA[2]
# find the beta prior using quantile1 and quantile3
priorB <- beta.select(quantile1,quantile3)
priorB_a <- priorB[1]; priorB_b <- priorB[2]
# find the best possible beta prior
diff_a <- abs(priorA_a - priorB_a); diff_b <- abs(priorB_b - priorB_b)
step_a <- diff_a / 100; step_b <- diff_b / 100
if (priorA_a < priorB_a) { start_a <- priorA_a; end_a <- priorB_a }
else { start_a <- priorB_a; end_a <- priorA_a }
if (priorA_b < priorB_b) { start_b <- priorA_b; end_b <- priorB_b }
else { start_b <- priorB_b; end_b <- priorA_b }
steps_a <- seq(from=start_a, to=end_a, length.out=1000)
steps_b <- seq(from=start_b, to=end_b, length.out=1000)
max_error <- 10000000000000000000
best_a <- 0; best_b <- 0
for (a in steps_a)
{
for (b in steps_b)
{
# priorC is beta(a,b)
# find the quantile1_q, quantile2_q, quantile3_q quantiles of priorC:
priorC_q1 <- qbeta(c(quantile1_p), a, b)
priorC_q2 <- qbeta(c(quantile2_p), a, b)
priorC_q3 <- qbeta(c(quantile3_p), a, b)
priorC_error <- abs(priorC_q1-quantile1_q) +
abs(priorC_q2-quantile2_q) +
abs(priorC_q3-quantile3_q)
if (priorC_error < max_error)
{
max_error <- priorC_error; best_a <- a; best_b <- b
}
}
}
print(paste("The best beta prior has a=",best_a,"b=",best_b))
}
library(LearnBayes)
##### not sure what is doing here
## we estimate the proportion of people who like chocolate are 0.85, but this prior doesn not smmaller than 0.6 and not bigger than 0.95
quantile1 <- list(p=0.5, x=0.85) # we believe the median of the prior is 0.85
quantile2 <- list(p=0.99999,x=0.95) # we believe the 99.999th percentile of the prior is 0.95
quantile3 <- list(p=0.00001,x=0.60) # we believe the 0.001st percentile of the prior is 0.60
findBeta(quantile1, quantile2, quantile3)
## [1] "The best beta prior has a= 52.22 b= 9.52105105105105"
curve(dbeta(x,52.22,9.52105105105105))
# calculating the likelihood of a function
calcuLikeForProp <- function(success, trials) {
curve(dbinom(success, trials, x))
}
calcuLikeForProp(45, 50)
* Calculating the posterior distribution
I don’t quite understand this part here