Set environment
env <- c("dplyr")
lapply(env, library, character.only = 1)
Contingency Table
Create table
data1 <- matrix( c(10,20,20,40), nrow=2, byrow=1,
dimnames=list( c("A","A1"), c("B","B1") ) )
table1 <- as.table( data1 )
table1
## B B1
## A 10 20
## A1 20 40
Calculate relative frequencies
propTable <- prop.table(table1)
propTable
## B B1
## A 0.1111111 0.2222222
## A1 0.2222222 0.4444444
Find probabilities for ‘A’ and ‘B’
P(A) : sum the table row
pA <- propTable %>% rowSums() %>% .["A"]= 0.3333333P(B) : sum the table column
pB <- propTable %>% colSums() %>% .["B"]= 0.3333333P(A and B) : is already on the rel freq table as \(P(A) \cap P(B)\) but we can still calculate
pA * pB= 0.1111111P(A or B) : P(A) + P(B) - P(A and B)
pA + pB - (pA * pB)= 0.5555556P(A or B) disjoint : P(A) + P(B)
pA + pB= 0.6666667
Bayes Theorem
Jane is getting married tomorrow, at an outdoor ceremony in the desert. In recent years, it has rained only 5 days each year. Unfortunately, the weatherman has predicted rain for tomorrow. When it actually rains, the weatherman correctly forecasts rain 90% of the time. When it doesn’t rain, he incorrectly forecasts rain 10% of the time.
What is the probability that it will rain on the day of Jane’s wedding?
Discussion
Calculate the probabilities we know. ( R - rain, NR - no rain, F - weatherman forecast )
- Relative
- P(R) :
pR <- 5/365= 0.0136986
- P(NR) :
pNR <- 360/365= 0.9863014
- P(R) :
- Conditional
- P(F|R) : 0.9
pFR <- 0.9
- p(F|NR) : 0.1
pFNR <- 0.1
- P(F|R) : 0.9
Proportional probability would suggest that there’s a 0.01 or 1.37% chance that it will rain on Jane’s wedding day.
Given the weatherman’s prediction history, we can use Bayes to update our initial probability - the prior. We now want to predict the probability of rain based on the new forecast information - the posterior : \(P(R|F)\)
Using Bayes : \(\large P(R|F) = \frac{P(F|R) \cdot P(R)}{P(F)}\) = \(\large \frac{P(F|R) \cdot P(R)} {P(F|R) \cdot P(R) + P(F|NR) \cdot P(NR)}\)
Substituting results in : \(\large \frac{0.9 \cdot 0.0136986} {0.9 \cdot 0.0136986 + 0.1 \cdot 0.9863014}\) = 0.1111111
Interpretation:
- The weatherman’s forecast increased our probability of rain from 1.37% to 11.11%. A big increase but overall, there’s a low probability of rain on Jane’s wedding day.
- While the weatherman’s rain prediction rate is high, the fact that it only rains a few days per year, significantly lowers his daily probability accuracy. Even if the weatherman had an accurate rain forecast rate of 99%, the probability of rain on the wedding day would still be only about 0.58 or 58% - slightly better than a coin toss.
GitHub
Related file(s) can be found at Git Me
No comments:
Post a Comment