Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I wanted to find the probability of my normally-distributed random variable being at least 15, so I set up this integral:

Integrate[PDF[NormalDistribution[14, 3.7], x], {x, 15, Infinity}]

Imagine my surprise when I got this result:

0.393476 - 1.75334*10^-15 I

Granted, the angle is miniscule. But why is Mathematica producing this result? This happens even if I type in the PDF manually:

0.107822 E^(-0.036523 (-14 + x)^2)

My TI in rectangular mode returns a result equal to the real component of what Mathematica produces. No complex component.

It doesn't make sense for a real-valued function to have complex area anyway. What's going on here? Whenever I see e's and i's in the same room I tend to blame Euler, but I'm having trouble figuring out what Mathematica is doing.

Using the "proper" Mathematica command for computing this probability returns a value that agrees with my calculator.

Probability[15 <= x, x \[Distributed] NormalDistribution[14, 3.7]]

I just want to know why Mathematica thinks the integral has complex area.

share|improve this question
    
Those complex components are effectively 0's. Along with the e's and i's, you can now blame Old MacDonald. The actual issue is that Integrate can do"interesting" things when one mixes approximate numbers with what is really a set of symbolic methods. NIntegrate, being more appropriate for numeric functionality, will not give this imaginary fuzz. –  Daniel Lichtblau Nov 15 '13 at 17:12

1 Answer 1

up vote 1 down vote accepted

I looked at 5 cases. Conclusion at bottom.

ClearAll[x, a, b];
pdf = PDF[NormalDistribution[14, 3.7], x]

Mathematica graphics

a0 = pdf[[1]]

Mathematica graphics

b0 = pdf[[2, 2, 1]]

Mathematica graphics

Case 1

int1 = Integrate[ a Exp[b (-14 + x)^2], x]

Mathematica graphics

int1 /. {a -> a0, b -> b0}

Mathematica graphics

Simplify[(int1 /. x -> Infinity) - (int1 /. x -> 15)]

Mathematica graphics

Case 2

 int2 = Integrate[ a Exp[b (-14 + x)^2], {x, 15, Infinity}]

Mathematica graphics

 int2 /. {a -> a0, b -> b0}

Mathematica graphics

Case 3

 int3 = Integrate[ a Exp[b (-14 + x)^2] /. {a -> a0, b -> b0}, x]

Mathematica graphics

 Simplify[(int3 /. x -> Infinity) - (int3 /. x -> 15)]

Mathematica graphics

Case 4

 int4 = Integrate[a Exp[b (-14 + x)^2] /. {a -> a0, b -> b0}, {x, 15, k}]

Mathematica graphics

 Limit[int4, k -> Infinity]

Mathematica graphics

Case 5

 int5 = Integrate[a Exp[b (-14 + x)^2] /. {a -> a0, b -> b0}, {x, 15, Infinity}]

Mathematica graphics

 Chop[%]

Mathematica graphics

Conclusion

The error can be Chopped. Complex number show up due the function Erfi which comes due to the term exp[x^2] in the integrand as you can see from above

 Expand[(-14 + x)^2]  (* 196 - 28 x + x^2 *)
 Integrate[Exp[x^2], x]

Mathematica graphics

So this answers the question as to where does the complex number comes from. For example, a complex number can show up like this:

  Erfi[Sqrt[b] ] /. b -> b0

Mathematica graphics

As a general point, it is best to delay as long as possible using floating point values in expressions, and substitute these at the very end to get the "most" accurate result. As you can see from the cases above, when the numbers are substituted at the end, less "noise" was present in the intermediate result.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.