# How to obtain the number of Markov Chain transitions in a simulation?

I would like to obtain the number of transitions from each state in a Markov Chain simulation to other states, including back to itself so I can compare the simulation results to the transition matrix. I’m fairly new to Markov Analysis so please be gentle. From the help we have this relatively simple example involving weather.

weather = DiscreteMarkovProcess[1, {{0.7, 0.3}, {0.4, 0.6}}];
Graph[weather, VertexLabels -> {1 -> "rain", 2 -> "no rain"},
VertexSize -> Small,
EdgeLabels ->
With[{sm = MarkovProcessProperties[weather, "TransitionMatrix"]},
Flatten@Table[DirectedEdge[i, j] -> sm[[i, j]], {i, 2}, {j, 2}]],
ImageSize -> Medium]


The transition matrix is obtained by

transitionmatrix =
MarkovProcessProperties[weather, "TransitionMatrix"] // MatrixForm


Now I simulate and plot 1000 days of behavior.

days = 1000;
data = RandomFunction[weather, {1, days}]
ListPlot[data, Filling -> Axis, Ticks -> {Automatic, {1, 2}},
PlotRange -> {{0, days + 200}, {0, 2.5}}, Frame -> True,
FrameLabel -> {"Time (days)", "Weather Condition"},
FrameTicks -> {Automatic, {1, 2}, None, None},
PlotLabel -> "Simulation Results",
Epilog -> {Text["Rain", {days + 100, 1}],
Text["No Rain", {days + 100, 2}]}]


So far, so good (I think). I can also determine the number of times we arrived in a state of rain (State 1) or in a state of no rain (State 2)

rainstates = Count[data["PathStates"], 1]
norainstates = Count[data["PathStates"], 2]


But from the simulation data, how can I determine

a) the number of times we arrived to State 1 from State 1 ?

b) the number of times we arrived to State 1 from State 2 ?

c) the number of times we arrived to State 2 from State 1 ?

d) the number of times we arrived to State 2 from State 2 ?

• Tally@Partition[data["Path"][[All, 2]], 2, 1]? Jan 30, 2014 at 15:23
• @belisarius Thank you, that is what I needed. Jan 30, 2014 at 15:47

Trying to remove the question from the unanswered queue:

You can get the number of transitions with

r = Tally@Partition[data["Path"][[All, 2]], 2, 1]
(*
{{{1, 1}, 387}, {{1, 2}, 172}, {{2, 1}, 171}, {{2, 2}, 269}}
*)


And get a visual representation with:

g1 = g;(*Your Graph*)
(PropertyValue[{g1, DirectedEdge @@ #[[1]]}, EdgeStyle] =
Thickness[.05 #[[2]]/Tr@r[[All, 2]]];
(PropertyValue[{g1, DirectedEdge @@ #[[1]]}, EdgeLabels] =
Style[#[[2]], Black, 18, Background -> Yellow])) & /@ r;
g1