Change of variables and Tonelli applied to a negative function I am trying to solve the following exercise. I believe I have solved the first two parts.  What do I do in the case of $f$ being negative? I cannot use the change of variables formula, or Tonelli for this. I assume I have to split the function into its positive and negative parts. How can I apply these theorems which are defined for a nonnegative function to a negative function?

Suppose that $X$ and $Y$ are independent and that $f(x, y)$ is nonnegative. Put $g(x)=E[f(x, Y)]$ and show that $E[g(X)]=E[f(X, Y)]$. Show more generally that $\int_{X \in A} g(X) d P=\int_{X \in A} f(X, Y) d P$. Extend to $f$ that may be negative.

Let $\mu$ be the distribution of $X$ and $\nu$ the distribution of $Y$. Since $X$ and $Y$ are independent, $\pi = (\nu \times \mu)$
\begin{align*}
 E[g(X)]\\
=\int_{\Omega} g(X) d P & \qquad  \text{Definition of expectation}\\
=\int_{\mathbb{R}} g(x)  \mu(dx) &   \qquad  \text{Lebesgue's change of variables formula}\\
=\int_{-\infty}^{\infty} E\left[f(x, Y)] \mu(dx)\right. &  \qquad  \text{ Definition of $g(x)$}
\end{align*}
Now, expanding the inner expectation,
\begin{align*}
=\int_{-\infty}^{\infty} \int_{\Omega} f(x, Y) d P \mu(dx) & \qquad \text{Definition of expectation} \\
=\int_{-\infty}^{\infty} \int_{-\infty}^{\infty} f(x, y) \nu(dy)\mu(dx) & \qquad \text{Lebesgue's change of variables}\\
=\int_{\mathbb{R} \times \mathbb{R}} f(x,y) \pi(d(y,x)) & \qquad \text{Tonelli theorem (similar to Fubini but for $L^+(\Omega)$) }\\
=\int_{\Omega} f(X, Y) d P  & \qquad \text{Lebesgue's change of variables}\\
=E[f(X, Y)]
\end{align*}
\begin{align*}
\int_{X \in A} g(X) d P\\
=\int_{A} g(x) \mu(dx) & \qquad \text{Lebesgue's change of variables}\\
=\int_{A} E[f(x, Y)) \mu(dx) & \qquad \text{Definition of $g(x)$}\\
=\int_{A} \int_{\mathbb{R}} f(x, y) \nu(dy)  \mu(dx) & \qquad \text{Change of variables }\\
=\int_{A \times \mathbb{R}} f(x,y) \pi(d(y,x)) & \qquad \text{Tonelli theorem}\\
=\int_{\left\{X \in A, Y \in \mathbb{R}^{2}\right\}} f(X, Y) d P & \qquad \text{Change of variables}\\
=\int_{X \in A} f(X, Y) d P
\end{align*}
 A: If the functions $f$ and $g$ are such that for each $x\in\mathbb R$, $f(x,Y)$, $f(X,Y)$, $g(X)$ are all integrable but possibly take negative values, then either the Fubini-Tonelli theorem or writing $f = f^+ - f^-$, $g = g^+ - g^-$ and using linearity together with a repetition of the argument you used for nonnegative functions provides the extension to functions that possibly take on negative values.

Added: Strictly speaking, we do not need to assume the functions are integrable, but we do require something just short of that. To fix ideas, let's just look at $g(X)$. By all accounts, we should be able to say
$$
\int g(X) = \int g^+(X) - \int g^-(X),
$$
but the point is that the quantity $\int g(X)$ is only defined when at least one of $\int g^+(X)<\infty$, $\int g^-(X)<\infty$ is assumed, to avoid having to assign values to expressions of the form $\infty - \infty$. Often, we do not work at this level of generality (though we surely still have occasion to), and instead we assume $g(X)$ is (absolutely Lebesgue) integrable in that we assume $\int |g(X)|<\infty$. Assuming absolute integrability, we may appeal to the "Fubini" part of the Fubini-Tonelli theorem, or we can use linearity just as well.
