Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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_] := 
dist] - (Expectation[RVfuncs[[1]], dist]*Expectation[RVfuncs[[2]], dist])

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

share|improve this question
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] – Rojolalalalalalalalalalalalala 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 – Rojolalalalalalalalalalalalala Oct 31 '13 at 16:43
About your edit and your CoVar function, what exactly is slower? – Rojolalalalalalalalalalalalala Oct 31 '13 at 16:44
Ok thanks I see your point. I will rephrase the question – Breugem Oct 31 '13 at 16:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.