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.3 Rejection Sampling

Given some evidence e, rejection sampling estimates P⁢(h∣e) using the formula

P⁢(h∣e)=P⁢(h∧e)P⁢(e).

This is computed by considering only the samples where e is true and by determining the proportion of these in which h is true. The idea of rejection sampling is that samples are generated as before, but any sample where e is false is rejected. The proportion of the remaining, non-rejected, samples where h is true is an estimate of P⁢(h∣e). If the evidence is a conjunction of assignments of values to variables, a sample is rejected when any variable is assigned a value different from its observed value.

Sample Tampering Fire Alarm Smoke Leaving Report
s1 f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e t⁢r⁢u⁢e f⁢a⁢l⁢s⁢e ✘
s2 f⁢a⁢l⁢s⁢e t⁢r⁢u⁢e f⁢a⁢l⁢s⁢e t⁢r⁢u⁢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 f⁢a⁢l⁢s⁢e ✘
s4 f⁢a⁢l⁢s⁢e t⁢r⁢u⁢e f⁢a⁢l⁢s⁢e t⁢r⁢u⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e ✔
s5 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 ✘
s6 f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e f⁢a⁢l⁢s⁢e t⁢r⁢u⁢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 f⁢a⁢l⁢s⁢e ✘
s8 t⁢r⁢u⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e t⁢r⁢u⁢e ✘
…
s1000 t⁢r⁢u⁢e f⁢a⁢l⁢s⁢e t⁢r⁢u⁢e f⁢a⁢l⁢s⁢e ✘
Figure 8.29: Rejection sampling for P⁢(t⁢a⁢m⁢p⁢e⁢r⁢i⁢n⁢g∣s⁢m⁢o⁢k⁢e∧⁢¬⁢r⁢e⁢p⁢o⁢r⁢t)
Example 8.41.

Figure 8.29 shows how rejection sampling is used to estimate P⁢(t⁢a⁢m⁢p⁢e⁢r⁢i⁢n⁢g∣s⁢m⁢o⁢k⁢e∧⁢¬⁢r⁢e⁢p⁢o⁢r⁢t). Any sample with S⁢m⁢o⁢k⁢e=f⁢a⁢l⁢s⁢e is rejected. The sample is rejected without considering any more variables. Any sample with R⁢e⁢p⁢o⁢r⁢t=t⁢r⁢u⁢e is rejected. The sample average from the remaining samples (those marked with ✔) is used to estimate the posterior probability of t⁢a⁢m⁢p⁢e⁢r⁢i⁢n⁢g.

Because P⁢(s⁢m⁢o⁢k⁢e∧⁢¬⁢r⁢e⁢p⁢o⁢r⁢t)=0.0128, we would expect about 13 samples out of the 1000 to have s⁢m⁢o⁢k⁢e∧⁢¬⁢r⁢e⁢p⁢o⁢r⁢t true; the other 987 samples would have s⁢m⁢o⁢k⁢e∧⁢¬⁢r⁢e⁢p⁢o⁢r⁢t false, and so would be rejected. Thus, 13 is used as n in Hoeffding’s inequality, which, for example, guarantees an error for any probability computed from these samples of less than 0.25 in about 86% of the cases, which is not very accurate.

The error in the probability of h depends on the number of samples that are not rejected, which proportional to P⁢(e). Hoeffding’s inequality can be used to estimate the error of rejection sampling, where n is the number of non-rejected samples. Therefore, the error depends on P⁢(e).

Rejection sampling does not work well when the evidence is unlikely. This may not seem like that much of a problem because, by definition, unlikely evidence is unlikely to occur. But, although this may be true for simple models, for complicated models with complex observations, every possible observation may be unlikely. Also, for many applications, such as in diagnosis, the user is interested in determining the probabilities because unusual observations are involved.