# Calculate probability function

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}

-

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]


{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}

-
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 – ubpdqn Aug 4 '13 at 12:01

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}

-
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)$ ? – Jens Jensen Aug 4 '13 at 16:40
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}$. – Sasha Aug 4 '13 at 23:56
Does this work for mms 8 as well? Because it can't calculate it when I try. – Jens Jensen Aug 5 '13 at 0:08
I was using version 9. – Sasha Aug 5 '13 at 4:51
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]]}}]


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

-