# Trying to understand a simple discrete Expectation proof of $E(h(X,Y))$

I was looking at the following proof and I'm having trouble understanding how the first part of the second line works?

Specifically: how were they able to pull the z out of the right summation? Going from the first line to the beginning of the second line, you aren't pulling $z$ out of the right summation. You are simply substituting $$P(h(X,Y) = z) = \sum_{\{(x,y): h(x,y) = z\}}P(X=x,Y=y).$$ This is true by definition of a probability mass function. You add over the input pairs $(x,y)$ such that $h(x,y) = z$.

On the other hand, going from the beginning of the second line to the right side of the second line, that isn't always true. It's true most of the time, however. I wouldn't worry about this re-arranging the order of summations, if I were you.

You are able to remove z out of the summation because you are explicitly looking for the probability when function value = z.

For example, consider a univariate random variable which is

z = x + y w.p. 0.2,

z = x-y w.p. 0.3 and

z = 2x w.p. 0.5. where x = 1, y = 0.

Then, E[Z] = 1 * 0.3 + 1 * 0.2 + 2 * 0.5 = 1 * 0.5 + 2 * 0.5.

= 1 * P(z=1) + 2 * P(z=2)

This is essentially what you are doing when you are getting z out of the summation.