3
$\begingroup$

Suppose $(X,Y)$ has a bivariate t distribution. I want to plot the density of the conditional density of $Y$ given $X$, i.e. $Y|X$. I wonder why the following does not work:

bivT = MultivariateTDistribution[{{1, ρ}, { ρ, 1}}, ν]
bivTCond[x_, y_, ν_, ρ_]:= 
    Evaluate@PDF[bivT, {x, y, ν, ρ}] /
      Evaluate@PDF[StudentTDistribution[0, 1, ν], {x, ν}]
Plot[bivTCond[1, y, 3, 0.5], {y, -3, 3}]

The last command does not produce any plot.

$\endgroup$
1
  • 3
    $\begingroup$ Note that Evaluate[PDF[...]]/Evaluate[PDF[...]] doesn't really do anything, since Evaluate is only special when it's at the first level (which is / in this case.) Perhaps you meant to use Evaluate[PDF[...]/PDF[...]]? $\endgroup$ Feb 2, 2012 at 16:44

2 Answers 2

5
$\begingroup$

Try this:

bivTCond[x_, y_, ν_, ρ_] := 
 Evaluate @ PDF[MultivariateTDistribution[{{1, ρ}, {ρ, 1}}, ν], {x, y}]/
 Evaluate @ PDF[StudentTDistribution[0, 1, ν], x]

Plot[bivTCond[1, y, 3, 0.5], {y, -3, 3}]

PDF of conditional t distribution

$\endgroup$
6
  • $\begingroup$ Mathematica markup is hard to read, so until the syntax highlighter is installed on this site, I'd suggest refraining from using it whenever possible. As such, I've transformed your variables from \[Nu] to Nu and the like. $\endgroup$
    – rcollyer
    Feb 2, 2012 at 15:04
  • $\begingroup$ Good suggestion. I'll do that in the future as well. Thanks! $\endgroup$
    – Cassini
    Feb 2, 2012 at 15:06
  • $\begingroup$ Thanks for the suggestion. Do you mind to explain why my function was not working. It would help me to understand how Mathematica works. $\endgroup$
    – user371
    Feb 2, 2012 at 15:15
  • 1
    $\begingroup$ Well, one error in your function is that you were passing {x,y,nu,rho} to the PDF, but the final argument to the PDF should only be the two random variables, not the statistical parameters. The same holds for the second distribution. $\endgroup$
    – Cassini
    Feb 2, 2012 at 15:20
  • $\begingroup$ Thanks for explanation. Here is another version which still gives me error. $\endgroup$
    – user371
    Feb 2, 2012 at 15:36
8
$\begingroup$

Here's a tutorial on how to debug such things

The first thing you must try when this happens is you should put a number in place of y and see what the expression evaluates to:

bivTCond[1, 1, 3, 0.5]

(*
==> {(
 8 Pi PDF[
   MultivariateTDistribution[{{1, rho}, {rho, 1}}, nu], {1, 1, 3, 
    0.5}])/(3 Sqrt[3]), 
 8 Sqrt[3] Pi PDF[
   MultivariateTDistribution[{{1, rho}, {rho, 1}}, nu], {1, 1, 3, 
    0.5}]}
*)

You can see that there are still free variables present (rho and nu). This is not a numerical quantity, so obviously it can't be used in a plot.

To understand why rho and nu were not properly substituted by numbers, read about := vs =. The first fix needed is using = instead of := in the definition of bivTCond. The Evaluate function is superfluous here, so I'll remove that one too:

bivT = MultivariateTDistribution[{{1, rho}, {rho, 1}}, nu]
bivTCond[x_, y_, nu_, rho_] = 
 PDF[bivT, {x, y, nu, rho}] / PDF[StudentTDistribution[0, 1, nu], {x, nu}]

Out test experssion still won't evaluate to a numerical quantity though:

bivTCond[1, 1, 3, 0.5]

(*
==> {(
 8 Pi PDF[
   MultivariateTDistribution[{{1, 0.5}, {0.5, 1}}, 3], {1, 1, 3, 
    0.5}])/(3 Sqrt[3]), 
 8 Sqrt[3] Pi PDF[
   MultivariateTDistribution[{{1, 0.5}, {0.5, 1}}, 3], {1, 1, 3, 
    0.5}]}
*)

The reason for this is that PDF was used with the wrong syntax. Check any of the examples on the doc page of MultivariateTDistribution to see how to use it: the parameters should not be included in the second argument of PDF

Let's fix this too:

bivT = MultivariateTDistribution[{{1, rho}, {rho, 1}}, nu]
bivTCond[x_, y_, nu_, rho_] = 
 PDF[bivT, {x, y}]/PDF[StudentTDistribution[0, 1, nu], {x, nu}]

And now it works:

Plot[bivTCond[1, y, 3, 0.5], {y, -3, 3}]

Mathematica graphics

The morale here is: build up your expressions step by step, checking that they work at each stage! Then you'll catch mistakes early. This is how all of us work, and how we debug these problems.

$\endgroup$
4
  • $\begingroup$ Many thanks indeed for a very helpful reply. $\endgroup$
    – user371
    Feb 2, 2012 at 15:48
  • $\begingroup$ @Mikael You should verify if you really wanted two number in PDF[StudentTDistribution[...], {x,nu}] though (I mean {x,nu}) ... $\endgroup$
    – Szabolcs
    Feb 2, 2012 at 15:55
  • $\begingroup$ Sure, I noticed that and realized that should remove nu. Thanks again. $\endgroup$
    – user371
    Feb 2, 2012 at 16:09
  • $\begingroup$ +1. Very good advice. I would just add something on localization of variables when using Set (or at least some ClearAll call on those symbols prior to Set), so that people won't start blindly using Set and get funny results. $\endgroup$ Feb 2, 2012 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.