3
$\begingroup$

I'm looking to graph a simple one-way Markov chain, which is effectively a decision tree with transitions probabilities. One way I've got this working in here in an MWE, here's a simple Markov chain for different outcomes of a simple test:

t1 = {0, p, 1 - p, 0, 0, 0, 0, 0, 0};
t2 = {0, 0, 0, 1, 0, 0, 0, 0, 0};
t3 = {0, 0, 0, 0, 1, 0, 0, 0, 0};
t4 = {0, 0, 0, 0, 0, s, 1 - s, 0, 0};
t5 = {0, 0, 0, 0, 0, 0, 0, h, 1 - h};
t6 = {0, 0, 0, 0, 0, 1, 0, 0, 0};
t7 = {0, 0, 0, 0, 0, 0, 1, 0, 0};
t8 = {0, 0, 0, 0, 0, 0, 0, 1, 0};
t9 = {0, 0, 0, 0, 0, 0, 0, 0, 1};

proc = DiscreteMarkovProcess[1, {t1, t2, t3, t4, t5, t6, t7, t8, t9}];
namelist = {"N", "Positive", "Negative", "Test", "Test", "TP", "FN", "TN", "FP" };

o = Graph[proc, VertexSize -> 0.7, VertexLabels ->Thread[Range[9] ->Map[Placed[Style[#, {Bold}], 
Center] &, namelist ]], ImageSize -> Full, ImagePadding -> 25,  EdgeLabels -> {DirectedEdge[i_, j_] :> 
 MarkovProcessProperties[proc, "TransitionMatrix"][[i, j]]},  VertexShapeFunction -> "Circle", 
EdgeShapeFunction ->  GraphElementData["FilledArrow", "ArrowSize" -> 0.01], EdgeLabelStyle -> Directive[Black, Bold, 14],   PlotLabel -> "Possible test outcomes",  LabelStyle -> {Directive[Bold], FontSize -> 40}]

If I run this, I generate a figure like so:

enter image description here

The problem is this is difficult to read. Is there a way to either get Mathematica to rotate this by 90 degrees OR to graph this as a decision tree with the centre circle N at the top layer, followed by positive and negative on the next etc? It is important the transition probabilities are displayed too. Any ideas welcome!

$\endgroup$
2
  • $\begingroup$ Have you tried TreePlot? $\endgroup$
    – MarcoB
    Commented Apr 20, 2021 at 11:41
  • $\begingroup$ That is actually brilliant - just calling in Treeplot[o] did exactly what I wanted! Thank you - if you want to write this as an answer I'll accept it!! $\endgroup$
    – DRG
    Commented Apr 20, 2021 at 12:00

1 Answer 1

3
$\begingroup$
SetProperty[o, {VertexSize -> 0.4, 
  GraphLayout -> {"LayeredEmbedding", "RootVertex" -> 1}}]

enter image description here

Replace "LayeredEmbedding" with "LayeredDigraphEmbedding" to get:

enter image description here

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