# Conditional Probability Problem

I'm a complete beginner in R and I'm already struggling. I have the following problem to solve for an assignment but I just can't seem to get my head around it.

Create a function which calculates conditional probabilities of possible outcomes of a roll of the first die D1 given the sum of the two (independent) dice S=D1+D2:
Pr(D1=d1|S=s).<d1

Input: two numeric outcomes
d1 and s for D1 and S (which is conditional event) respectively.

Output: calculate the conditional probability written above.

This is the code I have so far:

d_1<-4 #user inputs value

d_2 <- 1:6

s<-7 # user inputs value

rolls<-expand.grid(d_1,d_2)

R <- data.frame(rolls,rowSums(rolls))
colnames(R) <- c("D_1", "D_2", "S")
R

#calculate the conditional probability Pr(D_1=d1|S=s1)

sum(R$D_1=d_1 & R$S=s)/sum(R\$S=s)

Hello Evelynn,

welcome in the forum. You will have to look at the problem more "statistically" and less from the R point of view. If you remember Bayes Theorem, you know that

P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)}

holds true for A and B not independent (otherwise P(B \cap A) = P(B) \cdot P(A) and the whole thing is trivial). Since there is no requirement on MC simulation, you can "hard code" the corresponding PDF of the sum of two dice (P(B)) as well as the conditional probability to obtain a sum S given dice throw D1 (P(S|D1)).

The joined probability density function of the sum of two die throws is just given with the discrete probability mass function P(S = 2) = \frac{1}{36}, P(S = 3) = \frac{2}{36}, \dots. For any given sum S on the other hand, there is only one possible value of D2 given D1 to obtain S. Hence, the conditional probability P(S|D1) is either \frac{1}{6} or 0, depending on the event D1 and its range (e.g. if D1 = 4 the probability of S to be 11 or greater i 0, since there is no 7 on a 6-sided die - but if S = 7, D2 has to be 3 but the probability of D2 to be 3 is just \frac{1}{6}).

With this, you can code your function to return a conditional probability given the input of S and D1:

prob_function <- function(D,S){
# Vector of P(S)
prob_S <- c(1:6,5:1)/36
# Check, if the sum is possible
if ((S - D) %in% 1:6){
# Conditional probability of D given S
(1/6 * 1/6) / prob_S[[(S - 1)]]
} else {
cat('No valid input.')
}
}
prob_function(D = 5, S = 5)
#> No valid input.
prob_function(D = 1, S = 3)
#> [1] 0.5


Created on 2022-10-09 with reprex v2.0.2

The function contains a sanity check just to be sure the input is actually possible (otherwise the results would make no sense (e.g. there is no P(S = 13) or P(S = 8|D = 1)).

I hope this makes it clear four you, how to write a custom function but also the logic behind the calculations in this example.

Kind regards

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.