Solved – How to calculate causal inference in bayesian networks

bayesianbayesian networkcausality

As beginner I am reading this Bayesian Networks example.

I have also read this question.

enter image description here

It calculates $P(S=1,W=1)$, the result is $0.2781$. I tried to expand the equation, but getting incorrect result:

$P(W,S) = P(W|S) \cdot P(S)$

$P(W,S) = ( P(W|S,R) \cdot P(R) + P(W|S,\bar{R}) \cdot P(\bar{R}) ) \cdot P(S)$

$P(R) = P(R|C) \cdot P(C) + P(R|\bar{C}) \cdot P(\bar{C})$

$P(R) = 0.8 \cdot 0.5 + 0.2 \cdot 0.5 = 0.5$

$P(\bar{R}) = 1 – 0.5 = 0.5$

$P(S) = P(S|C) \cdot P(C) + P(S|\bar{C}) \cdot P(\bar{C})$

$P(S) = 0.1 \cdot 0.5 + 0.5 \cdot 0.5 = 0.3$

$P(W|S,R) = 0.99$

$P(W|S,\bar{R}) = 0.9$

$P(W,S) = (0.99 \cdot 0.5 + 0.9 \cdot 0.5) \cdot 0.3 = 0.2835 \neq 0.2781$

What is wrong with my calculation? May I start with $P(W,S)$ for the $P(S=1,W=1)$?

Then how to solve $P(W=1) = 0.6471$?

Best Answer

The short answer

Intuitively, the issue is the fact that $P(S)$ and $P(R)$ are only independent when you condition on $C$, so $P(S,R) \neq P(S)P(R)$.

This was your mistake - you accidentally implied $P(S,R) = P(S)P(R)$ in one of your calculations.

I'll show you where this happened exactly below, but that is what motivates the approach of the other answers - Using marginalization as they do is a good way to ensure you don't accidentally imply that $P(S,R) = P(S)P(R)$

Where you got tripped up in your calculations

Let's see where this applies in your calculations. Let's take this equation from your post:

$$P(W,S) = ( P(W|S,R) \cdot P(R) + P(W|S,\bar{R}) \cdot P(\bar{R}) ) \cdot P(S)$$

This is the problem, because you calculate $P(S)$ separately, and then multiply it through, resulting in some cases of $P(S)P(R)$ where you really meant $P(S,R)$

Let's go back to the beginning and walk through the equations, using your notation, to see how this could have been avoided.

$$ \begin{aligned} P(W,S) &= P(W | S) P(S) \\ &= P(W | S) P(S | C) P(C) + P(W | S) P(S | \bar{C}) P(\bar{C}) \end{aligned} $$

This looks similar to what you did, but the crucial point is to avoid "factoring out" $P(S)$ and calculating it separately. Instead of doing that, let's go deeper and condition on $R$ and $\bar{R}$.

I'll start with the first term:

$$ \begin{aligned} P(W | S) P(S | C) P(C) &= P(W | S, R) P(S | C) P(R | C) P(C) \\ &+ P(W | S, \bar{R}) P(S | C) P(\bar{R} | C) P(C) \\ &= (0.99)(0.1)(0.8)(0.5) + (0.9)(0.1)(0.2)(0.5) \\ &= 0.0396 + 0.009 \\ &= 0.0486 \end{aligned} $$

Following the same logic, the second term is as follows

$$ \begin{aligned} P(W | S) P(S | \bar{C}) P(\bar{C}) &= P(W | S, R) P(S | \bar{C}) P(R | \bar{C}) P(\bar{C}) \\ &+ P(W | S, \bar{R}) P(S | \bar{C}) P(\bar{R} | \bar{C}) P(\bar{C}) \\ &= (0.99)(0.5)(0.2)(0.5) + (0.9)(0.5)(0.8)(0.5) \\ &= 0.0495 + 0.18 \\ &= 0.2295 \end{aligned} $$

Add these together, and you get $0.2781$!

Bonus Round: Sprinklers on a rainy day (i.e., the importance of conditional independence)

If you intuitively understand that $P(S,R) \neq P(S)P(R)$, then you can skip this last section. However, I think it's a fun example for building intuition, so I'll go into a bit of detail.

So what's the difference between $P(S,R)$ and $P(S)P(R)$? Let's calculate it, starting with the latter.

$$P(S)P(R) = (0.3)(0.5) = 0.015$$

Now let's calculate $P(S,R)$ to show the difference. We'll do so by conditioning on $C$ and $\bar{C}$.

Note that since $S$ and $R$ are conditionally independent on $C$, we can say that $P(S,R | C) = P(S | C) P(R | C) P(C)$

The rest is a fairly straightforward calculation:

$$ \begin{aligned} P(S,R) &= P(S,R | C) P(C) + P(S,R | \bar{C})P(\bar{C}) \\ &= P(S | C) P(R | C) P(C) + P(S | \bar{C}) P(R | \bar{C}) P(\bar{C}) \\ &= (0.1)(0.8)(0.5) + (0.5)(0.2)(0.5) \\ &= 0.04 + 0.05 \\ &= 0.09 \end{aligned} $$

In this case, there's some intuition - The chances of it both raining and sprinkling at the same time are lower once you take into account the conditional dependence. This is because it's most likely to rain on cloudy days, but that's precisely when the sprinkler is very unlikely!

Related Question