Note: this is fixed in version 9.
My question concerns the usage of NExpectation
and Expectation
and why I see the behavior I see in the following example.
First take some data and derive an EmpiricalDistribution
:
data = {4, 104, 96, 80, 22, 76, 106, 18, 98, 44, 112, 78, 50, 120, 2, 6,
100, 10, 68, 80, 42, 66, 100, 58, 4, 76, 18, 102, 6, 16, 52, 32, 62,
36, 18, 4, 54, 98, 38, 74, 16, 22, 102, 2, 2, 4, 22, 72, 100, 82, 48,
16, 34, 44, 130, 50, 48, 74, 60, 96, 8, 118, 30, 58, 84, 4, 70, 66,
40, 14, 92, 68, 42, 56, 56, 16, 40, 12, 22, 26, 98, 4, 80, 100, 36,
88, 48, 26, 28, 94, 22, 26, 78, 16, 52, 8, 10, 2};
dist = EmpiricalDistribution[data];
You can plot PDF
s and CDF
s of the distribution:
Row[{DiscretePlot[PDF[dist, x], {x, data}, Joined -> True, ImageSize -> 300],
DiscretePlot[CDF[dist, x], {x, 0, Max@data, 1}, Joined -> True, ImageSize -> 300]}]
They look like this:
That covers the background. Now execute the following and it gets a little odd:
Expectation[X \[Conditioned] X > 4, X \[Distributed] dist]
NExpectation[X \[Conditioned] X > 4, X \[Distributed] dist]
N[Expectation[X \[Conditioned] X > 4, X \[Distributed] dist]]
620/11
NExpectation[X \[Conditioned] X > 4, X \[Distributed]DataDistribution[<<"Empirical">>, {51}]]
56.3636
So, what gives?
How come NExpectation[...]
doesn't calculate an answer, but N[Expectation[...]]
does? Clearly, Expectation
handles EmpiricalDistribution
without a problem. One would think that NExpectation
would as well. Doesn't this seem odd? Just hoping that understanding why Mathematica does this might yield additional insights into Mathematica itself.
HistogramDistribution
works as expected in all 3 cases. $\endgroup$TracePrint
ofNExpectation
. For a kernel function it's extremely verbose. $\endgroup$