Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]

enter image description here

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}]}]

enter image description here

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 ?

share|improve this question
2  
Tally@Partition[data["Path"][[All, 2]], 2, 1]? –  belisarius Jan 30 at 15:23
    
@belisarius Thank you, that is what I needed. –  Steve Jan 30 at 15:47

1 Answer 1

up vote 4 down vote accepted

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

Mathematica graphics

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.