I am working with a function that computes the covariance between two variables. After computing this, I'd like to use Plot or Plot3D to see how this covariance depends on the parameters of the mean and covariance matrices. The problem is the output is not a number and the graphs appears empty.


CoVar[RVfuncs_, dist_] := Expectation[RVfuncs[[1]]RVfuncs[[2]], dist] - Expectation[RVfuncs[[1]], dist] Expectation[RVfuncs[[2]], dist];

Σ= {{σ^2, σ1}, {σ1, σ^2}};

$\mu$= {$\mu_1$, $\mu_2$};

f = CoVar[{x1 + 3 x2, 3 x2 - 0.4 x1}, {x1, x2} $\sim$ MultinormalDistribution[$\mu$, $\Sigma$]];

When I try to use plot f, I get an empty graph.

Plot[f /. {$\sigma_1$ -> 2, $\mu_1$ -> 1/3, $\mu_2$ -> 1/2}, {$\sigma$, 0, 1}];

This code is just to illustrate my problem. When I try to run the previous code, it actually works.

In my actual code I compute the covariance between two variables $z_1$ and $z_2$ each depending on $x_1$ and $x_2$. Those variables are a solution to a previously solved system. So my actual code looks something like this:

Sol = Solve[systemtosolve,{z1,z2}];

f = CoVar[{z1, z2}, {x1, x2} $\sim$ MultinormalDistribution[$\mu$, $\Sigma$]];

In that case Plot[ ] doesn't seem to work.

When I use Table[ ] instead of Plot[ ] as someone suggested, instead of getting a list of numbers, I get a list of a number plus a number in scientific notation:

{-0.000569735 - 1.73472*10^-22, -0.000710862 - 6.93889*10^-22, -0.000946073 -1.56125*10^-21}

I guess that's the origin of my problem, the output of my Covar[ ] function is not a number, no idea why.

I would truly appreciate some idea of what am I doing wrong. Thanks!

  • $\begingroup$ try again posting the code as it is not readable now. select the cell, convert to inputForm, then copy it as text and paste. You seem be to missing some "\" on symbols used. $\endgroup$ – Nasser Nov 25 '14 at 2:40
  • $\begingroup$ Quick experiment: Try replacing Plot with Table and look at the result. Usually that explains it. $\endgroup$ – Bill Nov 25 '14 at 2:47

You need to declare "sigma" as a variable so that plot can act on it.

Σ[σ_] := {{σ^2, σ1}, {σ1, \σ^2}};
  • $\begingroup$ Hi, thanks for your suggestion. I tried to do that but it didn't work either. I edited my text to make clear that the original code was a simplification of my problem. $\endgroup$ – Jason Nov 26 '14 at 0:37
CoVar[RVfuncs_, dist_] := 
  Expectation[RVfuncs[[1]] RVfuncs[[2]], 
    dist] - (Expectation[RVfuncs[[1]], dist] Expectation[RVfuncs[[2]], dist]);

Σ = {{σ^2, σ1}, {σ1, σ^2}};
μ = {a, b};

f = CoVar[{x1 + 3 x2, 3 x2 - 0.4 x1}, {x1, x2} \[Distributed] 
    MultinormalDistribution[μ, Σ]];

 f /. {σ1 -> 2, a -> 1/3, b -> 1/2},
 {σ, 0, 3}]

enter image description here


{HoldAll, Protected, ReadProtected}

Since Plot has attribute HoldAll use Evaluate to make plotting more efficient

 Evaluate[f /. {σ1 -> 2, a -> 1/3, b -> 1/2}],
 {σ, 0, 3}]

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.