I have this task with two questions I would like to compute in Mathematica.

Suppose a pair of random variables $(X, Y)$ has a equal distribution on the following 7 points:

$\begin{matrix} & x & y\\ 1 & -1 & 0\\ 2 & 0 & 0\\ 3 & 1 & 0\\ 4 & -2& 1\\ 5 & 2 & 1\\ 6 & -1 & 3\\ 7 & 1 & 3 \end{matrix}$

The simultaneously probability function $p(x, y)$ is then given by: $p (1,0) = \ldots = P (1, 3) = 1/7 \text{ and } p(x, y) = 0$ for all other points

1) Find the conditional mean $E (Y\; |\; X = x)$ and the conditional variance $V (Y\; | \;X = x)$ for $x = -2, -1,0,1,2.$

2) Compute the variance on Y and the variance on the conditional mean of Y given X

E(X)=0
E(Y)=8/7


I have tried over and over but without any luck.

Ps. I use Mathematica 8.

-
I still need help with 2) – Jens Jensen Aug 5 '13 at 1:00

Assuming you have version 9 you can do the following.

data = {{-1, 0}, {0, 0}, {1, 0}, {-2, 1}, {2, 1}, {-1, 3}, {1, 3}};

dist = EmpiricalDistribution[data];

Table[Expectation[y \[Conditioned] x == i, {x, y} \[Distributed] dist], {i, -2, 2}]

(*{1, 3/2, 0, 3/2, 1}*)


Note: Conditional probabilities and expectations didn't work for EmpiricalDistribution in version 8. In that case you could code this up yourself as...

Table[Mean[Cases[data, {i, y_} :> y]], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)


Edit: An incredibly inefficient but distribution-based solution in M8 is to use ProbabilityDistribution.

pdist =
ProbabilityDistribution[Block[{tally = Tally[data], nprobs},
nprobs = Normalize[tally[[All, 2]], Total];
Piecewise[
Transpose[{nprobs, (And @@ Thread[{x, y} == #]) & /@
tally[[All, 1]]}]]
], {x, -3, 3, 1}, {y, -3, 3, 1}]

Table[
Expectation[
y \[Conditioned] x == i, {x, y} \[Distributed] pdist], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)


For completeness, to compute the variance using Expectation it gets a little messy.

mu = Expectation[y \[Conditioned] x == i, {x, y} \[Distributed] pdist];
Table[Expectation[(y - mu)^2 \[Conditioned]
x == i, {x, y} \[Distributed] pdist], {i, -2, 2}]

(*{0, 9/4, 0, 9/4, 0}*)

-
Sorry, I didn't mention I have Mathematica 8. – Jens Jensen Aug 4 '13 at 21:30
Great. That works to compute the mean $(E \; | \; X = x)$ but what about the variance and question 2? – Jens Jensen Aug 4 '13 at 21:40
Well, in the hard coded case you simply replace Mean with Variance. Since there aren't many repeated data points the variance is going to be undefined for some of them. – Andy Ross Aug 4 '13 at 21:41
Wonderful edit! It is a very nice solution. However the question 2 has not been answered yet. – Jens Jensen Aug 4 '13 at 21:57
With Variance[data[[All,2]] you compute variance for y (small character) and not Y (big character). It is the last one I am interested to calculate in MMA. $V(Y) = \frac{76}{49}$ $V(E(Y|X))=\frac{13}{49}$ But I would like to learn how to compute it in MMA. Hope that his is clear. – Jens Jensen Aug 4 '13 at 22:08

cas = Cases[list, {#, y_} :> y] & /@ Range[-2, 2];
ans = {Mean[#], Mean[(# - Mean[#])^2]} & /@ cas;
Style[Prepend[
MapThread[Prepend[#1, #2] &, {ans, Range[-2, 2]}], {"x",
"E[Y|X=x]", "Var[Y|X=x]"}] // Grid, 20]


1. The underlying discrete uniform distribution make all the answers a matter of counting and the Mathematica functions are not really necessary.
2. Variance uses unbiased estimator and will (appropriately) fail for lists with one element. Using it in this context for larger lists lead to answer 9/2 due to correction factor ($n-1$ rather than $n$), so the definition of variance provides the suitable answer. (noting the cases: {{1}, {0, 3}, {0}, {0, 3}, {1}})
3. ProbabilityDistribution is a nice function but only complicates and as @AndyRoss has stated is inefficient for this specific problem. (This is really a restatement of 1).
-
wrote: "Variance uses unbiased estimator and will (appropriately) fail for lists with one element" .......... The real problem is that Mathematica uses the same term Variance to calculate the symbolic calculation of population variance (with regards to a distribution), and the same name to calculate sample variance ... which is something entirely different. – wolfies Aug 5 '13 at 14:53
@wolfies thank you for correcting me. Inherent in unbiased , is estimation from sample. My only point was that if you cal the function Variance the calculation will be different in this case. Similarly, use of "PDF for PMF. I guess symbolic overloading is part of making a tractable system. Thank you again for correcting my sloppiness. – ubpdqn Aug 5 '13 at 19:31
:) I wasn't correcting you, because what you said is correct. What I was trying to say is that the confusion mostly comes about because mma itself uses the same function name Variance to carry out 2 conceptually very different operations ... and in this instance, for example, it may be unclear as to what one is calculating ... especially given list based input describing the pmf. – wolfies Aug 5 '13 at 19:49
Great solution for for 1) However, I still need a solution for 2) – Jens Jensen Aug 6 '13 at 7:32