Answers.Body: <p>Importance sampling is typically used when the distribution of interest is difficult to sample from - e.g. it could be computationally expensive to draw samples from the distribution - or when the distribution is only known up to a multiplicative constant, such as in Bayesian statistics where it is intractable to calculate the marginal likelihood; that is</p>
  <p><span class="math-container">$$p(\theta|x) = \frac{p(x|\theta)p(\theta)}{p(x)} \propto p(x|\theta)p(\theta)$$</span></p>
  <p>where <span class="math-container">$p(x)$</span> is our marginal likelihood that may be intractable and so we can't calculate the full posterior and so other methods must be used to generate samples from this distribution. When I say intractable, note that</p>
  <p><span class="math-container">$$p(x) = \int_{\Theta} p(x|\theta)p(\theta) d\theta$$</span></p>
  <p>and so intractable here means that either a) the integral has no analytical solution or b) a numerical method for computing this integral may be too expensive to run.</p>
  <p>In the instance of your die example, you are correct that you could calculate the theoretical expectation of the bias dice analytically and this would probably be a relatively simple calculation. However, to motivate why importance sampling may be be useful in this scenario, consider calculating the expectation using Monte Carlo methods. It would be much simpler to uniformly sample a random integer from 1-6 and calculate the importance sampling ratio <span class="math-container">$x \frac{g(x)}{f(x)}$</span> than it would be to draw samples from the bias dice, not least because most programming languages have built in methods to randomly sample integers.</p>
  <p>As your question is tagged as reinforcement learning I will add why it is useful in the RL domain. One reason is that it may be our policy of interest is expensive to sample from, so instead we can just generate actions from some other simple policy whilst still learning about the policy of interest. Second, we could be interested in a policy that is deterministic (greedy) but still be able to explore, so we can have an off-policy distribution that explores much more frequently.</p>
  <p>NB: it may not be clear how you can use importance sampling if the distribution is only known up to a constant so see <a href="https://ai.stackexchange.com/a/21360/36821">this answer</a> for an explanation.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-04T09:20:04.427Z
Answers.LastActivityDate: 2021-04-07T09:40:46.220Z
Answers.LastEditDate: 2021-04-07T09:40:46.220Z
Answers.UserDisplayName: David
Body: <p>I was studying the off-policy policy improvement method. Then I encountered <em>importance sampling</em>. I completely understood the mathematics behind the calculation, but I am wondering what is the practical example of importance sampling.</p>
  <p>For instance, in a <a href="https://www.youtube.com/watch?v=V8f8ueBc9sY&amp;t=509s&amp;ab_channel=BenLambert" rel="nofollow noreferrer">video</a>, it is said that we need to calculate the expected value of a biased dice, here <span class="math-container">$g(x)$</span>, in terms of the expected value of fair dice, <span class="math-container">$f(x)$</span>. Here is a screenshot of the video.</p>
  <p><a href="https://i.sstatic.net/TuBMq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/TuBMq.png" alt="enter image description here" /></a></p>
  <p>Why do we need that, when we have the probability distribution of the biased dice?</p>
  
Tags: <reinforcement-learning><monte-carlo-methods><off-policy-methods><importance-sampling>
Title: Why do we need importance sampling?
UserDisplayName: Alireza Hosseini
ThreadText: # Why do we need importance sampling?
  
  (Asked by: username_0 on 2021-01-04)
  
  I was studying the off-policy policy improvement method. Then I encountered *importance sampling*. I completely understood the mathematics behind the calculation, but I am wondering what is the practical example of importance sampling.
  
  For instance, in a [video](https://www.youtube.com/watch?v=V8f8ueBc9sY&t=509s&ab_channel=BenLambert), it is said that we need to calculate the expected value of a biased dice, here $g(x)$, in terms of the expected value of fair dice, $f(x)$. Here is a screenshot of the video.
  
  [![enter image description here](https://i.sstatic.net/TuBMq.png)](https://i.sstatic.net/TuBMq.png)
  
  Why do we need that, when we have the probability distribution of the biased dice?
  
  **Tags:** reinforcement-learning, monte-carlo-methods, off-policy-methods, importance-sampling
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-01-04. Score: 7
  
  Importance sampling is typically used when the distribution of interest is difficult to sample from - e.g. it could be computationally expensive to draw samples from the distribution - or when the distribution is only known up to a multiplicative constant, such as in Bayesian statistics where it is intractable to calculate the marginal likelihood; that is
  
  $$p(\theta|x) = \frac{p(x|\theta)p(\theta)}{p(x)} \propto p(x|\theta)p(\theta)$$
  
  where $p(x)$ is our marginal likelihood that may be intractable and so we can't calculate the full posterior and so other methods must be used to generate samples from this distribution. When I say intractable, note that
  
  $$p(x) = \int\_{\Theta} p(x|\theta)p(\theta) d\theta$$
  
  and so intractable here means that either a) the integral has no analytical solution or b) a numerical method for computing this integral may be too expensive to run.
  
  In the instance of your die example, you are correct that you could calculate the theoretical expectation of the bias dice analytically and this would probably be a relatively simple calculation. However, to motivate why importance sampling may be be useful in this scenario, consider calculating the expectation using Monte Carlo methods. It would be much simpler to uniformly sample a random integer from 1-6 and calculate the importance sampling ratio $x \frac{g(x)}{f(x)}$ than it would be to draw samples from the bias dice, not least because most programming languages have built in methods to randomly sample integers.
  
  As your question is tagged as reinforcement learning I will add why it is useful in the RL domain. One reason is that it may be our policy of interest is expensive to sample from, so instead we can just generate actions from some other simple policy whilst still learning about the policy of interest. Second, we could be interested in a policy that is deterministic (greedy) but still be able to explore, so we can have an off-policy distribution that explores much more frequently.
  
  NB: it may not be clear how you can use importance sampling if the distribution is only known up to a constant so see [this answer](https://ai.stackexchange.com/a/21360/36821) for an explanation.
  
  
  ---
  
  