(Source: Pieter Abbeel, CS188)
e.g. for
C | P(C) |
---|---|
red | 0.2 |
green | 0.3 |
blue | 0.5 |
# principle of sampling from the above probability table:
def get_a_C():
#step 1
x = np.random.rand()
#step 2
if x < 0.2:
C = 'red'
elif x < 0.5:
C='green'
else:
C='blue'
return C
print get_a_C()
blue
We are sampling from the right distribution (can easyly be shown).
If the numbers of sample goes to infinity, we get as estimate the correct join probabilities.
e.g. estimate of $P(C \mid r^1, d^0)$:
If we have a low number of counts we have a bad estimate of $P$. In the worste case: no counts for numerator and denominator $\rightarrow P \approx \frac{0}{0}$
Process:
Problem:
Idea: fix evidence variable and sample the rest.
e.g.: shape $\rightarrow$ color
and
square | circle | triangle | |
---|---|---|---|
red | 0.2 | 0.5 | 0.3 |
green | 0.3 | 0.2 | 0.3 |
blue | 0.5 | 0.3 | 0.4 |
and $Q=P( Shape \mid Color=blue)$
Samples with weights:
Process:
How to compute $X_i$ from $P(X_i \mid x_1, x_2, \dots , x_{i-1}, x_{i+1}, \dots , x_{n}) $ efficently?
$$ P(X_i \mid x_1, x_2, \dots , x_{i-1}, x_{i+1}, \dots , x_{n}) = $$$$ \frac{P(x_1, x_2, \dots , x_{i-1}, X_i, x_{i+1}, \dots ,x_{n})}{P(x_1, x_2, \dots x_{i-1}, x_{i+1}, \dots , x_{n})} = $$$$ =\frac{P(x_1, x_2, \dots , x_{i-1}, X_i, x_{i+1}, \dots ,x_{n})} {\sum_{x'_i} P(x_1, x_2, \dots x_{i-1}, x'_i, x_{i+1}, \dots , x_{n})} $$Factorization of $P(X_i \mid X_1, X_2, \dots , X_{i-1}, X_i, X_{i+1}, \dots , X_{n})$ by the topology of the Bayes net: e.g.
$$ P(X_i \mid X_1, X_2, \dots , X_{i-1}, X_i, X_{i+1}, \dots , X_{n}) = P(X_1) P(X_2 \mid X_1) P(X_3 \mid X_1) \dots P(X_i \mid \dots) P( \dots \mid \dots X_i \dots ) P( \dots \mid \dots X_i \dots )\dots $$The important point is that there are a lot of factors without any dependencies on $X_i$ (either on the left or right side of the bar '$\mid$'). So the fraction above can be reduced from this terms:
$$ P(X_i \mid x_1, x_2, \dots , x_{i-1}, x_{i+1}, \dots , x_{n}) = $$
$$
=\frac{P(X_i \mid \dots) P( \dots \mid \dots X_i \dots ) P( \dots \mid \dots X_i \dots )}
{\sum_{x'_i} P(x'_i \mid \dots) P( \dots \mid \dots x'_i \dots ) P( \dots \mid \dots x'_i \dots ) }
$$