# Finding the covariance of two discrete random variables

I have two discrete random variavles; $X$ and $Y$ with:

And I want to calculate $Cov(Y,Y+e^X)$, and I've tried the following:

\[ScriptCapitalD] = EmpiricalDistribution[{1/8, 2/8, 5/8} -> {1, 2, 3}];
\[ScriptCapitalD]1 = EmpiricalDistribution[{1/5, 4/5} -> {-1, 1}];
Covariance[\[ScriptCapitalD]1, \[ScriptCapitalD]1 + e^\[ScriptCapitalD]]


But this doesnt return any value, what am I doing wrong?

I have also tried calculating stuff like $P(X=3,Y=1)$ and $P(X\cdot Y \geq 2)$ without any luck

• And $X$ and $Y$ are independent?
– JimB
Commented Dec 20, 2016 at 18:13
• Yes, they are, forgot to mention. Commented Dec 20, 2016 at 19:12
• This isn't a fix of your problem, but you should be aware that there's no such thing as e; there's E - Mathematica is case sensitive. Commented Dec 20, 2016 at 19:51

This solution uses built-in functions :

dx = EmpiricalDistribution[{1/8, 2/8, 5/8} -> {1, 2, 3}];
dy = EmpiricalDistribution[{1/5, 4/5} -> {-1, 1}];
Covariance[
TransformedDistribution[{y, y + Exp[x]}, {x \[Distributed] dx,
y \[Distributed] dy}], 1, 2]
(* 16/25 *)


To calculate these kinds of expressions, use Expectation:

\[ScriptCapitalD] = EmpiricalDistribution[{1/8, 2/8, 5/8} -> {1, 2, 3}];
\[ScriptCapitalD]1 = EmpiricalDistribution[{1/5, 4/5} -> {-1, 1}];

mu = Expectation[Exp[x], x \[Distributed] \[ScriptCapitalD]]
mu1 = Expectation[y, y \[Distributed] \[ScriptCapitalD]1]

Expectation[(y - mu1) (y + Exp[x] - mu1 - mu), {x \[Distributed] \[ScriptCapitalD], y \[Distributed] \[ScriptCapitalD]1}]

• Mathematica 10.4.1 needs a Simplify or Expand applied to the last line to simplify to the answer of 16/25.
– JimB
Commented Dec 20, 2016 at 18:27