8
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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? $\endgroup$
    – Luiz Meier
    Commented Aug 31, 2014 at 16:32
  • $\begingroup$ 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... $\endgroup$ Commented Aug 31, 2014 at 16:43
  • $\begingroup$ @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. $\endgroup$
    – Luiz Meier
    Commented Aug 31, 2014 at 16:46
  • $\begingroup$ 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. $\endgroup$ Commented Aug 31, 2014 at 16:52

3 Answers 3

13
$\begingroup$
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"]

Mathematica graphics

$\endgroup$
7
  • $\begingroup$ You answer is more readable and can also be used with 'unlimited' number of percentages also. Thank you. $\endgroup$
    – Luiz Meier
    Commented Aug 31, 2014 at 16:53
  • $\begingroup$ 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. $\endgroup$ Commented Aug 31, 2014 at 16:58
  • 1
    $\begingroup$ Right. All the answers are good. This one is more. Relax. $\endgroup$
    – Luiz Meier
    Commented Aug 31, 2014 at 17:00
  • 1
    $\begingroup$ @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 $\endgroup$ Commented Aug 31, 2014 at 17:06
  • 1
    $\begingroup$ @LuizRobertoMeier ToExpression@StringReplace[ToString@FullForm@h, "Tooltip" :> ".1"] $\endgroup$ Commented Sep 1, 2014 at 13:50
12
$\begingroup$

Update: Getting EdgeLabels from proc and using them directly in Graph:

Graph[vl = {"A", "E"}, proc, GraphStyle -> "DiagramBlue", 
 EdgeLabels -> Flatten[MapIndexed[DirectedEdge @@ (#2 /. MapIndexed[#2[[1]]
   -> # &, vl]) -> # &, proc[[2]], {2}]]]

enter image description here

Using MarkovProcessProperties[proc, "TransitionMatrix"] in place of proc[[2]] gives the same result.


Original answer:

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)]
(* or Tooltip -> Automatic *)

enter image description here

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} *)
$\endgroup$
9
$\begingroup$

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"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.