Expectation of Random Variable as an integral I am currently taking a course on probability based on measure theory. We are now doing random variables and we just learned that $E(X) = \int X dP$. However, I am having trouble interpreting this statement. X is a function from the sample space to the reals. Doesn't this integral simply return $\int f(x)dx$, when we actually want $\int xf(x)dx$, as this is the expectation?
 A: You start with a probability space $(\Omega,\mathcal F,\mathbb P)$, and a Borel measurable function $X:\Omega\to\mathbb R$. This induces a measure $\mu$ on the real line $\mathbb R$, via
$$
\mu(B)=P(X^{-1}(B))=P(X\in B)
$$
for any Borel set $B$. If this measure $\mu$ can be written in the form $\mu(B)=\int_B f(x)\,dx$ for some function $f$, then we say that $X$ has density $f$. 
Now, it turns out to be the case that, for any measurable function $g:\mathbb R\to \mathbb R$, that
$$
\int_\Omega g(X(\omega))\,d\mathbb P=\int_\mathbb R g(x)\,d\mu
$$
See for example the Wikipedia page on pushforward measures.. To prove this, you have to first prove it for simple functions $g$, then leverage that result to prove it for general nonnegative $g$, then extend it to general $g$. This proof method follows the way integration is defined, and is common in low-level measure theory results.
In particular, when $g(x)=x$, you get
$$
E[X]=\int_\Omega X\,dP=\int_\mathbb R x\,d\mu\tag{1}
$$
Finally, the fact that $\mu(B)=\int_B f(x)\,dx$ for all $B$ can be used to show that
$$
\int_{\mathbb R} g(x)\,d\mu=\int_{\mathbb R} g(x)\cdot f(x)\,dx
$$
For a proof, see the Lebesgue-Radon-Nikodym derivative. Proving this is similar to proving the first result, going from simple $g$ to positive $g$ to general $g$.
Again, setting $g(x)=x$, we get
$$
\int_{\mathbb R}x\,d\mu=\int_{\mathbb R}x\cdot f(x)\,dx\tag2
$$
Combining $(1)$ and $(2)$ shows that the expression for the integral you just learned indeed agrees with the usual one. 
