8.6 Stochastic Simulation

The third edition of Artificial Intelligence: foundations of computational agents, Cambridge University Press, 2023 is now available (including full text).

8.6.2 Forward Sampling in Belief Networks

Forward sampling is a way to generate a sample of every variable in a belief network so that each sample is generated in proportion to its probability. This enables us to estimate the prior probability of any variable.

Suppose X1,…,Xn is a total ordering of the variables so that the parents of each variable come before the variable in the total order. Forward sampling draws a sample of all of the variables by drawing a sample of each variable X1,…,Xn in order. First, it samples X1 using the cumulative distribution, as described above. For each of the other variables, due to the total ordering of variables, when it comes time to sample Xi, it already has values for all of Xi’s parents. It now samples a value for Xi from the distribution of Xi given the values already assigned to the parents of Xi. Repeating this for every variable generates a sample containing values for all of the variables. The probability distribution of a query variable is estimated by considering the proportion of the samples that have assigned each value of the variable.

Example 8.40.

To create a set of samples for the belief network of Figure 8.3, suppose the variables are ordered: T⁢a⁢m⁢p⁢e⁢r⁢i⁢n⁢g, F⁢i⁢r⁢e, A⁢l⁢a⁢r⁢m, S⁢m⁢o⁢k⁢e, L⁢e⁢a⁢v⁢i⁢n⁢g, R⁢e⁢p⁢o⁢r⁢t.

Sample Tampering Fire Alarm Smoke Leaving Report
s1 f⁢a⁢l⁢s⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e
s2 f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e
s3 f⁢a⁢l⁢s⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e
s4 f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e t⁢r⁢u⁢e
s5 f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e
s6 f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e
s7 t⁢r⁢u⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e
s8 t⁢r⁢u⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e t⁢r⁢u⁢e
…
s1000 t⁢r⁢u⁢e f⁢a⁢l⁢s⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e
Figure 8.28: Sampling for a belief network

First the algorithm samples T⁢a⁢m⁢p⁢e⁢r⁢i⁢n⁢g, using the cumulative distribution. Suppose it selects T⁢a⁢m⁢p⁢e⁢r⁢i⁢n⁢g=f⁢a⁢l⁢s⁢e. Then it samples F⁢i⁢r⁢e using the same method. Suppose it selects F⁢i⁢r⁢e=t⁢r⁢u⁢e. Then it samples a value for A⁢l⁢a⁢r⁢m, using the distribution P(Alarm∣Tampering=false,Fire=true). Suppose it selects A⁢l⁢a⁢r⁢m=t⁢r⁢u⁢e. Next, it samples a value for S⁢m⁢o⁢k⁢e using P(Smoke∣Fire=true). And so on for the other variables. It has thus selected a value for each variable and created the first sample of Figure 8.28. Notice that it has selected a very unlikely combination of values. This does not happen very often; it happens in proportion to how likely the sample is. It repeats this until it has enough samples. In Figure 8.28, it generated 1000 samples.

The probability that R⁢e⁢p⁢o⁢r⁢t=t⁢r⁢u⁢e is estimated from the proportion of the samples where the variable R⁢e⁢p⁢o⁢r⁢t has value t⁢r⁢u⁢e.