Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

Practice: MCMC and Simulation of Systems

Four practice problems on Metropolis sampling, Gibbs sampling, queues and inventory, each with a runnable solution you can check against theory.

This final practice set mixes the second half of the course: Markov chain Monte Carlo and simulation of systems. Each problem has an exact answer you can compare with, which is the best habit when you test a sampler or a simulator. Work out your approach first, then run the solution.

Problem 1: sample a Gamma distribution with Metropolis

The Gamma distribution with shape 3 and scale 1 has unnormalized density xยฒ e^{โˆ’x} for x > 0. Its mean is 3 and its variance is 3. Write a random-walk Metropolis sampler for it (use log densities, reject any proposal with x โ‰ค 0), run 50,000 steps from x = 10, discard 2,000 steps of burn-in, and compare the mean and variance with the exact values.

Hint: the log density is 2ยทlog(x) โˆ’ x; for x โ‰ค 0 set it to โˆ’โˆž.

Python Solution 1: Metropolis for Gamma(3, 1)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Problem 2: Gibbs sampling a trivariate normal

Three variables have zero mean, unit variance, and every pair has correlation 0.5. For such a distribution each full conditional is normal: given the other two, the mean of one variable is 0.5ยท(sum of the others) / (1 + 0.5) = (sum of the others)/3 and the variance is 1 โˆ’ 2ยท0.5ยฒ / (1 + 0.5) = 1 โˆ’ 1/3 = 2/3. Write a Gibbs sampler and check that the sample correlations come out near 0.5.

Hint: the conditional mean of xโ‚ given xโ‚‚, xโ‚ƒ is (xโ‚‚ + xโ‚ƒ)/3.

Python Solution 2: three-variable Gibbs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Problem 3: a busy queue

An M/M/1 queue has arrival rate ฮป = 0.9 and service rate ฮผ = 1. Predict the mean wait in the queue and the mean number in the system, then simulate with Lindley's recursion using five independent replications to get an honest estimate and spread.

Hint: W_q = ฯ/(ฮผ โˆ’ ฮป) = 9, L = ฯ/(1 โˆ’ ฯ) = 9. Near ฯ = 1 a single run converges slowly, so average across replications.

Python Solution 3: replicated M/M/1 simulation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The five replication means differ noticeably from one another, which shows why a single long run would not have been enough to trust. Little's law then gives the mean number in the system from the mean sojourn time W_q + 1/ฮผ.

Problem 4: a different newsvendor

A shop buys a perishable item for 5, sells it for 12, and recovers 2 for each unsold unit. Demand is normal with mean 200 and standard deviation 40. Find the best order quantity, first with the critical-ratio formula and then by simulation.

Hint: underage cost is 12 โˆ’ 5 = 7, overage cost is 5 โˆ’ 2 = 3, so the critical ratio is 7/(7 + 3) = 0.7.

Python Solution 4: newsvendor with salvage value

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Because the critical ratio 0.7 exceeds 0.5, the best quantity is above average demand: here a lost sale costs more than an unsold item.

Self-check

Before you move on, make sure you can answer these without looking back:

  1. What does detailed balance guarantee about a Markov chain?
  2. Why do we need to discard burn-in, and how can you detect that a chain has not converged?
  3. In an M/M/1 queue, what happens to the wait when utilization goes from 0.9 to 0.99?
  4. What does the critical ratio compare?

If any of these feels shaky, revisit MCMC diagnostics, queue simulation or inventory simulation.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. For the Gamma(3, 1) target in Problem 1, a proposal x <= 0 should be:

    The target has zero density for x <= 0, so the acceptance ratio is zero and the chain stays put.
  2. 2. In the trivariate Gibbs sampler, the conditional mean of x1 given the others is:

    With pairwise correlation 0.5 the regression coefficients are 0.5 / 1.5 = 1/3 each.
  3. 3. Why average over several replications in the busy-queue problem?

    Near rho = 1 successive waits are strongly dependent, so independent replications give honest error bars.
  4. 4. A critical ratio above 0.5 means the best order is:

    The normal quantile at a ratio above 0.5 is positive, so Q* = mu + sigma z exceeds mu.

Summary

  • Metropolis needs only an unnormalized (log) density, and must reject proposals outside the support.
  • Gibbs sampling uses full conditionals; for multivariate normals they are normal with simple means and variances.
  • Compare simulations with exact results (queue formulas, critical ratio) to validate them, and use replications near instability.
  • You now have a toolkit: sampling, integration, variance reduction, MCMC and system simulation. Go back to the track page for the full list of lessons.

Back to the course: Monte Carlo Simulation