How do computers compute the expected value of an infinite distribution?

110 Views Asked by At

I was trying to compute:

$$E[X] = \int^{\infty}_{-\infty} xP(x)dx$$

but it might be a distribution over a sample space that is infinite. How do computers actually deal with this in an efficient and accurate way for any general distribution?

This question is close to a programming one because I am trying to program such an algorithm for any arbitrary distribution. Maybe there is a library that does this? Though, I am also interested in the mathematics of it too.

Note that I am trying to get an algorithm or something of that style because I do want a numerical answer.


Context:

I was particularly trying to compute then KL divergence, which is an expectation and if the sample space is infinite I run into the problem I posted. Though, I was more interested in a general answer than something specific to the KL divergence.

1

There are 1 best solutions below

0
On

If you know the distribution (i.e., have an explicit formula for it), any one of the many numerical quadrature formulas will do, but perhaps you'd have to "cut off" the tails (or estimate them by an approximate formula). Or perhaps some transformation reduces the integral to a finite range. There are variants (e.g. Gauss-Hermite formulas for infinite ranges.

Check out your numerical library, it might have routines for integration. Or your symbolic algebra package might help, giving an exact formula in terms of some higher functions, or offering a numerical approximation.