# How to derive expected value of a function of a discrete randomized variable

It is well known that the expected value for a function $$f$$ of a discrete random variable $$\rho$$ over $$x$$, the expected value is $$Ef=\sum_{-\infty}^{\infty}f(x)P(\rho=x)$$. I do not see how to derive this from(show it is equal to) the construction below.

Let $$\rho$$ be a discrete random variable on $$x$$ and define $$f=f(\rho)$$. I understand the motivation behind the expected value for $$\rho$$, given by $$E\rho=\sum_{-\infty}^{\infty}xP(\rho=x)$$.

Since $$f$$ is itself a discrete random variable taking only the values $$y=f(x)$$, it follows that $$P(f=y)=\sum_{x|f(x)=y}P(\rho=x)$$. Thus by the same motivation, $$Ef=\sum_{-\infty}^{\infty}yP(f=y)$$, which becomes $$Ef=\sum_{-\infty}^{\infty}y\sum_{x|f(x)=y}P(\rho=x)$$.

In Rozanov's Probability Theory: A Concise Course, it says this is equal to the well-known formula. How is this equal to the well-known formula? Sorry for any inaccuracies, I am a beginner in this.

Start with $$\mathbb Ef=\sum_{y=-\infty}^{\infty}y\sum_{x|f(x)=y}P(\rho=x)$$. $$\mathbb Ef=\sum_{y=-\infty}^{\infty}\color{red}{y}\sum_{x|f(x)=y}P(\rho=x) = \sum_{y=-\infty}^{\infty}\sum_{x|f(x)=y}\color{red}{y}P(\rho=x)=\sum_{y=-\infty}^{\infty}\sum_{x|f(x)=y}\color{red}{f(x)}P(\rho=x)$$ We got rid of $$y$$ under the sign of sum. In these two sums, you first take $$y$$ and then take all $$x$$ such that $$f(x)=y$$. This is the way to iterate over the all $$x$$ values: $$\{x\in\mathbb Z\}=\bigcup_{y\in\mathbb Z}\{x: f(x)=y\}$$. So this sums are equal to $$\mathbb Ef= \sum_{x=-\infty}^{\infty}f(x)P(\rho=x).$$