MC For Programmers2022
MC For Programmers2022
programmers
First Edition – April/2022
Ricardo M. Czekster
Birmingham, United Kingdom
This book originated as I perceived a need for better and simpler explanations sur-
rounding Markov Chains (MC). If you take the formal literature on this subject and
if you do not have a rather strong mathematical background to understand the con-
cepts, you are probably restricted to use MC, which I always thought that I could
change that. In my view, this is a nice opportunity to sharp programming skills as
the numerical methods proposed pose interesting challenges.
The purpose of this book is to present MC firstly to programmers (at any level), but
it is not restricted though; I think that broader audiences might enjoy it as well. The
idea is to grasp the basic notions and then implement solutions that are reproducible.
Proof of that is that I have shared C code in GitHub and supporting spreadsheets so
there are multiple ways of verifying answers.
I hope you enjoy the next pages. Please, feel free to send me your remarks, sug-
gestions, comments, and (eventual) critics.
I shall keep track of changes among editions, errata, etc., in a yearly rate. First
edition of the book was published in early April/2022 and it covered MC (types DTMC
and CTMC), basic notions, and introductory models.
v
vi
Table of Contents
Introduction 1
1 Markov Chains 3
1.1 Model and system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Emergence of Markov Chains . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Basic modelling primitives . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Input and output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6 Values decorating transitions . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 Markov property or memoryless property . . . . . . . . . . . . . . . 7
1.8 Let’s code! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.8.1 Challenge 01 . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.8.2 Challenge 02 . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.9 What is considered a ‘proper’ MC? . . . . . . . . . . . . . . . . . . . 11
1.9.1 Irreducible MC . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.10 Limitations of Markov Chains . . . . . . . . . . . . . . . . . . . . . . 13
1.11 Comments on the spreadsheets . . . . . . . . . . . . . . . . . . . . . . 13
2 DTMC 15
2.1 Remembering matrix operations . . . . . . . . . . . . . . . . . . . . . 16
2.2 The Belfast weather model . . . . . . . . . . . . . . . . . . . . . . . . 17
2.3 Let’s code! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.3.1 Challenge 03 . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.4 Solution methods for DTMC . . . . . . . . . . . . . . . . . . . . . . . 19
2.4.1 Power matrix, or Matrix-Matrix Multiplication . . . . . . . . 20
2.4.2 Vector-Matrix Multiplication . . . . . . . . . . . . . . . . . . 20
2.4.3 Forward simulation . . . . . . . . . . . . . . . . . . . . . . . . 21
2.4.4 Useful comments . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.4.5 Direct solution method . . . . . . . . . . . . . . . . . . . . . . 22
2.4.6 PRISM DTMC model . . . . . . . . . . . . . . . . . . . . . . . 23
2.5 Let’s code! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.5.1 Challenge 04 . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.5.2 Challenge 05 . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
vii
2.6 Comments on the spreadsheets . . . . . . . . . . . . . . . . . . . . . . 26
3 CTMC 29
3.1 Infinitesimal Generator . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2 Computing the embedded DTMC . . . . . . . . . . . . . . . . . . . . 31
3.3 The Lily Pad model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.4 Let’s code! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.4.1 Challenge 06 . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.5 Solution methods for CTMC . . . . . . . . . . . . . . . . . . . . . . . 35
3.5.1 Forward simulation . . . . . . . . . . . . . . . . . . . . . . . . 35
3.6 Race condition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.7 Comments on the spreadsheets . . . . . . . . . . . . . . . . . . . . . . 37
3.8 Let’s code! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.8.1 Challenge 07 . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.9 Solution methods (cont.) . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.9.1 Direct solution method . . . . . . . . . . . . . . . . . . . . . . 39
3.9.2 PRISM CTMC models . . . . . . . . . . . . . . . . . . . . . . 40
5 Final considerations 49
5.1 What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Index 55
List of Figures
1.1 A simple CTMC model with two states and two transitions. . . . . . . 6
1.2 An example of periodic MC with three states. . . . . . . . . . . . . . 11
1.3 Two MC for demonstrating irreducibility (in this case, the lack thereof)
properties (note that Q0 and Q1 are not irreducible). . . . . . . . . . . 12
ix
x LIST OF FIGURES
List of Tables
xi
xii LIST OF TABLES
Introduction
Markov Chains is a recurrent topic in systems analysis. The vast literature on the
subject and applicability attests its importance and timeliness. A host of authors
have delved into the formalism, proposing new modelling primitives, or enhancing
numerical tractability throughout the years. However, if one wants to learn about its
fundamental underpinnings, however simple, the overwhelming number of outcomes
prevents others from deepening the knowledge. Another problem is that sometimes
the wealth of previous results about Markov Chains focuses on specific audiences,
e.g., statisticians, mathematicians, and so on, seldom on computer programmers.
We aim to bridge this gap in this work, by presenting Markov Chains in simple
terms and then tackling the programming issues within the formalism. This book is
dedicated to programmers and people initiating studies in Markov Chains. It aims to
expose Markov Chains to programmers (at any level) having elementary mathemat-
ical background and willingness to delve into this modelling and analysis approach.
If you are looking for more formal aspects, please look elsewhere in the vast lit-
erature on this subject. This book is about understanding the basic principles sur-
rounding Markov Chains and how to implement basic numerical methods to derive
actionable indices for analysis. It has been written for programmers or engineers with
enthusiasm for computing (and coding), eager to get their hands dirty on Markov
Chains and willing to grasp results in short time.
In pursuing this objective, I shall:
The book does not address the mathematical foundations of Markov Chains and
intricate solution mechanisms such as Chapman-Kolmogorov equations, Perron-Fro-
benius theory, Krylov sub-spaces, Chebyshev methods, Arnoldi or GMRES solution
methods, and other similar concepts. Please, do submerse in this literature (else-
where) if it is relevant to you. There are a lot of interesting notions lurking in previous
research and, depending on your background, it might prove useful in your career.
For example, you could start looking at this book by Häggström [2] or Trivedi & Bob-
1
bio work on Reliability and Availability engineering [3] which are both remarkably
interesting.
Before we embark any further in this journey, a few housekeeping, and notes of
caution:
• From time to time, there will be programming ‘challenges’; the idea is that
one tries to code and test and come up with one’s own solution. There will
be presented matching code and comments to challenges in the last chapter –
most of the code will be produced using the C Programming Language.
– All code is covered by the GPLv3 License – free to redistribute citing the
source. Feel free to use it as you wish.
– For the spreadsheets MS-Excel was used – do test it on other platforms.
– GNU/Linux is the recommended OS for testing the software provided
here – it was tested on a Virtual Machine running KUbuntu 18.04 and
GNU/Linux kernel 5.4.100(generic).
• There is a website that accommodates all models, auxiliary code, and spread-
sheets available at: https://github.com/czekster/markov – clone it and
compile the projects thereof (the book is CC-BY-4.0 and the code is GPLv3).
• At all times, try to fully understand the code, what it does, how it does, and
why it does. Understanding the sequences of commands is crucial to improve
reasoning about the subject.
Enjoy.
Chapter 1
Markov Chains
This is a gentle introduction to Markov Chains (MC). Before delving into MC however,
we need to comment on basic key notions and ideas.
mathematics.
3
4 CHAPTER 1. MARKOV CHAINS
It was picked up by Alan Scherr in the 1960’s in his thesis on time shared systems
(MIT), where he applied to study scalability issues [4]. Nowadays, analysts employ
MC to study a variety of phenomena, from economic models to Internet searching
(PageRank® algorithm [5] [6]). The following paper [7] details the top five most
significant MC applications, with interesting discussions (in information theory, in
indexing the Internet, and so on).
1.5 Types
The literature provides a distinction for Markov Processes and Markov Chains. It
explains that modellers use the first for describing continuous processes whereas the
second one for discrete time considerations. For the present work we will call Con-
tinuous Time Markov Chains (CTMC) when working with durations and rates when
modelling states and Discrete Time Markov Chains (DTMC) when analysts would
decorate transitions with probabilities [3].
Important
• CTMC: when working with durations (that are converted to rates), res-
idence time in states (or sojourn times).
• DTMC: when decorating transitions with probabilities.
That is what will happen to the model, intuitively. A bit more formally, we care
about observations of the system at time T. If these are discrete, e.g., T = {0, 1, 2, . . .},
then we have a discrete stochastic (random) process whereas if T is continuous, where
T = {0 < t ≤ ∞} the process is continuous [11]. In CTMC, to proper work with the
Markov property, the time spent in a state must be independent of the time already
spent in that state. Thus, this time must be exponentially distributed whereas in
DTMC, the residence time in a state that respects the Markov property must be ruled
by a geometrically distributed value, which are the only distributions that exhibit the
memoryless property [11].
Table 1.1: Duration and rate for decorating transitions in CTMC models.
Observed duration Rate or frequency
(time spent IN the (OUT of the state) Remarks
state) in min over one hour
If any given entity has stayed in the
state for 60 minutes, the rate exiting
60 60/60 = 1 this state in one hour will be one, i.e.,
it would leave the
state once in one hour.
If in a state for 30 min, the rate out
30 60/30 = 2
would be modelled as two.
Note that one could have used 60 1
and 30 1
to decorate transitions without any difference
for the solution. That is because of, in terms of proportions, they are the same.
for reasons that will be explained later (note for impatient readers: the diagonal is
obliterated and the negative line sum of the rest of transitions is used). Now, let’s
reason about a simple model with two states as depicted in Figure 1.1 (this shall be
referred as “The lighting model”):
On Off
Figure 1.1: A simple CTMC model with two states and two transitions.
In this example, the modeller had observed that when a light switch was On, it
remained lit for 60 minutes whereas when Off, someone would invariably turn back
on every 30 minutes. As we shall discuss in detail next one could take this represen-
tation of the behaviour of a system and convert it to a table that will be subjected to
a numerical method that will be able to answer the following: “At any given time of
the day, what is the probability of the system in the On state or in the Off state”.
In other words: “You were somehow ‘teleported’ into this room: what is the proba-
bility that the light is either On or Off?”. Having the ability to answer this one might
take measures to decide on making changes to the system or promoting ways to help
users turn the light off more frequently to save money, and so on. Of course, this is
a very simple model, and it respects all requirements of MC in terms of well-formed
models, and well-behaved durations, and so on. We shall discuss cases where these
assumptions may not hold true.
1.7. MARKOV PROPERTY OR MEMORYLESS PROPERTY 7
November
December
September
February
October
January
August
March
April
June
May
July
1 16.4 3.2 22.9 4.4 3.3 43.7 7.3 23.1 1.3 5.4 6.7 1.5
2 53.7 14.0 4.7 3.6 6.0 50.3 12.2 28.8 1.0 4.4 12.3 39.6
3 48.4 11.7 15.2 0.1 46.5 26.7 28.9 21.9 6.2 22.7 13.4 11.8
4 6.0 5.4 19.5 4.9 28.3 17.6 7.4 14.5 4.7 1.7 11.2 24.6
5 1.6 35.9 4.1 7.6 13.2 17.5 27.5 18.2 46.1 4.9 10.4 28.1
6 20.9 22.7 14.9 119.9 5.2 102.1 3.4 83.2 16.6 45.8 2.1 11.4
7 22.8 27.1 5.9 40.3 28.8 8.4 18.8 34.4 5.6 32.3 66.3 1.0
8 28.6 1.4 2.0 4.4 13.8 22.3 2.0 34.2 1.6 37.2 8.5 2.9
9 40.7 2.8 5.0 36.3 1.2 9.8 61.3 31.8 4.5 10.6 20.4 40.3
10 20.9 27.4 11.2 6.8 50.0 20.8 1.2 9.9 10.1 3.1 27.6 12.7
11 0.3 4.7 15.1 3.3 20.4 26.7 29.8 42.1 8.5 4.5 14.9 36.3
12 29.5 2.1 41.3 3.5 3.5 0.7 24.8 26.2 2.2 9.3 8.7 0.2
13 46.0 1.5 41.2 51.5 38.2 7.5 6.1 12.0 79.2 2.3 42.2 0.7
14 3.4 52.5 148.5 10.6 5.5 21.3 10.2 11.0 16.7 49.7 2.0 11.4
15 9.1 8.7 39.0 5.2 1.4 20.2 2.6 22.0 42.1 57.5 28.7 17.3
16 82.0 10.3 1.7 2.9 8.6 38.9 91.0 10.2 23.1 17.2 5.9 0.3
17 14.7 21.8 24.9 94.5 10.1 19.8 6.6 32.0 32.2 2.3 20.9 21.9
18 20.9 51.7 37.7 29.7 1.1 1.8 9.5 15.5 20.8 12.9 61.7 5.1
19 2.9 18.0 30.0 21.8 0.1 5.7 128.7 16.3 5.8 60.6 3.4 49.5
20 41.4 13.7 57.5 2.9 32.4 33.0 6.7 16.3 30.2 9.6 34.9 5.2
21 0.7 11.1 9.6 9.3 30.1 11.2 3.9 6.4 6.6 8.8 32.5 19.8
22 38.4 8.1 20.9 36.6 6.3 1.6 52.9 37.7 5.3 31.6 24.2 40.5
23 19.1 6.0 10.6 3.0 48.1 54.8 28.9 40.1 0.5 5.7 38.1 16.2
24 45.3 50.1 13.8 10.1 10.2 12.9 31.7 2.3 5.1 40.0 32.9 55.5
25 6.7 6.7 5.9 6.7 89.7 33.6 6.8 9.4 3.5 46.6 13.0 4.1
26 21.4 40.8 4.9 1.0 7.3 31.8 13.7 21.0 39.1 25.5 15.0 9.5
27 7.1 10.9 1.2 24.9 3.1 3.4 7.0 42.0 50.3 28.4 67.3 7.1
28 24.8 4.2 11.6 51.0 22.9 13.2 9.9 93.9 6.2 23.4 75.2 5.6
29 62.5 3.0 2.2 21.2 17.1 49.0 5.4 31.2 0.7 50.1 15.1
30 9.1 4.6 29.0 32.1 6.9 34.2 28.7 26.6 5.9 0.4 20.6
31 112.8 59.4 2.4 12.2 15.9 24.4 22.2
µ 27.7 17.0 22.2 20.9 19.1 22.7 23.8 26.0 17.8 20.5 25.0 17.3
these numbers). This is because one wants to work with the exponential distribution
(at least for the time being).
What might happen in real life is that real measurements might not fit into the
exponential distribution. In this case, we are in violation of employing MC to work
with our problem and we shall resort to other technique, for instance, simulation.
This shall be commented further in due time. For the time being, let’s be happy with
the fact that we can generate numbers from the exponential (memoryless) distribu-
tion. The last line (µ) in the table contains the monthly average.
If you take a closer look at this table, you will see that the average (for all table)
1.8. LET’S CODE! 9
is 21.7 minutes, the minimum value is 0.1 minutes (3.2 seconds!) and the maximum
value is 148.5 minutes (2.5 hours!!). So, there are good days and bad days, but on
average, the expected behaviour is to approximate to the rate I set forth: 20 minutes.
It is true that it is memoryless, sometimes the bus took let’s say 7 minutes (January
27th ) but on January 28th , it took 24.8 minutes to arrive! As a matter of fact, it is
capturing the memoryless property at its finest.
This is contrasted with Discrete Time. The best idea of understanding the concept
is to consider a sort of metronome, with pulses ticking the same (without any losses).
So, the system governed by a DTMC would change states following the rules set forth
by the probabilities that decorated the transitions, without the notion of time passing
(as it happened in CTMC).
To build a DTMC simulation, it suffices to model a situation where a visitor stands
on a given state and draw a pseudo-random number from the uniform distribution
and then use this value to consult and then jump to the next state. And then repeat
this process many times and counts the visits to each state. Time is considered to jump
on every metronome pulse, moment that the system changes state. We will stop now
explaining about simulation, this topic is covered in detail in the next chapters.
1.8.1 Challenge 01
File: challenge01.c
Do: write a program that given a parameter N (passed in the command line),
it computes and shows N uniformly distributed numbers (each line shows 10
numbers, to ease output).
Notes:
• The parameter (N) is an integer, but you will have to convert from the
command line using the atoi function (this function converts a string
to an integer).
• Use #include <stdlib.h> and #include <time.h> and auxiliary
function random().
• Use a seed based on the current time (for increased randomness) with
3 Most programming languages employ a pseudo-random generator that produces number from known
formulas. See Linear Congruential Generators in the Rosetta code project: https://rosettacode.org/
wiki/Linear_congruential_generator#C.
10 CHAPTER 1. MARKOV CHAINS
function srand(time(NULL)).
Good programming practice write a function to compute a uniform
number and another function to show N uniform numbers, separating
concerns in your code.
1.8.2 Challenge 02
For the next challenge we shall use parts of the previous challenge (Section 1.8.1)
to generate exponentially distributed numbers (adding the following ‘cell’ formula:
=(-1/A1)*LN(1-RAND() – in MS-Excel, with parameter in cell A1)4 .
This challenge will use auxiliary mathematical libraries already available in most
GNU/Linux distributions.
File: challenge02.c
Do: write a program that given a parameter N (passed in the command line)
corresponding to the PARAMETER of the desired exponential distribution
and another parameter M (the number of samples to compute), with func-
tions to compute samples from the exponential distribution given the uniform
distribution.
• The function prototype is float next_exp(float rate);
Notes:
• The parameter (N) could be a float value, so conversions will use the
atof function.
A B
1
1
Starting in state A one might return to state A after three hops (one to B, then
to C, then back to A) every time. This characteristic defines a periodic MC, so we
shall concern modelling chains that are aperiodic. Finally, we will address positive
12 CHAPTER 1. MARKOV CHAINS
recurrent MC, i.e., those chains that are recurrent, they eventually return to the state
after a number of steps and positive if the time it took to return is relatively fast
(intuitively speaking).
That’s the mantra of MC in (almost every) book ever written about this subject.
Start getting acquainted with the notion that not everything that you put down as
a model will be considered a MC and have a steady-state solution. If your MC ad-
heres to these properties, discovering the solution vector shall be possible within a
decent amount of time, using appropriate numerical methods. We will not cover here
MC with absorbing states, i.e., states with only incoming transitions and no outgo-
ing transitions. It is worth noting that non-ergodic MC will not yield results if one
employs a (let’s say) classic numerical method. As we shall see these models might
yield results if one resorts to simulation approaches.
1.9.1 Irreducible MC
Consider the next two MC, Q0 and Q1 depicted in Figure 1.3. In model Q0 , whenever
state E gets visited, it stays switching between E and F states indefinitely. In terms of
result vector, we shall see probabilities only for those two states.
1 1
2 3 4 2 3 4
A B C D A B C D
5 6 7 5 6 7
8 8
1
9 9
E F E F
10 10
Figure 1.3: Two MC for demonstrating irreducibility (in this case, the lack thereof)
properties (note that Q0 and Q1 are not irreducible).
In model Q1 , there are in fact two MC to consider, one with states A, B, C, and D,
and another with E and F. Thus, one could solve each one separately, instead of it all.
We are concerned here about chains that are irreducible, i.e., starting in any state, it
is possible to reach any other state (not necessarily in one step). If Q1 had a transition
from F to A, B, C, or D, it would be sufficient to deem the matrix irreducible, because
it would respect the property.
William Stewart’s book [11], on page 38, discusses so called Nearly Completely
Decomposable (NCD) stochastic matrices, cases where transitions among states are
1.10. LIMITATIONS OF MARKOV CHAINS 13
weak (or low), where the matrix is irreducible however the model could have been
broken in two (or more) to ease analysis. The literature refers to partitioning the state
space in subsets with strong and weak interactions. Please, refer to the source for
further information about NCD. Interested readers may also enjoy other properties
such as aggregation or lumpability as well as other notions that surface as recurrent
topics in MC.
Play with the spreadsheet, change parameters, see what happens, build code that
replicates (note: it will never yield the same results5 since you are employing different
sets of pseudo-random numbers) the results of the spreadsheet.
5 Unless you make the seed parameter to the srand(int seed); function constant.
14 CHAPTER 1. MARKOV CHAINS
Chapter 2
DTMC
Let’s start discussing DTMC, where models have probabilities in the transitions, as
depicted in Figure 2.1 (this model was covered and commented earlier, the only dif-
ference is that for the other one we used rates – CTMC):
0.50
0.50 On Off
1.0
For this MC, one may assume that since the On state is the most visited (because
of the self-loop present in this model), the final probability vector π (our result) will
be higher on this state (intuitively). The restriction of DTMC models is that they
must have each line summing to 1.0, and for the case of numerical solution, that it
also respects previously discussed MC properties.
There are several ways of solving this DTMC. For instance, after representing
the model in a matrix, one could multiply it by itself many times, until it reaches
convergence (steady state). This is known as the Power Matrix method. But first,
let’s create the model appropriately in Table 2.1:
15
16 CHAPTER 2. DTMC
After solution using the Power Matrix method (please, consult file in reposi-
tory called spreadsheets/Chapter02-DTMC-2states.xlsx), we compute that
πon = 0.66667 (around 67%) and πOff = 0.33334 (around 33%). Note that in the MS-
Excel file we have employed the function MMULT for the same matrix. To do this, one
must first select an empty 2x2 result cell (where the results will be), pressing F2, then
putting the formula =MMULT(D3:E4,D3,E4) and pressing CTRL-SHIFT-ENTER. This
is required in MS-Excel when operating with matrix multiplication (where the results
are matrices as well). It was necessary to multiply the matrix by itself seven times
(M7 ) before reaching convergence (steady state) for this model. You may try to repli-
cate these results as well.
Another way of reaching the same conclusions is to employ a Vector-Matrix Mul-
tiplication (VMM) method that from an initial vector multiplies the matrix iteratively
until it finds the results. This method is more lightweight than the Power Method,
because instead of a Matrix-Matrix multiplication, now it is a Vector-Matrix prod-
uct, which is less cumbersome. It suffices to say that this method produces the same
results as before. It took 21 steps for reaching the steady state.
Another difference in the MS-Excel for this method is needed: we should fix the
matrix position (using the symbol ‘$’ before cell placements). The initial vector cho-
sen was [ 1 0 ] however, any initial vector summing 1.0 would work – perhaps
the iterative method will have the side effect of performing better, i.e., producing the
results in fewer iterations. That is what is called preconditioning, and there is a wealth
of research on methods to accelerate solution of MC. If the vector does not sum 1.0,
you will have to adjust the results, because let’s say, it sums 0.25, this 0.25 will be
distributed 66% to the On state (≈ 0.16667) and 34% to the Off state (≈ 0.08333),
which is equal to the answer.
A B C J K L
Let X = D E F and Y = M N O
G H I P Q R
Then,
AJ + BM + CP AK + BN + CQ AL + BO + CR
X × Y = DJ + EM + F P DK + EN + F Q DL + EO + F R (2.1)
GJ + HM + IP GK + HN + IQ GL + HO + IR
There are several APIs for handling matrix multiplication efficiently. We are
showing the basic process here because we are interested in coding this down the
2.2. THE BELFAST WEATHER MODEL 17
line (for our next model). As we have shown, MS-Excel (and other spreadsheet-
based applications) have built-in functions to help multiplying matrices altogether.
For Vector-Matrix Multiplication, the process is easier, i.e., only the first line of X is
used (Equation 2.2):
J K L
Let X = A B C and Y = M N O
P Q R
Then,
(2.2)
X × Y = AJ + BM + CP AK + BN + CQ AL + BO + CR
This concludes the basic reviewing of matrix operations that we will require to
code the solution.
0.8 0.2
0.05
Rainy Sunny
0.5
0.7 0.1
0.15 0.3
Cloudy
0.2
Just by looking at transitions and their weight (the values associated to the mod-
elled probabilities decorating transitions), one might assume that the expected prob-
ability result for both Rainy and Cloudy states will be high. Seldom it transitions to
1 Häggström [2] contrasts two other weather models, with comparisons, namely the Gothenburg model
the Sunny state, and from this state, it returns with high probability towards Rainy
(0.5) or Cloudy (0.3) states.
The solution of this model (spreadsheets/Chapter02-DTMC-3states.xlsx)
shows that the final probability vector π is distributed as: πRainy = 0.7625, πCloudy =
0.16875, πSunny = 0.06875. So, Rainy or Cloudy days account for almost the totality
of probabilities, Rainy + Cloudy = 0.7625 + 0.16875 = 0.93125, which is the same
of thinking Rainy + Cloudy = 1 − Sunny (for this model), yielding the same result.
So, one could expect that any given day, in Belfast, to be either Rainy or Cloudy,
and prepare for this.
2.3.1 Challenge 03
Before we start discussing the challenge, it is worth noticing that we are doing vali-
dation instead of verification2 .
File: challenge03.c
Do: write a program that declares a static matrix and runs some tests (e.g.,
sum of lines equals to one, etc.) and output features.
Notes:
• The matrix corresponding to the model could be defined hard coded
with static declarations (float m[3][3];), however, you could im-
plement a way of opening a (text) file with the model (where values are
separated by spaces or commas). That would require dynamic alloca-
tion and pointers.
Good programming practice:
• Use #define LIN 3 and #define COL 3.
• Employ error codes (as #define) for returning values in the
function, for example, ERR_NOT_SQUARE, ERR_ABSORBING,
ERR_LINE_SUM, SUCCESS).
Implement:
2 The word verification conjures the notion of formal verification processes that models might undergo
against specifications.
2.4. SOLUTION METHODS FOR DTMC 19
then diverged (the opposite of converged), and you should state this to whomever
call the method and show the last computed vector.
Note that there are no convergence insurances that this method will produce valid
π probabilities vector after execution. It is the job of the modeller to analyse (partial)
results and investigate causes when diverging, refining models, reviewing transition
probabilities, and so on.
The power matrix method may converge faster than this method, however, VMM
requires less mathematical operations to complete on each iteration. On each iter-
ation of VMM it is possible to see the π vector being redistributed throughout the
positions, always summing to 1.0 (as a matter of fact it will sum to the value set
earlier).
Let’s revisit the Belfast weather model and apply the VMM method:
0.80 0.15 0.05
1 0 0 × 0.70 0.20 0.10
0.50 0.30 0.20
Initial Vector: 1 0 0
Iter.1: 0.8 0.15 0.05
Iter.2: 0.77 0.165 0.065
Iter.3: 0.764 0.168 0.068
Iter.4: 0.7628 0.1686 0.0686
Iter.5: 0.76256 0.16872 0.06872
Iter.6: 0.762512 0.168744 0.068744
Iter.7: 0.7625024 0.1687488 0.0687488
Iter.8: 0.76250048 0.16874976 0.06874976
Iter.9: 0.762500096 0.168749952 0.068749952
Note that by Iteration 9, the residue between Iteration 8 and this one is negligi-
ble. Depending on accuracy requirements, this could be handled by continuing the
process until the residue is smaller than a pre-defined threshold.
• Define a start state to visit initially (any state belonging to the model).
• Modify the probability matrix of the DTMC to withhold the cumulative prob-
abilities on each position, that will ease the selection of the next state.
For example, suppose a three state DTMC with the following probabilities:
0 1 2 0 1 2
0 0.2 0.4 0.4 0 0.2 0.6 1.0
1 0.1 0.2 0.7 becomes 1 0.1 0.3 1.0
2 0.3 0.6 0.1 2 0.3 0.9 1.0
On the left-hand side, there is the original matrix, and on the right-hand side, an
accumulated version, on each state, of the same matrix.
When making draws for each state, let’s say in state 0, and a pseudo-random
number equals to 0.345632, then it suffices to consult the accumulated table and see
that if the number was between 0.0 and 0.2, then the next state would be 0, if the
number was between 0.200001 and 0.6 the next state would 1, otherwise state 3.
Let’s continue the process. From that chosen start state, one:
1. Check if the number of samples reached the desired value: if yes, stop the
process (and go directly to step 5);
2. Add the visit to this state in a counting vector withholding all visits;
5. If the probability is less than the position at the cumulative matrix, jumps to
that position;
7. Compute visits statistics (e.g., divide each position of the counting visits vector
by the total number of samples);
8. Present the vector to the user (it should approximate to the analytical counter-
part);
π×P =π (2.3)
Recall that π is a vector of unknowns, i.e., for the Belfast weather model (with
three states, according to Section 2.2 with a probability vector having size three:
[πRainy , πCloudy , πSunny ]). This is what we would like to compute, i.e., the probabil-
ity of each state in the model. To simplify the discussion, we shall refer this symbolic
probability vector as [π1 , π2 , π3 ].
2.4. SOLUTION METHODS FOR DTMC 23
If you recall the Belfast weather model, here are the state transitions and the
required operations for performing π × P = π:
0.80 0.15 0.05 π1
π1 π2 π3 × 0.70 0.20 0.10 = π2
0.50 0.30 0.20 π3
We should work with the variables and also add the following equation to the
mix: π1 + π2 + π3 = 1. So, after operating (after some algebra) on the equations, we
come up with the following system to solve:
(0.8 − 1)π1 + 0.70π2 + 0.50π3 = 0
−0.20π1 + 0.70π2 + 0.50π3 = 0
0.15π + (0.2 − 1)π + 0.30π
0.15π − 0.80π + 0.30π
1 2 3 = 0 1 2 3 = 0
0.05π 1 + 0.10π 2 + (0.2 − 1)π 3 = 0
0.05π 1 + 0.10π 2 − 0.80π 3 = 0
1.00π1 + 1.00π2 + 1.00π3 = 1 1.00π1 + 1.00π2 + 1.00π3 = 1
This is a system of linear equations with three unknowns. The best (and fastest)
way to solve this is using MATLAB® (or GNU/Octave) for discovering the π proba-
bility vector (refer to file matlab/matlabsolution_dtmc.m in the GitHub repos-
itory).
% MATLAB Model
M = [ -0.20, 0.70, 0.50;
0.15, -0.80, 0.30;
0.05, 0.10, -0.80;
1.00, 1.00, 1.00 ];
b = [0; 0; 0; 1];
pi = linsolve(M,b);
The solution vector is: [πRainy , πCloudy , πSunny ] = [0.7625, 0.1688, 0.0688] which
(not surprisingly) corresponds to the solution for this model (using the Power method
or VMM as explained earlier).
dtmc
module Module1
n : [0..N] init sRainy; // total states, initial state sRainy
// transitions
[] (n=sRainy) -> 0.80 : (n’=sRainy) +
0.15 : (n’=sCloudy) +
0.05 : (n’=sSunny);
[] (n=sCloudy) -> 0.20 : (n’=sCloudy) +
0.70 : (n’=sRainy) +
0.10 : (n’=sSunny);
[] (n=sSunny) -> 0.20 : (n’=sSunny) +
0.50 : (n’=sRainy) +
0.30 : (n’=sCloudy);
endmodule
Observe that it yielded the same probability vector as output as all other methods
explored here so far.
For the next challenge it will be required to work with C pointers and dynamic al-
location so you should sharpen up your skills around these concepts before venturing
into the challenge.
2.5.1 Challenge 04
We will implement a solution to work with the power method and the VMM.
2.5. LET’S CODE! 25
File: challenge04.c
2. Vector-Matrix Multiplication
Notes:
• We will need to use dynamic allocation (employing pointers) in C for
the next tasks.
2.5.2 Challenge 05
This challenge will implement a basic forward DTMC simulator.
File: challenge05.c
Do: write a program that runs a simple DTMC simulator, where given a
model, a visitor jumps from state to state during a pre-defined number of
steps.
Notes:
• Implement a function void dtmc_simulator(float** m, int
size, int samples, int* results); that simulates a DTMC
(the function returns the solution vector π in results).
• The results should approximate to the analytical solution as observed
in previous challenge (Section 2.5.1).
by the model’s matrix until the last two resulting vectors are the same between two
iterations (in this case, it ran for nine iterations).
Finally, the DTMC simulator will run a simulation using pseudo-random numbers
(uniformly distributed) mimicking the idea of visiting states one after the other for a
fixed number of times (in the spreadsheet, it is set to run until it produces 1,000 state
samples).
After it runs, it computes the resulting probabilities. In terms of results, one no-
tices that they are quite close to the analytical ones (computed previously), however,
a larger number of samples would produce even closer results.
28 CHAPTER 2. DTMC
Chapter 3
CTMC
In terms of modelling, one could wonder about the expressiveness of CTMC over
DTMC. The idea is that one could think about state permanence, or durations, in-
stead of probabilities. CTMC are different than DTMC when modelling situations
because one thinks on the time spent in the state (residence or sojourn time) and
how it transitions to other states, using rate = duration
1
to represent the rate out of
the state.
Figure 3.1 shows how the balancing process works for each state: the negative
sum of outgoing rates is used to even out the rates, so each line in the model will sum
to zero.
b
-(a+b+c) state
c
Here, this is not a MC per se, but only a representation of how and why one
does not consider self-loop transitions. The side-effect of this is that, in CTMC, the
diagonal is not used in the modelling process, because it is destroyed to accommodate
the sum of exits and balance out the rates in and out the state.
29
30 CHAPTER 3. CTMC
destroying the diagonal (self-loops in the model) and substituting it with the negative
sum of rates of this state, balancing rates in and rates out.
Let’s revisit the Lighting model once again in Figure 3.2:
On Off
The same process is repeated for higher order matrices, i.e., a Generic model with
four states (Figure 3.3):
1
1 9
2
5
2
0
4 1
2
7
1 It contrasts with matrix M in DTMC, where each line sums 1.0, whereas Q sums 0.0.
3.2. COMPUTING THE EMBEDDED DTMC 31
Note that both models respect previously mentioned properties to be MC, i.e.,
they are ergodic (Section 1.9).
• Because of the way the IG is built, this value will be always located in the
diagonal.
• Find the highest value in absolute terms and then use it with its signal –
more about this in a moment.
Qij
4. For each cell of Q, i.e., Qij , compute Mij
′
= Iij − maxQ
.
Let’s examine this example using the Generic model presented in previous section
and use an Identity Matrix of order 4 (I 4 ) to derive the embedded DTMC. For this case,
maxQ = −11.
I4 Q M′
z }| { z }| { z }| {
−10
1 0 0 0 1 5 4 0.09 0.09 0.45 0.36
0 1 0 0 2 −2 0 0 0.18 0.82 0.00 0.00
, ,
0 0 1 0 0 9 −11 2 0.00 0.82 0.00 0.18
0 0 0 1 7 0 1 −8 0.64 0.00 0.09 0.27
We created a new version of the model and called it M ′ , which is the embed-
ded DTMC of Q. Let’s compute the first cell: M00 ′ Q00
= I00 − maxQ
−10
= 1 − ( −11 ) =
0.090909091 (the matrix shows a less accurate number for presentation purposes).
The process is subsequently repeated for all cells of Q.
For reference, these are all intermediary computations for reaching M ′ :
32 CHAPTER 3. CTMC
1 − ( −10
−11 )
1
0 − ( −11 ) 5
0 − ( −11 ) 4
0 − ( −11 )
2 −2
0 − ( −11 ) 1 − ( −11 ) 0 0
M′ =
0 9
0 − ( −11 ) 1 − ( −11
−11 )
2
0 − ( −11 )
7 1 −8
0 − ( −11 ) 0 0 − ( −11 ) 1 − ( −11 )
So, this is the embedded DTMC of the CTMC, the discrete time version of it (where
each line sums to one). One may use any of those previously discussed solution meth-
ods available for DTMC to unveil the probability vector π.
Take some time to look at the resulting DTMC. Observe that now there are some
self-loop transitions (from 0 to 0, from 1 to 1 and from 3 to 3). If one was modelling
from the onset using DTMC, one should take those transitions into account. That
is why one might think that employing CTMC for modelling is sometimes best to
represent complex behaviour.
6 2
11 9 3
A 9
B 2
C 2
D
8
7
Figure 3.4: Lily Pad model showing possible pads and transitioning rates.
Consider Table 3.1 for thinking about the rates decorating transitions in the model.
Note that for modelling time, the observer has attended the pond and remained col-
lecting frog residence times over a period of time, to have a nice idea of jumps and
durations.
3.3. THE LILY PAD MODEL 33
Table 3.1: Residence time observed in lily pads and corresponding rates.
Time
From To Rate
(min)
5.4 B 11
10 A C 6
60 D 1
6.7 A 9
6.7 B C 9
30 D 2
7.5 A 8
30 C B 2
20 D 3
12 A 5
8.6 D B 7
30 C 2
One might think that, when modelling, it is easier to consider states and ‘entities’
changing states where each has remained some time in the state before transitioning
to other state. The reasoning is that sometimes, time is the only observable thing
to model the system. Recall that for modelling DTMC one requires the probabilities
for switching states. Because of the formula to convert durations to rates, lower
time spent in the state corresponds to higher rates out of it, as seen in the table. For
example, from pad A to pad B with a duration of 5.4 min, the converted rate was equal
to 11, whereas from pad A to pad D, the frog stayed for 60 min and the rate out in
one hour was one.
The IG (Q) for this model (maxQ = −20) and its embedded DTMC M ′ is:
−18 11 6 1 0.10 0.55 0.30 0.05
9 −20 9 2 0.45 0.00 0.45 0.10
Q= M’ =
8 2 −13 3 0.40 0.10 0.35 0.15
5 7 2 −14 0.25 0.35 0.10 0.30
As previously mentioned, with the embedded DTMC M’, one could employ Power
matrix, VMM, simulation, or direct methods to discover π. Using the VMM, we reach
the following results (starting with vector [ 1 0 0 0 ]):
π = 0.302449696 0.243404608 0.327575347 0.12657035
We come to the conclusion that the frog has a preference for lily pads A, B, and
C (1 − 0.12657035 = 0.87342965, ≈87% of the time), given the probabilities that
we have computed. It is worth mentioning that those are the same results if one
employed the Power matrix method using the embedded DTMC.
34 CHAPTER 3. CTMC
3.4.1 Challenge 06
This challenge ‘fix’ the diagonal of a CTMC and then convert it to its embedded
DTMC representation.
File: challenge06.c
Do: write a program that will take a CTMC as parameter and then compute
the main diagonal accordingly and convert it to its embedded DTMC.
Notes:
• Implement a function void fix_ctmc(float** q, int size);
that will destroy the current main diagonal and replace it with the sum
of line accordingly.
– The idea is that if you are traversing Q, then if you are on the
diagonal position (when i==j), then you proceed with the proper
computation (as discussed earlier), otherwise consider it as zero.
Suggested variations:
4. From an initial state S0 , draw a number from the exponential distribution (con-
sidering the rates in Q) and then visit the next state, accumulating all these
visits in a counting vector (to present to modellers afterwards).
• The formula to generate an exponential distributed variable using a pseudo-
random number generated using the uniform distributions is Exp(rate) =
1
(− rate ) × log(1 − U(0; 1), e), where U(0, 1) is a pseudo-random number
between 0 and 1 and log is the natural logarithm of a number (base e).
Recall the Q matrix for the Generic model using CTMC (Section 3.2):
−10 1 5 4
2 −2 0 0
Q=
0 9 −11 2
7 0 1 −8
For reference, these are all intermediary computations for computing Q′ (MAXQ =
−11):
2 −2 − (−11 × 1) 0 0
The new matrix Q′ is equal to (observe that the only change is in the main diag-
onal):
−10 1 5 4 1 1 5 4
2 −2 0 0 2 9 0 0
Q= Q’ =
0 9 −11 2 0 9 0 2
7 0 1 −8 7 0 1 3
If you simulate this matrix, you will reach results that are approximations of the
other solution methods for this model.
4
state 1
9 state 2
state
1
state 3
modelled 5 state 4
rates
Figure 3.5: Hypothetical situation showing the problem of choosing the next state in
CTMC simulation.
The answer is: the fastest. As we have discussed earlier, it does not matter the
highest rate when you use the exponential distribution. Only after you draw a num-
ber from the distribution you know the parameter to use to decide “which transition
will win the ‘race’ and become the next state visited”.
Important Remember that Figure 3.5 does not show previous transitions
for reaching the state: recall that in MC the past is irrelevant, what only mat-
ters is where to ‘jump’ next.
3.7. COMMENTS ON THE SPREADSHEETS 37
In a CTMC simulation you are working with time; so, this means that you will
draw pseudo-random numbers using each state rates and then you will choose the
one that happened first in time. Figure 3.6 details this process, and shows the winning
transition for this particular case.
4
state 1 Rate Uniform Exponential Rank
4 0.3024550 0.0900470 2
9 state 2
9 0.8295417 0.1965850 4
state
1 0.0528990 0.0543496 1
1
state 3 5 0.6072475 0.1869151 3
5 state 4
So, given the pseudo-random numbers, and after establishing a rank (to simulate
the lowest time), one decides that state 3 will be the next state visited2 . This is
the core of the CTMC simulator, visiting states, and depending on the rates on each
state, deciding where to go next, simulating time. Since you are simulating, you could
choose how long would you like to simulate in terms of time, e.g., 10 hours.
3.8.1 Challenge 07
File: challenge07.c
Do: write a program that will take a CTMC as parameter and then simulates
it for some time (passed as parameter in the command line).
2 Pure chance has selected the minimum rate in this example – be mindful of this. I suggest you open
There is a CTMC simulator written for MS-Excel in the following files (on Tab
CTMC simulator):
After multiplication, one has the following system of equations (plus the one for
ensuring that all π will sum to 1.0, i.e., π1 + π2 + π3 + π4 = 1):
−10π1 + 2π2 + 7π4 = 0
π1 − 2π2 + 9π3 = 0
5π1 − 11π3 + π4 = 0
4π1 + 2π3 − 8π4 = 0
π + π + π + π
1 2 3 4 = 1
For this model one could start by defining π1 = 1 and then doing algebra to dis-
cover π3 and π4 (on the third and fourth equations), then circling back to π2 (second
equation). Then, the next step would be to uniformise the results, i.e., all probabilities
sum to 1.0.
We submitted this system of linear equations to MATLAB® (please, refer to file
matlab/matlabgeneric_model_dtmc.m in the GitHub repository).
40 CHAPTER 3. CTMC
% MATLAB Model
Q = [-10, 2, 0, 7;
1, -2, 9, 0;
5, 0, -11, 1;
4, 0, 2, -8;
1, 1, 1, 1];
pi = linsolve(Q,b);
module Module1
n : [0..N] init sA; // total states, initial state sA
// transitions
[] (n=sA) -> 11 : (n’=sB) + 6 : (n’=sC) + 1 : (n’=sD);
[] (n=sB) -> 2 : (n’=sD) + 9 : (n’=sC) + 9 : (n’=sA);
[] (n=sC) -> 2 : (n’=sB) + 8 : (n’=sA) + 3 : (n’=sD);
[] (n=sD) -> 5 : (n’=sA) + 7 : (n’=sB) + 2 : (n’=sC);
endmodule
------------------------
Printing steady-state probabilities in plain text format below:
0:(0)=0.30244974694693344
1:(1)=0.24340456372498753
2:(2)=0.32757532070253004
3:(3)=0.12657036862554888
// Generic model
ctmc
module Module1
n : [0..N] init s0; // total states, initial state s0
// transitions
[] (n=s0) -> 1 : (n’=s1) + 5 : (n’=s2) + 4 : (n’=s3);
[] (n=s1) -> 2 : (n’=s0);
[] (n=s2) -> 9 : (n’=s1) + 2 : (n’=s3);
[] (n=s3) -> 7 : (n’=s0) + 1 : (n’=s2);
endmodule
------------------------
Printing steady-state probabilities in plain text format below:
0:(0)=0.20235285239955195
1:(1)=0.567058916201716
2:(2)=0.1035294076461007
3:(3)=0.1270588237526314
42 CHAPTER 3. CTMC
Chapter 4
In this chapter we will comment about other projects and some models based on MC
that are worth discussing.
4.1 Projects
The code present here and in the repository are far from having a bug-free status. As
a starting project, one could improve the code and translate it to C++, or adding more
controls, working with increased number of states1 .
For OP=2, the user must also inform the number of samples at some point and for
OP=4, the simulation time (in time units).
1 Please consider doing this and then a subsequent Pull Request on the GitHub repository.
43
44 CHAPTER 4. MORE PROJECTS AND MODELS
Suggestion: before looking at the solution, take all challenges and try to come up
with your own solution.
4.1.2 Visual MC
Find a way of representing MC visually, using an auxiliary library or API like graphviz
or some other.
4.2 Models
4.2.1 Birth and Death model
Figure 4.1 shows a four states Birth and Death [15] process in CTMC with symbolic
rates λ (for arrivals, i.e., births) and µ (for departures, or deaths).
0 1 2 3
0 1 2 3
0 −λ λ 0 0
1 µ
−(µ + λ) λ 0
2 0 µ −(µ + λ) λ
3 0 0 µ −µ
There are known equations that work with this type of models (closed form) [15,
14]. And also check out the model S6-birth-and-death-ctmc.txt in the GitHub
repository for a six states Birth and Death process.
4.2. MODELS 45
1 2 3
6 5 4
7 8 9
The model, as follows, shows the probabilities for reaching adjacent cells in the
maze.
1 2 3 4 5 6 7 8 9
1 0 0.50 0 0 0 0.50 0 0 0
2 0.33 0 0.33 0 0.33 0 0 0 0
3
0 0.50 0 0.50 0 0 0 0 0
0
4 0 0.33 0 0.33 0 0 0 0.33
0
5 0.25 0 0.25 0 0.25 0 0.25 0
6 0.33 0 0 0 0.33 0 0.33 0 0
0
7 0 0 0 0 0.50 0 0.50 0
8 0 0 0 0 0.33 0 0.33 0 0.33
9 0 0 0 0.50 0 0 0 0.50 0
The transition probabilities depend on the number of doors in the cell. For ex-
ample, cells 1, 3, 7, and 9 have two doors, so the probability is 0.50 whereas cells 2,
4, 6, and 8 have three doors each (probability of 0.33) and finally, cell 5 has 4 doors
(probability of 0.25). This model will not yield an analytical solution, however, via
simulation, it is possible to reach satisfactory results.
The mouse in the maze model is in GitHub’s repository:
• File: S9-maze-model-dtmc.txt.
up down
The authors use special numerical methods to solve this CTMC (e.g., Laplace
transform method – they explain why they do it in the book) and then discuss the
following equations for assessing availability:
µ
πup
=
λ+µ
λ
πdown =
λ+µ
The instantaneous system Availability is A(t) = πup and correspondingly, the
instantaneous system Unavailability is U(t) = 1 − A(t) = πdown .
• r2 = 7∗24
1
= 0.005952381 → base longevity interval (from S0 to SP ), equal to
7 days.
• r3 = 3 → the mean repair time after rejuvenation is 20 min.
• r4 = 1
(14−7)∗24 = 0.005952381 → rejuvenation frequency for an application.
S0 SF SP SR
S0 0 0 0.005952381 0
SF 2 0 0 0
SP 0 0.000115741 0 0.005952381
SR 3 0 0 0
The insertion of an intermediary state SR that models a preemptive action from
the probable failure state to replenish the application is inserted, as shown in the
model. After this process is concluded, the application returns to the stable (robust)
state. The rates for each state are discussed in the paper, the point the authors make
is that rejuvenating applications costs less and helps applications stay (or return) in
stable states more frequently. The seminal paper discusses how they modelled each
rate and the reasoning behind it and other interesting notions about aging and reju-
venation. This idea has further implications in cyber-security, as studied by Czekster
et al. (2021) [18].
It is worth pointing out that the model has only four states, modelling a CTMC
that helps analysts consider different possibilities when designing or handling sys-
tems. Solving the model and having numerical outputs enormously help analysts
understand behaviours and effectively cope with trade-offs.
48 CHAPTER 4. MORE PROJECTS AND MODELS
Chapter 5
Final considerations
Markov Chains have multiple applications, and one hopes you have had a good jour-
ney reading this book, and your knowledge about the formalism has dramatically
improved. Now it is possible to address more sophisticated problems using MC as
another tool in your toolbox. However, don’t think that because you now have a
good fancy hammer (i.e., MC), every problem is a nail, and you must torture it until
it fits the framework and reasoning discussed here. You should be able to apply MC
depending on the problem at hand, the data you have, and the circumstances (the
context) surrounding your analysis.
As one notices, the code present in the book (and in the repository) is far from
perfect. If one looks deeper one may find a lot of defects and problems. It handles
models with few number of states, having line sizes no greater than 1k (it does not
even say that this is problem while trying to open). So, you could improve the code,
translate it to C++ perhaps, add more controls, work with increased number of states,
and so on. The sky is the limit – go for it!
The problems and models tackled here have reduced number of states, that is be-
cause it is targeted at educational purposes only. It is worth remarking that useful1
real world modelling tends to require much larger order matrix to represent some-
what complex behaviours and relationships. Given all this, think that your Markovian
Journey has just began.
• Investigate structured MC: Queueing Networks (QN) [19, 12], Stochastic Petri
Nets (SPN), PEPA [20], or PTA/MDP.
49
50 CHAPTER 5. FINAL CONSIDERATIONS
• Explore other related formalisms, such as Generalized Stochastic Petri Nets [21],
Well Formed Networks, Layered Queueing Networks [22], Superposed Gener-
alized SPNs (SGSPN) [23], and so on.
To deepen the knowledge in MC, it is suggested to give more time to PRISM and
the inherent modelling formalisms thereof. It is a well-established and stable tool
that researchers around the world use in a daily basis. You could for example explore
other tool features such as investigate (in depth) other formalisms (suitable for CTMC
or DTMC, PTA, and MDP), generation of automatic charts with variables, simulation,
transient analysis, synchronising events. The tool comes with several examples and
has a decent User Guide with lots of useful information to start modelling.
Using PRISM over the years one can attest its robustness and usefulness, how-
ever it is worth looking also into other tools. Mind that there is a wealth of tools to
consider, for instance, the Modest Toolset (https://www.modestchecker.net/)
or the Möbius Tool (https://www.mobius.illinois.edu/). And also, you may
explore other quantitative analysis tools such as SHARPE (Symbolic Hierarchical Au-
tomated Reliability and Performance Evaluator) [24] – https://sharpe.pratt.
duke.edu/.
You could start learning more about dependability reading the landmark paper
by Avizienis et al. (2004) [25].Then, look at Jain’s book on performance [14], the
“Queueing networks and Markov chains” book [15], and Trivedi’s “Reliability and
Availability engineering” [3].
Your ‘real’ journey has just started!
About the author
51
Bibliography
[5] L. Page, S. Brin, R. Motwani, and T. Winograd, “The PageRank citation ranking:
Bringing order to the web.” Stanford InfoLab, Tech. Rep., 1999.
[7] P. Von Hilgers and A. N. Langville, “The five greatest applications of Markov
chains,” in Proceedings of the Markov Anniversary meeting. Citeseer, 2006, pp.
155–158.
[9] J. R. Norris and J. R. Norris, Markov chains. Cambridge university press, 1998,
no. 2.
[10] S. P. Meyn and R. L. Tweedie, Markov chains and stochastic stability. Springer
Science & Business Media, 2012.
[12] ——, Probability, Markov chains, queues, and simulation. Princeton University
Press, 2009.
53
54 BIBLIOGRAPHY
[25] A. Avizienis, J.-C. Laprie, B. Randell, and C. Landwehr, “Basic concepts and tax-
onomy of dependable and secure computing,” IEEE Transactions on Dependable
and Secure Computing, vol. 1, no. 1, pp. 11–33, 2004.
Index
A M
aggregation, 13 Markov Chains, 5
aperiodic, 11 Markov Chains, MC, 3
Markov Process, 5
B Markov property, 5
Belfast model, 17 model, 3
C N
CTMC, 5, 29 Nearly Completely Decomposable, 12
Czekster, 51
P
D periodic, 11
digraph, 4 Power Matrix, 15
DTMC, 5 preconditioning, 16
DTMC simulation, 9 PRISM model, 23
duration, 7 pseudo-random, 9
E R
eigenvector, 4 residence time, 5
Embedded DTMC, 31
ergodic, 11, 31 S
ergodicity, 4 seed parameter, 13
exponential, 7 sojourn time, 5
stochastic Petri nets, 49
G structured MC, 49
Google PageRank algorithm, 4, 22 system, 3
I T
Identity Matrix, 31 time shared system, 4
Infinitesimal Generator, 29 transitions, 4
irreducible, 11
V
L Vector-Matrix Multiplication, 16
level-of-detail, 4
W
Lily Pad model, 40
well-formed, 11
Linear Congruential Generator, 9
lumpability, 13
55
Yet another venture by the lazy panda collection
URL: https://pixabay.com/photos/mammal-wildlife-animal-zoo-panda-3074618/
Simplified Pixabay License