Calculating the expected value of truncated normal

Using the mills ratio result, let XN(μ,σ2), then

E(X|X<α)=μσϕ(aμσ)Φ(aμσ)

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μσ)=12πe12(xμσ)2fX,μ,σ(x)=12πσe12(xμσ)2
As you can see, we have an extra σ in the denominator of fX,μ,σ(x), which yields:
ϕ(xμσ)=σfX,μ,σ(x)
dnorm method gives you fX,μ,σ(x), where you need to multiply it with σ to obtain ϕ. Since your σ=2, this can be practically done via subtracting the second term again, which is 10.7124=0.2876:
10.28760.2876=0.4247
which is close to your estimate.

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

Leave a Comment