Skip to content

NExpectation ​

A numeric-only E[f(x)]E[f(x)] — falls back to summation/quadrature where Expectation stays symbolic.

NExpectation(f, Distributed(x, dist))tries the exact Expectation first; otherwise sums (discrete) or integrates (continuous) numerically. Always returns a machine float.
Typed signatures
TypePackageApplies
(any, expression<Distributed>) -> numberstatisticsalways

Domain: Statistics

Details
  • Tries Expectation(f, Distributed(x, dist)) first — a real (exact) answer whenever it resolves, just converted to a float. Only falls back to a numeric approximation past Expectation's own reach: f past a linear/quadratic polynomial in x, or a distribution Expectation has no closed form for at all.
  • Discrete, finite support (e.g. BinomialDistribution): an exact finite sum, converted to a float.
  • Discrete, infinite support (e.g. PoissonDistribution): summed with an infinite-tail cutoff — stops once the cumulative PDF mass is within 10−1210^{-12} of 11 and the current term is below 10−1210^{-12}.
  • Continuous: double-exponential (tanh-sinh) quadrature, generalized to a finite interval, a semi-infinite one, or the whole real line depending on the distribution's domain, converging to the same 10−1210^{-12} relative tolerance.
  • Distributions this package has no tabulated support/domain for (including every wave-4 compound distribution — TruncatedDistribution, MixtureDistribution, etc.) stay unevaluated — a documented gap, not a bug.

Examples

Basic #

E[sin⁡X]=sin⁡(μ)e−σ2/2E[\sin X] = \sin(\mu)e^{-\sigma^2/2} for X∼N(2,3)X \sim N(2,3); ≈0.01010138197\approx 0.01010138197 — cross-checked against wolframscript's N[Expectation[Sin[x], x \[Distributed] NormalDistribution[2,3]], 10]. Expectation itself stays unevaluated here (Sin is past its linear/quadratic reach).

live ↗
InNExpectation(sin⁡(x),Distributed(x,NormalDistribution⁡(2,3)))\mathrm{NExpectation}(\sin(x), \mathrm{Distributed}(x, \operatorname{NormalDistribution}(2, 3)))Out0.010 101 381 966 839 1880.010\,101\,381\,966\,839\,188

E[X3]=6/λ3=6/8=0.75E[X^3] = 6/\lambda^3 = 6/8 = 0.75 exactly, up to quadrature round-off; cross-checked against wolframscript's N[Expectation[x^3, x \[Distributed] ExponentialDistribution[2]], 10].

live ↗
InNExpectation(x3,Distributed(x,ExponentialDistribution⁡(2)))\mathrm{NExpectation}(x^3, \mathrm{Distributed}(x, \operatorname{ExponentialDistribution}(2)))Out0.750 000 000 000 000 10.750\,000\,000\,000\,000\,1
Scope #

E[1/(X+1)]=(1−e−μ)/μ≈0.3167376439E[1/(X{+}1)] = (1-e^{-\mu})/\mu \approx 0.3167376439 for X∼Poisson(3)X \sim \mathrm{Poisson}(3) — an infinite discrete sum with a tail cutoff, cross-checked against wolframscript's N[NExpectation[1/(x+1), x \[Distributed] PoissonDistribution[3]], 10].

live ↗
InNExpectation(1x+1,Distributed(x,PoissonDistribution⁡(3)))\mathrm{NExpectation}(\frac{1}{x+1}, \mathrm{Distributed}(x, \operatorname{PoissonDistribution}(3)))Out0.316 737 643 877 377 70.316\,737\,643\,877\,377\,7

A finite discrete sum over {0,…,5}\{0,\ldots,5\}; cross-checked against wolframscript's N[NExpectation[x^3, x \[Distributed] BinomialDistribution[5, 2/5]], 10].

live ↗
InNExpectation(x3,Distributed(x,BinomialDistribution⁡(5,25)))\mathrm{NExpectation}(x^3, \mathrm{Distributed}(x, \operatorname{BinomialDistribution}(5, \frac{2}{5})))Out15.4415.44
About
Examples4

See also: Expectation, NProbability, Distributed