Simulating truncated normal distribution

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? • 1) rejection sampling---reject if it falls outside interval 2) inverse cdf see stats.stackexchange.com/questions/247094/… – kjetil b halvorsen Dec 4 '17 at 19:57
• @kjetilbhalvorsen sorry but I don't understand your answer, are these distributions the same or not? Is it worth to test it with kolmogorov-smirnov test? – Tobias Molenaar Dec 4 '17 at 20:06
• OK, sorry (you used rejection sampling) Those distributions are close enough for most purposes, I woud not bother with a test. And, there are better tests than Kolmogoroff! – kjetil b halvorsen Dec 4 '17 at 20:08
• @kjetilbhalvorsen So you say that by definition they are not the same? What other tests do you suggest? – Tobias Molenaar Dec 4 '17 at 20:09
• I wouldn't bother with a test, but I would do better graphics. The main deviation to be seen in your plot between the two distributions is caused by the poor resolution of the histogram! I would use a qqplot against the theoretical distribution. If you want a test it could be based on the correlation in that plot. – kjetil b halvorsen Dec 4 '17 at 20:12

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.