# Calculating the expected value of truncated normal

Using the mills ratio result, let $$X \sim N(\mu, \sigma^2)$$, then

$$E(X| X<\alpha) = \mu - \sigma\frac{\phi(\frac{a- \mu}{\sigma})}{\Phi(\frac{a-\mu}{\sigma})}$$

However, when calculating it in R. I don't obtain the correct results as

> mu <- 1
> sigma <- 2
> a <- 3
> x <- rnorm(1000000, mu, sigma)
> x <- x[x < a]
> mean(x)
[1] 0.4254786
>
> mu -  sigma * dnorm(a, mu, sigma) / pnorm(a, mu, sigma)
[1] 0.7124


What am I doing wrong?

Your formula implementation is wrong because, $$\phi\left(\frac{x-\mu}{\sigma}\right)=\frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2}\neq f_{X,\mu,\sigma}(x)=\frac{1}{\sqrt{2\pi}\sigma}e^{-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2}$$ As you can see, we have an extra $$\sigma$$ in the denominator of $$f_{X,\mu,\sigma}(x)$$, which yields: $$\phi\left(\frac{x-\mu}{\sigma}\right)=\sigma f_{X,\mu,\sigma}(x)$$ dnorm method gives you $$f_{X,\mu,\sigma}(x)$$, where you need to multiply it with $$\sigma$$ to obtain $$\phi$$. Since your $$\sigma=2$$, this can be practically done via subtracting the second term again, which is $$1-0.7124=0.2876$$: $$1-0.2876-0.2876=0.4247$$ which is close to your estimate.