Skip to content
GATEiit

GATE DA · Artificial Intelligence

Approximate inference through sampling

Estimating Bayesian network probabilities from random samples: prior (ancestral) sampling, rejection sampling, likelihood weighting and Gibbs sampling (MCMC), with their weights, costs and accuracy. GATE asks sample generation, weights and estimates from given samples.

Before you start

Concept notes

1. Monte Carlo estimates and prior sampling

When exact inference is too costly, we estimate probabilities from N random samples. If N(x) samples have X=x, the estimate is P^(x)=N(x)/N. By the law of large numbers it converges to the true value (the estimator is consistent), and its standard error falls like 1/N: to halve the error you need four times as many samples.

Prior (ancestral, direct) sampling generates one complete sample from the network's joint distribution:

  1. Order the nodes topologically (parents before children).
  2. For each node, look up the CPT row for the sampled values of its parents and draw its value from that row.

To draw a binary variable with P(X=1∣parents)=p from a uniform random number u∈[0,1), set X=1 if u<p, else X=0. (Read the question for its convention; some use u≤p or the other branch first.)

The probability of generating a full assignment is exactly ∏iP(xi∣parents(xi)), the joint probability. So the fraction of samples matching any event estimates its probability. Prior sampling handles queries with no evidence.

1. C cloudy2. R rain3. S sprinkler4. W wet grass P(C1) 0.5 P(R1 | C) 0.8, 0.2P(S1 | C) 0.1, 0.5 P(W1 | R, S)
The network used in this topic. Prior sampling visits nodes in topological order: C, then R and S, then W

2. Rejection sampling

To estimate a posterior P(X∣e), rejection sampling draws prior samples and throws away every sample that disagrees with the evidence. Among the kept samples,

P^(x∣e)=N(x,e)N(e).

It is consistent, because the kept samples are distributed as P(⋅∣e).

Its weakness is waste. The expected fraction of samples kept is P(e). With 10 evidence variables, P(e) may be 10−4 or less, so out of a million samples only about a hundred survive. The number of samples needed grows exponentially with the number of evidence variables.

Method Evidence handling Weakness
Prior sampling none cannot condition
Rejection sampling discard mismatches rejects a fraction 1−P(e)
Likelihood weighting fix evidence, weight sample weights collapse when evidence is late or unlikely
Gibbs sampling fix evidence, random walk correlated samples, burn-in needed

When a GATE question gives a list of samples and asks for a rejection-sampling estimate, first cross out every sample that disagrees with the evidence, then count among the rest.

3. Likelihood weighting

Likelihood weighting (LW) avoids rejections. It visits nodes in topological order:

  • For a non-evidence variable, sample its value from its CPT row as in prior sampling.
  • For an evidence variable, do not sample: fix it to the observed value and multiply the sample's weight by P(ei∣parents(Ei)).

The weight starts at 1, so the final weight is

w=∏evidence EiP(ei∣parents(Ei)).

The estimate is a weighted count:

P^(x∣e)=∑samples with X=xw∑all samplesw.

Why it works: non-evidence values are drawn from their CPTs, and the weight supplies the missing evidence factors, so weight times sampling probability equals the joint P(x,e). LW is consistent.

Weakness: evidence only affects descendants through sampling. Ancestors of the evidence are drawn from the prior, ignoring the evidence, so many samples can get tiny weights when the evidence is unlikely or sits near the leaves. A few heavy samples then dominate the estimate.

4. Gibbs sampling (Markov chain Monte Carlo)

Gibbs sampling builds a Markov chain whose states are complete assignments with the evidence fixed:

  1. Start from any assignment that agrees with the evidence.
  2. Repeatedly pick a non-evidence variable Xi (in turn or at random) and resample it from its distribution given its Markov blanket:

P(xi∣mb(Xi))=α P(xi∣parents(Xi))∏Yj∈Children(Xi)P(yj∣parents(Yj)).

  1. Each state visited is a sample; estimate P(x∣e) as the fraction of states with X=x.

The chain's stationary distribution is the true posterior P(⋅∣e) (provided every state with non-zero probability can be reached), so the estimate converges to it.

Practical points:

  • Early states depend on the arbitrary start; discard a burn-in period.
  • Consecutive samples are correlated, so N Gibbs samples carry less information than N independent samples.
  • Each step needs only the CPTs of the variable and its children, which is cheap.
  • Gibbs can mix slowly when variables are nearly deterministic (CPT entries near 0 or 1).

All four samplers here are consistent: their estimates approach the exact answer as N→∞.

Formulas to remember

Monte Carlo estimate
P^(x)=N(x)N
Standard error
SE≈p(1−p)N∝1N
Rejection sampling estimateExpected fraction kept is P(e)
P^(x∣e)=N(x,e)N(e)
Likelihood weight
w=∏Ei∈EP(ei∣parents(Ei))
Likelihood weighting estimate
P^(x∣e)=∑X=xw∑w
Gibbs conditional
P(xi∣mb(Xi))=α P(xi∣pa(Xi))∏Yj∈ch(Xi)P(yj∣pa(Yj))

Common mistakes

  • In likelihood weighting, sampling the evidence variables instead of fixing them and multiplying in their probabilities.
  • Averaging the weights of the matching samples instead of dividing their total by the total weight of all samples.
  • Using the full network to resample in Gibbs; only the Markov blanket is needed, and the result must be normalised.
  • Forgetting that rejection sampling estimates are ratios over kept samples only, not over all samples drawn.
  • Expecting a handful of samples to match the exact posterior; the error falls only like 1/N.
  • Sampling a child before its parents; prior sampling needs a topological order.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Generating one prior sample

numerical

In the network of the figure, P(W=1∣R,S)=0.99,0.85,0.75,0.05 for (R,S)=(1,1),(1,0),(0,1),(0,0). Use the rule "value 1 if u<p" with random numbers 0.31,0.65,0.42,0.70 for C, R, S, W in that order. What sample is produced?

  1. Step 1: Sample C.

    Try it on paper first.

Example 2: Rejection sampling and its waste

numerical

With the same CPTs, P(W=1)=0.6096. You draw 1000 prior samples to estimate P(R=1∣W=1), and 438 of the samples with W=1 also have R=1. Suppose exactly the expected number of samples is kept. Find the estimate and the number rejected.

  1. Step 1: How many samples are kept?

    Try it on paper first.

Example 3: Likelihood weighting by hand

numerical

Evidence S=1, W=1; query R. Four LW samples drew (C,R)=(1,1),(0,0),(1,0),(0,1). Find each weight and the estimate of P(R=1∣S=1,W=1).

  1. Step 1: Write the weight formula.

    Try it on paper first.

Example 4: One Gibbs sampling step

numerical

Evidence S=0, W=1. The current state has C=1. Gibbs now resamples R. Find the distribution it samples from.

  1. Step 1: Identify the Markov blanket of R.

    Try it on paper first.

Previous year questions

1 official GATE DA question on this topic, newest first.

Practise these 1 PYQs →

Topic test

9 questions: up to 1 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE DA syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.