1
$\begingroup$

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

$\endgroup$
7
  • $\begingroup$ First ...How to write that PDF in Mathematica? Have you tried? $\endgroup$ Commented Oct 23, 2014 at 0:37
  • $\begingroup$ @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[] $\endgroup$
    – Wyvern666
    Commented Oct 23, 2014 at 0:43
  • $\begingroup$ This is a function definition. Not PDF. Check how to define own PDF here reference.wolfram.com/language/ref/ProbabilityDistribution.html $\endgroup$
    – Nasser
    Commented Oct 23, 2014 at 0:46
  • 1
    $\begingroup$ @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[]? $\endgroup$
    – Wyvern666
    Commented Oct 23, 2014 at 1:07
  • 1
    $\begingroup$ 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 $\endgroup$
    – Nasser
    Commented Oct 23, 2014 at 1:18

2 Answers 2

5
$\begingroup$

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]) *)
$\endgroup$
3
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.