Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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, \[Rho]}, { \[Rho], 1}}, \[Nu]]
bivTCond[x_, y_, \[Nu]_, \[Rho]_]:= 
    Evaluate@PDF[bivT, {x, y, \[Nu], \[Rho]}] /
      Evaluate@PDF[StudentTDistribution[0, 1, \[Nu]], {x, \[Nu]}]
Plot[bivTCond[1, y, 3, 0.5], {y, -3, 3}]

The last command does not produce any plot.

share|improve this question
3  
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[...]]? –  Brett Champion Feb 2 '12 at 16:44

2 Answers 2

up vote 5 down vote accepted

Try this:

bivTCond[x_, y_, Nu_, Rho_] := 
 Evaluate@PDF[
    MultivariateTDistribution[{{1, Rho}, {Rho, 1}}, Nu], {x, 
     y}]/Evaluate@PDF[StudentTDistribution[0, 1, Nu], x]
Plot[bivTCond[1, y, 3, 0.5], {y, -3, 3}]

enter image description here

share|improve this answer
    
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. –  rcollyer Feb 2 '12 at 15:04
    
Good suggestion. I'll do that in the future as well. Thanks! –  David Skulsky Feb 2 '12 at 15:06
    
Thanks for the suggestion. Do you mind to explain why my function was not working. It would help me to understand how Mathematica works. –  Mikael Anderson Feb 2 '12 at 15:15
1  
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. –  David Skulsky Feb 2 '12 at 15:20
    
Thanks for explanation. Here is another version which still gives me error. –  Mikael Anderson Feb 2 '12 at 15:36

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.

share|improve this answer
    
Many thanks indeed for a very helpful reply. –  Mikael Anderson Feb 2 '12 at 15:48
    
@Mikael You should verify if you really wanted two number in PDF[StudentTDistribution[...], {x,nu}] though (I mean {x,nu}) ... –  Szabolcs Feb 2 '12 at 15:55
    
Sure, I noticed that and realized that should remove nu. Thanks again. –  Mikael Anderson Feb 2 '12 at 16:09
    
+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. –  Leonid Shifrin Feb 2 '12 at 16:50

Your Answer

 
discard

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

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