Graph and Markov Chain
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"]
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}]]]
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 *)
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"]