When exact inference is too costly, estimate the posterior from random samples. Prior, rejection and likelihood-weighting sampling generate each variable in a topological order (parents before children) from its CPT given the already-sampled parents; Gibbs sampling instead resamples one variable at a time. The estimates are consistent (they converge to the true probability as the number of samples grows) but are only approximations for finite N.
Convention for sampling by hand: for a binary variable, draw one random number u uniform on [0,1) and set X=1 if u<P(X=1∣parents), else X=0. Use the given random numbers in order, one per sampled variable, in topological order, unless the question states another rule.
PRIOR (FORWARD) SAMPLING
P^(x1,…,xn)=NN(x1,…,xn)
Each sample is drawn from the full joint, ignoring evidence; N(⋅) counts matching samples out of N.
WORKED EXAMPLE
Network A→B with P(A=1)=0.4, P(B=1∣A=1)=0.9, P(B=1∣A=0)=0.2. Evidence B=1. Using the convention above, four samples use the random-number pairs (uA,uB): (0.25,0.95), (0.70,0.10), (0.35,0.50), (0.85,0.15). Estimate P(A=1∣B=1) by rejection sampling.
▸ Solution
Sample 1: 0.25<0.4 so A=1; 0.95≥0.9 so B=0. Rejected.
Sample 2: 0.70≥0.4 so A=0; 0.10<0.2 so B=1. Kept, A=0.
Sample 3: 0.35<0.4 so A=1; 0.50<0.9 so B=1. Kept, A=1.
Sample 4: 0.85≥0.4 so A=0; 0.15<0.2 so B=1. Kept, A=0.
Kept 3 samples, one with A=1: estimate 1/3.
Answer: P^(A=1∣B=1)=1/3≈0.333 (the exact value is 0.75; four samples is very few).
LIKELIHOOD WEIGHTING
w=j∏P(ej∣Pa(Ej)),P^(Q=q∣e)=∑sw(s)∑s:Q(s)=qw(s)
Evidence variables are fixed, not sampled; each sample is weighted by the probability of the evidence given its parents' values in that same sample (a parent may itself be an evidence variable, fixed at its observed value). Non-evidence variables are sampled as usual, so no sample is rejected. The method is consistent because the probability of drawing the non-evidence values z times the weight equals P(z,e).
GIBBS SAMPLING: RESAMPLING ONE VARIABLE
P(xi∣mb(Xi))=αP(xi∣pa(Xi))Yj∈Children(Xi)∏P(yj∣pa(Yj))
All other variables are held at their current values; α normalises over the values xi. Only the CPTs of Xi and of its children are needed, because Xi is independent of everything else given its Markov blanket.