2
$\begingroup$

I need to use the pdf of a Wishart distribution which is only defined in the package MultivariateStatistics`. However, after loading the package, some or all of the multivariate distributions pdfs I use (e.g. MultinormalDistribution, MultivariateTDistribution) change their font colour to red.

The programme still works, but I find this strange. I've tried reading this link on shadowing on the Wolfram site, but I'm not sure I understood it...

It means that by loading the package, now Mathematica will use the definitions from the package, and not the global definitions. Did I understand correctly? If so, I assume the definition is the same, with the possible exception of the input sintax, for example WishartMatrixDistribution and WishartDistribution have their inputs in reverse order to each other, but they should describe the same distribution...

How can I be sure that I'm using these new definitions in a proper way? Should I be worried of a possible conflict in the main programme?

I'm using version 11. At request, here's a minimal example:

Needs["MultivariateStatistics`"]

PDF[MultinormalDistribution[{0, 0}, IdentityMatrix[2]], {x, y}]

(* E^(1/2 (-x^2 - y^2))/(2 π) *)
$\endgroup$
  • $\begingroup$ Can you say which version of Mathematica you are using? Can you give a minimum example of code that you need to execute to get the problem? $\endgroup$ – mikado Nov 23 '18 at 23:01
  • $\begingroup$ @mikado I've added the information you asked for. $\endgroup$ – An old man in the sea. Nov 24 '18 at 8:22
  • 1
    $\begingroup$ Hints: i.stack.imgur.com/CoMtN.png -- but I don't have time to write an answer today... $\endgroup$ – Michael E2 Nov 24 '18 at 23:52
2
$\begingroup$

Michael E2 has already hinted at what happens here. To get a deeper understanding of the issue a good place to start is the tutorial

Modularity and the Naming of Things

There you will find the sections Contexts and Contexts and Packages that will provide most of the information needed to see why the coloring is taking place.

If you try the following, Mathematica will tell you what has happened in your case:

MultivariateStatistics`MultinormalDistribution 

MultinormalDistribution::shdw: Symbol MultinormalDistribution appears in multiple contexts {MultivariateStatistics`, System`}; definitions in context MultivariateStatistics` may shadow or be shadowed by other definitions.

The symbol MultinormalDistribution is defined in two contexts that are both on the $ContextPath.

The straight forward solution is simply to remove the superfluous symbol-definition:

Remove["MultivariateStatistics`MultinormalDistribution"]

This will only keep the definition for MultinormalDistribution contained in the System` context - the coloring should now be normal again.

A strategy to prevent this from the start is to prevent that MultivariateStatistics` is added to $ContextPath when loading a package (cf. here):

Block[ {$ContextPath} , Needs["MultivariateStatistics`] ]

$ContextPath will then not contain MultivariateStatistics` and there is no shadowing. To refer to a symbol contained in the package you will have to use a full reference, e.g. MultivariateStatistics`MultiPoissonDistribution. thus, here we could probably simply remove the two symbols contained in more than one context.

$\endgroup$

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.