[Math] Simulating truncated normal distribution

density functionnormal distributionprobabilitystatistics

The truncated normal distribution has pdf:
$$f(x;\mu,\sigma,a,b) = \frac{\varphi\left(\frac{x-\mu}{\sigma}\right)}{\sigma\left(\Phi\left(\frac{b-\mu}{\sigma}\right)-\Phi\left(\frac{a-\mu}{\sigma}\right)\right)}.$$ And $f=0$ otherwise.
Here $\varphi(x) = \frac{\exp\left(-\frac{x^2}{2}\right)}{\sqrt{2\pi}}$, $\Phi(x) = \frac{1}{2}(1+\operatorname{erf}(x/\sqrt{2}))$ and $a<b$, $\sigma>0$. I want to draw samples using only the normal distribution (using Python 2.7), and i do this by taking a value from the normal distribution with the same mean $\mu$ and sd $\sigma$ over and over again until this value lies between $a,b$.

I have made a histogram using a sample size of 100.000 and plotted it with this pdf and it looks similar.

My question is: are these distributions the same? enter image description here

Best Answer

Yes, they are the same.

Suppose $X\sim N(\mu,\sigma^2)$ and $A$ is some subset of the interval $[a,b].$ $$ \Pr( X\in A \mid a\le X\le b) = \frac{\Pr(X\in A)}{\Pr(a\le X\le b)} = \frac{\int_A \varphi_{\mu,\sigma} (x)\,dx}{\Phi(b) - \Phi(a)} $$ where $$ \varphi_{\mu,\sigma} (x) \,dx = \varphi\left( \frac{x-\mu} \sigma \right) \, \frac{dx} \sigma \text{ and } \varphi(x) = \frac 1 {\sqrt{2\pi}} e^{-x^2/2}. $$

Thus the conditional density of $X$ given the event $a\le X\le b,$ is $$ \frac{\varphi_{\mu,\sigma}(x)}{\Phi(b)-\Phi(a)}, $$ which is just the density that you've got.

Related Question