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