Truncated Normal Distribution – Compute Truncated Normal Distribution with Specific Mean and Variance

rsimulationtruncated normal distributiontruncation

I have a simple setting: I simulate demand patterns that are distributed according to a truncated normal distribution with a given mean and variance after truncation. The truncation is from the left at zero (as demand is always positive).

However, all functions that draw from truncated normal distributions require me to specify the mean and variance of the normal distribution before truncation as e.g. the truncnorm function from the truncnorm package in R.

Example:

val = rtruncnorm(10000, a=0,  mean = 100, sd = 240)
print(mean(val))
[1] 232.2385
print(sd(val))
[1] 162.853

I would like the resulting variable val to have mean 100 and standard deviation 240. Is there any function in R to do this or is there a formula to compute the mean and variance before truncation given the truncation limits and the post-mean and post-variance?

Best Answer

What Jarle Tufto points out is that, if $X\sim\mathcal N^+(\mu,\sigma^2)$, then, defining $$\alpha=-\mu/\sigma\quad\text{and}\quad\beta=\infty$$ Wikipedia states that $$\Bbb E_{\mu,\sigma}[X]= \mu + \frac{\varphi(\alpha)-\varphi(\beta)}{\Phi(\beta)-\Phi(\alpha)}\sigma$$ and $$\text{var}_{\mu,\sigma}(X)=\sigma^2\left[1+\frac{\alpha\varphi(\alpha)-\beta\varphi(\beta)}{\Phi(\beta)-\Phi(\alpha)} -\left(\frac{\varphi(\alpha)-\varphi(\beta)}{\Phi(\beta)-\Phi(\alpha)}\right)^2\right]$$ That is, $$\Bbb E_{\mu,\sigma}[X]= \mu + \frac{\varphi(\mu/\sigma)}{1-\Phi(-\mu/\sigma)}\sigma\tag{1}$$ and $$\text{var}_{\mu,\sigma}(X)=\sigma^2\left[1-\frac{\mu\varphi(\mu/\sigma)/\sigma}{1-\Phi(-\mu/\sigma)} -\left(\frac{\varphi(\mu/\sigma)}{1-\Phi(-\mu/\sigma)}\right)^2\right]\tag{2}$$ Given the numerical values of the truncated moments $(\Bbb E_{\mu,\sigma}[X],\text{var}_{\mu,\sigma}(X))$, one can then solve numerically (1) and (2) as a system of two equations in $(\mu,\sigma)$, assuming $(\Bbb E_{\mu,\sigma}[X],\text{var}_{\mu,\sigma}(X))$ is a possible value for a truncated Normal $\mathcal N^+(\mu,\sigma^2)$.

Related Question