# Calculating conditional probability from a dataset [closed]

Say I have a table of data with four columns labelled A, B, C, D, where each entry is either a 0 or 1, how do I calculate P(D=1|A=1,B=1,C=1) directly from the data? Here, event D has a causal link with events A, B, C. Can I apply the conditional probability formula P(D|A,B,C)=P(A,B,C,D)/P(A,B,C)? Also, to calculate the joint probability P(A=1,B=1,C=1) directly from the data, do I just count number of rows with A=1, B=1and C=1, divided by total number of rows? Here, events A, B, C, D are not independent. Thanks, any help is greatly appreciated!

• I'm not sure whether this is 'on topic' here. It seems to be about software as much as it is about statistics. Also, precise answers will differ depending on the software used. I show two simple examples in R that may help you ask a more targeted question. – BruceET Jun 18 at 23:11

In R conditioning can be done by use of [ ]-notation (say, "such that").

Example 1: Here, all columns are independent, so conditioning D on A, B, and C, has no effect on the probability, but it does decrease the sample size, so there is a slight difference in the simulated proportion. [With 100,000 realizations, one can expect 2 or 3 place accuracy.]

set.seed(1234) # for reproducibility
m = 10^5

a = rbinom(m, 1, .7)
mean(a)
[1] 0.69942    # aprx P(A=1) = 0.7

b = rbinom(m, 1, .8)
mean(b)
[1] 0.80134    # aprx P(B=1) = 0.8

c = rbinom(m, 1, .9)
mean(c)
[1] 0.90018    # aprx P(C=1) = 0.7

d = rbinom(m, 1, .4)
mean(d)
[1] 0.39876    # aprx P(D=1) = 0.4
mean(d[a==1 & b==1 & c==1])
[1] 0.3970629  # aprx P(D=1 | A=1, B=1, C= 1) = 0.4


Example 2: Here is a situation where the result of conditioning is not completely trivial: Roll two fair dice. The if $$T = X_1 + X_2$$ is the total on the two dice, then $$P(T = 4) = 3/36 = 1/12 = 0.0833,$$ but $$P(T=4 | X_1 < 3, X_2 < 4) = 2/6 = 1/3 = 0.333.$$

set.seed(618)
m = 10^6
x1 = sample(1:6, m, rep=T)
x2 = sample(1:6, m, rep=T)
t = x1 + x2
mean(t)         # Mean of a sample of one million
[1] 6.997249     # aprx E(T) = 7
mean(t==4)      # Proportion of TRUEs in logical vector
[1] 0.083411     # aprx P(T = 4) = 0.0833
mean(t[x1<3 & x2<4] == 4)
[1] 0.3335913    # aprx P(T=4 | X1<3, X2<4) = 0.3333.
mean(t[x1<3 & x2<4])
[1] 3.501377     # can you verify the exact answer?

• Not saying [ ] is used only for conditioning. but here restricting the simulated population to a subset is exactly what is needed. I hope you're not disputing the usefulness of the approach or the accuracy of the results. This method is used often in simulation. – BruceET Jun 19 at 17:33
• After re-wording it is more clear :) – Tim Jun 19 at 17:54