# How to calculate causal inference in bayesian networks

As beginner I am reading this Bayesian Networks example.

I have also read this question.

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$?

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}$.

\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!

Just use marginalization: \begin{eqnarray} p(W) &=& \sum_S \sum_R \sum_C p(W,S,R,C) \\ &=& \sum_S \sum_R \sum_C p(W \mid S,R)p(S\mid C)p(R \mid C) p(C) \\ &=& \sum_S \sum_R p(W\mid S,R) \bigg[\frac{1}{2}p(S \mid C=1)p(R \mid C=1) + \frac{1}{2} p(S\mid C=0)p(R \mid C=0) \bigg] \end{eqnarray} If you plugin the numbers from the CPTs carefully, you will get p(W=1)=0.6471.

Same for your first question: $p(W,S) = \sum_R \sum_C p(W,S,R,C)$ and just do the summations for all possible combinations of ${R,C}$.

• But I still want to know what is wrong in my calculation, thank you anyway.
– silo
Commented Dec 4, 2016 at 2:52

By using marginalization, as mentioned by @YBE,

\begin{align} P(W=1,S=1) & = \sum_R \sum_C P(C,S=1,R,W=1) \\ & = \sum_R \sum_C P(R \mid C) P(S=1 \mid C) P(W=1 \mid R,S=1) P(C) \\ & = \sum_R \bigg[ P(R \mid C=1) P(S=1 \mid C=1) P(W=1 \mid R,S=1) P(C=1) \\ & \quad + P(R \mid C=0) P(S=1 \mid C=0) P(W=1 \mid R,S=1) P(C=0) \bigg] \\ & = \sum_R \bigg[ 0.5 \cdot 0.1 \cdot P(R \mid C=1) P(W=1 \mid R,S=1) \\ & \quad + 0.5 \cdot 0.5 \cdot P(R \mid C=0) P(W=1 \mid R,S=1) \bigg] \\ & = 0.5 \cdot 0.1 \cdot \bigg[ P(R=1 \mid C=1) P(W=1 \mid R=1,S=1) \\ & \qquad + P(R=0 \mid C=1) P(W=1 \mid R=0,S=1) \bigg] \\ & \quad + 0.5 \cdot 0.5 \cdot \bigg[ P(R=1 \mid C=0) P(W=1 \mid R=1,S=1) \\ & \qquad + P(R=0 \mid C=0) P(W=1 \mid R=0,S=1) \bigg] \\ & = 0.5 \cdot 0.1 \cdot ( 0.8 \cdot 0.99 + 0.2 \cdot 0.9 ) + 0.5 \cdot 0.5 \cdot ( 0.2 \cdot 0.99 + 0.8 \cdot 0.9 ) \\ & = 0.2781 \end{align}

And,