# 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?.

• First ...How to write that PDF in Mathematica? Have you tried? – Dr. belisarius Oct 23 '14 at 0:37
• @belisarius Yes, like this: Piecewise[{{1, 0 < x < 1/2 || 1 < x < 3/2}, {0, false}}]. The problem is that i dont know how to pass that to ExpectedValue[] – Wyvern666 Oct 23 '14 at 0:43
• This is a function definition. Not PDF. Check how to define own PDF here reference.wolfram.com/language/ref/ProbabilityDistribution.html – Nasser Oct 23 '14 at 0:46
• @Nasser It is supposed to be the pdf of a continous random variable, why the discrete plot?. The thing is how to pass that to ExpectedValue[]? – Wyvern666 Oct 23 '14 at 1:07
• Just use Plot. I just typed DiscretePlot cause I was looking at example. Once you have Distribution, you can use Mean on it. As in Mean[D0] gives 3/4 There is also expectation, reference.wolfram.com/language/ref/Expectation.html – Nasser Oct 23 '14 at 1:18

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]) *)


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.