Truncated normal distribution (source code)

= Truncated normal distribution
{title2=$TN_A(\mu,\sigma^2)$}
{wiki}

A <truncated normal distribution> is a <normal distribution> conditioned on belonging to a retained <measurable set> $A$ of positive <probability>. If $X\sim N(\mu,\sigma^2)$, $\sigma>0$, and $Z=\mathbb P(X\in A)>0$, its <probability density function>[density] is
$$
f_A(x)=\frac{1}{\sigma Z}\phi\left(\frac{x-\mu}{\sigma}\right)\mathbf1_A(x),
$$
where $\phi$ is the <normal probability density>[standard normal density]. The <indicator function>[indicator] is essential: the <probability density function>[density] is zero outside $A$. For an <interval> $A=(a,b)$, possibly with infinite endpoints,
$$
Z=\Phi\left(\frac{b-\mu}{\sigma}\right)-\Phi\left(\frac{a-\mu}{\sigma}\right).
$$
The same expression holds for any endpoint inclusion convention because the original <normal distribution> has no <atom of a measure>[atoms].

For a standard <normal distribution>[normal] variable restricted to positive values, $Z=1/2$ and the <probability density function>[density] is $2\phi(x)$ for $x>0$. Its <mean> is $\sqrt{2/\pi}$: integrate $2x\phi(x)$ using $\phi'(x)=-x\phi(x)$. <symmetric probability distribution>[Symmetry] gives <second moment> one, hence <variance> $1-2/\pi$. Truncation therefore changes the <mean> and <variance>; they are not generally the original parameters $\mu,\sigma^2$.

<sign-truncated normal sampling> uses <inverse transform sampling> to generate this <conditional probability>[conditional] law directly. <rejection sampling>[Rejection] from the original <normal distribution>[normal] is also exact but may be slow when the retained set has very small <probability>.