1
$\begingroup$

The following working program uses Graph and Markov Chain

P = {{1/2, 1/2, 0, 0}, {1/2, 1/2, 0, 0}, {1/4, 1/4, 1/4, 1/4}, {0, 0, 
   0, 1}}; proc = DiscreteMarkovProcess[3, P];
Graph[proc, GraphStyle -> "DiagramBlue", 
 EdgeLabels -> 
  With[{sm = MarkovProcessProperties[proc, "TransitionMatrix"]}, 
   Flatten@Table[DirectedEdge[i, j] -> sm[[i, j]], {i, 2}, {j, 2}]]]

sm = MarkovProcessProperties[proc, "TransitionMatrix"]
sm == P

Since I couldn't make it work for larger matrices, I clarified in the last two lines that sm is just P. But, if I try to replace sm by P in the first part, all hell breaks loose. So, I tried copy paste changing just P to a larger matrix, but this does not work. Why?

P = {{0, 1/4, 1/2, 1/4, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 0, 1/3, 0, 2/3,
     0}, {0, 0, 0, 0, 0, 1},
   {0, 0, 1/4, 0, 3/4, 0}, {1/4, 0, 0, 0, 3/4, 0}};
P // MatrixForm
proc = DiscreteMarkovProcess[1, P];
Graph[proc, 
 EdgeLabels -> 
  With[{sm = MarkovProcessProperties[proc, "TransitionMatrix"]}, 
   Flatten@Table[DirectedEdge[i, j] -> sm[[i, j]], {i, 6}, {j, 6}]]]
$\endgroup$

2 Answers 2

1
$\begingroup$
P2 = {{0, 1/4, 1/2, 1/4, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 0, 1/3, 0, 2/3, 0}, 
      {0, 0, 0, 0, 0, 1}, {0, 0, 1/4, 0, 3/4, 0}, {1/4, 0, 0, 0, 3/4, 0}}; 

proc2 = DiscreteMarkovProcess[1, P2];

tm2 = MarkovProcessProperties[proc2, "TransitionMatrix"];

You can specify the edge labels using patterns:

Graph[proc2, EdgeLabels -> {DirectedEdge[i_, j_] :> tm2[[i, j]]}]

enter image description here

Why your code does not work:

EdgeList @ Graph[proc2]

enter image description here

and the spec

With[{sm = MarkovProcessProperties[proc2, "TransitionMatrix"]}, 
 Flatten @ Table[DirectedEdge[i, j] -> sm[[i, j]], {i, 6}, {j, 6}]]

refers to non-existent edges (e.g, 1 -> 5 or 6 ->6).

Using a simpler example: Compare

Graph[{1 -> 2, 2 -> 3}, EdgeLabels -> {DirectedEdge[1, 2] -> 100}]

enter image description here

with

Graph[{1 -> 2, 2 -> 3}, 
 EdgeLabels -> {DirectedEdge[1, 2] -> 100, DirectedEdge[2, 2] -> 50}]

enter image description here

$\endgroup$
0
0
$\begingroup$

@kglr Thanks! Your code can be shortened a bit, since tm2 is precisely P, to

    P = {{0, 1/4, 1/2, 1/4, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 0, 1/3, 0, 2/3, 0}, {0, 0, 0, 0, 0, 1}, {0, 0, 1/4, 0, 3/4, 0}, {1/4, 0, 0, 0, 3/4, 0}}; 
proc = DiscreteMarkovProcess[1, P]; 
Graph[proc, EdgeLabels -> {DirectedEdge[i_, j_] :> P[[i, j]]}]

The command RuleDelayed (:>, :>) seems very useful :)

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