# Graph and Markov Chain

I have the following code:

proc = DiscreteMarkovProcess[1, {{0.6, 0.4}, {0.3, 0.7}}]

Graph[{"A", "E"}, proc, GraphStyle -> "DiagramBlue"]


How can I 'force' to show the percentage values (0.6, 0.4 etc) on each arrow? I can see the values when I mouse hover it. Thank you.

-
I did the following: <pre values = {{0.6, 0.4}, {0.3, 0.7}}; proc = DiscreteMarkovProcess[1, values]; Graph[{"A", "E"}, proc, GraphStyle -> "DiagramBlue", EdgeLabels -> Table[values[i], {i, Length[values]}]]; /pre> It is showing the entire 'matrix' instead of each value as the two solutions below. Any further idea, please? –  Luiz Roberto Meier Aug 31 at 16:32
Answers to your question were posted before you made this comment. I don't understand what your comment is, but if you want a different answer, I will try to amend mine... –  Kellen Myers Aug 31 at 16:43
@KellenMyers : I'm just trying to find a way more readable and simple to solve it. I already give an up to you and to kguler . As you can see, the Table can be used with EdgeLabels. –  Luiz Roberto Meier Aug 31 at 16:46
And, as I said, I have amended my answer to show you how to do that properly if that's how you want to generate your labels. –  Kellen Myers Aug 31 at 16:52

proc = DiscreteMarkovProcess[1, {{0.6, 0.4}, {0.3, 0.7}}]
h = Graph[{"A", "E"}, proc, GraphStyle -> "DiagramBlue"]
ToExpression@StringReplace[ToString@FullForm@h, "Tooltip" :> ".1"]


-
You answer is more readable and can also be used with 'unlimited' number of percentages also. Thank you. –  Luiz Roberto Meier Aug 31 at 16:53
I will just point out, for the record, that no one's answer was incompatible with any larger system -- in some cases, explicitly so (e.g. my second answer) and in other cases after trivial and obvious modifications. –  Kellen Myers Aug 31 at 16:58
Right. All the answers are good. This one is more. Relax. –  Luiz Roberto Meier Aug 31 at 17:00
@LuizRobertoMeier Thanks. Anyway, I recommend waiting a few hours before accepting an answer. An open question always attract more attention and you may get more and better answers –  belisarius Aug 31 at 17:06
@LuizRobertoMeier ToExpression@StringReplace[ToString@FullForm@h, "Tooltip" :> ".1"] –  belisarius Sep 1 at 13:50
proc = DiscreteMarkovProcess[1, {{0.6, 0.4}, {0.3, 0.7}}];
g = Graph[{"A", "E"}, proc, GraphStyle -> "DiagramBlue"];

SetProperty[g, Sequence @@ (AbsoluteOptions[g, EdgeLabels] /. Tooltip -> 1/2)]


Few more alternatives -- all give the same picture

g1 = Graph[{"A", "E"}, proc, GraphStyle -> "DiagramBlue",
EdgeLabels -> {e_ :> PropertyValue[{g1, e}, "Probability"]}]


Or

g2 = Graph[{"A", "E"}, proc];
edgeprobs = (Join @@ {# -> PropertyValue[{g2, #}, "Probability"]} & /@ EdgeList[g2]);
Graph[{"A", "E"}, proc, GraphStyle -> "DiagramBlue",  EdgeLabels -> edgeprobs]


Or

g3 = Fold[SetProperty[{#, #2},
EdgeLabels -> PropertyValue[{#, #2}, "Probability"]] &,
g3, EdgeList[g3]]


An alternative way to get the probabilities associated with edges:

props = Properties /. AbsoluteOptions @ g
(* {"E" \[DirectedEdge] "E" -> {"Probability" -> 0.7},
"E" \[DirectedEdge] "A" -> {"Probability" -> 0.3},
"A" \[DirectedEdge] "E" -> {"Probability" -> 0.4},
"A" \[DirectedEdge] "A" -> {"Probability" -> 0.6}} *)

props /. {"Probability" -> a_} :> a
(* {"E" \[DirectedEdge] "E" -> 0.7,
"E" \[DirectedEdge] "A" -> 0.3,
"A" \[DirectedEdge] "E" -> 0.4,
"A" \[DirectedEdge] "A" -> 0.6} *)

-

From the documentation for DiscreteMarkovProcess, you can come up with this:

Graph[proc, GraphStyle -> "DiagramBlue",
EdgeLabels ->
With[{sm = MarkovProcessProperties[proc, "TransitionMatrix"]},
Flatten@Table[DirectedEdge[i, j] -> sm[[i, j]], {i, 2}, {j, 2}]],
VertexLabels -> {1 -> "A", 2 -> "E"}
]


I took the liberty of changing how the vertices are labeled (otherwise the edge label commands would become unnecessarily further complicated).

Considering that's how it's done in the documentation, I suspect this is the cleanest way to do it canonically. There may be a better hack that might pick apart the Markov process data structure and piece together a graph, but this is how Mathematica devs seem to have intended it.

SECOND VERSION

Based on comments following the question, perhaps a different approach would help.

This time I will try to be "constructive" in some sense. Let's start with the probabilities:

proc = {{0.6, 0.4}, {0.3, 0.7}};


Now, you want to create your edge labels as a table. Every individual edge label will be of the form:

DirectedEdge[2,2] -> 0.7


Since that is the (2,2) entry in your matrix of probabilities. To construct that table:

edge = Table[DirectedEdge[i, j] -> prob[[i, j]],
{i, 1, Length[prob]}, {j, 1, Length[prob[[i]]]}];


Here we use Length[prob] etc. to make sure our indices are right, but you can also just do:

edge = Table[DirectedEdge[i, j] -> prob[[i, j]], {i, 1, 2}, {j, 1, 2}];


That list, however, is still in an array/matrix format. So we flatten it:

edge = Flatten[edge];


Finally, we need some vertex labels to go with it:

vert = {1 -> "A", 2 -> "E"};


And now we are done:

proc = DiscreteMarkovProcess[1, prob];
Graph[proc, VertexLabels -> vert, EdgeLabels -> edge, GraphStyle -> "DiagramBlue"]

-