Q:

Calculate expected value of piece-wise pdf

I have the following PDF of a random variable:
$f(x)=\begin{cases}
 1 & \text{if}\ \ \ 0<x<\frac{1}{2} \vee 1 < x < \frac{3}{2}\\ 
 0 & \text{otherwise}.
\end{cases}$
How can i do to calculate the expected value?.

A:

You could use ProbabilityDistribution.
d = ProbabilityDistribution[
      Piecewise[{{1, 0 < x < 1/2}, {1, 1 < x < 3/2}}, 0], 
        {x, -\[Infinity], \[Infinity]}]

Mean[d]

(* 3/4 *)

Or alternatively MixtureDistribution.
d2 = MixtureDistribution[{1, 1}, {UniformDistribution[{0, 1/2}], 
   UniformDistribution[{1, 3/2}]}]

Mean[d2]

(* 3/4 *)

Since these are distributions all sorts of functions work with them.  For example, you can easily compute other expected values using Expectation.
Expectation[Log[x], Distributed[x, d]]

(* 1/2 (-2 - Log[2] + Log[27/8]) *)

A:

Just use $E(X)=\int^{\infty}_{-\infty}x \, PDF(x)\, dx$
f[x_] := Piecewise[{{1, 0 < x < 1/2 || 1 < x < 3/2}, {0, false}}]
ExpectedValue=Integrate[x f[x], {x, -Infinity, Infinity}]

yields 3/4.

