<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Importance Sampling |</title><link>https://chaeniverse.github.io/tags/importance-sampling/</link><atom:link href="https://chaeniverse.github.io/tags/importance-sampling/index.xml" rel="self" type="application/rss+xml"/><description>Importance Sampling</description><generator>HugoBlox Kit (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Tue, 17 Jan 2023 00:00:00 +0000</lastBuildDate><image><url>https://chaeniverse.github.io/media/icon_hu_da05098ef60dc2e7.png</url><title>Importance Sampling</title><link>https://chaeniverse.github.io/tags/importance-sampling/</link></image><item><title>Importance Sampling</title><link>https://chaeniverse.github.io/blog/importance-sampling/</link><pubDate>Tue, 17 Jan 2023 00:00:00 +0000</pubDate><guid>https://chaeniverse.github.io/blog/importance-sampling/</guid><description>&lt;h2 id="monte-carlo"&gt;Monte Carlo&lt;/h2&gt;
&lt;p&gt;기댓값을 계산하는 방법. $X \sim f(x)$ 일 때 $g(X)$ 의 기댓값은 다음 세 가지 형태로 동치다.&lt;/p&gt;
$$\underbrace{E[g(X)]}_{\text{①}} \;=\; \underbrace{\int g(x) f(x)\,dx}_{\text{②}} \;\approx\; \underbrace{\frac{1}{n}\sum_{i=1}^{n} g(x_i)}_{\text{③}}$$&lt;p&gt;①, ②는 정의상 같고, ③은 $f$ 에서 뽑은 샘플 $\{x_i\}$ 의 평균으로 ②를 근사한 것 (대수의 법칙).&lt;/p&gt;
&lt;h3 id="예제-1-단순-mc"&gt;예제 1: 단순 MC&lt;/h3&gt;
&lt;p&gt;$g(x) = e^{-2x}$, $X \sim \mathrm{Unif}(0,1)$ 일 때 $E[g(X)]$ 을 구하라.&lt;/p&gt;
&lt;p&gt;이때 $f(x) = 1$ (Unif(0,1) 의 밀도)이므로&lt;/p&gt;
$$E[g(X)] = \int_0^1 e^{-2x}\,dx \;\approx\; \frac{1}{N}\sum_{i=1}^{N} e^{-2 x_i}, \qquad x_i \sim \mathrm{Unif}(0,1)$$&lt;p&gt;균등 분포에서 $N$ 개를 뽑아 $g$ 에 넣고 평균을 내면 끝.&lt;/p&gt;
&lt;h2 id="importance-sampling"&gt;Importance Sampling&lt;/h2&gt;
&lt;p&gt;문제는 다음 두 경우다.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;$f(x)$ 에서 샘플링이 어렵다.&lt;/strong&gt; 닫힌 형태로 sampling이 안 되는 분포.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;$g(x) \cdot f(x)$ 가 $f$ 의 꼬리/희소 영역에 몰려 있다.&lt;/strong&gt; $f$ 에서 뽑은 대부분의 샘플이 $g \cdot f$ 가 큰 영역을 못 맞춰서 분산이 매우 커진다.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;이럴 때는 다른 분포 $\phi(x)$ (&amp;ldquo;proposal&amp;rdquo; 또는 &amp;ldquo;importance distribution&amp;rdquo;)에서 샘플을 뽑고 가중치로 보정한다. 핵심 항등식:&lt;/p&gt;
$$E_f[g(X)] = \int g(x) f(x)\,dx = \int g(x) \cdot \frac{f(x)}{\phi(x)} \cdot \phi(x)\,dx = E_\phi\!\left[g(X) \cdot \frac{f(X)}{\phi(X)}\right]$$&lt;p&gt;→ $\phi$ 에서 샘플 $\{x_i\}$ 을 뽑고&lt;/p&gt;
$$E_f[g(X)] \;\approx\; \frac{1}{N}\sum_{i=1}^{N} g(x_i) \cdot \underbrace{\frac{f(x_i)}{\phi(x_i)}}_{\text{weight}}$$&lt;p&gt;여기서 $f(x)/\phi(x)$ 가 &lt;strong&gt;importance weight&lt;/strong&gt;. (이 가중치 때문에 &amp;ldquo;importance&amp;rdquo; sampling이라는 이름이 붙은 것)&lt;/p&gt;
&lt;h2 id="예제-2"&gt;예제 2: $\int (x-1)(x-2)\,dx$&lt;/h2&gt;
&lt;p&gt;이 식은 따로 정해진 $f(x)$ 가 없다 (단순 적분). $\phi(x) \sim \mathrm{Exp}(1)$, 즉 $\phi(x) = e^{-x}$ ($x&gt;0$) 를 proposal로 쓰자.&lt;/p&gt;
$$\int (x-1)(x-2)\,dx = \int \underbrace{\frac{(x-1)(x-2)}{e^{-x}}}_{\text{new } g(x)} \cdot \underbrace{e^{-x}}_{\text{new } f(x)}\,dx$$&lt;p&gt;→ $X \sim \mathrm{Exp}(1)$ 에서 $N$ 개를 뽑아 새 $g$ 에 넣고 평균:&lt;/p&gt;
$$\widehat{E[g(X)]} = \frac{1}{N}\sum_{i=1}^{N} \frac{(x_i-1)(x_i-2)}{e^{-x_i}}, \qquad x_i \sim \mathrm{Exp}(1)$$&lt;h2 id="예제-3--with--proposal"&gt;예제 3: $\int e^{-|x|}\,dx$ with $N(0,1)$ proposal&lt;/h2&gt;
&lt;p&gt;$\phi(x) \sim N(0,1)$, 즉 $\phi(x) = \dfrac{1}{\sqrt{2\pi}} e^{-x^2/2}$ 를 쓰자.&lt;/p&gt;
$$\int e^{-|x|}\,dx = \int \underbrace{\frac{e^{-|x|}}{\frac{1}{\sqrt{2\pi}} e^{-x^2/2}}}_{\text{new } g(x)} \cdot \underbrace{\frac{1}{\sqrt{2\pi}} e^{-x^2/2}}_{\text{new } f(x)}\,dx$$&lt;p&gt;→ $X \sim N(0,1)$ 에서 $N$ 개 sampling 후 새 $g$ 에 넣어 평균을 구하면 된다.&lt;/p&gt;
&lt;h2 id="왜--을-proposal로-골랐나"&gt;왜 $N(0,1)$ 을 proposal로 골랐나&lt;/h2&gt;
&lt;p&gt;원래의 $g(x) = e^{-|x|}$ 는 0 근처에 봉우리가 있는 양옆 대칭 함수다. $\phi(x) \sim N(0,1)$ 도 비슷한 모양 — 0 근처에 봉우리, 양쪽으로 빠르게 감소.&lt;/p&gt;
&lt;blockquote class="border-l-4 border-neutral-300 dark:border-neutral-600 pl-4 italic text-neutral-600 dark:text-neutral-400 my-6"&gt;
&lt;p&gt;&lt;strong&gt;proposal $\phi(x)$ 를 $|g(x) f(x)|$ 와 모양이 비슷하게 잡으면 분산이 작아져 효율이 좋다.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;만약 $\phi$ 가 $|g \cdot f|$ 가 큰 영역을 거의 안 덮으면, 거의 모든 샘플의 weight $f(x)/\phi(x)$ 가 0 근처가 되고, 가끔 한 샘플의 weight가 매우 커져서 추정량의 분산이 폭발한다.&lt;/p&gt;
&lt;h2 id="한-줄-요약"&gt;한 줄 요약&lt;/h2&gt;
&lt;blockquote class="border-l-4 border-neutral-300 dark:border-neutral-600 pl-4 italic text-neutral-600 dark:text-neutral-400 my-6"&gt;
&lt;p&gt;&lt;strong&gt;샘플링은 $\phi$ 에서, 평균은 weight $f/\phi$ 로 보정.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Proposal $\phi$ 의 모양을 신경써서 고를수록 추정량의 분산이 작아진다.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr&gt;
&lt;blockquote class="border-l-4 border-neutral-300 dark:border-neutral-600 pl-4 italic text-neutral-600 dark:text-neutral-400 my-6"&gt;
&lt;p&gt;원문:
&lt;/p&gt;
&lt;/blockquote&gt;</description></item></channel></rss>