Recall the hit-and-run example from lecture and HW4. We noted that the Bayes’ probabilities are calculated as the following formula \[ P(B|W) = \frac{pq}{pq + (1-p)(1-q)},\] where \(P(B) = q\) and \(P(W|B) = p\) (check notes). We also noted in HW4 that if \(P(W|B)=p > \frac{1}{2}\) we will have \[ P(B|W) > P(B).\]
In this problem, we will explore how changing the level of Witness-Reliability, that is \(p\), will affect the chance of getting a Blue Cab driver arrested.
Recall that \(P(B)\) is the probability that the Blue cab is at fault with no added information. We will now run multiple iteration of Bayes’ rule, and at every iteration, we will update the value of \(P(B)\) with the value of \(P(B|W)\) from earlier iteration. From our experience from the homework, if \(p > \frac{1}{2}\), we must have \(P(B) \longrightarrow 1\) as the number of iterations increase (intuitively, the probability that the Blue cab is at fault will go to 1 as the number of “reliable witness” who say that “the Blue Cab is at fault” increases).
Write a function called “bayes” that takes input \(q, p\) and outputs \(P(B|W)\) (use the formula above)
bayes <- function(p,q){
return(p*q/(p*q+(1-p)*(1-q)))
}
In this part, we will assume that we are using testimony of reliable witnesses with fixed reliability \(p\).
Initialize \(p=0.6\), \(q=0.01\), and q_vals = c(), the empty vector. Run a for loop (1 in 1:40) inside it, update q at every iteration to \(q = (p, \text{bayes}(p,q))\), and concatenate the vector q_vals with this new value of q.
p <- 0.6
q <- 0.01
q_vals <-c()
for (i in 1:40){
q <-bayes(p,q)
q_vals <-c(q_vals,q)
}
Construct a plot with x-axis 1:40 and y-axis q_vals.
x <- 1:40
y <- q_vals
plot(x,y)
lines(x,y, type="l", col = "red")
lines(x,q_vals, col = "blue")
Repeat the above experiment with \(p=0.4\), \(q=0.99\).
p <- 0.4
q <- 0.99
q_vals_2 <-c()
for(i in 1:40){
q <- bayes(p,q)
q_vals_2 <- c(q_vals_2, q)
x <- 1:40
y <- q_vals_2
}
plot(x,y)
lines(x,y, type="l", col = "red")
lines(x,q_vals, col ="blue")
lines(x,q_vals_2, col ="green")
Suppose the police decide to arrest the Blue cab driver if \(P(B|W) \ge 0.9\).
Suppose we intialize \(p=0.6\) (witness reliability), \(q=0.01\) (proportion of Blue cabs in the city), how many witnesses with reliability level \(p\) would we need for the police to consider arresting the Blue cab?
You will need a counter variable initialized to zero and a while loop inside which you update \(q\) using the bayes function, and update the counter by 1 at every iteration of the while loop.
gamma <- 0.9 #threshold to convict blue cab
p <- 0.6
q <- 0.01
counter <- 0
while (q<gamma){
q <- bayes(p,q)
counter <- counter +1
}
p <-0.6
q <- 0.01
q_vals <- c()
for(i in 1:80){
q <- bayes(p,q)
q_vals <- c(q_vals, q)
}
x <- 1:80
y <- q_vals
plot(x,y,
main = "Graph of increments of P(B|W)",
xlab = "numver of witnesses",
ylab = "updates for P(B|W)")
lines(x,y,
type = "l",
col = "red")
abline(h = 0.9, col = "blue")
abline(v= 69, col = "blue")
In this problem we will work with two types of witnesses (1) reliable witnesses with fixed reliability level \(p_1 > \frac{1}{2}\) and (2) unreliable witness with fixed reliability level \(p_2 < \frac{1}{2}\).
Initialize \(p_1=0.6\), \(p_2 = 0.45\), \(q=0.01\), and q_vals = c(), the empty vector. Run a for loop (1 in 1:90) inside it, update q at every even iteration to \(q = (p_1, \text{bayes}(p_2,q))\), and at every even iteration to \(q = (p_2, \text{bayes}(p_1,q))\) (You will need to use a “if” command), and finally concatanating the vector q_vals with the new value of q at every iteration.
p_1 <- 0.6
p_2 <- 0.45
q <- 0.01
q_vals <-c()
for(i in 1:90){
if(i%%2 ==0){
q <- bayes(p_1,q)
q_vals <- c(q_vals, q)
}
else{
q <- bayes(p_2, q)
q_vals <- c(q_vals, q)
}
}
q_vals
## [1] 0.008196721 0.012244898 0.010040907 0.014986124 0.012294875 0.018329632
## [7] 0.015047118 0.022402134 0.018403980 0.027354256 0.022492622 0.033363715
## [13] 0.027464186 0.040638231 0.033496963 0.049417772 0.040799306 0.059975481
## [19] 0.049611847 0.072616452 0.060208392 0.087673255 0.072894647 0.105496891
## [25] 0.088003658 0.126441816 0.105886668 0.150843827 0.126898001 0.178990248
## [31] 0.151372795 0.211083075 0.179597042 0.247197586 0.211770603 0.287241276
## [37] 0.247965772 0.330920215 0.288086280 0.377721263 0.331833891 0.426917920
## [43] 0.378691022 0.477604302 0.427927127 0.528756142 0.478633257 0.579311105
## [49] 0.529783538 0.628255576 0.580315765 0.674703197 0.629218162 0.717952779
## [55] 0.675607612 0.757518714 0.718787066 0.793133536 0.758275370 0.824727703
## [61] 0.793809308 0.852394584 0.825322997 0.876349002 0.852912669 0.896886201
## [67] 0.876795161 0.914345769 0.897266944 0.929082782 0.914668177 0.941446629
## [73] 0.929354005 0.951766844 0.941673535 0.960344706 0.951955798 0.967449240
## [79] 0.960501451 0.973316311 0.967578851 0.978149764 0.973423202 0.982123771
## [85] 0.978237726 0.985385805 0.982196026 0.988059823 0.985445070 0.990249407
Construct a plot with x-axis 1:90 and y-axis q_vals.
x<- 1:90
plot(x, q_vals)
lines(x,q_vals, col ="red")
What do you notice? (Compared to case with single witness)
Suppose the police decide to arrest the Blue cab driver if \(P(B|W) \ge 0.9\).
Initialize \(p_1=0.6\), \(p_2 = 0.45\), \(q=0.01\). How many witnesses will we have to have if we are going to sequentially work with one reliable witness followed by an unreliable witness?
p_1 <- 0.6
p_2 <- 0.45
q <- 0.01
counter <- 0
while(q <0.9){
if(counter%%2 == 0){
q <- bayes(p_1, q)
counter <- counter +1
}
else{
q <- bayes(p_2,q)
counter <- counter +1
}
}
counter
## [1] 65