Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Importance Sampling and Approximate Inference | Markov Chain Monte Carlo and Approximate Inference
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Sampling Methods for Machine Learning

bookImportance Sampling and Approximate Inference

To understand importance sampling, you need to grasp the challenge it addresses: often, you want to estimate expectations with respect to a probability distribution that is difficult to sample from directly. Suppose you wish to estimate the expected value of a function under a complex target distribution p(x), but drawing samples from p(x) is infeasible. Instead, you can draw samples from a simpler proposal distribution q(x) and then adjust, or reweight, those samples to correct for the mismatch between q(x) and p(x). This process is called importance sampling. The key intuition is that you are "borrowing" samples from an easier distribution and compensating for this by assigning each sample a weight that reflects how much more or less likely it would be under the target distribution compared to the proposal.

12345678910111213141516171819202122
import numpy as np # Define target distribution: standard normal def p(x): return (1 / np.sqrt(2 * np.pi)) * np.exp(-0.5 * x**2) # Proposal distribution: normal with mean 2, std 1.5 def q(x): return (1 / (1.5 * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x - 2) / 1.5)**2) # Function to estimate: expectation of f(x) = x^2 under p(x) def f(x): return x**2 n_samples = 10000 samples = np.random.normal(2, 1.5, size=n_samples) # draw from q(x) weights = p(samples) / q(samples) # compute importance weights # Importance sampling estimator estimate = np.sum(f(samples) * weights) / np.sum(weights) print("Estimated E_p[x^2]:", estimate) # Compare to true value for standard normal: E[x^2] = 1
copy

While importance sampling is a powerful tool, it is not always practical. A central issue is variance: if the proposal distribution q(x) does not sufficiently overlap with the target p(x), the importance weights can become extremely variable. This means that a few samples dominate the estimator, resulting in high variance and unreliable results. To diagnose this, you can look at the effective sample size (ESS), which quantifies how many independent samples from the target distribution your weighted sample set is equivalent to. If ESS is much smaller than your total number of samples, your estimator is likely unreliable. In practice, importance sampling is most effective when the proposal closely matches the target, especially in the regions where the function you are integrating is large.

The concepts behind importance sampling are foundational to broader strategies for approximate inference in machine learning and statistics. When direct sampling from a target distribution is not possible, you often turn to techniques that use samples from simpler distributions and correct for discrepancies — importance sampling is a prime example. Many advanced algorithms, including some Markov Chain Monte Carlo (MCMC) variants and variational inference methods, build on these ideas to approximate otherwise intractable expectations and probabilities. Understanding the strengths and limitations of importance sampling gives you a clearer perspective on how and why these broader approximate inference techniques work.

question mark

Which statement best describes the main idea of importance sampling?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

bookImportance Sampling and Approximate Inference

Veeg om het menu te tonen

To understand importance sampling, you need to grasp the challenge it addresses: often, you want to estimate expectations with respect to a probability distribution that is difficult to sample from directly. Suppose you wish to estimate the expected value of a function under a complex target distribution p(x), but drawing samples from p(x) is infeasible. Instead, you can draw samples from a simpler proposal distribution q(x) and then adjust, or reweight, those samples to correct for the mismatch between q(x) and p(x). This process is called importance sampling. The key intuition is that you are "borrowing" samples from an easier distribution and compensating for this by assigning each sample a weight that reflects how much more or less likely it would be under the target distribution compared to the proposal.

12345678910111213141516171819202122
import numpy as np # Define target distribution: standard normal def p(x): return (1 / np.sqrt(2 * np.pi)) * np.exp(-0.5 * x**2) # Proposal distribution: normal with mean 2, std 1.5 def q(x): return (1 / (1.5 * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x - 2) / 1.5)**2) # Function to estimate: expectation of f(x) = x^2 under p(x) def f(x): return x**2 n_samples = 10000 samples = np.random.normal(2, 1.5, size=n_samples) # draw from q(x) weights = p(samples) / q(samples) # compute importance weights # Importance sampling estimator estimate = np.sum(f(samples) * weights) / np.sum(weights) print("Estimated E_p[x^2]:", estimate) # Compare to true value for standard normal: E[x^2] = 1
copy

While importance sampling is a powerful tool, it is not always practical. A central issue is variance: if the proposal distribution q(x) does not sufficiently overlap with the target p(x), the importance weights can become extremely variable. This means that a few samples dominate the estimator, resulting in high variance and unreliable results. To diagnose this, you can look at the effective sample size (ESS), which quantifies how many independent samples from the target distribution your weighted sample set is equivalent to. If ESS is much smaller than your total number of samples, your estimator is likely unreliable. In practice, importance sampling is most effective when the proposal closely matches the target, especially in the regions where the function you are integrating is large.

The concepts behind importance sampling are foundational to broader strategies for approximate inference in machine learning and statistics. When direct sampling from a target distribution is not possible, you often turn to techniques that use samples from simpler distributions and correct for discrepancies — importance sampling is a prime example. Many advanced algorithms, including some Markov Chain Monte Carlo (MCMC) variants and variational inference methods, build on these ideas to approximate otherwise intractable expectations and probabilities. Understanding the strengths and limitations of importance sampling gives you a clearer perspective on how and why these broader approximate inference techniques work.

question mark

Which statement best describes the main idea of importance sampling?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
some-alt