The PDF of the half-normal distribution is:

$$f(x;\sigma)=\frac{\sqrt{2}}{\sigma\sqrt{\pi}}\exp\left(-\frac{x^2}{2\sigma^2}\right)\,\quad x>0\,.$$

The integral $$\int_{0}^{\infty}f(x;\sigma)\mathrm{d}x=\int_{0}^{\infty}\frac{\sqrt{2}}{\sigma\sqrt{\pi}}\exp\left(-\frac{x^2}{2\sigma^2}\right)\mathrm{d}x$$

should be equal to one. The code used to check this:

Integrate[(Sqrt[2/Pi]/s)*Exp[-(x^2)/(2*s^2)], {x, 0, Infinity},
    Assumptions -> Re[s^2] > 0]

Mathematica gives $\frac{1}{\sqrt{\sigma}}$ which doesn't make sense. What am I doing wrong?

  • 1
    $\begingroup$ And it is equal to $1$. $\endgroup$
    – André Nicolas
    Apr 14 '14 at 3:33
  • $\begingroup$ See the Mathematica code I added to the question. $\endgroup$
    – user142972
    Apr 14 '14 at 3:48
  • $\begingroup$ Luckily not my problem, I can't afford Mathematica. Conceivably a bug. More likely an input typo. $\endgroup$
    – André Nicolas
    Apr 14 '14 at 4:22

Using Assuming[Element[s, Reals] && s > 0, ...] tells Mathematica that Re[s^2]>1 and also s itself is real to allow it to simplify final result to 1.

Mathematica did not simplify $\sqrt{\frac{1}{s^2}}$ to $\frac{1}{s}$ since it did not know if $s$ was real or not and if it was real, if it is positive or not. That is why you did not get the result 1 expected.

Assuming[Element[s, Reals] && s > 0, Integrate[(Sqrt[2/Pi]/s)*Exp[-(x^2)/(2*s^2)], 
     {x, 0, Infinity}]]

(* 1 *)
  • $\begingroup$ thanks this is the right answer, but for this OP, it might be helpful to provide a bit more textual explanation. $\endgroup$
    – Verbeia
    Apr 14 '14 at 6:19
  • $\begingroup$ Take s in the denominator outside the integral and you need no assumptions. It just gives 1. $\endgroup$
    – Bill Watts
    Mar 5 '19 at 8:33

Here are some variants:

td = TruncatedDistribution[{0, Infinity}, NormalDistribution[0, s]];
Assuming[Element[s, Reals] && s > 0, 
Integrate[Sqrt[2/Pi] Exp[-x^2/(2 s^2)]/s, {x, 0, Infinity}]]
Integrate[Sqrt[2/Pi] Exp[-x^2/(2 s^2)]/s, {x, 0, Infinity}, 
 Assumptions -> {Element[s, Reals], s > 0}]
Integrate[PDF[td, x], {x, 0, Infinity}, 
 Assumptions -> {Element[s, Reals], s > 0}]
 PDF[HalfNormalDistribution[Sqrt[2/Pi]/s], x], {x, 0, Infinity}, 
 Assumptions -> {Element[s, Reals], s > 0}]

Note all the integrals yield 1. As @Nasser has observed the assumptions need to be declared.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy