2

This may seem a trivial question but I am coming to Mathematica from object oriented programming languages.

The results of many analyses are composite datatype. I want to access part of the result. For example:

dist = MixtureDistribution[{2, 1}, {NormalDistribution[], NormalDistribution[2, 1/2]}];
weights = dist.getWeights() -- result is {2,1}

Another example:

hmm = HiddenMarkovProcess[{0.5, 0.5}, {{0.7, 0.3}, {0.25, 0.75}}, {{0.2, 0.6, 0.2}, {0.4, 0.2, 0.4}}];
initial = hmm.getInitialProbabilities() -- result is {0.5,0.5}
transition = hmm.getTransitionProbabilities() -- result is {{0.7, 0.3}, {0.25, 0.75}}

I searched Mathematica Documentation and could not find a direct answer. In all the examples, the result is passed to other functions for further plotting/processing.

  • 1
    In the first case, for example, the weights are the first "argument" to the function MixtureDistribution, so you extract them by getting the first Part: Part[dist, 1], dist[[1]] or First[dist]. – 2012rcampion Apr 06 '15 at 02:32
  • 1
    in mathematica you can make such defenition weights /: dist[weights] := dist[[1]] and now dist[weights] will return {2,1} – k_v Apr 06 '15 at 06:55
  • 1
    or even more convinient dist[] = MixtureDistribution[{2, 1}, {NormalDistribution[], NormalDistribution[2, 1/2]}]; dist["weights"] := dist[][[1]] – k_v Apr 06 '15 at 07:22
  • 2
    If you are coming from OO programing I would strongly encourage you to get familiar with the paradigm of functional programming for which Mathematica is well suited. Discussions on why OO is not encouraged, you find also on this forum – penguin77 Apr 06 '15 at 07:33
  • thank you all for your comments. I knew there is an easy answer to it. I am still confusing lists {} and "objects" []. Apparently the operator [[]] works for both. I am reading through the Wolfram Language Introduction to Programmers to get more familiar with it. It is available online http://www.wolfram.com/language/fast-introduction-for-programmers – Hani Safadi Apr 06 '15 at 19:21
  • 1
    Remember that the list notation {1,2,3} is just shorthand for List[1,2,3] so it is really no different from any other head[args] expression. – Simon Woods Apr 06 '15 at 19:34

1 Answers1

5

As people have said in the comments, Part is the simplest way to access parts of an expression. For example

hmm[[2]]
(* {{0.7, 0.3}, {0.25, 0.75}} *)

For accessing properties by name, the MarkovProcessProperties function looks like the sort of thing you want, but it doesn't currently work for HiddenMarkovProcess - though there is some stuff in the internal code that suggests this will be implemented eventually.

In the meantime, you can access "properties" for HiddenMarkovProcess using the undocumented internal function RandomProcesses`HiddenMarkovProcessDump`hmmProperty - in the code below I assign a shorter symbol name to make it less unwieldy:

prop = RandomProcesses`HiddenMarkovProcessDump`hmmProperty;

prop[hmm, "InitialProbabilityVector"]
(* {0.5, 0.5} *)

prop[hmm, "TransitionMatrix"]
(* {{0.7, 0.3}, {0.25, 0.75}} *)

The complete list of accessible properties appears to be:

"ConditionalTransitionMatrix"
"EffectiveHMM"
"EffectiveTransitionMatrix"
"Emissions"
"InitialProbabilityVector"
"ProcessNParameterQ"
"ProcessParameterQ"
"SilentStates"
"StandardizedEmissions"
"TransitionMatrix"

Note that because this is an undocumented internal function, it is liable to change without warning in future versions.

Simon Woods
  • 84,945
  • 8
  • 175
  • 324