# Compute (Co)Variance of Random Variable(s) Directly

Is there a direct command in Mathematica that computes the covariance between two random variables?

Hence, instead of typing

Expectation[f(x)*g(y),{y~some distribution,x~some distribution}] -
Expectation[f(x),{y~some distribution,x~some distribution}]*
Expectation[g(y),{y~some distribution,x~some distribution}]


Is there a command that archieves the same by typing simply

Covariance[{f(x),g(y)},{y~some distribution,x~some distribution}]


I have tried to use the following function:

CoVar[RVfuncs_, dist_] :=
Expectation[RVfuncs[]*RVfuncs[],
dist] - (Expectation[RVfuncs[], dist]*Expectation[RVfuncs[], dist])


It works, but it is much slower than writing out the three "Expectation" parts.

• The covariance between 2 random variables whose distributions are specified as in your examples will always be 0. You need to specify a multivariate distribution, such as Covariance[BinormalDistribution[{m1, m2}, {sd1, sd2}, corr], 1, 2] – Rojo Oct 31 '13 at 16:27
• How about the case x = y – Breugem Oct 31 '13 at 16:33
• I don't understand what you mean. If you tell Mathematica that {x~someDist, y~someDist}, it assumes they are independent, so unless someDist is a delta, you don't get x=y, and in such case I would think the covariance is indeterminate – Rojo Oct 31 '13 at 16:43
• About your edit and your CoVar function, what exactly is slower? – Rojo Oct 31 '13 at 16:44
• Ok thanks I see your point. I will rephrase the question – Breugem Oct 31 '13 at 16:45