# Calculating the expected value of truncated normal

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

$$E(X|X<α)=μ−σϕ(a−μσ)Φ(a−μσ)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?

## Answer

Your formula implementation is wrong because,
$$ϕ(x−μσ)=1√2πe−12(x−μσ)2≠fX,μ,σ(x)=1√2πσe−12(x−μσ)2\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 $$fX,μ,σ(x)f_{X,\mu,\sigma}(x)$$, which yields:
$$ϕ(x−μσ)=σfX,μ,σ(x)\phi\left(\frac{x-\mu}{\sigma}\right)=\sigma f_{X,\mu,\sigma}(x)$$
dnorm method gives you $$fX,μ,σ(x)f_{X,\mu,\sigma}(x)$$, where you need to multiply it with $$σ\sigma$$ to obtain $$ϕ\phi$$. Since your $$σ=2\sigma=2$$, this can be practically done via subtracting the second term again, which is $$1−0.7124=0.28761-0.7124=0.2876$$:
$$1−0.2876−0.2876=0.42471-0.2876-0.2876=0.4247$$
which is close to your estimate.

Attribution
Source : Link , Question Author : Kozolovska , Answer Author : gunes