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?.
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.
Piecewise[{{1, 0 < x < 1/2 || 1 < x < 3/2}, {0, false}}]
. The problem is that i dont know how to pass that toExpectedValue[]
$\endgroup$Mean
on it. As inMean[D0]
gives3/4
There is also expectation, reference.wolfram.com/language/ref/Expectation.html $\endgroup$