How to find the expectation of this random variable

Assume $\theta \sim U[0,1]$.

Fix $x\in \mathbb{R}$ and consider the following random variable:

$ g_{x}\left(\theta\right)=\begin{cases} 2-|x| & |x|\leq\theta\\ 0 & \text{else} \end{cases} $

How can I calculate the expectation of $ \mathbb{E}\left[g_{x}\left(\theta\right)\right] $?

Im not sure how to start. The answer should be

$$ \begin{cases} 1.5-2|x|+0.5x^{2} & |x|\leq1\\ 0 & \text{else} \end{cases} $$

And I'm not sure how or why.

Thanks in advance, any help would be appreciated.


Solution 1:

Remember what is random and what is fixed.

So for any $x,g_x(\theta)$ only takes two values: $0\text{ and }2-|x|$

Let $c_x= 2-|x|$

The probability calcs are simple:

$$P(g_x=c_x) = P(\theta \geq |x|)=\max(0,1-|x|):=p_x$$ $$\text{ and } P(g_x=0)=1-p_x$$

Therefore,

$$E[g_x] = p_xc_x + (1-p_x)(0)=p_xc_x$$

We can split into two cases:

Case 1: $|x| \leq 1$: $$p_x= 1-|x| \implies E[g_x]=(2-|x|)(1-|x|)$$

Case 2: $|x|>1$:

$$p_x = 0 \implies ???$$

Can you take it from here -- I also don't think the given answer is correct.

Solution 2:

If $x>0$ the expectation is

$$(2-x)\int_x^1 d\theta =2-3x+x^2$$

If $x<0$ the expectation is

$$(2+x)\int_{-x}^1 d\theta =2+3x+x^2$$

Thus the correct expectation is

$$ \mathbb{E}\left[g_{x}\left(\theta\right)\right]= \begin{cases} 2-3|x|+x^{2} & |x|\leq1\\ 0 & \text{else} \end{cases} $$