7
$\begingroup$

I would like to calculate the following with Mathematica:

Let $X$ be a random variable with probability function $$p(x)= \left\{ \begin{matrix} \frac{x}{6} & \text{for }x \in \{1,2,3\}\\ 0 & \text{Otherwise} \end{matrix}\right. $$ Calculate $P(X \leq 2)$, $E(X)$ and $var(x)$

My major problem is to create the probability function with the correct distribution since there haven't been any specific distribution informed in the task. So how do I solve this?

If it is any help, then the results are respectively:

{0.5, 2.333, 0.5556}
$\endgroup$

3 Answers 3

9
$\begingroup$

This can done as follows (consistent with your own definition):

p=ProbabilityDistribution[x/6, {x, 1, 3, 1}]

This is discrete PMF using your definition. The PMF definition can be displayed:

PDF[p, x]

Your questions follow:

{Probability[x <= 2, x \[Distributed] p], Mean[p], Variance[p]}

yields:

{1/2, 7/3, 5/9}

or numerically:

N@{Probability[x <= 2, x \[Distributed] p], Mean[p], Variance[p]}

yielding:

{0.5, 2.33333, 0.555556}
$\endgroup$
1
  • $\begingroup$ Noting the relative ease to calculate: P(X<=2)=1/6+2/6=1/2; E[x]-1/6+2 x 2/6+ 3 x 3/6=14/6=7/3 and similarly variance E[x^2]-E[x]^2 $\endgroup$
    – ubpdqn
    Commented Aug 4, 2013 at 12:01
6
$\begingroup$

You could also use EmpiricalDistribution:

In[1]:= di = EmpiricalDistribution[{1, 2, 3}/6 -> {1, 2, 3}];

In[2]:= {Mean[di], Variance[di], CDF[di, 2]}

Out[2]= {7/3, 5/9, 1/2}

In[3]:= N[%]

Out[3]= {2.33333, 0.555556, 0.5}
$\endgroup$
4
  • 1
    $\begingroup$ Thank you, I didn't know the function EmpiricalDistribution... With the setup you have created, how would you calculate eg. $P(X_1+X_2 \leq 2)$ ? $\endgroup$ Commented Aug 4, 2013 at 16:40
  • 1
    $\begingroup$ Assuming you mean that $X_1$ and $X_2$ are independent and identically distributed, I would do Probability[x1 + x2 <= 2, {Distributed[x1, di], Distributed[x2, di]}] which yields $\frac{1}{36}$. $\endgroup$
    – Sasha
    Commented Aug 4, 2013 at 23:56
  • $\begingroup$ Does this work for mms 8 as well? Because it can't calculate it when I try. $\endgroup$ Commented Aug 5, 2013 at 0:08
  • $\begingroup$ I was using version 9. $\endgroup$
    – Sasha
    Commented Aug 5, 2013 at 4:51
5
$\begingroup$
d = ProbabilityDistribution[
  Piecewise[{{x/6, x == 1 || x == 2 || x == 3}}, 0], {x, 0, 10, 1}]

Grid[{{DiscretePlot[Evaluate[PDF[d, x]], {x, 0, 10}, 
    PlotLabel -> "PDF", ExtentSize -> 0.5, 
    PlotStyle -> ColorData[1, 1]],
   DiscretePlot[Evaluate[CDF[d, x]], {x, 0, 10}, PlotLabel -> "CDF", 
    ExtentSize -> Right, PlotStyle -> ColorData[1, 1]]}}]

Mathematica graphics

Mean[d] // N
(*2.33333333333333*)

Variance[d] // N
(*0.555555555555556*)

Probability[x <= 2 , x \[Distributed] d] // N
(*0.5*)

Expectation[x, x \[Distributed] d] // N
(*2.33333333333333*)
$\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.