7
$\begingroup$

How do I get Graph to display the transition probabilities for a Markov process as labels on the graph's edges? The information is clearly present in the graph, but only displays when I hover over the edges. Is there a way to get the information to display as edge labels (without going through complex machinations)?

For example,

mp = DiscreteMarkovProcess[{1, 0, 0}, ({
     {0.6, 0.1, 0.3},
     {0.2, 0.7, 0.1},
     {0.3, 0.3, 0.4}
    })];
Graph[mp]

gives

with no labeling of edges, even though I can hover over any edge to see the associated transition probability (as a tooltip).

$\endgroup$
11
$\begingroup$

You can extract the probabilities from the properties of the edges and assign them as edge labels using

g = Graph[mp];
Scan[(PropertyValue[{g, #}, EdgeLabels] = PropertyValue[{g, #}, "Probability"]) &,
      EdgeList[g]]

g

enter image description here

You can find this (and other) properties using the PropertyList function:

PropertyList[{g, 1 \[DirectedEdge] 2}]

(* {"Probability", EdgeLabels, EdgeShapeFunction, EdgeStyle} *)

From here, I used PropertyValue to set the properties. I imagine there are a few other (and possibly better/simpler) ways to accomplish this.

$\endgroup$
  • $\begingroup$ great answer - appreciate the conciseness and the extra tidbit at the end on PropertyList. $\endgroup$ – Paul_A Oct 14 '15 at 3:18
5
$\begingroup$

You can use MarkovProcessProperties[mp, "TransitionMatrix"] as edge labels directly in Graph:

mp = DiscreteMarkovProcess[{1, 0, 0}, {{.6, .1, .3}, {.2, .7, .1}, {.3, .3, .4}}];

Graph[mp, EdgeLabels -> {DirectedEdge[i_, j_] :> 
    MarkovProcessProperties[mp, "TransitionMatrix"][[i, j]]}]

enter image description here

$\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.