2

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 π) *)
gwr
  • 13,452
  • 2
  • 47
  • 78
An old man in the sea.
  • 2,527
  • 1
  • 18
  • 26

1 Answers1

2

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.

gwr
  • 13,452
  • 2
  • 47
  • 78