2
$\begingroup$

If I try to input a symbolic multivariate distribution, I do not get a useful result.

PDF[MultinormalDistribution[m, S], x]

(* During evaluation of MultinormalDistribution::vrprm: The value m at position 1 in MultinormalDistribution[m,S] is expected to be a list of real numbers. *)

(* During evaluation of MultinormalDistribution::vrprm: The value m at position 1 in MultinormalDistribution[m,S] is expected to be a list of real numbers.*)
(* PDF[MultinormalDistribution[m, S], x] *)

Clearly, Mathematica could return an appropriate symbolic expression. Is there any way of making it do so?

$\endgroup$
8
$\begingroup$

You need to supply Mathematica with the structure of your variables:

(* dimension of distribution *)
n = 2;

PDF[
 MultinormalDistribution[
  Array[Subscript[m, ##] &, n],
  Array[Subscript[S, Sequence @@ Sort@{##}] &, {n, n}]
  ],
 Array[Subscript[x, ##] &, n]
 ]

enter image description here We use Array to construct variables of the form Subscript[var,i,j,…] (you can of course use any other form that is more convenient). Note that S is explicitly made symmetric by sorting the indices. The code works without this, but this way there are no "fake" parameters.

Note: As pointed out by @BobHanlon in the comments, Subscript can often cause issues if used blindly. For actual use, it is much safer to simply use indexed variables such as Array[x, n].

$\endgroup$
  • $\begingroup$ Rather than using subscripted variables, I recommend using indexed variables and then using Format to display the indexed variables as subscripted variables. Subscripted variables tend to cause problems in solvers. $\endgroup$ – Bob Hanlon Apr 27 at 12:59
  • $\begingroup$ I am aware of that - I just thought that for a quick demonstration, it would be good enough and it's easy to read. But I will add a comment to the answer to point out potential issues. $\endgroup$ – Lukas Lang Apr 27 at 13:03
  • $\begingroup$ A very useful answer, but I was hoping for an expression that worked in terms of vector and matrix inputs (of unspecified dimension). $\endgroup$ – mikado Apr 27 at 13:32
  • $\begingroup$ @BobHanlon is there an answer you can link to that would detail an appropriate use of the Format method you reference? $\endgroup$ – CA Trevillian Apr 29 at 14:01
  • 2
    $\begingroup$ @CATrevillian - For a simple example, consider Format[x[n_]] := Subscript[x, n]; x /@ {m, n, 1, 2, 3} Also see documentation details section and link $\endgroup$ – Bob Hanlon Apr 29 at 16:12

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.