# Compute truncated normal distribution with specific mean and variance

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?

• Formulas for post mean and variance in terms of pre mean and pre variance are in wikipedia. This gives you two non-linear equations that can be solved with rootSolve::multiroot. May 13, 2019 at 20:39
• Thanks for the suggestion. I checked wikipedia. The equations involve $\alpha$, which depends on the untruncated mean and standard deviation as well. Together with this, I can't see a way to use these equations to get values for the truncated mean and sd. May 13, 2019 at 21:20
• That comment appears to contradict your post. The post strongly implies there is truncation to the left of $a=0$ only. Is that how we should continue understanding the question?
– whuber
May 13, 2019 at 21:58
• Definitely. I can't see how my comment contradicts my post though. Can you explain? May 13, 2019 at 22:04
• I wonder whether mean 100 and standard deviation 240 is actually possible for the truncated distribution? Is there a theoretical result that all combinations of (positive) mean and variance can be attained? May 24, 2019 at 8:44

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)$$.