# Calculating expectation of a function of a continuous random variable?

Assume we have a continuous random variable $$X$$, with a probability density function

$$f(x) = 1 - \frac{x}{2},\; 0 < x < 2$$

Let's say I want to calculate $$E(2X)$$. I will use two different methods, and get different results, so I obviously understand something wrong.

METHOD 1:

Using the formula for the expectation of a function of a random variable, I get:

$$E(2X) = \int_{-\infty}^{+\infty} 2xf(x)dx = \int_0^2 (2x-2x^2)dx = \frac{4}{3}$$

I understand this formula intuitively, as it's pretty much exactly the same as in the discrete case, if we replace the integral with a finite sum and the probability density function with the probability mass function. Only the values change, and not the distribution, so we only apply the function 2x to the values, and not the distribution $$f(x)$$.

METHOD 2:

Let's say I declare a new continuous random variable, $$Y = 2X$$. I can calculate the probability density function of $$Y$$:

$$g(y) = |\frac{dx}{dy}|f(x) = \frac{1}{2}f(\frac{y}{2}) = \frac{1}{2}(1-\frac{y}{4})$$

for $$0.

Then I can calculate:

$$E(2X) = E(Y) = \int_{-\infty}^{+\infty} y\;g(y)dx = \int_0^4 \frac{1}{2}(1-\frac{y}{4})dx = \frac{2}{3}$$

I don't understand what I did wrong here.

But I also don't understand the formula for $$g(y)$$ intuitively - shouldn't $$g(y)$$ and $$f(x)$$ be the same functions if they're injective, similar to the discrete case?

Anyway, if someone can point out what I did wrong, I would be very grateful.

You forgot to multiply by $$y$$ for the second computation.
$$g(y)$$ and $$f(x)$$ are different functions, but they are related by $$g(y)=\frac{1}{2}f\big(\frac{y}{2}\big)$$ and are both decreasing (also see here). For instance, $$h_{1}(x)=x$$ and $$h_{2}(x)=2x$$ are injective, but $$h_{1}(1)=1$$ and $$h_{2}(1)=2$$.
It is a good idea to check if $$g(y)=\frac{1}{2}(1-\frac{y}{4})$$ for $$0 is a valid pdf. Indeed we have $$g(y)\geq 0$$ for $$0 and $$\int_{-\infty}^{\infty}g(y)dy=\frac{1}{2}\int_{0}^{4}(1-\frac{y}{4})dy=1,$$ so $$g(y)$$ is a valid pdf.
Next we have $$E(2X) = E(Y) = \int_{-\infty}^{+\infty} y\;g(y)dx = \int_0^4 \frac{\color{red}{y}}{2}(1-\frac{y}{4})dx = \frac{4}{3}$$