Computer Simulation Techniques: The Definitive Introduction!

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 175

Clients

CPU

Computer Simulation Techniques:


The definitive introduction!

2 3

3 7

b
4 5 6
9 6 9

Yes Is CPU No
MCL = tarr queue MCL = MCL + 1
empty?

A new arrival A departure


event occurs event occurs

b
Is
MCL = tarr b
?

Yes

A new arrival
event occurs

i
Harry Perros
Simulation Techniques Harry Perros

ii
Harry Perros Simulation Techniques

Computer Simulation Techniques:


The definitive introduction!

Harry Perros

iii
Simulation Techniques Harry Perros

Copyright © 2021 by Harry Perros

All rights reserved. This book or any portion thereof may not be reproduced or used in any
manner whatsoever without the express written permission of the author except for the use of
brief quotations in a book review or scholarly journal.

iv
Harry Perros Simulation Techniques

To Isabelle

v
Table of Contents
FORWARD ....................................................................................................................... XI
AUTHOR ........................................................................................................................ XIII
CHAPTER 1: INTRODUCTION ...................................................................................... 1
1.1. INTRODUCTION ....................................................................................................................... 1
1.2. BUILDING A SIMULATION MODEL ......................................................................................... 2
1.4. A TOKEN-BASED ACCESS SCHEME ......................................................................................... 9
1.5. A TWO-STAGE MANUFACTURING SYSTEM ........................................................................ 15
PROBLEMS ..................................................................................................................................... 21
COMPUTER PROJECTS .................................................................................................................. 22
CHAPTER 2: GENERATION OF PSEUDO-RANDOM NUMBERS ........................ 25
2.1. INTRODUCTION .................................................................................................................... 25
2.2. PSEUDO-RANDOM NUMBERS .............................................................................................. 26
2.3. THE CONGRUENTIAL METHOD ........................................................................................... 28
2.3.1. GENERAL CONGRUENTIAL METHODS ............................................................................ 30
2.3.2. COMPOSITE GENERATORS ............................................................................................... 30
2.4. TAUSWORTHE GENERATORS .............................................................................................. 31
2.5. THE LAGGED FIBONACCI GENERATORS ............................................................................ 31
2.6. STATISTICAL TESTS FOR PSEUDO-RANDOM NUMBER GENERATORS ............................. 33
2.6.1. HYPOTHESIS TESTING ...................................................................................................... 33
2.6.2. FREQUENCY TEST (MONOBIT TEST) .............................................................................. 40
2.6.3 SERIAL TEST ...................................................................................................................... 41
2.6.4. RUNS TEST ......................................................................................................................... 44
2.6.5. CHI-SQUARED TEST FOR GOODNESS OF FIT .................................................................. 44
PROBLEMS ..................................................................................................................................... 45
COMPUTER PROJECTS .................................................................................................................. 45
CHAPTER 3: GENERATING STOCHASTIC VARIATES ......................................... 47
3.1. INTRODUCTION .................................................................................................................... 47
3.2. THE INVERSE TRANSFORMATION METHOD ...................................................................... 47
3.3. SAMPLING FROM CONTINUOUS-TIME PROBABILITY DISTRIBUTIONS ........................... 50
3.3.1. SAMPLING FROM A UNIFORM DISTRIBUTION ............................................................... 50
3.3.2. SAMPLING FROM AN EXPONENTIAL DISTRIBUTION ..................................................... 51
3.3.3. SAMPLING FROM AN ERLANG DISTRIBUTION ............................................................... 52
3.3.4. SAMPLING FROM A NORMAL DISTRIBUTION ................................................................. 53
3.4. SAMPLING FROM DISCRETE-TIME PROBABILITY DISTRIBUTIONS ................................. 55
3.4.1. GENERATING BINOMIAL DISTRIBUTED STOCHASTIC VARIATES ................................. 55
3.4.2. GENERATING GEOMETRICALLY DISTRIBUTED STOCHASTIC VARIATES ..................... 55
3.4.3. GENERATING POISSON DISTRIBUTED STOCHASTIC VARIATES ................................... 56
3.5. GENERATING STOCHASTIC VARIATES FROM AN EMPIRICAL DISTRIBUTION ................ 58
3.6. THE REJECTION METHOD .................................................................................................... 60
vii
Simulation Techniques Harry Perros
COMPUTER PROJECTS ................................................................................................................... 61
CHAPTER 4: SIMULATION DESIGNS ....................................................................... 63
4.1. INTRODUCTION ..................................................................................................................... 63
4.2. EVENT-ADVANCE DESIGN .................................................................................................... 63
4.3. FUTURE EVENT LIST ............................................................................................................ 64
4.4. EVENT LIST STORED IN A SEQUENTIAL ARRAY ................................................................. 65
4.5. EVENT LIST STORED IN A LINKED LIST .............................................................................. 66
4.5.1. DEFINING A LINKED LIST ................................................................................................. 68
4.5.2. CREATION OF A NEW NODE ............................................................................................. 69
4.5.3. DELETION OF A NODE ....................................................................................................... 70
4.5.4. INSERTING A NODE IN A LINKED LIST ............................................................................. 70
4.5.5. REMOVING THE FIRST NODE OF A LINKED LIST ............................................................ 73
4.5.6. TIME COMPLEXITY ............................................................................................................ 74
4.5.7. DOUBY LINKED LISTS ....................................................................................................... 75
4.6. UNIT-TIME ADVANCE DESIGN ............................................................................................. 75
4.6.1. SELECTING A UNIT TIME .................................................................................................. 79
4.6.2. IMPLEMENTATION ............................................................................................................ 80
4.6.3. EVENT-ADVANCE VS. UNIT-TIME ADVANCE .................................................................. 80
4.7. ACTIVITY-BASED SIMULATION DESIGN ............................................................................. 80
4.8. EXAMPLES ............................................................................................................................. 83
4.8.1. AN INVENTORY SYSTEM ................................................................................................... 83
4.8.2. A ROUND-ROBIN QUEUE .................................................................................................. 85
PROBLEMS ..................................................................................................................................... 90
COMPUTER PROJECTS ................................................................................................................... 91
CHAPTER 5: ESTIMATION TECHNIQUES FOR ANALYZING
ENDOGENOUSLY CREATED DATA ........................................................................... 93
5.1. INTRODUCTION ..................................................................................................................... 93
5.2. COLLECTING ENDOGENOUSLY CREATED DATA ................................................................. 93
5.3. TRANSIENT STATE VS STEADY-STATE SIMULATION ........................................................ 96
5.3.1. TRANSIENT-STATE SIMULATION .................................................................................... 96
5.3.2. STEADY-STATE SIMULATION ........................................................................................... 96
5.4. ESTIMATION TECHNIQUES FOR STEADY-STATE SIMULATION ........................................ 97
5.5. ESTIMATION OF THE CONFIDENCE INTERVAL OF THE MEAN ......................................... 98
5.5.1. ESTIMATION OF THE AUTOCORRELATION FUNCTION (ACF) .................................. 103
5.5.2. THE BATCH MEANS METHOD ........................................................................................ 106
5.5.3. THE REPLICATION METHOD ......................................................................................... 107
5.6. ESTIMATION OF THE CONFIDENCE INTERVAL OF A PERCENTILE ................................ 109
5.7. ESTIMATION OF THE CONFIDENCE INTERVAL OF A PROBABILITY .............................. 110
5.8. ESTIMATION TECHNIQUES FOR TRANSIENT-STATE SIMULATION .............................. 111
COMPUTER PROJECT .................................................................................................................. 112
CHAPTER 6: VALIDATION OF A SIMULATION MODEL .................................. 121
COMPUTER PROJECTS ................................................................................................................ 123

viii
Harry Perros Simulation Techniques
CHAPTER 7: VARIANCE REDUCTION TECHNIQUES ........................................ 127
7.1. INTRODUCTION ................................................................................................................. 127
7.2. THE ANTITHETIC VARIATES TECHNIQUE ....................................................................... 128
7.3. THE CONTROL VARIATES TECHNIQUE ............................................................................ 133
COMPUTER ASSIGNMENTS ....................................................................................................... 136
CHAPTER 8: SIMULATION PROJECTS .................................................................. 137
8.1. A SIMULATION OF A CPU PRIORITY SCHEDULER ......................................................... 137
8.1.1. THE SIMULATION LOGIC ............................................................................................... 139
8.1.2. TASK 1: HAND SIMULATION ........................................................................................ 143
8.1.3. TASK 2: WRITE THE BASIC SIMULATION CODE ......................................................... 145
8.1.4. TASK 3: STATISTICAL ESTIMATION OF THE RESPONSE TIME ................................. 146
8.1.5. CORRECT SIMULATION RESULTS ................................................................................. 147
8.2. A SIMULATION OF THE FLOW OF SIP MESSAGES .......................................................... 150
8.2.1. THE SIMULATION LOGIC .............................................................................................. 151
8.2.2. IMPLEMENTATION AND RESULTS ............................................................................... 153
8.3. A SIMULATION MODEL OF A MULTIPLEXER ................................................................... 155
8.3.1. SIMULATION LOGIC ....................................................................................................... 156
8.3.2. RESULTS ......................................................................................................................... 157
8.3.3. AN EXTENSION OF THE SIMULATION MODEL ............................................................. 158

ix
Forward
This book is based on my lecture notes for an undergraduate course
on discrete-event simulation techniques that I used to teach back in
the early 80s. In the early 90s I prepared the first version of this book
and made it available through my web site free of charge to anyone
interested in discrete-event simulation. In a light-hearted moment over
a few beers, I decided to call it the “definitive introduction!”. I am not
sure how “definitive” it is, but the title stuck!

Over the years I have received many positive comments despite errors
and other issues that resulted from not putting enough time into the
writing of the book. In this version, I reformatted the manuscript, re-
moved errors and improved the flow of the presentation in many sec-
tions. I also removed some advanced topics on data structures and
random number generation methods, as I want to keep the book at an
introductory level.

I would like to emphasize that this book was written for people from
all disciplines that use discrete-event simulation. As such, I describe
topics that maybe well-known to computer science students. The
knowledgeable reader can skip these sections.

How to use the book: This is a self-study book. In Chapter 1 you will
find three examples. Select one of the three and do the corresponding
computer project given at the end of the Chapter. Then, after you read
each new Chapter, do the computer project that corresponds to the
example that you have selected, described at the end of the Chapter.
By the time you reach the end of the book, you will have developed a
very sophisticated simulation model! You can use any high-level pro-
gramming language you like. After you finish this book, I strongly
recommend that you learn one or two simulation languages.

Errors: I am not responsible for any errors in the book, and if you do
find any, please let me know (hp@ncsu.edu).

Copyright: Please remember that it is illegal to reproduce parts of


this book or all of the book in other publications without my consent.

xi
Simulation Techniques Harry Perros
Please acknowledge this book, if you use it in a course, or in a project,
or in a publication.

Acknowledgment: My many thanks to many students at NC State


University for their contributions to the writing of this book.

Enjoy!

Harry Perros, February 2021

xii
Author

HARRY G. PERROS is an Emeritus Professor of Computer Science and


an Alumni Distinguished Graduate Professor, NC State University,
and an IEEE Fellow. He received the B.Sc. degree in Mathematics in
1970 from Athens University, Greece, the M.Sc. degree in Operation-
al Research with Computing from Leeds University, England, in
1971, and the Ph.D. degree in Operations Research from Trinity Col-
lege Dublin, Ireland, in 1975. He has held visiting faculty positions at
INRIA, Rocquencourt, France (1979), NORTEL, Research Triangle
Park, North Carolina (1988-89 and 1995-96), University of Paris 6,
France (1995-96, 2000, 2002, 2012), University of Paris 13, France
(2005-2006), and Victoria University, Wellington, New Zealand
(2006).

He has published 220 journal and conference papers in the area of


performance modeling of computer and communication systems, and
he has organized several international conferences. He has also pub-
lished seven print books: Queueing Networks with Blocking: Exact
and Approximate Solutions, Oxford University Press 1994, An Intro-
duction to ATM Networks, Wiley 2001, Connection-Oriented Net-
works, Wiley 2005, Networking Services: QoS, Signaling, Processes,
Amazon.com 2014, VBR Video Traffic Models, Wiley-ISTE 2014,
Bandwidth Allocation for Video under QoS Constraints, Wiley-ISTE
2015, and An Introduction to IoT Analytics, CSC press 2021, and a
free e-book Computer Simulation Techniques – The Definitive Intro-
duction, 2002, revised in 2021.

In 1995 he founded the IFIP Working Group 6.3 on the Performance


of Communication Systems, and he was the chairman from 1995 to
2002. He served as an associate Editor in several Journals and edited
several special issues and conference proceedings. He was also the
co-founder and program coordinator of the Master of Science degree
in Computer Networks at NC State University.

He is an avid sailor, and he also enjoys playing music, reading, cook-


ing and traveling.

xiii
Simulation Techniques Harry Perros

xiv
Harry Perros Simulation Techniques

Chapter 1: Introduction
1.1. Introduction
Simulation is a very popular technique used in many areas for model-
ing different types of systems. It is easy to apply since it does not re-
quire knowledge of advanced mathematics. The only requirement is
basic programming skills. There are also several powerful simulation
languages that can be used which alleviate the burden of having to
write a computer program. Many of these languages have also a
graphic interface that allows the user to describe the system under
study. In this book, we will not make use of simulation languages.
Rather, the reader will learn how to build a simulation model from
scratch. This allows one to better understand and use a simulation
language.

Simulation is part of a collection of modeling tools used in Opera-


tions Research (OR) to study the performance of systems in many
diverse domains, such as, manufacturing, the internet, transportation,
and health, to name a few. A model is a representation of a real-life
system. In general, models can be classified into: iconic, analogue,
and symbolic.

An iconic model is an exact replica of the properties of the real-life


system, but smaller in scale. Examples are: model airplanes, maps,
etc. An analogue model uses a set of properties to represent the prop-
erties of a real-life system. For instance, a hydraulic system can be
used as an analogue of electrical, traffic and economic systems. Sym-
bolic models represent the properties of the real-life system through
the means of symbols, such as mathematical equations and computer
programs. Simulation models are symbolic models.

Symbolic models are classified into two groups: deterministic models


and stochastic models. Deterministic models are models which do not
contain the element of probability. These are typically optimization
techniques, such as, linear programming, non-linear programming
and dynamic programming. Stochastic models are models which con-
tain the element of probability, such as queueing theory and stochas-
1
Simulation Techniques Harry Perros
tic processes. Simulation techniques rely heavily on the element of
randomness. However, deterministic simulation techniques in which
there is a no randomness, are not uncommon.

1.2. Building a simulation model


Any real-life structure that is modelled using simulation, or any other
OR model, is viewed as a system. A system is a collection of entities
which are logically related and which are of interest to a particular
application. The following features of a system are of interest:

• Environment: Each system can be seen as a subsystem of a


broader system.
• Interdependency: No activity takes place in total isolation.
• Sub-systems: Each system can be broken down to sub-
systems.
• Organization: Virtually all systems consist of highly orga-
nized elements or components, which interact in order to carry
out the mission of the system.
• Change: The present condition or state of the system usually
varies over a long period of time.

Levels of
Detail

Figure 1.1: Beard's managerial pyramid

When building a simulation model of a real-life system, one does not


simulate the whole system. Rather, one simulates those sub-systems
which are related to the problems at hand. This involves modeling
parts of the system at various levels of detail. This can be graphically
depicted using Beard's managerial pyramid shown in figure 1.1. The
2
Harry Perros Simulation Techniques
collection of the shaded areas form the parts of the system that are
incorporated in the model.

A simulation model is, in general, used in order to study real-life sys-


tems which do not currently exist. In particular, one is interested in
quantifying the performance of a system under study for various val-
ues of its input parameters. Such quantified measures of performance
can be very useful in the managerial decision process. The basic steps
involved in carrying out a simulation exercise are depicted in figure
1.2.

Define the Problem a

Design Simulation
Experiments (alternatives)
Analyze Data

Run the Simulator


Formulate Sub-Models

Analyze the Results


Combine Sub-Models

Implement Results
Collect Data

Write the Simulation


Program

Debug

Validate Model Earlier


Steps

Figure 1.2: Basic steps involved in carrying out a simulation study

All the relevant variables of a system under study are either exoge-
nous or endogenous. An exogenous variable is a variable whose value
is fixed prior to running a simulation and it is provided as input to the
simulation program. An endogenous variable is one whose value is
estimated by the simulation.
3
Simulation Techniques Harry Perros
For instance, let us assume that we want to simulate a single queue
served by one or more servers. Such a queueing system arises at a
bank, where arriving customers form a single queue and one or more
tellers serve the customers waiting in the queue on a first-come-first-
bases. The arrival rate of customers at the bank, the service time of a
customer at a server, and the number of servers are examples of ex-
ogenous variables. The mean waiting time of a customer in the queue,
and the mean number of customers in the queue, are endogenous be-
cause they are estimated by running the simulation.

Some of the variables of the system are of paramount importance as


they are used to define the state of the system. These variables are
known as status variables, and they form the backbone of any simula-
tion model. During a simulation run, these variables indicate the state
of the system at any time instance. The selection of these variables is
affected by the type of information one wants to maintain about the
simulated system.

Below, we describe the basic simulation methodology through three


different examples, namely, the machine interference problem, a to-
ken-based access scheme, and a two-stage manufacturing system.

1.3. The machine interference problem


Let us consider a single server queue with a finite population known
as the machine interference problem. This problem arose originally
out of a need to model the behavior of machines. Later on, it was used
extensively in computer modeling. Let us consider 𝑀 machines. Each
machine is operational for a period of time and then it breaks down.
We assume that there is one repairman. A machine remains broken
down until it is fixed by the repairman. Broken-down machines are
served in a FIFO manner, and the service is non-preemptive. The total
down time of a machine is made up of the time it has to "queue" for
the repairman and the time it takes for the repairman to fix it. A ma-
chine becomes immediately operational after it has been fixed. Thus,
each machine the cycle shown in figure 1.3, which is repeated contin-
uously.

4
Harry Perros Simulation Techniques

Figure 1.3: The basic cycle of a machine

In general, one has information regarding the operational time and the
repair time of a machine. However, in order to determine the down
time of a machine, one has to calculate the queueing time for the re-
pairman. If this quantity is known, then one can calculate the utiliza-
tion of a machine, and other quantities of interest, such as, the utiliza-
tion of the repairman.

Repairman
Finite
Population
of Machines

Figure 1.4: The machine interference problem

Let us now look at the repairman's queue. This can be visualized as a


single server queue fed by a finite population of machines, i.e., cus-
tomers, as shown in figure 1.4

For simplicity, we assume that the operational time of each machine


is equal to 10 units of time. Also, the repair time of each machine is
equal to 5 units of time. In other words, we assume that all the ma-
chines have identical constant operational times. They also have iden-
tical and constant repair times.

The first and most important step in building a simulation model of


the above system, is to identify the basic events whose occurrence
will alter the status of the system. This brings up the problem of hav-
ing to define the state variables of the above problem. The selection

5
Simulation Techniques Harry Perros
of the status variables depends mainly upon the type of performance
measures that we wish to obtain.

In this problem, the most important status variable is 𝑛, the number of


broken-down machines, i.e., those waiting in the queue plus the one
being repaired.

• If 𝑛 = 0, then we know that the queue is empty and the re-


pairman is idle.
• If 𝑛 = 1, then the queue is empty and the repairman is busy.
• If 𝑛 > 1, then the repairman is busy and there are 𝑛 − 1
broken-down machines in the queue.
Now, there are two events whose occurrence will cause 𝑛 to change.
These are:

1. A machine breaks down, i.e., an arrival occurs at the queue.


2. A machine is fixed, i.e., a departure occurs from the queue.

The flow-charts given in figures 1.5, and 1.6 show what happens
when each of these events occur.

In order to incorporate the two basic events in the simulation model,


we need a set of variables, known as clocks, which keep track of the
time instants at which an arrival or departure event will occur. In par-
ticular, for this specific model, we need to associate a clock for each
machine. The clock will simply show the time instant at which the
machine will break down, i. e., it will arrive at the repairman's queue.
Obviously, at any instance, only the clocks of the operational ma-
chines are of interest. In addition to these clocks, we require to have
another clock which shows the time instant at which a machine cur-
rently being repaired will become operational, i.e., it will cause a de-
parture event to occur. Thus, in total, if we have 𝑚 machines, we
need 𝑚 + 1 clocks. Each of these clocks is associated with the occur-
rence of an event. In particular, 𝑚 clocks are associated with 𝑚 arri-
val events and one clock is associated with the departure event. In
addition to these clocks, it is useful to maintain a master clock, which
simply keeps track of the simulated time.

6
Harry Perros Simulation Techniques
A machine
breaks down

Repairman yes Join the


busy queue
?

Repairman
becomes busy

Repair
starts

Figure 1.5: An arrival event

A machine
is repaired

Other Repairman
machines to be no
becomes
repaired? idle

A new repair
starts

Figure 1.6: A departure event

The heart of the simulation model centers around the manipulation of


these events. In particular, using the above clocks, the model decides
which of all the possible events will occur next. Then the master
clock is advanced to this time instant, and the model takes action as
indicated in the flow-charts given in figures 1.5 and 1.6. This event
manipulation approach is depicted in figure 1.7.
7
Simulation Techniques Harry Perros
a

A machine Choose A machine


Breaks down next event is repaired

Create
Queue operation time
no
empty? a

ye Queue yes
s empty? a
Generate a
new service
no
Generate a
a new service

Figure 1.7: Event manipulation

We are now ready to carry out the hand simulation given below in
table 1.1. Let us assume that we have 3 machines. Let 𝐶𝐿1, 𝐶𝐿2, and
𝐶𝐿3 be the clocks associated with machine 1, 2, and 3 respectively
(arrival event clocks). Let 𝐶𝐿4 be the clock associated with the depar-
ture event. Finally, let 𝑀𝐶 be the master clock and let 𝑅 indicate
whether the repairman is busy or idle. We assume that at time zero all
three machines are operational and that 𝐶𝐿1 = 1, 𝐶𝐿2 = 4, 𝐶𝐿3 = 9.
These are known as initial conditions.

We note that in order to schedule a new arrival time we simply have


to set the associated clock to 𝑀𝐶 + 10. Similarly, each time a new
repair service begins we set 𝐶𝐿4 = 𝑀𝐶 + 5. A very important aspect
of this simulation model is that we only check the system at time in-
stants at which an event takes place. We observe in the above hand
simulation that the master clock in the above example gives the time
instants at which something happened (i.e., an event occurred). These
8
Harry Perros Simulation Techniques
times are: 0, 1, 4, 6, 9, 11, 16, . .. We note that in-between these in-
stants no event occurs and, therefore, the system's status remains un-
changed. In view of this, it suffices to check the system at time in-
stants at which an event occurs. Furthermore, having taken care of an
event, we simply advance the Master clock to the next event which
has the smallest clock time. For instance, in the above hand simula-
tion, after we have taken care of a departure at time 11, the simulation
advances to time 16. This is because after the time instant 11, there
are three events that are scheduled to take place in the future. These
are: a) arrival of machine 1 at time 16; b) arrival of machine 2 at time
21; and c) a departure of machine 3 at time 16. Obviously, the next
event to occur is the latter event at time 16.

State of
𝑴𝑪 CL1 𝑪𝑳𝟐 𝑪𝑳𝟑 𝑪𝑳𝟒 𝒏
server
0 1 4 9 - 0 idle
1 - 4 9 6 1 busy
4 - - 9 6 2 busy
6 16 - 9 11 1 busy
9 16 - - 11 2 busy
11 16 21 - 16 1 busy
16 - 21 26 21 1 busy

Table 1.1: Hand simulation of the machine interference problem

1.4. A token-based access scheme


The second example presented in this Chapter deals with a computer
network which consists of a number of nodes connected via a shared
wired or wireless transport medium, as shown in figure 1.9. The circle
indicates a node and the line the shared medium. Access to the shared
medium is controlled by a token. That is, a node cannot transmit on
the network unless it has the token. In this example, we simulate a
simplified version of such an access scheme, as described below.

There is a single token that visits the nodes in a certain sequence. The
nodes are logically connected so that they form a logical ring. In gen-
eral, the order in which the nodes are logically linked may not be the
same as the order in which they are attached to the network. We ass-

9
Simulation Techniques Harry Perros

Initiate MC=0, CL1=1, CL2=4


simulation CL3=9, n=0, R=0 (idle)

Arrival Next Departure


(ith machine) event (th machine)

MC=CLi MC=CL4

No CLi=MC+10
n=0 ? n=n+1

yes n=n-1

R=1

Yes
n=0 ? R=0
CL4=MC+5
No
A
A CL4=MC+5

Figure 1.8: Fowchart of the simulation program for the


machine interference problem

Figure 1.9: Nodes interconnected by a shared medium

10
Harry Perros Simulation Techniques
ume that the token never gets lost. A node cannot transmit unless it
has the token. When a node receives the token from its previous logi-
cal upstream node, it may keep it for a period of time up to 𝑇. During
this time, the node transmits packets. A packet is assumed to consist
of data and a header. The header consists of the address of the sender,
the address of the destination, and various control fields. The node
surrenders the token when: a) time 𝑇 has run out, or b) it has trans-
mitted out all the packets in its queue before 𝑇 expires, or c) it re-
ceives the token at a time when it has no packets in its queue to
transmit. If time 𝑇 runs out and the node is in the process of transmit-
ting a packet, it will complete the transmission and then it will sur-
render the token. Surrendering the token means, that the node will
transmit it to its next downstream logical neighbor.

token
...

Figure 1.10. The conceptual queueing system

Conceptually, this network can be seen as comprising of a number of


queues, one per node. Only the queue that has the token can transmit
packets. The token can be seen as a server which switches cyclically
between the queues, as shown in figure 1.10. Once the token is
switched to a queue, packets waiting in this queue can be transmitted
on the network. The maximum time that a queue can keep the token is
𝑇 units of time, and the token is surrendered following the rules de-
scribed above. The time it takes for the token to switch from one
queue to the next is known as switch-over time. Such queueing sys-
tems are referred to as polling systems, and they have been studied in
the literature under a variety of assumptions.

11
Simulation Techniques Harry Perros
It is much simpler to use the queueing model given in figure 1.10
when constructing the simulation model. The following events have
to be taken into account in this simulation. For each queue, there is an
arrival event and service completion event. For the token, there is a
time of arrival at the next queue event and the time when the token
has to be surrendered to the next node, known as the time-out. For
each queue, we keep track of the time of arrival of the next packet,
the number of customers in the queue, and the time a packet is sched-
uled to depart, if it is being transmitted. For the token, we keep track
of the time of arrival at the next queue, the id of the queue, indicated
by a number, that holds the token, and the time-out.

In the hand simulation given below in table 1.2, we assume that the
token-based network consists of three nodes. That is, the queueing
system in figure 1.10 consists of three queues. The inter-arrival times
to queues 1, 2, and 3 are constant equal to 10, 15, and 20 unit times
respectively. 𝑇 is assumed to be equal to 15 unit times. The time it
takes to transmit a packet is assumed to be constant equal to 6 unit
times. The switch over time is set to 1 unit time. For initial conditions
we assume that the system is empty at time zero, and the first arrival
to queues 1, 2, and 3 will occur at time 2, 4, and 6 respectively. Also,
at time zero, the token is in queue 1. In case when an arrival and a
departure occur simultaneously at the same queue, we will assume
that the arrival occurs first. In addition, if the token and a packet ar-
rive at a queue at the same time, we will assume that the packet ar-
rives first. We define the following clocks:

• 𝑀𝐶: Master clock


• 𝐴𝑇𝑖: Arrival time clock at queue 𝑖, 𝑖 = 1,2,3
• 𝐷𝑇𝑖: Departure time clock from queue 𝑖, 𝑖 = 1,2,3
• 𝑇𝑂𝑈𝑇: Time out clock for token
• 𝐴𝑁𝐻: Arrival time clock of token to next queue
The logic for each of the events in this simulation is summarized in
figures 1.11 to 1.14. In figure 1.11, we show what happens when an
arrival occurs, figure 1.12 deals with the case where a service is com-
pleted at queue 𝑖, figure 1.13 describes the action taken when a time-
out occurs, and figure 1.14 summarizes the action taken when the to-
ken arrives at a queue.
12
Harry Perros Simulation Techniques
Arrival MC=ATi
occurs

Join the qi = qi + 1
queue

Schedule ATi = MC + new


Next Arrival inter-arrival time

Return

Figure 1.11: Arrival event at queue 𝒊

Service is MC = DT i
completed

Departs from q = q-1


queue

yes
Queue Pass H = (H+1) mod3
empty? token
no
Schedule arrival ANH =
Token time MC + switch over time
time out
? yes
Return
no

Schedule new DTi = MC + new service time


service

Return

Figure 1.12: Service completion at queue 𝒊

13
Simulation Techniques Harry Perros
This means that
Time-out node is still
occurs transmitting.

Raise a
flag

Return

Figure 1.13: Time-out of token

Arrival of token MC = ANH


at next queue

Queue yes Pass H = (H + 1) mod3


empty token
?
no Schedule ANH = MC +
next arrival (switch over time)
Schedule
TOUT = MC + T time-out yes

DT i = MC +
DT4 = MC + Schedule next
(new service time)
(new service time) service
completion

Return

Figure 1.14: Arrival of token at next queue

14
Harry Perros Simulation Techniques
Initialize
simulation

Locate i.e. search event list


i. e. search event list for
next event for smallest
smallest numberclock value

Take
appropriate i. e. i.e. jumptotothe
branch theappropriate
appropriate part of the
part of(or
program theprocedure)
program
action

If newevent
Update events
list
are scheduled
if new
update nextare
events
schedule
event

Is
no simulation
over
?
yes

End

Figure 1.15: Event manipulation

The set of all pending events in a simulation is known as the event


list. As will be seen later on, the event list is implemented as a data
structure. Each time we want to locate the next event, we search the
event list for the event with the smallest clock. An event that has just
taken place is deleted from the event list, and newly generated events
are inserted in the event list. This event manipulation forms the basis
of the simulation, and it is summarized in figure 1.15. The hand sim-
ulation is given in table 1.2.

1.5. A two-stage manufacturing system


Let us consider a two-stage manufacturing system as depicted by the
queueing network shown in figure 1.16. Stage 1 consists of an infinite
capacity queue, referred to as queue 1, served by a single server, re-

15
Simulation Techniques Harry Perros
Queue 𝟏 Queue 𝟐 Queue 𝟑 Token
Node
𝑴𝑪 𝑨𝑻𝟏 𝑫𝑷𝟏 Q 𝑨𝑻𝟐 𝑫𝑷𝟐 Q 𝑨𝑻𝟑 𝑫𝑷𝟑 Q 𝑻𝑶𝑼𝑻 𝑨𝑵𝑯
No

0 2 0 4 0 6 0 1 1
1 2 0 4 0 6 0 2 2
2 12 1 4 0 6 0 3 3
3 12 9 1 4 0 6 0 1 18
4 12 9 1 19 1 6 0 1 18
6 12 9 1 19 1 26 1 1 18
9 12 0 19 1 26 1 1 10
10 12 0 19 16 1 26 1 2 25
12 22 1 19 16 1 26 1 2 25
16 22 1 19 0 26 1 2 17
17 22 1 19 0 26 23 1 3 32
19 22 1 34 1 26 23 1 3 32
22 32 2 34 1 26 23 1 3 32
23 32 2 34 1 26 0 3 24
24 32 30 2 34 1 26 0 1 39
26 32 30 2 34 1 46 1 1 39
30 32 36 1 34 1 46 1 1 39
32 42 36 2 34 1 46 1 1 39
34 42 36 2 49 2 46 1 1 39
36 42 42 1 49 2 46 1 1 39
39 42 42 1 49 2 46 1 1 *
42 52 42 2 49 2 46 1 1 *
42 52 1 49 2 46 1 1 43
43 52 1 49 49 2 46 1 2 58

Table 1.2: Hand simulation for the token-based access scheme

16
Harry Perros Simulation Techniques
ferred to as server 1. Stage 2 consists of a finite capacity queue, re-
ferred to as queue 2, served by a single server, referred to as server 2.

Stage 1 Stage 2

Queue 1 Server 1 Queue 2 Server 2

Figure 1.16: A two-stage queueing network.

When queue 2 becomes full, server 1 stops working. More specifical-


ly, upon service completion at server 1, the server gets blocked if
queue 2 is full. That is, the server cannot serve any other customers
that may be waiting in queue 1. Server 1 will remain blocked until a
customer departs from queue 2. In this case, a space will become
available in queue 2 and the served customer in front of server 1 will
be able to move into queue 2, thus freeing the server to serve other
customer in queue 1.

Each server may also break down. For simplicity, we will assume that
a server may break down independently of whether it is busy or idle.
A broken-down server cannot provide service until it is repaired. If a
customer was in service when the breakdown occurred, the customer
will resume its service after the server is repaired without any loss to
the service it received up to the time of the breakdown. That is, after
the server becomes operational again, the customer will receive the
balance of its service.

The following events may occur.

1. Arrival of a customer to queue 1 (clock 𝐴𝑇)


2. Service completion at server 1 (clock 𝐷𝑇1)
3. Service completion at server 2 (clock 𝐷𝑇2)
4. Server 1 breaks down (clock 𝐵𝑅1)
5. Server 1 becomes operational (clock 𝑂𝑃1)
6. Server 2 breaks down (clock 𝐵𝑅2)
7. Server 2 becomes operational (clock 𝑂𝑃2)

17
Simulation Techniques Harry Perros
Below, we identify the events that may be triggered when each of the
above events occurs.

1. Arrival to queue 1.

a. Arrival to queue 1 (new value for 𝐴𝑇 clock): This event is al-


ways scheduled each time an arrival occurs.
b. Service completion at server 1 (new value for 𝐷𝑇1 clock):
This event will be triggered if the new arrival to queue 1 finds
the server idle and starts its service.

2. Service completion at server 1:

a. Service completion at server 1(new value for 𝐷𝑇1 clock): This


event will occur if there is one or more customers waiting in
queue 1.
b. Service completion at server 2 (new value for 𝐷𝑇2 clock):
This event will occur if the customer who just completed its
service at server 1 finds server 2 idle and starts its service.

The occurrence of a service completion event at server 1 may


cause server 1 to get blocked if queue 2 is full.

3. Service completion at server 2:

a. Service completion at server 2 (new value for 𝐷𝑇1 clock):


This event will occur if there is one or more customers in
queue 2.
b. Service completion at server 1 (new value for 𝐷𝑇1 clock):
This event will occur if server 1 was blocked.

4. Server 1 breaks down:

Server 1 becomes operational (new value for 𝑂𝑃1 clock): This


event gives the time in the future when the server will be repaired
and will become operational. If the server was busy when it
broke down, update the clock of the service completion event at
server 1 to reflect the delay due to the repair.

18
Harry Perros Simulation Techniques
5. Server 1 becomes operational:

a. Server 1 breaks down (new value for 𝐵𝑅1 clock): This event
gives the time in the future when the server will break down.
During this time the server is operational.
b. Service completion time (new value for 𝐷𝑇1 clock): If the
server was idle when it broke down and queue 1 is not empty
at the time when it becomes operational, then a new service
will begin.

6. Server 2 breaks down:

Server 2 becomes operational (new value for 𝑂𝑃2 clock): This


event gives the time in the future when server 2 will be repaired,
and therefore it will become operational. During that time the
server is broken down.

If server 2 was busy when it broke down, update the clock of the
service completion event at server 2 to reflect the delay due to the
repair.

7. Server 2 becomes operational:

a. Server 2 breaks down (new value for 𝐵𝑅2 clock): This event
gives the time in the future when server 2 will break down.
During this time the server is operational.
b. Service completion time (new value for 𝐷𝑇2 clock): If the
server was idle when it broke down and queue 2 is not empty
at the time when it becomes operational, then a new service
will begin.

In the hand-simulation given in table 1.3, it is assumed that the buffer


capacity of the second queue is 4 (this includes the customer in ser-
vice). All service times, inter-arrival times, operational and repair
times are constant with the following values: inter-arrival time = 40,
service time at node 1 = 20, service time at node 2 = 30, operational
time for server 1 = 200, operational time for server 2 = 300, re-
pair time for server 1 = 50, and repair time for server 2 = 150.
Initially the system is assumed to be empty. The first arrival occurs at
19
Simulation Techniques Harry Perros
time 10, server 1 will break down for the first time at time 80, and
server 2 at time 90. Please note that the symbols 𝑛R and 𝑠R , 𝑖 = 1,2,
used in the header of table 1.3 indicate the number of customers in
queue 𝑖, and the status of server 𝑖 respectively.

Since we are dealing with integer numbers, it is possible that more


than one clock may have the same value. That is, more than one event
may occur at the same time. In the simulation of the two-stage manu-
facturing system, simultaneous events can be taken care in any arbi-
trary order. In general, depending on the nature of the problem, the
order with which simultaneously occurring events are dealt with may
matter, and it should be reflected in the simulation model. If the
clocks can be defined as real numbers, then events will never occur at
the same time.

Stage 𝟏 Stage 𝟐
𝑴𝑪 𝑨𝑻 𝒏𝟏 𝑫𝑻𝟏 𝑩𝑹𝟏 𝑶𝑷𝟏 𝒔𝟏 𝒏𝟐 𝑫𝑻𝟐 𝑩𝑹𝟐 𝑶𝑷𝟐 𝒔𝟐

10 50 1 30 80 busy 90 idle
30 50 0 80 idle 1 60 90 busy
50 90 1 70 80 busy 1 60 90 busy
60 90 1 70 80 busy 0 90 idle
70 90 0 80 idle 1 100 90 busy
80 90 0 130 down 1 100 90 busy
90 90 0 130 down 1 250 240 down
90 130 1 150 130 down 1 250 240 down
130 170 2 150 130 down 1 250 240 down
130 170 2 150 330 busy 1 250 240 down
150 170 1 170 330 busy 2 250 240 down
170 210 2 170 330 busy 2 250 240 down
170 210 1 190 330 busy 3 250 240 down
190 210 0 330 idle 4 250 240 down
210 250 1 230 330 busy 4 250 240 down

20
Harry Perros Simulation Techniques
230 250 1 330 blocked 4 250 240 down
240 250 1 330 blocked 4 250 540 busy
250 290 2 330 blocked 4 250 540 busy
250 290 1 270 330 busy 4 280 540 busy
270 290 1 330 blocked 4 280 540 busy
280 290 0 330 idle 4 310 540 busy
290 330 1 310 330 busy 4 310 540 busy
310 330 1 310 330 busy 3 340 540 busy
310 330 0 330 idle 4 340 540 busy
330 370 1 350 330 busy 4 340 540 busy
330 370 1 400 380 down 4 340 540 busy
340 370 1 400 380 down 3 370 540 busy
370 410 2 400 380 down 3 370 540 busy
370 410 2 400 380 down 2 400 540 busy
380 410 2 400 580 busy 2 400 540 busy

Table 3: Hand simulation for the two-stage manufacturing system

Problems
1. Do the hand simulation of the machine interference problem, dis-
cussed in section 1.3, for the following cases:
a. The machines are not repaired in a FIFO manner, but the re-
pairman selects randomly a broken machine to fix.
b. There are two repairmen.
c. There are five machines and a single repairman.

2. Do the hand simulation of the token-based access scheme, de-


scribed in section 1.4, for the following cases:
a. Packets have priority 1 or 2 (1 being the highest). The packets
in a queue are served according to their priority. Packets with
the same priority are served in a FIFO manner.
b. There are five queues.

21
Simulation Techniques Harry Perros
3. Do the hand simulation of the two-stage manufacturing system,
described in section 1.5, for the following cases:
a. The servers do not breakdown.
b. Add a third stage, and assume that the servers do not break-
down. In this case, server 2 gets blocked if queue 3 is full in
the same way that server 1 gets blocked if queue 2 is full.

Computer projects
Implement one of the following three simulation projects and use it
for the remaining projects described in the subsequent Chapters. At
the end, you will have developed a very sophisticated simulation
model. If you feel energetic you can implement all three simulation
projects!

1. The machine interference problem. Write a computer program to


simulate the machine interference problem described in section
1.3. Each time an event occurs, print out a line of output to show
the current values of the clocks and of the other status parameters
as in the hand simulation. Run your simulation until the master
clock is equal to 20. Check by hand to make sure that the simula-
tion advances from event to event correctly, and whether it up-
dates the clocks and the other status parameters correctly.

2. The token-based access scheme. Write a computer program to


simulate the token-based access scheme as described in section
1.4. Each time an event occurs, print out a line of output to show
the current values of the clocks and of the other status parameters
as in the hand simulation. Run your simulation until the master
clock is equal to 100. Check by hand to make sure that the simula-
tion advances from event to event correctly, and whether it up-
dates the clocks and the other status parameters correctly.

3. The two-stage manufacturing system. Write a computer program


to simulate the two-stage manufacturing system as described in
section 1.5. Each time an event occurs, print out a line of output to
show the current values of the clocks and of the other status pa-
rameters as in the hand simulation. Run your simulation until the
master clock is equal to 500. Check by hand to make sure that the

22
Harry Perros Simulation Techniques
simulation advances from event to event correctly, and whether it
updates the clocks and the other status parameters correctly.

23
Harry Perros Simulation Techniques
Chapter 2:
Generation of pseudo-random numbers
2.1. Introduction
Numbers chosen at random are useful in a variety of applications. For
instance, in numerical analysis, random numbers are used in the solu-
tion of complicated integrals. In computer programming, random
numbers make a good source of data for testing the effectiveness of
computer algorithms. Random numbers also play an important role in
cryptography.

In simulation, random numbers are used in order to introduce ran-


domness in the model. For instance, let us consider the machine inter-
ference simulation model discussed in the previous Chapter, section
1.3. In this model it was assumed that the operational time and the
repair time of a machine are constant. It is possible that one may find
a real-life system where this is true. However, in general, one will
observe that the time a machine is operational varies. Also, its repair
time may vary. In order to make the simulation model realistic, we
can collect a representative sample of operational times by observing
the machine over a long period of time. We can then fit a theoretical
distribution to this sample, or simply summarize the sample data into
a histogram. Then, each time we want to determine the next opera-
tional time of the machine, we generate randomly a number from the
theoretical distribution or the histogram. Similarly, we can do the
same for the repair times.

In order to generate random numbers, one needs to be able to gener-


ate first uniformly distributed random numbers in [0,1] known as
pseudo-random numbers. These pseudo-random numbers are then
used to generate random numbers from a theoretical or an empirical
distribution. These random numbers are referred to as stochastic vari-
ates or random variates. In this Chapter, we focus our discussion on
pseudo-random numbers. In sections 2.2 to 2.5, we discuss how to
generate pseudo-random numbers, and in section 2.6, we present a
number of statistical tests that can be used to test whether a sequence

25
Simulation Techniques Harry Perros
of pseudo-random numbers is random. The generation of stochastic
variates is described in Chapter 3.

2.2. Pseudo-random numbers


There is no such a thing as a single random number. Rather, we speak
of a sequence of random numbers that follow a specified theoretical
or empirical distribution. There are two main approaches to generat-
ing random numbers. In the first approach, a physical phenomenon is
used as a source of randomness from where random numbers can be
generated. Random numbers generated in this way are called true
random numbers.

A true random number generator requires a completely unpredictable


and non-reproducible source of randomness. Such sources can be
found in nature, or they can be created from hardware and software.
For instance, the elapsed time between emissions of particles during
radioactive decay is a well-known randomized source. Also, the
thermal noise from a semiconductor diode or resistor can be used as a
randomized source. Finally, sampling human-computer interactions,
such as keyboard or mouse activity of a user, can give rise to a ran-
domized source.

True random number generators are ideal for some applications, but
in general they are not very practical. An alternative approach to gen-
erating random numbers, which is the most common approach, is to
use a mathematical algorithm. Efficient algorithms have been devel-
oped that can be easily implemented in a computer program to gener-
ate a sequence of random numbers. These algorithms produce num-
bers in a deterministic fashion. That is, given a starting value, known
as the seed, the same sequence of random numbers can be produced
each time as long as the seed remains the same. Despite the determin-
istic way in which random numbers are created, these numbers appear
to be random since they pass a battery of statistical tests designed to
test various properties of random numbers. In view of this, these ran-
dom numbers are referred to as pseudo-random numbers.

An advantage of generating pseudo random numbers in a determinis-


tic fashion is that they are reproducible, since the same sequence of

26
Harry Perros Simulation Techniques
random numbers is produced each time we run a pseudo-random gen-
erator given that we use the same seed. This is helpful when debug-
ging a simulation program, beecause we want to reproduce the same
sequence of events in order to verify the accuracy of the simulation.

Pseudo-random numbers and in general random numbers are typically


generated on demand. That is, each time a random number is re-
quired, the appropriate generator is called which then returns a ran-
dom number. Consequently, there is no need to generate a large set of
random numbers in advance and store them in an array for future use.

We note that the term pseudo-random number is reserved for random


numbers that are uniformly distributed in the space [0,1]. All other
random numbers, including those that are uniformly distributed with-
in any space other than [0,1], are referred to as stochastic variates or
random variates. For simplicity, we refer to pseudo-random numbers
as random numbers.

In general, an acceptable method for generating random numbers


must yield sequences of numbers or bits that are:

a. Uniformly distributed
b. Statistically independent
c. Reproducible
d. Non-repeating for any desired length.

Historically, the first method for creating random numbers by com-


puter was Von Neuman's mid-square method. His idea was to take the
square of the previous random number and to extract the middle dig-
its. For example, let us assume that we are generating 10-digit num-
bers and that the previous value was 5772156649. The square of this
value is 33317792380594909291 and the next number is
7923805949.

The mid-square method was relatively slow and statistically unsatis-


factory, and it was abandoned in favor of other algorithms. In the fol-
lowing sections, we describe the congruential method, the Taus-
worthe generators, and the lagged Fibonacci generators.

27
Simulation Techniques Harry Perros
2.3. The congruential method
This is a very popular method and most of the random number gener-
ators in programming languages are based on some variation of it.
The advantage of this method is that it is very simple, fast, and it pro-
duces pseudo-random numbers that are statistically acceptable for
computer simulation.

The congruential method uses the following recursive relationship to


generate random numbers:

𝑥R[\ = 𝑎𝑥R + 𝑐 (𝑚𝑜𝑑 𝑚),

where 𝑥R , 𝑎, 𝑐 and 𝑚 are all non-negative numbers. Given that the


previous random number was 𝑥R , the next random number 𝑥R[\ can be
generated as follows. Multiply 𝑥R by 𝑎 and then add 𝑐. Then, compute
the modulus 𝑚 of the result. That is, divide the result by 𝑚 and set
𝑥R[\ equal to the remainder of this division. For example, for 𝑥c =
0, 𝑎 = 𝑐 = 7, and 𝑚 = 10 we obtain the following sequence of
numbers: 7, 6, 9, 0, 7, 6, 9, 0, . ..

The method using the above expression is known as the mixed con-
gruential method. A simpler variation of this method is the multipli-
cative congruential method. This method utilizes the relation 𝑥R[\ =
𝑎𝑥R (𝑚𝑜𝑑 𝑚). Historically, multiplicative congruential generators
came before the mixed congruential generators. Below we limit our
discussion to mixed congruential generators.

The numbers generated by a congruential method are between 0 and


𝑚 − 1. Uniformly distributed random numbers between 0 and 1 can
be obtained by simply dividing the resulting 𝑥R by 𝑚.

The number of successively generated pseudo-random numbers after


which the sequence starts repeating itself is called the period. If the
period is equal to 𝑚, then the generator is said to have a full period.
Theorems from number theory show that the period depends on the
value of 𝑚. The larger its value, the larger is the period. In particular,
the following conditions on 𝑎, 𝑐, and 𝑚 guarantee a full period:

28
Harry Perros Simulation Techniques

a. 𝑚 and 𝑐 have no common divisor.


b. 𝑎 = 1 (𝑚𝑜𝑑 𝑟) if 𝑟 is a prime factor of 𝑚. That is, if 𝑟 is a
prime number (divisible only by itself and 1) that divides 𝑚,
then it divides 𝑎 − 1.
c. 𝑎 = 1 (𝑚𝑜𝑑 4) if 𝑚 is a multiple of 4.
It is important to note that one should not use any arbitrary values for
𝑎, 𝑐, and 𝑚. Systematic testing of various values for these parameters
have led to generators which have a full period and which are statisti-
cally satisfactory. A set of such values is: 𝑎 = 314, 159, 269, 𝑐 =
453, 806, 245, and 𝑚 = 2ef (for a 32 bit machine).

Typically, we do not write our own random number generator, but


use the one available in the programming language used to write the
simulation code.

In order to get a generator started, we need an initial seed value for


𝑥c . It will become obvious later on that the value of the seed does not
affect the sequence of the generated random numbers after a small set
of random numbers has been generated.

The implementation of a pseudo-random number generator involves a


multiplication, an addition and a division. The division can be avoid-
ed by setting 𝑚 equal to the size of the computer word. For, if the to-
tal numerical value of the expression 𝑎𝑥R + 𝑐 is less than the word
size, then it is in itself the result of the operation 𝑎𝑥R + 𝑐 (𝑚𝑜𝑑 𝑚),
where 𝑚 is set equal to the word size. Now, let us assume that the ex-
pression 𝑎𝑥R + 𝑐 gives a number greater than the word size. In this
case, an overflow will occur. If the overflow does not cause the exe-
cution of the program to be aborted, but it simply causes the signifi-
cant digits to be lost, then the remaining digits left in the register is
the remainder of the division (𝑎𝑥R + 𝑐)/𝑚. This is because the lost
significant digits are the multiples of the value of 𝑚, which is the
quotient of the above division.

In order to demonstrate the above idea, let us consider a fictitious


decimal calculator whose register can accommodate a maximum of 2
29
Simulation Techniques Harry Perros
digits. Obviously, the largest number that can be held in the register is
99, and we set 𝑚 = 100. For 𝑎 = 8, 𝑥c = 2, and 𝑐 = 10, we have
that 𝑎𝑥c + 𝑐 = 26, and 26 (𝑚𝑜𝑑 100) = 26. However, if 𝑥c = 20,
then the product 𝑎𝑥c (which is equal to 8𝑥20) will cause an overflow
to occur. The first significant digit will be lost and thus the register
will contain the number 60. If we now add 𝑐 (which is equal to 10) to
the above result we will obtain 70, which is, the remainder of the di-
vision 170/100.

2.3.1. General congruential methods


The mixed congruential method described above is a special case of
the following generator:

𝑥R[\ = 𝑓(𝑥R , 𝑥Ri\ , . . . ) (𝑚𝑜𝑑 𝑚),

where 𝑓(. ) is a function of previously generated pseudo-random


numbers. A special case of the general congruential method is the
quadratic congruential generator, which has the form:

𝑥R[\ = 𝑎\ 𝑥Rf + 𝑎f 𝑥Ri\ (𝑚𝑜𝑑 𝑚).

The special case of 𝑎\ = 𝑎f = 1, 𝑐 = 0, and 𝑚 being a power of 2


has been found to be related to the midsquare method. Another
special case that has been considered is the additive congruential
method, which is based on the expression:

𝑓(𝑥R , 𝑥Ri\ , . . . ) = 𝑎\ 𝑥R + 𝑎f 𝑥Ri\ + ⋯ + 𝑎k 𝑥Rik .

The case of 𝑓(𝑥R , 𝑥Ri\ , ) = 𝑥R + 𝑥Ri\ has received attention.

2.3.2. Composite generators


Composite generators can be used by combining two generators,
since this achieves a better statistical behavior than either individual
generator. For instance, we can use a congruential generator to fill a
vector of size 𝑛 with random numbers. A second generator is then
used to generate a random integer 𝑟 uniformly distributed in [1, 𝑛].
The random number stored in the 𝑟th position of the vector is the ran-
dom number returned from the composite generator. The first genera-
30
Harry Perros Simulation Techniques
tor replaces the random number in the 𝑟th position with a new ran-
dom number, and the procedure repeats. It has been demonstrated that
such a combined generator has good statistical properties, even if the
two individual generators used are not as good.

2.4. Tausworthe generators


Tausworthe generators are additive congruential generators obtained
when the modulus 𝑚 is equal to 2. That is,

𝑥R = 𝑎\ 𝑥Ri\ + 𝑎f 𝑥Rif + ⋯ + 𝑎l 𝑥li\ (𝑚𝑜𝑑 2),

where 𝑥R , 𝑖 = 1,2, …, and 𝑎R , 𝑖 = 1,2, … , 𝑛, are all binary. That is, the
generator produces a sequence of bits. 𝑥R is obtained from the above
expression by adding some of preceding bits and then carrying out a
modulo 2 operation. This is equivalent to the exclusive OR operation
(XOR) defined in the following table:

𝑨 𝑩 𝑨 𝐗𝐎𝐑 𝑩
1 1 0
0 1 1
1 0 1
0 0 0

The generated bits can be put together sequentially to form an 𝑙 −bit


binary integer between 0 and 2𝑙 − 1. Several bit selection techniques
have been suggested in the literature.

In the composite generator scheme discussed in the previous section,


one of the generators (but not both) could be a Tausworthe generator.

Tausworthe generators are independent of the computer used and its


word size and have very long cycles. However, they are slow since
they only produce bits. A fast variation of these generators is the tri-
nomial-based Tausworthe generator. Two or more such generators
have to be combined in order to obtain a statistically good output.

2.5. The lagged Fibonacci generators


The lagged Fibonacci generators (LFG) are an important improve-

31
Simulation Techniques Harry Perros
ment over the congruential generators, and they are widely used in
simulation. They are based on the well-known Fibonacci sequence, an
additive recurrence relation, whereby each element is computed using
the two previously computed elements, as shown below:

𝑥R = 𝑥Ri\ + 𝑥Rif

where 𝑥c = 0 and 𝑥\ = 1. The beginning of the Fibonacci sequence


is: 0, 1, 1, 2, 3, 5, 8, 13, 21. Based on this recurrence relation, the LFG
can be expressed as follows:

𝑥R = 𝑥Riu O 𝑥Rik (𝑚𝑜𝑑𝑚)

where 0 < 𝑗 < 𝑘, and appropriate initial conditions have been made.
In this generator, the next element is determined by combining two
previously calculated elements that lag behind the current element
utilizing an algebraic operation O. This operation O can be an addi-
tion, or a subtraction, or a multiplication as well as it can be a binary
𝑋𝑂𝑅 operation. If O is the addition operation, then the LFG is called
the additive LFG (ALFG). Likewise, if O is the multiplication opera-
tion, then it is called the multiplicative LFG (MLFG).

The additive LFG is the most frequently used generator. In this case,
the next element is calculated as follows:

𝑥R = 𝑥Riu + 𝑥Rik (𝑚𝑜𝑑𝑚)

where 0 < 𝑗 < 𝑘. As can be seen, it is very easy to implement and


also it is quite fast. A very long period, equal to 𝑚k − 1, can be ob-
tained if 𝑚 is a prime number. However, using a prime number may
not be very fast. Thus, typically 𝑚 is set to 2ef or 2z{ . In this case,
the maximum period of the additive LFG is (2k − 1)2|i\ .

The multiplicative LFG is: 𝑥R = 𝑥Riu × 𝑥Rik (𝑚𝑜𝑑𝑚), where 𝑚 is set


to 2ef or 2z{ and 0 < 𝑗 < 𝑘. The maximum period is (2k − 1)2|ie .

In general, LFGs generate a sequence of random numbers with very


good statistical properties, and they are nearly as efficient as the line-
ar congruential generators. Their execution can also be parallelized.
32
Harry Perros Simulation Techniques
However, LFGs are highly sensitive to the seed. That is, the statistical
properties of an output sequence of random numbers varies from seed
to seed. Determining a good seed for LFGs is a difficult task.

2.6. Statistical tests for pseudo-random number generators


It is a good practice to check statistically the output of a pseudo-
random number generator prior to using it. In this section, we de-
scribe the following four statistical tests:

1. Frequency test
2. Serial test
3. Runs test
4. Chi-squared test for goodness of fit.

For more information, see “A statistical test suite for random and
pseudorandom number generators for cryptographic applications”,
NIST special publication 800-22.

Statistical testing involves testing whether a particular assumption,


known as a hypothesis, is correct. Before we proceed to describe the
statistical tests, we review statistical hypothesis testing.

2.6.1. Hypothesis testing


Statistical hypothesis is used in Statistics to test whether a hypothesis,
of which the truth is not known, is correct or not. The hypothesis, no-
tated as 𝐻c , is known as the null hypothesis, and the opposite to the
hypothesis, notated as 𝐻~ , is known as the alternative hypothesis.

For example, let us consider the population of the weights of all men
in the USA of age 20 to 29. According to the 1970 census, the popu-
lation mean 𝜇 and standard deviation 𝜎 were 170 and 40 lbs respec-
tively. Now, let us assume that we want to test if the population mean
has changed in 2020. We formulate the null hypothesis that the popu-
lation mean 𝜇 is still 170, versus that it has changed (increased or de-
creased). That is,

𝐻c : 𝜇 = 170
𝐻~ : 𝜇 ≠ 170
33
Simulation Techniques Harry Perros
This is known as a two-tailed test or a two-sided test. Depending upon
the nature of the hypothesis, we may also formulate a one-tailed test
(or one-sided test) to the right. For instance, if we are only concerned
whether the average population weight 𝜇 has increased, then we for-
mulate the hypothesis:

𝐻c : 𝜇 = 170
𝐻~ : 𝜇 > 170

On the other hand, if we want to test whether the average population


weight 𝜇 has decreased, then we formulate a one-tailed test (or one-
sided test) to the left, as follows:

𝐻c : 𝜇 = 170
𝐻~ : 𝜇 < 170

We test the two-tailed test hypothesis by drawing a random sample of


weights from the 2020 population of all men in the USA of age 20 to
29, and calculate the sample mean 𝑥̅ . We note that if we sample 𝑛
observations from a population which has an arbitrary distribution
with a mean 𝜇 and variance 𝜎 f , then the sample mean 𝑥̅ is normally
distributed with mean 𝜇 and variance 𝜎 f /𝑛, for 𝑛 ≥ 30. Accordingly,
if the null hypothesis is true, that is, the population mean has not
changed, then the sample mean 𝑥̅ is normally distributed with mean
170 and standard deviation 40/√𝑛, where n is the sample size. As-
suming that 𝑛 = 64, we have that 40/√64 = 5.

Now, we compare 𝑥̅ to the population mean 𝜇 = 170. If 𝑥̅ = 170,


then we can argue that the null hypothesis is true, but what can we
say if 𝑥̅ = 165 or 𝑥̅ = 180? In general, if 𝑥̅ is very close to 𝜇, then
we accept the null hypothesis that the population mean has not
changed. On the other hand, if it is far away from 𝜇, then we reject
the null hypothesis, and accept the alternative hypothesis that the
population mean has changed (increased or decreased).

In view of this, we need a cut-off point which will act as a threshold.


In fact, since we are doing a two-tailed test, we need two symmetrical
points 𝑎 and 𝑏. We choose the two points so that 95% of the popula-
tion (or any other percentage, such as 90% and 99%) lies between
34
Harry Perros Simulation Techniques
them, as shown in figure 2.1. This means that 2.5% of the population
lies within each tail (−∞, 𝑎) or (𝑏, +∞). If the population mean has
not changed, then 95% of the sample means 𝑥̅ are between 𝑎 and 𝑏.
Consequently, if 𝑥̅ lies in-between 𝑎 and 𝑏, we have a 95% probabil-
ity that the null hypothesis is correct. Otherwise, we reject it and ac-
cept the alternative hypothesis. This probability is known as the con-
fidence level.
0.25

0.2

fX(t)
0.15

fX(t)
0.1

0.05

0 2 4 6
a 8 10

µ
12
b 14 16 18 20

"
!

2.5% 95% 2.5%

Figure 2.1: Accept 𝑯𝟎 if ‹


𝒙 is between 𝒂 and 𝒃

We note that the correct way to say that “we accept the null hypothe-
sis” is to say that “we fail to reject the null hypothesis”. However, for
simplicity, the former expression is often used. In the opposite direc-
tion, if we reject the null hypothesis, then it is correct to say that “we
accept the alternative hypothesis”.

In order to carry out the test, we transform points 𝑎, 𝑏 and 𝑥̅ to the


standard normal distribution using the 𝑧-transform. This is the normal
distribution with 𝜇 = 0 and 𝜎 f = 1. A point 𝑥 in a normal distribu-
tion with mean 𝜇 and variance 𝜎 f is transformed to an equivalent
point in the standard normal distribution using the expression 𝑧 =
(𝑥 − 𝜇)/𝜎. By equivalent point we mean that the distance of 𝑧 from
its mean 0 expressed in standard deviations of 1, is the same as the
distance of 𝑥 from its mean 𝜇 expressed in standard deviation of 𝜎.
Using the table for the standard normal distribution (available in the
Internet and in any introductory Statistics book), we can calculate that
at 95% confidence, 𝑎 corresponds to −1.96 and 𝑏 to 1.96. Now, we
transform 𝑥̅ to 𝑧, i.e., 𝑧 = (𝑥̅ − 170)/5 and check it against the val-
35
Simulation Techniques Harry Perros
ues of 𝑎 and 𝑏. For the two-tailed test, we accept the null hypothesis
if |𝑧| ≤ 1.96, see figure 2.2. Otherwise, we reject it. For instance, let
𝑥̅ = 173. Then, 𝑧 = 3/5 = 0.6 which is less than 1.96, and therefore
we accept the null hypothesis that the population mean of weights has
not changed. Likewise, if 𝑥̅ = 165, since 𝑧 = −5/5 = −1 >
−1.96. However, if 𝑥̅ = 180, then we reject the null hypothesis since
𝑧 = 10/5 = 2 > 1.96.
0.25

0.2

0.15

ffXX(t)
(t)
0.1

0.05

0 2 4 6 8 10 12 14 16 18 20

-1.96 0µ 1.96
z
2.5% 95% 2.5%

Figure 2.2: Two-tailed test at 𝟗𝟓% confidence


0.25

0.2

fX(t)
0.15

fX(t)
0.1

0.05

0 2 4 6 8 10 12 14 16 18 20

0µ 1.645
z
95% 5%

Figure 2.3: One-tailed test on the right at 𝟗𝟓% confidence

In the case of the one-tailed test on the right, we are only concerned
with how far 𝑥̅ is to the right of the population mean 𝜇. That is, we
choose point 𝑏 so that the tail (𝑏, +∞) accounts for 5%, for a 95%
confidence. This point corresponds to 1.645 in the standard normal
36
Harry Perros Simulation Techniques
distribution, as shown in figure 2.3. Now, given a sample mean 𝑥̅ , we
calculate its corresponding 𝑧 value, i.e., 𝑧 = (𝑥̅ − 170)/5, and we
accept the null hypothesis if 𝑧 < 1.645. Otherwise, we reject it and
accept the alternative hypothesis. For instance, if 𝑥̅ = 175, then 𝑧 =
1<1.645, and we accept the null hypothesis. Likewise, if 𝑥̅ = 155,
for 𝑧 = −3 < 1.645. However, if 𝑥̅ = 180, then 𝑧 = 2 > 1.645, and
therefore we reject the null hypothesis and accept the alternative hy-
pothesis.
0.25

0.2

fX(t)
0.15

fX(t)
0.1

0.05

0 2 4 6 8 10 12 14 16 18 20

-1.645 0µ
z
5% 95%

Figure 2.4: One-tailed test on the left at 𝟗𝟓% confidence

Finally, in the case of the one-tailed test on the left, we are only con-
cerned with how far 𝑥̅ is to the left of the population mean 𝜇. That is,
we choose point 𝑎 so that the tail (−∞, 𝑎) accounts for 5%, for a
95% confidence. This point corresponds to −1.645 in the standard
normal distribution, as shown in figure 2.4. Now, given a sample
mean 𝑥̅ , we calculate its corresponding 𝑧 value, i.e., 𝑧 = (𝑥̅ − 170)/
5, and we accept the null hypothesis if 𝑧 > −1.645. Otherwise, we
reject it and accept the alternative hypothesis. For instance, if 𝑥̅ =
165, then 𝑧 = −1 > −1.645, and we accept the null hypothesis.
Likewise, if 𝑥̅ = 180, for 𝑧 = 2 > −1.645. However, if 𝑥̅ = 155,
then 𝑧 = −3 < −1.645, and therefore we reject the null hypothesis
and accept the alternative hypothesis.

There are two errors associated with hypothesis testing, namely, type
I error and type II error. A type I error occurs when we reject the null

37
Simulation Techniques Harry Perros
assumption, whereas in fact it is correct. A type II error occurs when
we accept the null hypothesis when in fact it is not correct. Since we
do not know the truth, we do not know whether we have committed a
type I or a type II error. The type I error is commonly known as a
false positive, and the type II error is known as a false negative. Table
2.1 summarizes the type I and type II errors.

Decision
Real situation
Ho is accepted H0 is rejected
Ho is true Valid Type I error
Ho is not true Type II error Valid

Table 2.1: Type I and type II errors

We note that we use the standard normal distribution if the population


variance 𝜎 f in known and/or the sample size 𝑛 ≥ 30. Otherwise, we
use the t-student distribution. This is a symmetric and bell-shaped dis-
tribution, like the normal distribution, but has heavier tails, meaning
that it is more prone to producing values that fall far from its mean. It
is commonly used instead of the normal distribution, since the stand-
ard deviation 𝜎 is typically not known. The 𝑡-student distribution is
also used if the sample size is small, less than 30.
0.25

0.2

fX(t)
0.15

fX(t)
0.1

0.05

0 2 4 6 8 10 12 14 16 18 20

0µ 1.645
z
5%
p-value

Figure 2.5: The p-value

38
Harry Perros Simulation Techniques
As discussed above, in order to carry out the hypothesis test, we need
to compare the 𝑧 value that corresponds to 𝑥̅ against the cut-off points
𝑎 and 𝑏 or one of them depending on whether we do a two-tailed test
or a one-tailed test. These points are determined using the standard
normal distribution or the 𝑡-student distribution for a given level of
confidence.

An easier way to do this is to calculate the 𝑝-value of 𝑥̅ . As shown in


figure 2.5, the 𝑝-value is the area under the standard normal curve (or
the 𝑡-student distribution curve) from 𝑧 to +∞, where 𝑧 is the value
that corresponds to 𝑥̅ .

For example, let us say that for a one-tailed test on the right, we com-
pute a 𝑝-value of 0.3. At 95% confidence, the area (𝑏, ∞) of the right
tail is 0.05. Since 0.3 > 0.05, we conclude that 𝑧 < 1.645 and accept
the null hypothesis. On the other hand, if the 𝑝-value is 0.001, then
that means that 𝑧 falls on the right side of 1.645, and in this case, we
reject the null hypothesis.

Statistical packages automatically report the 𝑝-value. For a one-sided


test to the left or to the right, the 𝑝-value is reported as the area under
the 𝑡-student distribution from |𝑡| to +∞, where 𝑡 is the calculated 𝑡-
student statistic. For a two-tailed test, the reported 𝑝-value is two
times the area of the tail |𝑡| to +∞.

The 𝑝-value allows us to decide quickly whether to accept or reject a


null hypothesis. Typically, we test a hypothesis at the confidence lev-
els of 90%, 95%, and 99%. For a one tailed test, the area of the tail
corresponding to these three confidence levels is 0.10, 0.05, and 0.01
respectively. For a two-tailed test, the area of both tails is also 0.10,
0.05, and 0.01 respectively. So, irrespectively of whether we do a
one-tailed or a two-tailed test, we simply compare the 𝑝-value to
these three thresholds, 0.10, 0.05, and 0.01, and accordingly we ac-
cept or reject the null hypothesis.

In general, it is safer to make sure that the conclusion holds for all
three confidence levels. That is, if the reported 𝑝-value is greater than
0.10, then we accept the null hypothesis. If it is less than 0.01, then

39
Simulation Techniques Harry Perros
we reject the null hypothesis. However, if it falls in-between these
values, say it is 0.07, then the conclusion is questionable. For, we re-
ject the null hypothesis at 90% confidence but accept it at 95% and
99% confidence.

We now proceed to describe the statistical tests for pseudo-random


number generators. In these tests, the null hypothesis is that a se-
quence of numbers produced by a pseudo-random generator is ran-
dom, and the alternative hypothesis is that it is not.

We note, that in many tests, the tested sequence has to be binary. In


this case, if the number generator generates numbers in [0,1], you can
represent each number by a binary string and then concatenate them
into a sequence of bits.

2.6.2. Frequency test (monobit test)


The frequency test is one of the most basic tests for pseudo-random
number generators. If a pseudo-random number generators fails this
test, then it is highly likely that it will also fail other more sophisticat-
ed tests.

The tested sequence is a bit string. In a true random sequence, the


number of 1’s and 0’s should be about the same. This test checks
whether this is correct or not, and it is carried out as follows:

1. Generate 𝑚 pseudo-random numbers and concatenate them


into a string of bits. Let the length of this string be 𝑛. The 0’s
are converted into −1’s and then the obtained set of −1 and 1
values is added up. Let 𝑆l be the sum of the −1 and 1 values.
2. Compute the test statistic: 𝑆–—˜ = |𝑆l |/√𝑛.
3. Compute the 𝑝-value: 𝑝-value= 𝑒𝑟𝑓𝑐(𝑆–—˜ /√2), where
𝑒𝑟𝑓𝑐(. ) is the complimentary error function available in pro-
gramming languages and statistical packages.

At 99% confidence, we accept the null hypothesis that the sequence


is random if p-value > 0.01.

40
Harry Perros Simulation Techniques
For example, let us consider the string: 1011010101. Then, 𝑆l =
1 − 1 + 1 + 1 − 1 + 1 − 1 + 1 − 1 + 1 = 2 and 𝑆c—˜ = 0.6324. 𝑝-
value = 𝑒𝑟𝑓𝑐(0.6324) = 0.5271 > 0.01, and thus, we accept the
hypothesis that the sequence is random.

The test is based on the observation, that if the sequence is random,


then 𝑆l will be close to zero. A large positive value of 𝑆l is indicative
of too many 1’s, and a large negative value of 𝑆l is indicative of too
many 0’s.

It is recommended that the sequence to be tested should be greater


than a 100.

2.6.3 Serial test


The tested sequence is a bit string. For a 𝑘-bit string, there are 2k dif-
ferent ways of combining. Each of these combinations has the same
chance of occurring if the sequence of the k bits is random. The serial
test determines whether the number of times each of these combina-
tions occurs is uniformly distributed. If 𝑘 = 1, then this test becomes
equivalent to the frequency test described above.

Let 𝑒 denote a sequence of 𝑛 bits created by a pseudo-random number


generator. The minimum recommended value for 𝑛 is 100. The serial
statistical test checks the randomness of overlapping blocks of 𝑘, 𝑘 −
1, 𝑘 − 2 bits found in 𝑒, where 𝑘 < |𝑙𝑜𝑔f (𝑛)| − 2. The test is carried
out as follows:

1. Augment 𝑒 by appending its first 𝑘 − 1 bits from the begin-


ning of 𝑒 to the end of 𝑒, and let 𝑒\› be the resulting bit string.
Likewise, augment 𝑒 by appending its first 𝑘 − 2 bits to the
end, and let 𝑒f› be the resulting bit string. Finally, augment 𝑒
by appending its first 𝑘 − 2 bits to the end, and let 𝑒e› be the
resulting bit string.
2. Compute the frequency of occurrence of each 𝑘, 𝑘 − 1 and
𝑘 − 2 overlapping bit combination using the bit string
𝑒\› , 𝑒f› , 𝑒e› respectively. Let 𝑐\ be the total number of combina-
tions of 𝑘 bits, and let 𝑓\,\ , 𝑓\,f , … , 𝑓\, be the frequency of oc-
currence for each of these combinations in 𝑒\› . Likewise, let 𝑐f
41
Simulation Techniques Harry Perros
be the total number of combinations of 𝑘 − 1 bits, and let
𝑓f,\ , 𝑓f,f , … , 𝑓f,œ• , be the frequency of occurrence for each of
these combinations in 𝑒f› . Finally, let 𝑐e be the total number of
combinations of 𝑘 − 2 bits, and let 𝑓e,\ , 𝑓e,f , … , 𝑓e,œ• , be the
frequency of occurrence for each of these combinations in 𝑒e› .
3. Compute the following statistics and difference statistics:
œ¡
f
2k f
𝑆k = žŸ𝑓\,R −𝑛
𝑛
R¢\
ϥ
ki\
f
2 f
𝑆ki\ = žŸ𝑓f,R −𝑛
𝑛
R¢\
ϣ
f
2kif f
𝑆kif = žŸ𝑓e,R −𝑛
𝑛
R¢\

𝛥𝑆kf = 𝑆kf − 𝑆ki\


f

𝛥f 𝑆kf = 𝑆kf − 2𝑆ki\


f f
+ 𝑆k[\
4. Compute the 𝑝-values:
𝑝 − value\ = IncompleteGamma (2kif , 𝛥𝑆kf /2)
𝑝 − valuef = IncompleteGamma (2kie , 𝛥f 𝑆kf /2)
where the function IncompletGamma (. ) can be found in
programming languages and statistical packages.
5. At 99% confidence level, we reject the null hypothesis that
the sequence is random if 𝑝 − value\ or 𝑝 − valuef < 0.01.
Else, we accept the null hypothesis that the sequence is ran-
dom.

The test is based on the idea that if the overlapping bit combinations
are not uniformly distributed, then 𝛥𝑆kf and 𝛥f 𝑆kf become large which
causes the 𝑝 − values to become small.

As an example we consider the sequence 𝑒 = 0011011101, with


𝑛 = 10, and 𝑘 = 3.

42
Harry Perros Simulation Techniques
1. Append the 𝑘 − 1 = 2 bits from the beginning of the se-
quence e to the end of e, obtaining 𝑒\› = 001101110100.
We repeat the process by appending the first 𝑘 − 2 = 1 bits of
𝑒 to the end of 𝑒, obtaining 𝑒f› = 00110111010. Do not need
to repeat the process for 𝑘 − 3 since 𝑘 − 3 = 0.
2. Calculate the frequency of occurrence of each 3-bit, 2-bit and
1-bit overlapping combination.
a. For the 3-bit blocks we use 𝑒\› = 001101110100. The 3-
bit combinations are: 000, 001, 010, 011, 100, 101, 110,
111, and their corresponding frequency of occurrence is:
𝑓\,\ = 0, 𝑓\,f = 1, 𝑓\,e = 1, 𝑓\,{ = 2, 𝑓\,² = 1, 𝑓\,z = 2,
𝑓\,³ = 2, 𝑓\,´ = 1.
b. For the 2-bit blocks we use 𝑒f› = 00110111010. The 2 bit
combinations are: 00,01,10,11, and their corresponding
frequency of occurrence is: 𝑓f,\ = 1, 𝑓f,f = 3, 𝑓f,e =
3, 𝑓f,{ = 3
c. For the 1-bit blocks we use 𝑒 = 00110111010. The 1-bit
combinations are: 0,1 and their corresponding frequency
of occurrence is 𝑓e,\ = 4, 𝑓e,f = 6.
3. Compute the following statistics and differences:
2e
𝑆ef = (0 + 1 + 1 + 4 + 1 + 4 + 4 + 1) − 10 = 2.8
10
2f
𝑆ff = (0 + 9 + 9 + 9) − 10 = 1.2
10
2
𝑆\f = (16 + 36) − 10 = 0.4
10
𝛥𝑆ef = 𝑆ef − 𝑆ff = 2.8 − 1.2 = 1.6

𝛥f 𝑆ef = 𝑆ef − 2𝑆ff + 𝑆\f = 0.8

4. Decision at 99% confidence level:


𝑝 − value\ = IncompleteGamma (2,0.8) = 0.9057
𝑝 − valuef = IncompleteGamma (1,0.4) = 0.8805
Since 𝑝 − value\ and 𝑝 − valuef are greater than 0.01, the
sequence passes the serial test.
43
Simulation Techniques Harry Perros
2.6.4. Runs test
Given a bit string, a run is an uninterrupted sequence of 1’s or 0’s.
For instance, for the bit string 001011110001111000 we have the
following runs: 00, 1, 0, 1111, 000, 1111,000. Now, let 𝑅 be the total
number of runs for a sample generated by a pseudo-random genera-
tor. The test is carried out by computing the following test statistic:
𝑅 − 𝑅µ
𝑧= ,
𝑠¶
where 𝑅µ and 𝑠¶ is the expected number of runs and the standard de-
viation respectively, given by the expressions:
2𝑛\ 𝑛f
𝑅µ = +1
𝑛\ +𝑛f

2𝑛\ 𝑛f (2𝑛\ 𝑛f − 𝑛\ − 𝑛f )
𝑠¶ =
(𝑛\ +𝑛f )f (𝑛\ +𝑛f − 1)

where 𝑛\ and 𝑛f are the number of 0› 𝑠 and 1’s.

For a sample, where 𝑛\ > 10 and 𝑛f > 10, the test statistic follows
the standard normal distribution. For instance, at 95% confidence, we
accept the null hypothesis that the bit string is random if 𝑧 > 1.96.

2.6.5. Chi-squared test for goodness of fit


This test checks whether a sequence of pseudo-random numbers in
[0,1] are uniformity distributed. The chi-squared (𝜒 f ) test can be used
to check whether an empirical distribution follows a specific theoreti-
cal distribution. In our case, we are concerned about testing whether
the numbers produced by a generator are uniformly distributed.

Let us consider a sequence of pseudo-random numbers between 0 and


1. We divide the interval [0,1] into 𝑘 subintervals of equal length,
where 𝑘 > 100. Let 𝑓R be the number of pseudo-random numbers
that fall within the 𝑖th subinterval (make sure that enough random
numbers are generated so that 𝑓R > 5). The 𝑓R values are called the
observed values. Now, if these generated random numbers are truly
uniformly distributed, then the mean number of random numbers that
44
Harry Perros Simulation Techniques
fall within each subinterval is 𝑛/𝑘, where 𝑛 is the sample size. This
value is called the theoretical value. The chi-squared test measures
whether the differences between the observed and the theoretical val-
ues is due to random fluctuations or due to the fact that the empirical
distribution does not follow the specific theoretical distribution. For
the case where the theoretical distribution is the uniform distribution,
the chi-squared statistic is given by the expression:
k
𝑘 𝑛 f
𝜒 f = ž ¸𝑓R − ¹
𝑛 𝑘
R¢\

and it has 𝑘 − 1 degrees of freedom. The null hypothesis is that the


generated random numbers are uniformly distributed in [0,1]. This
hypothesis is rejected if the computed value of 𝜒 f is greater than the
one obtained from the chi-squared tables for 𝑘 − 1 degrees of free-
dom and 𝑎 level of significance. The chi-squared tables can be found
in any introductory statistics book, and of course, the function to gen-
erate 𝜒 f values can be found in software packages.

Problems
1. Consider the multiplicative congruential method for generating
random digits with 𝑚 = 10. Determine the length of the cycle for
the values of 𝑎 and 𝑥c : 𝑎 = 2, 𝑥c = 1, 3, 5.

Computer projects
1. Use the four statistical tests described in section 2.6, to test the
random number generator of the programming language that you
plan to use for your project.

2. This is a more involved exercise! Implement the random number


generators described in this Chapter, and then test them using the
statistical tests described in section 2.6. Measure the execution
time for each generator for the same output size. Present your re-
sults in a table that gives the execution speed of each generator and
whether it has failed or passed each statistical test. Discuss your re-
sults. Which generator would you chose?

45
Simulation Techniques Harry Perros

46
Harry Perros Simulation Techniques
Chapter 3:
Generating Stochastic Variates
3.1. Introduction
In the previous Chapter, we examined techniques for generating ran-
dom numbers. In this Chapter, we discuss techniques for generating
random numbers from a given distribution. These random numbers
are called stochastic variates or random variates. Pseudo-random
numbers are uniformly distributed in the space [0,1], and they are
typically referred to as random numbers. As will be seen in this
Chapter, random numbers are used in the generation of stochastic var-
iates.

There are many techniques for generating stochastic variates. The in-
verse transformation method is the most commonly used technique.
This is presented in section 3.2. In sections 3.3 and 3.4, we use the
inverse transformation method to generate stochastic variates from
known continuous and discrete theoretical distributions. In section
3.5, we describe how to generate stochastic variates from empirical
distributions. Finally, in section 3.6 we describe an alternative method
for generating stochastic variates known as the rejection method.

3.2. The inverse transformation method


This method is applicable only to cases where the cumulative density
function can be inversed. Assume that we wish to generate stochastic
variates from a probability density function (pdf) 𝑓(𝑥). Let 𝐹(𝑥) be
its cumulative density function. We note that 𝐹(𝑥) is defined in the
region [0,1]. We explore this property of the cumulative density func-
tion to obtain the following simple stochastic variates generator.

We generate a random number 𝑟 and set it equal to 𝐹(𝑥). That is,


𝐹(𝑥) = 𝑟. 𝑥 is then obtained by inverting 𝐹(𝑥). That is, 𝑥 =
𝐹 i\ (𝑟), where 𝐹 i\ (𝑟) indicates the inverse transformation of 𝐹(𝑥).
Below, we demonstrate this method through four simple examples.

Example 1: Generate stochastic variates with probability density


function 𝑓(𝑥) = 2𝑥, 0 ≤ 𝑥 ≤ 1.
47
Simulation Techniques Harry Perros
A graphical representation of this probability density function is giv-
en in figure 3.1a. We first calculate the cumulative density function
𝐹(𝑥). We have
¿
𝐹(𝑥) = ¾ 2𝑡𝑑𝑡 = 𝑥 f , 0 ≤ 𝑥 ≤ 1.
c

Let 𝑟 be a random number. Then, 𝑟 = 𝑥 f or 𝑥 = √𝑟. That is, a sto-


chastic variate from the distribution 𝑓(𝑥) = 2𝑥 can be generated by
taking the square root of a random number. This inversion is shown
graphically in figure 3.1b.

2
1
# "
! " r

0 0
" 1 " 1
a) 𝒇(𝒙) b) Inversion of 𝑭(𝒙)
Figure 3.1: Sampling from 𝒇(𝒙) = 𝟐𝒙

Example 2: Generate random variates with probability density func-


tion:

3𝑥 f , 0≤𝑥≤1
𝑓(𝑥) = Â .
0, otherwise

Let 𝐹(𝑡) be the cdf of 𝑓(𝑡). Then


Ç
𝑡
𝐹(𝑥) = ¾ 3𝑥 f 𝑑𝑥 = 𝑥 e | = 𝑡 e − 0 = 𝑡 e , 0 ≤ 𝑡 ≤ 1.
c 0

Let 𝑟 be a pseudo-random number. By the method of inverse


transformation, we have 𝑟 = 𝑡 e . Therefore, 𝑡 = 𝑟 \/e .

48
Harry Perros Simulation Techniques
Example 3: Generate random variates from

5𝑥, 0 ≤ 𝑥 ≤ 4
𝑓(𝑥) = Â .
𝑥 − 2, 4 < 𝑥 ≤ 10

We first normalize 𝑓(𝑡). We have:


{ \c
¾ 5𝑥𝑑𝑥 + ¾ (𝑥 − 2)𝑑𝑥
c {

1 f 4 𝑥f 10
= 5 𝑥 È + É − 2𝑥Ê È = 70.
2 0 2 4

Therefore
⎧ 1 5 f
⎪ 𝑥 0≤𝑥≤4
70 2
𝐹(𝑥) = 1 f .
⎨ É40 + 𝑥 − 2𝑥Ê 4 < 𝑥 ≤ 10
⎪70 2

Procedure:

1. Draw a random number 𝑟.


2. If 𝑟 ≤ 0.4 then 𝑟 = (5/140)𝑥 f or 𝑥 = Ï(140/5)𝑟.
\ ¿•
3. Otherwise, set r =³c ¸40 + f
− 2𝑥¹ and solve for x.

Example 4: Generate random variates from

𝑥, 0 ≤ 𝑥 ≤ 1/2
𝑓(𝑥) = Â .
1 − 𝑥, 1/2 < 𝑥 ≤ 1

This is the triangle distribution, and the area under the curve is equal
to 0.5 × 0.5 = 0.25. We divide 𝑓(𝑡) by 0.25 so it becomes a pdf. The
sampling procedure is as follows.

1. Draw a random number r.


2. If 𝑟 ≤ 0.5 then set 𝑟 = 𝑥/0.25 from where we obtain 𝑥 =
0.25𝑟.

49
Simulation Techniques Harry Perros
3. Otherwise, set 𝑟 = (1 − 𝑥)/0.25 from where we obtain 𝑥 =
1 − 0.25𝑟.

3.3. Sampling from continuous-time probability distributions


In this section, we use the inverse transformation method to generate
stochastic variates from a uniform distribution, an exponential distri-
bution, and an Erlang distribution. We also present a technique for
generating variates from the normal distribution.

3.3.1. Sampling from a uniform distribution


The probability density function of the uniform distribution is defined
as follows:

1
𝑓(𝑥) = Ð𝑏 − 𝑎 𝑎 < 𝑥 < 𝑏 ,
0 otherwise
and it is shown graphically in figure 3.2.

1
"−$

$ "
Figure 3.2: The uniform distribution

The cumulative density function is:


¿
1 𝑥−𝑎
𝐹(𝑥) = ¾ 𝑑𝑡 =
~ 𝑏−𝑎 𝑏−𝑎

The inverse transformation method for generating stochastic variates


is as follows. Generate a random number 𝑟. Then
𝑥−𝑎
𝑟=
𝑏−𝑎

50
Harry Perros Simulation Techniques
or, 𝑥 = 𝑎 + (𝑏 − 𝑎)𝑟. That is, 𝑟 is multiplied by (𝑏 − 𝑎) and
then shifted by 𝑎.

3.3.2. Sampling from an exponential distribution


The probability density function of the exponential distribution is
given by the expression:

𝑓(𝑥) = 𝜆𝑒 iÒ¿ , 𝜆 > 0, 𝑥 ≥ 0,

The cumulative density function is:


¿
𝐹(𝑥) = ¾ 𝜆𝑒 iÒ¿ = 1 − 𝑒 iÒ¿ .
c

#fX(t)
" !"
FX(t)

X X
a) Probability density function b) Cumulative function
Figure 3.3: The exponential distribution and its cumulative

The probability density function of the exponential distribution and


its cumulative function are shown in figure 3.3. The inverse transfor-
mation method for generating stochastic variates is as follows. Gener-
ate a random number 𝑟. Then,

𝑟 = 1 − 𝑒 iÒ¿

or

1 − 𝑟 = 𝑒 iÒ¿

or

51
Simulation Techniques Harry Perros
1
𝑥 = − 𝑙𝑜𝑔(1 − 𝑟).
𝜆
We note that 1/𝜆 is the mean of the exponential distribution. Since
1 − 𝐹(𝑥) is also uniformly distributed in [0,1], we can simplify the
above expression by setting 𝑟 = 𝑒 iÒ¿ , or

1
𝑥 = − 𝑙𝑜𝑔(𝑟).
𝜆
3.3.3. Sampling from an Erlang distribution
In many cases the exponential distribution may not represent well the
duration of a real-life activity, such as, the service time at a bank
teller and the repair time of a broken-down machine. In this case, one
can model this time as a mixture of exponential distributions, such as
the Erlang distribution shown in figure 3.4. In this figure, the Erlang
distribution consists of four exponential stages, each with the same
mean 1/𝑎. That is the amount of time 𝑥 spent in each stage is expo-
nentially distributed, i.e., 𝑓(𝑥) = 𝑎𝑒 i~¿ . Let 𝑥\ , 𝑥f , 𝑥e , 𝑥{ be the
amount of time spent in stage 1,2,3, and 4 respectively. Then, the sum
𝑥 = 𝑥\ + 𝑥f + 𝑥e + 𝑥{ follows the Erlang distribution.

1/a 1/a 1/a 1/a

Figure 3.4: The Erlang distribution

When, we model an activity, such as a service time, as an Erlang dis-


tribution, we do not necessarily think of this service time as consist-
ing of a number of exponentially distributed service times. We use
this distribution because it is a more general distribution than the ex-
ponential distribution, and it has been used successfully to model the
duration of an activity, such as a service time. Other mixtures of ex-
ponential distributions have also been developed, but they are not dis-
cussed here since they are beyond the scope of this book. The inter-
ested reader can look up the phase-time distribution, which is the
most general case.

52
Harry Perros Simulation Techniques
Erlang variates can be generated very simply as follows. Let us as-
sume that we want to generate a stochastic variate 𝑥 from an Erlang
distribution with 𝑘 stages, each with a mean 1/𝑎. In this case, we
generate 𝑘 exponential variates 𝑥\ , 𝑥f , … , 𝑥k , and then set 𝑥 equal to
their sum, i.e., 𝑥 = 𝑥\ + 𝑥f + ⋯ + 𝑥k . Specifically, let 𝑟\ , 𝑟f , … , 𝑟k be
𝑘 random numbers. Then,
k
1
𝑥 = − ž log ( 𝑟R ) .
𝑎
R¢\

Example: A customer in a bank may receive a service which has an


Erlang distribution with three stages, each phase with a mean 10, or
an Erlang distribution with four phases, each phase with a mean 5,
with probability 0.4 and 0.6 respectively. Setup a procedure to gener-
ate random variates of a customer's service.

Draw a random number 𝑟. If 𝑟 ≤ 0.4 then generate a stochastic vari-


ate from the first Erlang distribution. Otherwise, generate a stochastic
variate from the second one.

3.3.4. Sampling from a normal distribution


The normal distribution (also known as the Gaussian distribution) is a
very important distribution in Statistics and in many areas of science
and engineering. This is because many variables are normally distrib-
uted, or they can be closely approximated by a normal distribution,
such as, height, intelligence, and measurement errors. Another main
reason for its importance is the central limit theorem, which says that
if a random variable 𝑋 is the sum of 𝑛 identical and independent ran-
dom variables which may not necessarily be normally distributed,
then 𝑋 follows the normal distribution as 𝑛 increases.

The normal distribution has the familiar bell-shaped symmetric curve


shown in figure 3.5. Half of the points are above the mean and the
other half below it. In addition, about 68% of all points fall within
one standard deviation of the mean, about 96% of the points lie with-
in two standard deviations of the mean, and almost all of the points lie
within three standard deviations of the mean.

53
Simulation Techniques Harry Perros
0.25

0.2

fX(t)
0.15

fX(t)
0.1

0.05

0 2 4 6 8 10 12 14 16 18 20

Figure 3.5: The normal probability density distribution

The pdf of a normally distributed random variable 𝑋 is:

1 \ (¿iÖ)•
i
𝑓Ô (𝑥) = 𝑒 f ו , −∞ < 𝑥 < +∞,
𝜎√2𝜋

where 𝜇 and 𝜎 f is the mean and variance of 𝑋 respectively. If 𝜇 = 0


and 𝜎 f = 1, then the normal distribution is known as the standard
normal distribution and its pdf is:

1 \ •
𝑓Ô (𝑡) = 𝑒 ifÇ , −∞ < 𝑡 < +∞.
√2𝜋
One way to generate variates from a normal distribution is to employ
the central limit theorem. An alternative popular method is to use the
Box-Muller algorithm that generates two normal variates at a time.
Let 𝑟\ and 𝑟f be two random numbers. Then, we can obtain two nor-
mal variates 𝑥\ and 𝑥f as follows:

𝑥\ = Ï−2𝑙𝑜𝑔Ø 𝑟\ cos (2𝜋𝑟f)

and

𝑥f = Ï−2𝑙𝑜𝑔Ø 𝑟\ sin(2𝜋𝑟f ).

54
Harry Perros Simulation Techniques
3.4. Sampling from discrete-time probability distributions
In this section, we describe how to generate stochastic variates from a
a binomial distribution, a geometric distribution, and a Poisson distri-
bution.

3.4.1. Generating binomial distributed stochastic variates


Consider a sequence of Bernoulli trials, that is, independent trials
where the outcome of each trial is either a success or a failure. Let 𝑝
be the probability of success and 1 − 𝑝 the probability of a failure in a
trial. Let 𝑋 be a random variable indicating the number of successes
in 𝑛 trials. Then, this random variable follows the binomial distribu-
tion, and the probability that 𝑋 = 𝑖 is:
𝑛
𝑝(𝑋 = 𝑖) = ¸ ¹ 𝑝R (1 − 𝑝)liR , 𝑖 = 0,1,2, …
𝑖
The easiest way to generate stochastic variates is to imitate the Ber-
noulli experiment. Let 𝑖 be a counter set to zero. We generate a ran-
dom number 𝑟\ and if 𝑟\ ≤ 𝑝, then we have a success in the first trial
and 𝑖 is increased by one, otherwise we have a failure and 𝑖 is not
changed. Next, we generate a second random number 𝑟f and if 𝑟f ≤
𝑝, then we have a success in the second trial and 𝑖 is increased by one,
otherwise we have a failure and 𝑖 is not modified. We continue in this
way until we generate 𝑛 random numbers. The final value of 𝑖 is a
stochastic variate from the binomial distribution.

3.4.2. Generating geometrically distributed stochastic variates


Consider an infinite sequence of Bernoulli trials, where the outcome
of each trial is either a success or a failure. Let 𝑝 and 1 − 𝑝 be the
probability of a success and a failure respectively. The random varia-
ble 𝑋 that gives the number of successive failures that occur before a
success occurs follows the geometric distribution. The probability
density function of the geometric distribution is:

𝑝(𝑋 = 𝑖) = 𝑝R (1 − 𝑝), 𝑖 = 0,1, ….

Its cumulative probability function is:

55
Simulation Techniques Harry Perros
k[\
𝐹Ô (𝑘) = 1 − 𝑝 .

A stochastic variate 𝑘 can be obtained using the inverse transfor-


mation technique. We observe that 1 − 𝐹Ô (𝑘) = 𝑝k[\ also varies be-
tween 0 and 1. Therefore, let 𝑟 be a random number, then

𝑟 = 𝑝k[\ ,

or

𝑙𝑜𝑔𝑟 = (𝑘 + 1)𝑙𝑜𝑔𝑝

or

𝑙𝑜𝑔𝑟
𝑘= − 1.
𝑙𝑜𝑔𝑞

The above expression can be further simplified since the quantity


(1 − 𝐹Ô (𝑘))/𝑝 = 𝑝k also varies between 0 and 1. We have

𝑟 = 𝑝k ,

or

𝑙𝑜𝑔𝑟
𝑘= .
𝑙𝑜𝑔𝑝

3.4.3. Generating Poisson distributed stochastic variates


A Poisson random variable 𝑋 gives the number of times that an event
occurs in a unit time. The probability that 𝑋 takes the value 𝑖 is given
by the expression:

𝜆R
𝑝(𝑋 = 𝑖) = 𝑒 iÒ , 𝑖 = 0,1,2, …
𝑖!
where 𝜆 is the average number of times the event occurs in a unit
time.

56
Harry Perros Simulation Techniques
There is an interesting relation between the Poisson distribution and
the exponential distribution, in that the inter-arrival time between
successive arrivals is exponentially distributed with a mean 1/𝜆.
Therefore, the easiest way to generate Poisson stochastic variates is to
generate exponentially distributed stochastic variates until their sum
exceeds the unit time. Let us assume that the unit time is equal to 1 (it
could be any value for that matter). Then, we generate exponentially
distributed stochastic variates 𝑥\ , 𝑥f , ⋯ , 𝑥l , 𝑥l[\ , so that
l l[\

ž 𝑥R ≤ 1 < ž 𝑥R .
R¢\ R¢\

The value 𝑛 is the Poisson stochastic variate. We recall that an expo-


nential variate 𝑥 is obtained using the expression: 𝑥 = −(1/
𝜆)𝑙𝑜𝑔Ø (𝑟), where 1/𝜆 is the mean and 𝑟 is a random number. Conse-
quently, the above expression can be re-written as:
l 1 l[\ 1
ž − 𝑙𝑜𝑔Ø 𝑟R ≤ 1 < ž − 𝑙𝑜𝑔Ø 𝑟R ,
R¢\ 𝜆 R¢\ 𝜆

or
l l[\
ž 𝑙𝑜𝑔Ø 𝑟R ≥ −𝜆 > ž 𝑙𝑜𝑔Ø 𝑟R
R¢\ R¢\

or
l l[\
𝑙𝑜𝑔Ø ÛÜ 𝑟R Ý ≥ −𝜆 > 𝑙𝑜𝑔Ø ÛÜ 𝑟R Ý,
R¢\ R¢\

or
l l[\
Ü 𝑟R ≥ 𝑒 iÒ > Ü 𝑟R .
R¢\ R¢\

The value 𝑛 for which the above expression holds is a Poisson sto-
chastic variate.

57
Simulation Techniques Harry Perros
3.5. Generating stochastic variates from an empirical distribution
Empirical probability distributions are used often in simulation, and
they can be either approximated by a known theoretical distribution
or used as is. Generating stochastic variates directly from an empiri-
cal distribution is quite easy and it is done using the inverse transfor-
mation method.
1

7/8
FX(t)
r
p(X) 4/8
3/8

1/8
1/8
0 1 2 3
0 1 2 3
X X
a) Probability density function b) Cumulative function
Figure 3.6: Generating a stochastic variate from a discrete distribution

Consider a discrete random variable 𝑋 that takes the values 0,1,2,3


with probability 1/8,3/8,3/8,1/8 respectively, shown in figure 3.6a.
Its cumulative distribution takes the values 1/8,4/8,7/8,1, and it is
shown in figure 3.6b. Let 𝑟 be a random number. Then we select a
stochastic variate as follows:

1. If 𝑟 ≤ 1/8, then 𝑋 = 0
2. If 1/8 ≤ 𝑟 ≤ 4/8, then 𝑋 = 1
3. If 4/8 ≤ 𝑟 ≤ 7/8, then 𝑋 = 2
4. If 7/8 ≤ 𝑟 ≤ 1, then 𝑋 = 3

In figure 3.6, 𝑟 falls in between 4/8 and 7/8, and therefore we chose
𝑋 = 2.

If the random variable 𝑋 is continuous, then it is described by a histo-


gram, as shown in figure 3.7. Let 𝑥R and 𝑓Ô (𝑥R ), 𝑖 = 1,2, ⋯ ,7, be the
mid-point and height respectively of the ith rectangular. The cumula-
tive distribution of 𝑋 can be approximated by the points 𝐹Ô (𝑥R ) =
𝑓Ô (𝑥\ ) + 𝑓Ô (𝑥f ) + ⋯ + 𝑓Ô (𝑥R ), 𝑖 = 1,2, ⋯ ,7, as shown in figure 3.8.
58
Harry Perros Simulation Techniques

f(x3 )
f(x4 )
f(x2 ) f(x5 )
f(x1 )
f(x6 )
f(x7 )

x1 x2 x3 x4 x5 x6 x7
Figure 3.7: A histogram of a continuous random variable

x1 x2 x3 x4 x5 x6 x7

Figure 3.8: Generating a stochastic variate from a continuous distribution

Generating a stochastic variate is straightforward. Let 𝑟 be a random


number and let 𝐹Ô (𝑥Ri\ ) ≤ 𝑟 ≤ 𝐹Ô (𝑥R ). Then, we select a stochastic
variate 𝑥 by interpolating between the values 𝑥Ri\ and 𝑥R , that is:

𝑟 − 𝐹Ô (𝑥Ri\ )
𝑥 = 𝑥Ri\ + (𝑥R − 𝑥Ri\ ) .
𝐹Ô (𝑥R ) − 𝐹Ô (𝑥Ri\ )

!" #

$ %
Figure 3.9: Normalized 𝒇(𝒙)

59
Simulation Techniques Harry Perros
3.6. The rejection method
The rejection technique can be used to generate random variates if
𝑓(𝑥) is bounded and 𝑥 takes a finite range of values, i.e., 𝑎 ≤ 𝑥 ≤ 𝑏.
It consists of the following steps:

1. Normalize the range of 𝑓(𝑥) by a scale factor 𝑐 so that


𝑐𝑓(𝑥) < 1, 𝑎 ≤ 𝑥 ≤ 𝑏, see figure 3.9.
2. Define 𝑥 as a linear function of 𝑟, i.e. 𝑥 = 𝑎 + (𝑏 − 𝑎) 𝑟,
where 𝑟 is a random number.
3. Generate pairs of random numbers (𝑟\ , 𝑟f ).
4. Accept a pair and use 𝑥 = 𝑎 + (𝑏 − 𝑎)𝑟\ as a random vari-
ate whenever the pair satisfies the relationship 𝑟f ≤ 𝑐𝑓(𝑎 +
(𝑏 − 𝑎)𝑟\ ), i.e., the pair (𝑥, 𝑟f ) falls under the curve in figure
3.9.
The idea behind this approach is that the probability of 𝑟f being less
than or equal to 𝑐𝑓(𝑥) is 𝑝[𝑟f ≤ 𝑐𝑓(𝑥)] = 𝑐𝑓(𝑥). Consequently, if
𝑥 is chosen at random from the range (𝑎, 𝑏) and then accepted if 𝑟f ≤
𝑐𝑓(𝑥), the probability density function of the accepted 𝑥's will follow
𝑓(𝑥).

We demonstrate the rejection method by giving two examples. The


first example deals with random variate generation, and the second
one with numerical integration.

Example 1: Use the rejection method to generate stochastic variates


with probability density function 𝑓(𝑥) = 2𝑥, 0 ≤ 𝑥 ≤ 1. We have:

1. Set 𝑐 = 1/2 such that 𝑐𝑓(𝑥) ≤ 1.


2. Generate 𝑟\ , and set 𝑥 = 𝑟\ .
3. Generate 𝑟f . If 𝑟f < 𝑐𝑓(𝑟\ ) = (1/2)2𝑟\ = 𝑟\, then accept 𝑟f ,
otherwise, go back to step 2.

Example 2: Use the rejection method to compute the area of the NE


quadrant of a unit circle. We note that a pair of random numbers
(𝑟\ , 𝑟f ) lies within or on the circumference of the NE quadrant of the
unit circle, if 𝑟\f + 𝑟ff ≤ 1. The numerical integration is accomplished
by carrying out the following two steps a large number of times:

60
Harry Perros Simulation Techniques
1. Generate a pair of random numbers (𝑟\ , 𝑟f ).
2. If 𝑟f < 𝑓(𝑟\ ), where 𝑓(𝑟\ ) = Ï1 − 𝑟\f , then 𝑟f is under (or
on) the curve and hence the pair (𝑟\ , 𝑟f ) is accepted. Other-
wise, it is rejected.

The area under the curve is obtained as the ratio:

Total number of accepted pairs


area = .
Total number of generated pairs

The rejection method is not very efficient when 𝑐(𝑏 − 𝑎) becomes


very large. In this case, the method of mixtures can be used, whereby
the distribution is broken into pieces and the pieces are then sampled
in proportion to the amount of distribution area each contains. This
process is identical to the rejection method for each piece of the dis-
tribution, plus a straightforward sampling of data.

3.7. Monte Carlo methods


Monte Carlo methods is a branch of mathematics which is concerned
with computational algorithms using random numbers. Monte Carlo
methods are used in physical and mathematical problems and are
most useful when it is difficult or impossible to use other approaches.
They are mainly used in three classes: optimization, numerical inte-
gration, and generation of stochastic random numbers. The previously
described rejection method for calculating the integral of a function is
an example of Monte Carlo.

Monte Carlo techniques are beyond the scope of this book, and the
interested reader is referred to the relevant litterature.

Computer projects
In this assignment you will modify the code you developed in Chapter
1 so that to introduce randomness in your simulation model.

1. The machine interference problem. Change your simulation pro-


gram so that the operational time and the repair time of a machine
are exponentially distributed with the same means as before.
Make sure that your clocks are defined as real variables. Run your
61
Simulation Techniques Harry Perros
simulation model as before. Each time an event occurs, print out a
line of output to show the new value of the clocks and the other
relevant parameters.

2. The token-based access scheme. Change your simulation program


so that the inter-arrival times are exponentially distributed with
the same means as before. The switch over time and the time pe-
riod 𝑇 remain constant as before. The packet transmission time is
calculated as follows. We assume that 80% of the transmitted
packets are short and the remaining 20% are long. The time to
transmit a short packet is exponentially distributed with a mean
2.5, and the time to transmit a long packet is exponentially dis-
tributed with mean 20.

Make sure that your clocks are defined as real variables. Run your
simulation model as before. Each time an event occurs, print out a
line of output to show the new value of the clocks and the other
relevant parameters.

3. The two-stage manufacturing system. Change your simulation


program so that the inter-arrival, service, operational, and repair
times are all exponentially distributed. Make sure that your clocks
are defined as real variables. Run your simulation model as be-
fore. Each time an event occurs, print out a line of output to show
the new value of the clocks and the other relevant parameters.

62
Harry Perros Simulation Techniques
Chapter 4:
Simulation Designs
4.1. Introduction
In this Chapter, we examine three different designs for building simu-
lation models. These are: the event-advance design, the unit-time ad-
vance design and the activity-based design. The first two designs are
event-based but utilize different ways of advancing the time. The
third design is activity-based rather than event-based. The two event-
based designs are the most commonly used.

4.2. Event-advance design


This is the design employed in the three examples described in Chap-
ter 1. The basic idea behind this design is that the status of the system
changes each time an event occurs. During the time that elapses be-
tween two successive events, the system's status remains unchanged.
In view of this, it suffices to monitor only the changes in the system's
status. In order to implement this idea, each event is associated with a
clock. The value of this clock gives the time instance in the future
that this event will occur. The simulation model, upon completion of
processing an event, checks the set of events that will occur in the
future, referred to as the future event list, and finds the one with the
smallest clock value, say event 𝑖. Then, it advances the master clock
that gives the current time in the simulation, to the time when event 𝑖
will occur. It takes appropriate action as dictated by the type of the
event, and then repeats the process of finding the next event, and so
on. The simulation model, therefore, moves through time by simply
visiting the time instances at which events occur. In view of this it is
known as the event-advance design.

In the machine interference problem, described in section 1.3 of


Chapter 1, there are two types of events. That is, the event of an arri-
val at the repairman's queue, and the event of a departure from the
repairman's queue. These events are known as primary events. Quite
often the occurrence of a primary event may trigger off the creation
of a new event. For instance, the occurrence of an arrival at the re-
pairman's queue may trigger the creation of a departure event if this
63
Simulation Techniques Harry Perros
arrival occurs at a time when the repairman is idle. Such triggered
events are known as conditional events. The basic approach of the
event-based design is shown in the flow chart in figure 4.1.

Find next Future


event event
list

Advance
time

Take appropriate
action depending
on the type
of event

no Any
A conditional
events
?
yes
Create a new Future
event(s) event
list

Figure 4.1: The event-advance simulation design

4.3. Future event list


Let us assume that a simulation model is currently at time 𝑡. The set
of all the events scheduled to occur in the future, i.e., events with a
clock greater than 𝑡, is known as the future event list, or simply the
event list. For each event scheduled to occur in the future, the list
contains the following information:

64
Harry Perros Simulation Techniques
• Time of occurrence (i.e., value of the event's clock)
• Type of event

The event type is used in order to determine what action should be


taken when the event occurs. For instance, using the event type the
program can determine which procedure to call.

In the simulation examples described in Chapter 1, there were only a


few events. For instance, in the case of the machine interference
problem there were only two: an arrival to the repairman's queue and
a service-ending (departure) event. However, when simulating com-
plex systems, the number of events may be very large. In such cases,
finding the next event might require more than a few comparisons.
Naturally, it is important to have an efficient algorithm for finding the
next event, since this operation may well account for a large percent-
age of the total CPU time required to run a simulation program. The
efficiency of this algorithm depends on how the event list is stored in
a computer. An event list should be stored in such a way so as to lend
itself to an efficient execution of the following operations.

1. Locating the next future event with the smallest clock time.
2. Deleting an event from the list after it has occurred.
3. Inserting a newly scheduled event in the event list.
Below we examine two different schemes for storing an event list. In
the first scheme, the event list is stored in a sequential array, and in
the second scheme it is stored as a linked list.

!"# !"% !"& . . . !"'

Figure 4.2: Event list stored in a sequential array

4.4. Event list stored in a sequential array


In this scheme, all future event times are stored in a sequential array.
The simplest way to implement this, is to associate each event type
with a different integer number 𝑖. The clock associated with this
event is always stored in the 𝑖th location of the array. For instance, in
figure 4.2, clock 𝐶𝐿\ for event type 1 is kept in the first location of
65
Simulation Techniques Harry Perros
the array, clock 𝐶𝐿f for event type 2 is kept in the second position of
the array, and so on.

Finding the next event is reduced to the problem of locating the


smallest value in an array. The following simple algorithm can be
used to find the smallest value in an array 𝐴.

minIndex ¬ 1
minValue ¬ A(1)
For i ¬ 1,n
If minValue ≤ A(i)
continue
else
minValue ¬ A(i)
minIndex ¬ i

Variable 𝑚𝑖𝑛𝐼𝑛𝑑𝑒𝑥 will eventually contain the location of the array


with the smallest value. If 𝑚𝑖𝑛𝐼𝑛𝑑𝑒𝑥 = 𝑖, then the next event is of
type 𝑖 and it will occur at time 𝐴(𝑖).

An event is not deleted from the array after it has occurred. If an


event is not valid at a particular time, then its clock can be set to a
very large value so that the above algorithm will never select it. A
newly scheduled event 𝑖 is inserted in the list by simply updating its
clock given by 𝐴(𝑖).

The advantage of storing an event list in a sequential array is that in-


sertions of new events and deletions of executed events can be done
very easily, i.e., in constant time. The time it takes to find the smallest
number in the array depends on the length of the array. Locating the
smallest number in an array does not take much time if the array is
small. However, it becomes time consuming if the array is large. To
overcome these problems, one should store the event list in a linked
list, described below.

4.5. Event list stored in a linked list


A linked list stores each data element in a different part of the
memory. Therefore, we no longer need contiguous memory locations
66
Harry Perros Simulation Techniques
and data can be dynamically added at runtime. In order to access the
data elements in their correct order, we store along with a data ele-
ment the address of the next data element. This is a pointer that points
to the location where the next data element is stored. This pointer is
referred to as a link. A data element and the link is referred to as a
node. In general, a node may consist of a number of data elements
and links. Linked lists are drawn graphically as shown in figure 4.3.

. . . NULL
head
Figure 4.3: A linked list

Each node is represented by a box consisting of as many compart-


ments as the number of data elements and links stored in the node. In
the example in figure 4.3, each node consists of two compartments,
one for storing a data element and the other for storing the pointer to
the next node. The pointer called head points to the first node in the
list. If the linked list is empty, i.e., it contains no nodes, then head is
set to a special value called NULL indicating that it does not point to
any node and that the list is empty. The pointer of the last node is al-
ways set to NULL indicating that this is the last node in the linked list.
Due to the fact that two successive nodes are connected by a single
pointer, this data structure is known as a singly linked list.

A single linked list can be used to store an event list as shown in fig-
ure 4.4. Each node consists of two data elements, a clock CL showing
the future time of an event and a value i indicating the type of event.
The nodes are arranged in an ascending order of the clocks.

CL i CL j . . . CL n NULL
head

Figure 4.4: Event list stored as a linked list

We now proceed to explain how to implement the basic operations on


a linked list, that is, create a linked list, add a new node, and delet a
node, which are also the same operations that one needs to carry out
67
Simulation Techniques Harry Perros
on an event list. We shall use the C programming language syntax to
explain the implementation.

4.5.1. Defining a linked list


Before we procced, let us see how the data elements and pointers are
organized in a node. A simple node which saves event type, event
clock, and pointer to the next node can be represented as follows:

Struc node
{
Int type;
Float clock;
struct node* next;
}

The above structure is an example of a self-referential structure since


it contains a pointer to a structure of the same type as itself which
would be the next node. In addition to the data elements (i.e. type and
clock) shown above the structure can contain other data which may
be specific to each event.
sizeof(struct node) bytes

. . . . . . . . .
Memory

nodePtr

Figure 4.5: Allocating memory for a node using malloc().

A new node is dynamically allocated each time a new event is creat-


ed. Dynamic allocation of memory refers to reserving a number of
bytes in the computer’s memory and setting up a pointer to the first
memory location of the reserved space. In C, the system call mal-
loc() is used to return such a pointer. Thus, a node can be dynami-
cally allocated as follows:

68
Harry Perros Simulation Techniques
struct node* nodePtr = (struct node*) malloc
(sizeof (struct node));

The above call can be visualized as shown in figure 4.5.

We use the cast (struct node*) to explicitly tell the compiler that
the memory location returned by this call will be pointed to by a vari-
able which is a pointer to the node structure that we have defined. We
can modify the fields of the structure any time in the program as fol-
lows:

nodePtr->type = ARRIVAL;
nodePtr->next = NULL;

The linked list is initially empty and the head does not point to any
node. Thus, the head must be explicitly initialized to point to NULL
using the command:

struct node* head = NULL;

After inserting nodes, the list can be traversed using the head and the
pointer to next node in each member node of the list.

4.5.2. Creation of a new node


The following function creates a new node and returns a pointer.

struct node* createNode(int type, float clock)


{
struct node* nodePtr = (struct node*) mal-
loc (sizeof (struct node));
nodePtr->type = type;
nodePtr->clock = clock;
nodePtr->next = NULL;
return nodePtr;
}
In the above function we allocate memory for the node and assign
values to the node passed into the function as parameters. The last

69
Simulation Techniques Harry Perros
line returns a pointer to the created node. The function can be called
from the program as follows:

newNodePtr = createNode(ARRIVAL, MCL+5.5);

where ARRIVAL is one of the predefined event types and MCL is the
master clock. Thus, we are creating a new ARRIVAL event that will
occur 5.5 units of time in the future. This function can be visualized
as shown in figure 4.6.

type clock NULL

newNodePtr

Figure 4.6: Result of calling createNode()

4.5.3. Deletion of a node


When a node is deleted, the memory allocated to it must be returned
to the system. This is done with the system call free() as follows:

free(nodePtr);

After the above call, nodePtr points to NULL.

4.5.4. Inserting a node in a linked list


In order to insert a node into the linked list, the pointer to the head of
the list and the pointer to the new node must be provided. The node
can then be inserted into the list in an appropriate position such that
the nodes are ordered according to a specified field. For the event list,
this field is the event clock.

struct node* insertNode(struct node* head,


struct node* newNodePtr)
{
if (head == NULL)
{ //Case 1
return newNodePtr;
}
70
Harry Perros Simulation Techniques
else if (head->clock > newNodePtr->clock)
{ // Case 2
newNodePtr->next = head;
return newNodePtr;
}
else
{ // Case 3
struct node* prev = NULL;
struct node* curr = head;
while ((curr!=NULL) && (curr->clock <=
newNodePtr->clock))
{
prev = curr;
curr = curr->next;
}
prev->next = newNodePtr;
newNodePtr->next = curr;
return head;
}
}
The above function can be called from a program as follows:

newNodePtr = createNode(ARRIVAL, MCL+5.5);


head = insertNode(head, newNodePtr);

As commented in the code above there are three separate cases that
we must handle.
Case 1: head is NULL
In this case, head is currently pointing to NULL, which means that the
list is empty and the node being inserted will be the first element in
the list. Thus, after the call to insertNode(), head must point to
the node that is being inserted. This can be visualized as shown in
figures 4.7 and 4.8.

Case 2: head ® clock >newNodePtr ® clock


In the second case, head is not pointing to NULL and the clock value
71
Simulation Techniques Harry Perros
of head is greater than the clock value of the new node. Thus, the
new node must be inserted before the head and the pointer to the new
head node must be returned. This can be visualized as shown in fig-
ures 4.9 and 4.10.

NULL
head

ARR 8.93 NULL

newNodePtr

Figure 4.7: Head and newNodePtr before the call to insertNode()

ARR 8.93 NULL

head

Figure 4.8: head after the call to insertNode()

ARR 8.93 DEP 15.8 NULL

head

ARR 6.7 NULL

newNodePtr

Figure 4.9: head and newNodePtr before the call to insertNode()

ARR 8.93 DEP 15.8 NULL


head

ARR 6.7

Figure 4.10: head after the call to insertNode()

72
Harry Perros Simulation Techniques
Note that in the above two cases, the pointer to the head changes.
Thus, the pointer returned by insertNode()must be assigned to
head.

Case 3: Insertion between head and last node of the list


In the third case, head is not pointing to NULL and the clock value of
head is less than the clock value of the new node. Thus, the new node
must be inserted after the head. This can be visualized as shown in
figures 4.11 and 4.12.

ARR 8.93 DEP 15.8

head

ARR 10.7 NULL

newNodePtr

Figure 4.11: head and newNodePtr before the call to insertNode()

ARR 8.93 DEP 15.8 NULL


head

ARR 10.7

Figure 4.12: head after the call to insertNode()

4.5.5. Removing the first node of a linked list


A linked list storing an event list is always maintained in an ascend-
ing order of the clock value. Therefore, the next event to occur is al-
ways given by the first node of the linked list. This node has to be
removed after the event has been processed. This is done as follows:

struct node* removeNode(struct node* head,


struct node** nextRef)
{
*nextRef = head;
73
Simulation Techniques Harry Perros
if (head != NULL)
{
head = head->next;
}
(*nextRef)->next = NULL;
return head;
}

We note that the first parameter passed in removeNode()function is


the head of the linked list. The second parameter struct node**
nextRef is a pointer which is used to return the memory location of
the next node to the calling function.

After the node is removed, all that needs to be done is to return the
node that was pointed to by head and make head to point to the next
node. This is done as follows:

struct node* nextNodePtr = NULL;


head = removeNode(head,&nextNodePtr);

4.5.6. Time complexity


Let us consider the time complexity of the insert operation on a linked
list, which is the most time-consuming operation on an event list. In
order to insert a node in a linked list we have to traverse the linked
list and compare each node until we find the correct insertion posi-
tion. The maximum number of nodes compared in the worst case will
be the total number of nodes in the list. Thus, the complexity of the
insert operation is linear on 𝑛.

Searching a linked list might be time consuming if 𝑛 is very large. In


this case, one can employ better searching procedures. For example, a
simple solution is to maintain a pointer 𝐵 to a node which is in the
middle of the linked list. This node logically separates the list into
two sublists. By comparing clock value of the node to be inserted
with the clock stored in this node, we can easily establish in which
sublist the insertion is to take place. The actual insertion can then be
located by sequentially searching the nodes of the sublist.

74
Harry Perros Simulation Techniques
Other data structures can be used, and the interested reader can con-
sult a textbook on data structures.

4.5.7. Douby linked lists


So far, we have examined singly linked lists. The main disadvantage
of these lists is that they can be only traversed in one direction, name-
ly from the first node to the last one. Doubly linked lists allow trav-
ersing a linked list in both directions. This is possible because any
two successive nodes are linked with two pointers, as shown in figure
4.13. Depending upon the application, a doubly-linked list may be
more advantageous than a singly-linked list. A doubly-linked list can
be processed using functions similar to those described above for sin-
gly linked lists.

i j . . . k NULL
head

Figure 4.13: A doubly linked list

4.6. Unit-time advance design


In the event-advance simulation, the master clock is advanced from
event to event. Alternatively, the master clock can be advanced in
fixed increments of time, each increment being equal to one unit of
time. In view of the way of advancing the master clock, this simula-
tion design is known as the unit-time advance design. Each time the
master clock is advanced by a unit time, all future event clocks are
compared with the current value of the master clock. If any of these
clocks is equal to the current value of the master clock, then the asso-
ciated event has just occurred and appropriate action has to take
place. If no clock is equal to the current value of the master clock,
then no event has occurred and no action has to take place. In either
case, the master clock is again increased by unit time and the cycle is
repeated.

The unit-time advance design is depicted in figure 4.14. Let us say


that at time 𝑡 the simulation has just completed processing event 𝐸,

75
Simulation Techniques Harry Perros
E Ei Ej

t t+1 … t1 … t3

Figure 4.14: The unit-time advance design

Master clock
is increased
by a unit-time

Is
any future no
event clock A
= MC
?
yes
An event has
occurred

Take appropriate
action

Any
conditional no
event(s) A
?
yes
Schedule new
event (j)

Figure 4.15: The unit-time advance design

76
Harry Perros Simulation Techniques
and that the next event 𝐸R will occur at time 𝑡\ . We increase the mas-
ter clock by a unit time and compare it to all the event clocks. Since
none of the clocks is equal to the master clock, the master clock is
increased by another unit time, and so on, until after some iterations
the master clock becomes equal to 𝑡\ . Event 𝐸R is then processed, the
event list is modified accordingly, and then the master clock is in-
creased by one unit time repeatedly until it hits the next event, and so
on.

The basic approach of the unit-time design is summarized in the flow-


chart given in figure 4.15. In this flow-chart, as in the case of an
event-advance design, a future event clock gives the time instance in
the future that an event will occur. Alternatively, a future clock can
simply give the duration of a particular activity. For instance, in the
machine interference problem, the departure clock will contain the
duration of a service, rather than the future time at which the service
will be completed. In this case, the unit-time design can be modified
as follows. Each time the master clock is advanced by a unit of time,
the value of each future clock is decreased by a unit time. If a clock
becomes equal to zero, then the associated event has occurred and
appropriate action has to take place. Obviously, the way one defines
the future event clock does not affect the unit-time simulation design.
Stage 1 Stage 2

Queue 1 Server 1 Queue 2 Server 2

Figure 4.16: A single server queue

We demonstrate the unit-time advance design by showing how to


simulate a single server queue. In this simple queueing system, shown
in figure 4.16, customers arrive from outside seeking to receive ser-
vice at the server. A customer goes straight into service if it arrives at
a time when the server is idle. A customer is forced to wait, if upon
arrival it finds the server is busy or one or more customers waiting in
the queue. A customer departs from the queueing system when it
completes its service, and the first customer waiting at the queue
starts its service immediately. The server becomes idle if there is no
77
Simulation Techniques Harry Perros
customer waiting at the queue when it completes a service. This
queueing system is similar to the machine interference problem, with
the exception that the population of customers is infinite.

MCL = MCL + 1

>1 ST =1

ST = ST - 1 ST = ST - 1

End of service
Take appropriate
action

>1 AT =1

AT = AT - 1 AT = AT - 1

An arrival has
occurred. Take
appropriate action

Figure 4.17: A unit-time advance design of a single server queue

The simulation logic is shown in figure 4.17. Variable 𝐴𝑇 gives the


inter-arrival time between two successive arrivals, 𝑆𝑇 contains the
service time of the customer in service, and 𝑀𝐶𝐿 is the master clock.

We note that the variables 𝐴𝑇 and 𝑆𝑇 have been assumed to be inte-


gers, multiples of the unit time. However, in many simulations, activi-
ty durations, or equivalently future time instances, are real variables.
This can be easily accommodated in the unit-time design by assuming
that an event has occurred when its corresponding activity duration or
future clock, becomes negative. For instance, if at time 𝑀𝐶 = 𝑡 vari-
78
Harry Perros Simulation Techniques
able 𝐴𝑇 = 0.8, then at time 𝑀𝐶 = 𝑡 + 1, 𝐴𝑇 = −0.2. This means,
that the event has occurred between 𝑡 and 𝑡 + 1, and appropriate ac-
tion needs to be taken.

One minor issue that the reader needs to be aware of when using the
unit-time design is regarding the estimation of the duration of an en-
dogenously created variable. For instance, let us assume that in the
simulation of the single server queue, we want to estimate the resi-
dence time of a customer. This is the elapsed time from the time in-
stance the customer arrives at the single server queue to the time in-
stance that it departs. If we use clocks, then this elapsed time is the
difference between the departure clock and the arrival clock. If we
use activity durations which are integers, then we can use the corre-
sponding master clock values at which these two events occurred.
However, if the duration of the activities are real numbers, then the
best we can do is to use the master clock value before or after the
time the event occurred. For instance, if an arrival event occurs be-
tween 𝑡 and 𝑡 + 1, then we can use 𝑡. This of course introduces an
error in the estimation of the residence time which depends on the
length of the unit time.

4.6.1. Selecting a unit time


The unit time is readily obtained in the case where all future event
clocks are represented by integer variables. For, each event clock is
simply a multiple of the unit time. Several heuristic and analytic
methods have been proposed for choosing a unit time when the
clocks, or equivalently the duration of the activities, are real varia-
bles. Specifically, a unit time should be small enough so that at most
one event occurs during the period of a unit of time. However, if it is
too small, the simulation program will spend most of its time in non-
productive mode, i.e., advancing the master clock and checking
whether an event has occurred. A simple heuristic rule is to set the
unit time equal to one-half of the smallest stochastic variate generat-
ed. For this, we run the simulation and record all the generated dura-
tions of the activities. Then, we set the time unit to half the value of
the smallest duration.

79
Simulation Techniques Harry Perros
4.6.2. Implementation
The main operation related to the processing of the future event list is
to compare all the future event clocks against the master clock each
time the master clock is increased by a unit time. An implementation
using a sequential array as described in section 4.4 would suffice in
this case.

4.6.3. Event-advance vs. unit-time advance


The unit-time advance method is advantageous in cases where there
are many events which occur at times close to each other. In this case,
the next event can be selected fairly rapidly provided that an appro-
priate value for the unit time has been selected. The best case, in fact,
would occur when the events are about a unit time apart from each
other.

The worst case for the unit-time advance method is when there are
few events and they are far apart from each other. In this case, the
unit-time advance design will spend a lot of non-productive time ad-
vancing the master clock and checking if an event has occurred. In
such cases, the event-advance design is obviously preferable.

4.7. Activity-based simulation design


In the event-based simulation design, the system is viewed as a col-
lection of events whose occurrence trigger changes in the system. In
an activity-based simulation, the system is viewed as a collection of
activities or processes. For instance, for the single server queue, the
activities are: a) inter arriving, b) being served, and c) waiting for ser-
vice. These activities, as will be seen below, are manipulated using a
set of conditions that determine when they start and stop. This is not a
commonly used design, due the fact that the conditions that determine
when activities start and stop can be very complex. However, it works
extremely fast compared to an event-based design by several orders
of magnitude.

We demonstrate this design by setting up an activity-based simulation


model of the single server queue studied above in section 4.6. Let 𝑆𝑇R
and 𝑊𝑇R be the service time and the waiting time respectively of the

80
Harry Perros Simulation Techniques
𝑖th arrival. Also, let 𝐴𝑇R[\ be the interarrival time between the 𝑖th and
(𝑖 + 1)st arrival. Finally, we assume that the 𝑖th arrival occurs at time
𝑎R , starts its service at time 𝑠R , and ends its service at time 𝑠R + 𝑆𝑇R , as
shown in figure 4.18. Let us assume now that we know the waiting
time 𝑊𝑇R and the service time 𝑆𝑇R of the 𝑖th arrival. Then, one the
following three situations may occur.
ST i
WT i

AT
i+1

a si a i+1 s i + STi
i

ith ith arrival (i+1)st ith arrival


arrival begins its arrival ends its
service service

Figure 4.18: Time components related to the ith arrival.

1. The (𝑖 + 1)st arrival occurs during the time that the 𝑖th arrival
is waiting.
2. The (𝑖 + 1)st arrival occurs when the 𝑖th arrival is in service.
This is the case shown in figure 4.18.
3. The (𝑖 + 1)st arrival occurs after the 𝑖th arrival has departed
from the server.

For each of these three cases, the waiting time 𝑊𝑇R[\ of the (𝑖 + 1)st
arrival can be easily determined as follows:

1. 𝑊𝑇R[\ = (𝑊𝑇R −𝐴𝑇R[\ ) +𝑆𝑇R

= (𝑊𝑇R +𝑆𝑇R )−𝐴𝑇R[\

= 𝑇𝑊R − 𝐴𝑇R[\ ,

where 𝑇𝑊R is the total waiting time of the 𝑖th customer in the
system, i.e., queueing and being served.

2. 𝑊𝑇R[\ =(𝑊𝑇R + 𝑆𝑇R )−𝐴𝑇R[\


81
Simulation Techniques Harry Perros
= 𝑇𝑊R − 𝐴𝑇R[\

We observe that in both cases 1 and 2, the waiting time of the


(𝑖 + 1)st arrival 𝑊𝑇R[\ is equal to 𝑇𝑊R − 𝐴𝑇R[\ .

3. 𝑊𝑇R[\ = 0

Having calculated 𝑊𝑇R[\ , the total waiting time 𝑇𝑊R[\ in the system
of the (𝑖 + 1)st arrival is calculated by generating a service time
𝑆𝑇R[\ . We have: 𝑇𝑊R[\ = 𝑊𝑇R[\ + 𝑆𝑇R[\ . For the next arrival 𝑖 + 2,
generate an inter-arrival time 𝐴𝑇R[f , and then calculate 𝑊𝑇R[f and
𝑇𝑊R[\ as above, and so on. The basic mechanism of this activity-
based simulation model is shown in figure 4.19.

Initial Empty system


conditions WT = ST = 0, TW = 0

Generate
AT

Next arrival occurs TW > AT Compare TW ≤ AT Next arrival


when current arrival TW with occurs when
is either waiting or server is idle
AT
is in service

Generate
Generate ST
ST

TW = ST
TW = ST + (TW - AT)

A
A

Figure 4.19: An activity-based simulation design for a single server queue

One of the endogenously parameters of interest when we simulate a


single server queue, is the waiting time of a customer before it starts
82
Harry Perros Simulation Techniques
receiving service. As can be seen, this is estimated without having to
manipulate events and without the use of a master clock.

4.8. Examples
In this section, we highlight the event-based simulation designs
through two examples. The first one, deals with an inventory system,
and the second one with the round-robin queue.

4.8.1. An inventory system


In an inventory system, one is mainly concerned with making deci-
sions in order to minimize the total cost of the operation. These deci-
sions are mainly related to how much inventory to order (or produce)
and the frequency of ordering (or production runs). The total cost of
running an inventory system consists of different types of costs. Here,
we will consider the following three costs: a) holding cost, b) setup
cost, and c) shortage cost. The holding cost is related to the cost of
keeping one item of inventory over a unit time. One of the most im-
portant components of this cost is that of the invested capital. The
setup cost is related to the cost in placing a new order or setup for a
production run. Finally, the shortage cost is associated with the cost
of not having available a unit of inventory when demanded. This cost
may be in the form of transportation charges, i.e., expediting deliver-
ies, increased overtime, and loss of future business.

Let 𝐼Ç be the inventory at time 𝑡, 𝑆 be the quantity added in the system


between time 𝑡 and 𝑡´, and 𝐷 be the demand between these two time
instances. Then, the inventory at time 𝑡´ is: 𝐼Ç´ = 𝐼Ç + 𝑆 − 𝐷. If 𝐼Ç´ is
below the re-ordering level, then an order is placed. The time it takes
for the order to arrive is known as the lead time. We assume that the
daily demand and the lead time follow known arbitrary distributions.
The inventory level is checked at the end of each day. If it is less than
or equal to the re-ordering level, an order is placed. The lead time for
the order begins to count from the following day. Orders arrive in the
morning before the business day begins and they can be disposed of
during the same day. During stockout days, orders are backlogged.
They are satisfied on the day the order arrives.

83
Simulation Techniques Harry Perros
The fluctuation in the inventory level is shown in figure 4.20. At time
𝑡\ an order is placed, which arrives at time 𝑡f . The new order is added
to the existing inventory. At time 𝑡e a new order is placed, which ar-
rives at time 𝑡{ . We see that at time 𝑡{ , the inventory has become neg-
ative, meaning that there are backorders. When the new order arrives,
the backorders are satisfied immediately. We observe that the two
lead times (𝑡f − 𝑡\ ) and (𝑡{ − 𝑡e ) are not the same. Depending on the
application, lead times maybe or may not be constant.

Stock
level

Reorder
point

t t t t
1 2 3 4
time

Figure 4.20: An inventory system

The simulation model is described in the flow charts given in figures


4.21 and 4.22. The model estimates the total cost of the inventory sys-
tem for specific values of the reordering point and the quantity or-
dered. The model keeps track of 𝐼Ç on a daily basis. In view of this,
the model was developed using the unit-time advance design. We
note that this design arises naturally in this case. The unit of time is
simply equal to one day. The lead time is expressed in the same unit
time. The basic input parameters to the simulation model are the fol-
lowing. a) 𝑅𝑂𝑃, reordering point, b) 𝑄, quantity ordered, c) 𝐵𝐼, the
beginning inventory, d) probability distributions for variables 𝐷 and
𝐿𝑇 representing the daily demand and lead time, respectively, e) 𝑇,
the total simulation time, and f) 𝐶1, 𝐶2, 𝐶3, representing the holding
cost per unit, the setup cost per order, and the shortage cost per unit,
respectively. The output parameters are 𝑇𝐶1, 𝑇𝐶2, 𝑇𝐶3 representing
the total holding, setup and shortage costs respectively.

84
Harry Perros Simulation Techniques

Initialization

Generate
MC t=t+1
AT

Print
t>T results
yes
no
End
>1
LT =1 order has
arrived
=0
LT = 0
LT = LT - 1 I=I+Q
no order
outstanding

Generate
daily demand D

I=I-D

Figure 4.21: A unit-time simulation design of an inventory system

4.8.2. A round-robin queue


We consider a client-server system where a number of clients send
requests to a server. A client is a user-operated software that runs on a
laptop or an app that runs on a mobile device. The user’s device is
connected to the server over the Internet. Each time a user sends a
request, the client software gets blocked, that is, the user cannot do
any more processing nor can send another request. The client remains
blocked until it gets an answer back from the server. This type of re-
quest, whereby a client get blocked until the request is executed, is
referred to as a synchronous request. The requests are executed by the
85
Simulation Techniques Harry Perros
server’s CPU in a round-robin fashion, as will be explained below.
For simplicity, we do not consider transmission delays between a us-
er’s device and the server. Also, the server is simply modelled by a
CPU, that processes the requests, and a queue in front of it where re-
quests wait for their turn to be executed.

no
I < ROP TC1 = TC1 + I*C1
?

yes
a
no
LT = 0

yes

Generate
LT

TC2 = TC2+ C2

no
I<0 TC1 = TC1 + I*C1
yes
a
TC3 = TC3 + I*C3

Figure 4.22: A unit time simulation design of an inventory system

We assume a finite number of users, and each user has always work
to do. That is, a user never becomes idle. A user spends some time
thinking (i.e., typing a line or thinking what to do next), upon comple-
tion of which a request is created which is then executed by the CPU.
86
Harry Perros Simulation Techniques
Since a user never becomes idle, the user’s state continuously cycles
through a think state and a CPU state, as shown in figure 4.23.

...
think CPU think CPU think CPU
state state state state state state

Figure 4.23: Cycling through a think time and a CPU time

The requests are executed by the CPU in a round robin manner. That
is, each request is allowed to use the CPU for a small quantum of
time. If the request is done at the end of this quantum or during the
quantum (not considered in this simulation model), then it departs
from the CPU. Otherwise, it is simply placed at the end of the CPU
queue. In this manner, each request in the CPU queue gets a chance to
use the CPU. Furthermore, short requests get done faster than long
ones. A request that leaves the CPU simply goes back to the originat-
ing user. At that instance the user goes into a think state. The think
time is typically significantly longer than the duration of a quantum.
For instance, the mean think time could be 30 seconds, whereas a
quantum could be less than 1 msec. A request, in general, would re-
quire many CPU quanta. If a request requires 5 seconds of CPU time,
and a quantum is 1 msec, then it would cycle through the CPU queue
5000 times!

Terminals
Users
Requests



• CPU

Figure 4.24: A round-robin queue

87
Simulation Techniques Harry Perros
We model this client-server scheme using the queueing system shown
in figure 4.24. The reader will realize that this is the same queueing
system as the machine interference problem, described in section 1.3.
In this case, the users are the machines and the CPU is the repairman.
A user in the think state is like a machine being operational. The main
difference from the machine interference problem is that the CPU
queue is served in a round robin fashion rather than in a FIF0 manner.

The events associated with this system are: a) arrival of a request at


the CPU queue, and b) service completion at the CPU. A request ar-
riving at the CPU queue may be either a new arrival (i.e., a user sends
a request, thus ending the think state), or it may be a request that has
just received a quantum of CPU time and it requires further pro-
cessing. A departing request from the CPU may either go back to the
originating user, or it may simply join the end of the CPU queue for
further execution. We observe that during the period of time that the
CPU is busy, departure events occur every quantum of time. Howev-
er, new arrivals of requests at the CPU queue occur at time instances
which may be a few hundreds of quanta apart.

An event-advance simulation model can be developed following the


same approach as in the machine interference problem. The future
event list may contain an event associated with a departure from the
CPU and a number of events are associated with future new arrivals
of requests. For instance, let us assume that the total number of users
is 10, and that at some arbitrary time there are seven users in the think
state, which means that there are three requests being processed by
the CPU. In this case, the event list consists of a departure from the
CPU event, and seven events each associated with a future arrival of a
request from a user in the think state. When a departure occurs, most
likely a new departure event will be scheduled to occur in the next
quantum of time. This new event will more likely be the next event to
occur. In view of this, most of the time, a newly created event will be
simply inserted at the top of the future event list. Such insertions can
be done in 0(1) time.

We now give an alternative simulation design of the round-robin


queue which utilizes both the event-advance and unit-time advance
designs! Specifically, all the events related to new arrivals of requests
88
Harry Perros Simulation Techniques
at the CPU queue are kept in a linked list as shown in figure 4.25.
Each node is associated with an event 𝑖 and it contains a future event
time 𝑡R and a client identification number 𝑐R . The event time simply
shows the time at which the user will stop thinking and will access
the CPU. Nodes are ordered in an ascending order of the data element
that contains the future event time. Thus, the next new arrival event is
given by the first node of the linked list.

!1 #1 !2 #2 . . . !1 #1 NULL
head
Figure 4.25: Future event list of all new arrivals to the CPU queue

All the information regarding the requests in the CPU queue, includ-
ing the one in service, is maintained in the separate linked shown in
figure 4.26. This linked list is known as a circular singly linked list,
since it is singly linked and the last node is linked to the first node.
Each node contains the number of quanta required by a request and its
terminal identification number. Pointers H and E point to the begin-
ning and end of the list respectively. The nodes are ordered in the way
that the requests are waiting in the CPU queue. Thus, the first node
represents the request currently in service, and the last node the re-
quest currently at the end of the CPU queue. When a new request ar-
rives at the CPU queue, a new node is created and it is attached after
the node pointed by E. If a request requires further service upon com-
pletion of its quantum, then its node is simply placed at the end of the
linked list. This is achieved by simply changing H to point to the next
node in the linked list, i.e., H¬LINK(H), and setting E to H. If a re-
quest does not require further service upon completion of its quan-
tum, then its node is removed and H¬LINK(H).

H !1 #1 !2 #2 . . . !% #% E

Figure 4.26: Future event list of all new arrivals to the CPU queue
89
Simulation Techniques Harry Perros
The simulation model operates under the unit-time advance design
during the period of time that the CPU is busy, with a unit time equal
to one quantum. During the time that CPU is idle, the simulation
model switches to an event-advance design. This hybrid design is
summarized in figure 4.27. Note that 𝑡𝑎𝑟𝑟 gives the time of the next
new arrival at the CPU queue. The remaining details of this program
are left to the reader as an exercise.

Is
yes CPU no
MCL = tarr MCL = MCL + 1
queue
empty
?
A new arrival A departure
event occurs event occurs

Is no
b MCL=tarr b
?

yes
A new arrival
event occurs

Figure 4.27: Hybrid simulation design of the round-robin queue

Problems
Consider the following systems:

a. Checkout stands at a supermarket


b. Teller's window at a bank
c. Elevators serving an office building
d. Traffic lights in a configuration of 8 city blocks
e. Outpatient clinic
f. Pumps at a gasoline station
90
Harry Perros Simulation Techniques
g. Parking lot
h. Runways at an airport

Choose any of the above systems. First, describe how the system op-
erates. (Make your own assumptions whenever necessary. Make sure
that these assumptions do not render the system trivial!) Then, set up
a simulation model to represent the operations of the system. State
clearly which are the state variables and what are the events. For each
caused event, state clearly what action the simulation model will take.

Computer projects
The objective of the projects below is to experiment linked data struc-
tures and other enhancements. These projects can be skipped without
affecting the process of the development of your simulation model.

1. Implement the hybrid simulation model of the round-robin queue


discussed in section 4.6.2.

2. The machine interference problem. Modify your simulation model


so that the event list is maintained in the form of a linked list. As-
sume that the queue of broken-down machines can be repaired by
more than one server (i.e., there are more than one repairman re-
pairing machines off the same queue). Parametrize your program
so that it can run for any number of machines and repairmen
(maximum 20 and 10, respectively). Run your simulation model
until 20 repairs have been completed. As before, each time an
event occurs, print out the usual line of output and also additional
information pertaining to the event list. Check by hand that your
linked list implementation is correct.

3. The token-based access scheme problem. Assume that transmis-


sions are not error-free. That is, when a packet arrives at the des-
tination node, it may contain errors. In this case, the packet will
have to be re-transmitted. The procedure is as follows.

Upon completion of the transmission of a packet, the host will


wait to hear from the receiving host whether the packet has been
received correctly or not. The time for the receiver to notify the
sender may be assumed to be constant. If the packet has been cor-
91
Simulation Techniques Harry Perros
rectly received, the host will proceed with the next transmission.
If the packet has been erroneously transmitted, the sender will re-
transmit the packet. There is 0.01 probability that the packet has
been transmitted erroneously. The sender will re-transmit the
packet immediately. This procedure will be repeated until the
packet is correctly transmitted. No more than 5 re-transmissions
will be attempted. After the 5th re-transmission, the packet will be
discarded, and the sender will proceed to transmit another packet.
All these re-transmissions take place while the host has the token.
When the token's time-out occurs, the host will carry on re-
transmitting until either the packet is transmitted correctly, or the
packet is discarded.

Describe how you will modify your simulation model in order to


accommodate the above acknowledgement scheme. Can this re-
transmission scheme be accommodated without introducing more
events? If yes, how? If no, what additional events need to be in-
troduced? Describe what action will be taken each time one of
these additional events takes place. Also, describe how these new
events will interact with the existing events (i.e., triggering-off
each other).

4. The token-based access scheme problem. Modify the simulation


design in order to take advantage of the structure of the system.
Specifically, do not generate arrivals to each of the nodes. Store
the residual inter-arrival time when the node surrenders the token.
Then, when the token comes back to the station, generate arrivals
(starting from where you stopped last time) until the token times-
out or it is surrendered by the node. This change leads to a con-
siderably simpler simulation model.

5. Two-stage manufacturing model. Modify your simulation model


so that the event list is maintained in the form of a linked list. Run
your simulation model until 20 customers have completed their
service at queue 2 for different values of the capacity of queue 2.

92
Harry Perros Simulation Techniques
Chapter 5:
Estimation techniques for analyzing
endogenously created data
5.1. Introduction
So far, we have examined techniques for building a simulation model.
These techniques were centered around the topics of random number
generation and simulation design. The reason why we develop a sim-
ulation model is because we need to estimate various performance
measures of a system under study. These measures are obtained by
collecting and analyzing endogenously created data. In this Chapter,
we will examine various estimation techniques that are commonly
used in simulation. Before we proceed, we discuss briefly how one
can collect data generated by a simulation program.

5.2. Collecting endogenously created data


The performance metrics that we typically want to estimate in a simu-
lation are a) frequency of occurrence of a particular event, and b) du-
ration of an activity. For instance, in a simulation of a parking lot, we
may want to know the probability 𝑝 of a car not finding a place to
park upon arrival at the parking lot. This can be easily calculated by
keeping track of the following two variables: 𝑋 for counting the num-
ber of simulated cars that arrived at the parking lot to find it full, and
𝑌 for counting the total number of arrivals at the parking lot. At the
end of the simulation, we compute 𝑝 as the ratio of these two varia-
bles, that is, 𝑝 = 𝑋/𝑌.

An example of a metric expressed as the duration of an activity, is the


down time of a machine in the machine interference problem. This is
the time the machine spends queueing up waiting for the repairman
plus its repair time. The down time of a machine can be obtained by
keeping the following information: a) time of arrival at the repair-
man's queue, and b) time at which the repair was completed. At the
end of the simulation, the downtime for each machine that broke
down is computed by taking the difference of its departure time mi-
nus its arrival time. Various statistics of the down time can be com-

93
Simulation Techniques Harry Perros
puted using this sample, such as, the mean, the standard deviation,
and percentiles.

H !" # !$ % . . . !& ' E

Figure 5.1: The linked list for the repairman's queue

The information on the arrival and departure times of the broken-


down machines can be collected by maintaining a linked list that rep-
resents the machines which are in the repairman's queue. This linked
list is shown in figure 5.1. Each node contains the following two data
elements: a) time of arrival at the repairman's queue 𝑡R , and b) id
number of the machine. The nodes are linked in FIFO manner so that
to represent the way in which the machines are served. That is, the
first node, pointed by H, represents the machine currently in service.
If a machine arrives at the repairman's queue, a new node will be ap-
pended after the last node pointed by E. The total down time of a ma-
chine is calculated at the instance when the machine departs from the
repairman. This is equal to the master clock's value at that instance
minus its arrival time. This information is saved in a one-dimensional
array, and at the end of the simulation it can be processed in order to
obtain various statistics.

An alternative and simpler method to using a linked list is to use a


two-dimensional array. Let us consider the simulation model of the
token-based access scheme. The simulation program can be enhanced
so that each node is associated with a two-dimensional array, as
shown in figure 5.2. The first column contains the arrival times of
packets, and the second column contains their departure time. For
node 𝑖, the locations between pointers Hi and Ei give the arrival
times of the packets currently in the queue, including the one in ser-
vice. The locations before Hi contain the arrival and departures times
of packets that already have been through the queue. When a packet
arrives at queue 𝑖, its arrival time is stored at the location after the one
pointed by Ei. When a packet departs from the node, its departure
time is stored in the corresponding location of the second column.

94
Harry Perros Simulation Techniques
Node 1 Node 2 Node 3
Arr Dep Arr Dep Arr Dep
x x x x x x
x x x x x x
x x x x H3 x
x x H2 x E3 x
H1 x E2 x
x
E1 x

Figure 5.2: Data structure for the token-based access scheme simulation

Instead of keeping two columns per node, one can keep one column.
When a packet arrives, its arrival time is stored in the next available
location. Upon departure of the packet, its arrival time is substituted
by its total time in the queue.

Another statistic of interest is the probability distribution of the num-


ber of broken-down machines. In this case, the maximum number of
broken-down machines does not exceed 𝑀, the total number of ma-
chines. In view of this, it suffices to maintain an array with 𝑀 + 1
locations. Location 𝑖 contains the total time during which the number
of broken-down machines (waiting and being served) was 𝑖, 𝑖 =
0,1,2, … , 𝑀. This is computed as follows. Let us assume that at time
𝑡\ a machine breaks down and joins the repairman’s queue which
brings the total number of broken-down machines to, say, 2. Later on
at time 𝑡f another machine breaks down which brings the number of
broken-down machines to 3. The time 𝑡f − 𝑡\ is a time period during
which the number of broken-down machines is 2. This time is saved
cumulatively in location 2. Likewise, if instead of an arrival, we have
a departure at time 𝑡f , the time period 𝑡f − 𝑡\ is also saved cummula-
tively in location 𝑖. At the end of the simulation, this location will
contain the total amount of time during which the number of broken-
down machines was 2. Likewise, for the other locations in the array.
The probability 𝑝(𝑛) that there are 𝑛 broken-down machines is then

95
Simulation Techniques Harry Perros
obtained by dividing the contents of the 𝑛th location by 𝑇, the total
simulation time.

5.3. Transient state vs steady-state simulation


In general, a simulation model can be used to estimate a parameter of
interest during the transient state or the steady state. Let us assume
that in the machine interference problem we are interested in obtain-
ing statistics pertaining to the number of broken-down machines. The
simulation starts by assuming that the system at time zero is at a giv-
en state. This is known as the initial condition. Evidently, the behav-
ior of the system will be affected by the particular initial condition.
However, if we let the simulation run for a long period, its statistical
behavior will eventually become independent of the particular initial
condition. In general, the initial condition will affect the behavior of
the system for an initial period of time 𝑇. Thereafter, the simulation
will behave statistically in the same way whatever the initial condi-
tion. During this initial period 𝑇, the simulated system is said to be in
the transient state. After 𝑇, the simulated system is said to be in the
steady state.

5.3.1. Transient-state simulation


Transient-state simulation is used when we are interested in studying
how the system behaves for a specific initial starting condition. Also,
there are cases where the system does not have a steady state, and
therefore one has to resort to transient-state simulation. Such a case
may arise when the system under study is constantly changing.

5.3.2. Steady-state simulation


In general, transient state simulation is not very common. Typically, a
simulation model is used to study the steady-state behavior of a sys-
tem, and for this, the simulation model has to run long enough so that
to get away from the transient state which is influenced by the initial
conditions. There are two basic strategies for choosing the initial con-
ditions. The first strategy is to begin with an empty system. That is,
we assume that there are no activities going on in the system at the
beginning of the simulation. The second strategy is to make the initial
condition to be as representative as possible of the typical states the

96
Harry Perros Simulation Techniques
system might find itself in. This reduces the duration of the transient
period. However, in order to set the initial conditions properly, an a
priori knowledge of the system is required. The easiest solution is to
assume an empty system.

One should be careful about the effects of the transient period when
collecting endogenously created data. For, the data created during the
transient period are dependent on the initial condition. Two methods
are commonly used to remove the effects of the transient period. The
first one requires a very long simulation run, so that the amount of
data collected during the transient period is insignificant relative to
the amount of data collected during the steady state period. The sec-
ond method, which is preferable, simply requires that no data collec-
tion is carried out during the transient period. This can be easily im-
plemented as follows. Run the simulation model until it reaches its
steady state, and then set to zero all the statistical counts, while leav-
ing the rest of the variables in the simulation unchanged. Continue to
simulate until a sufficient number of observations have been obtained.

This brings us to the problem of how to determine when the simula-


tion system has reached its steady state. There are various methods to
do that, but in practice, we just discard the first few hundreds of the
collected data. If in doubt, run your simulation multiple times, and
each time increase the transient period. Select the transient period
past which no change in the performance metrics collected during the
steady state is observed.

5.4. Estimation techniques for steady-state simulation


Most of the performance measures that one would like to estimate
through simulation are related to the probability distribution of an en-
dogenously created random variable. The most commonly computed
measure of an endogenously created random variable is the mean. For
instance, in the machine interference problem we are interested in the
down time, and for this, we estimate the mean down time. However,
the mean does not provide a sense of the upper bound of the down
time. For this, we need to calculate a percentile which gives us a
probabilistic upper bound, that is, an upper bound that holds for most
of the down times but not all of them. Typical percentiles used are the

97
Simulation Techniques Harry Perros
90th, 95th, and 99th percentile. For instance, the 95th percentile of
the down time is the down time such that only 5% of all down times
are greater than it. Percentiles are easy to compute and they are used
in service level agreements (SLAs)

Let us assume that we estimate that the mean and the 95th percentile
of the down time is 10 and 40 hours respectively. We see that down
times can be a lot larger than 10, which is something that cannot be
determined by simply looking at the mean. If the 95th percentile of
40 hours is not acceptable to the management, then more repairmen
can be added so that to bring it (and the mean too) down. Obviously,
a 95th percentile of 20 hours is much preferable since it guarantees a
faster turnaround on the repair times. The simulation model can be
used to do what-if analysis in order to find the required number of
repairmen so that the 95th percentile is below a given threshold.

In general, it is a good practice to estimate the mean and a percentile


of an endogenously created random variable. Below, we describe var-
ious estimation procedures. We assume that the observations have
been collected when the simulation is in steady state.

5.5. Estimation of the confidence interval of the mean


Let us assume that we have collected a sample of n endogenous data
for a performance metric, such as the down time in the machine inter-
ference problem, when the simulation is in steady state. Let
𝑥\ , 𝑥f , … , 𝑥l be the n observations. Then, the sample mean is:

1 l
𝑥̅ = ž 𝑥R .
𝑛 R¢\

Typically, when we estimate the mean of a performance metric, we


also provide an error of the estimation. For instance, in opinion polls,
we typically hear that 𝑥% of the people prefer candidate 𝑋 and that
the error is 3%. This means that the true percentage of the population
who prefer candidate 𝑋 is within 𝑥 ± 3%. This interval is known as
the confidence interval, because the true population percentage lies in
this interval with some probability, such as 95%. This probability is

98
Harry Perros Simulation Techniques
known as the confidence level. The confidence interval of the sample
mean 𝑥̅ is given by the expression:
𝜎 𝜎
Û𝑥̅ − 1.96 , 𝑥̅ + 1.96 Ý
√𝑛 √𝑛

at 95% confidence level. The confidence interval provides an indica-


tion of the error associated with the sample mean. It is a very useful
statistic and it should always be computed. Unfortunately, quite fre-
quently it is ignored. The confidence interval tells us that the true
population mean lies within the interval 95% of the time. That is, if
we repeat the above experiment 100 times, 95% of these times, on
the average, the true population mean will be within the interval.

The derivation of the confidence interval is very simple and it is


based on the Central Limit theorem. This theorem says that if a ran-
dom variable 𝑋 is the sum of 𝑛 identical and independent random var-
iables which may not necessarily be normally distributed, then 𝑋 fol-
lows the normal distribution as 𝑛 increases. Therefore, if we sample 𝑛
observations from a population which follows an arbitrary distribu-
tion with a mean 𝜇 and variance 𝜎 f , then the sample mean 𝑥̅ is nor-
mally distributed with mean 𝜇 and variance 𝜎 f / 𝑛, for 𝑛 ≥ 30.

Now, let us fix points 𝑎 and 𝑏 in this distribution so that 95% of the
observations (that is, of the sample means 𝑥̅ ) fall in-between the two
points. Points 𝑎 and 𝑏 are symmetrical around 𝜇, and the two tails
(−∞, 𝑎) and (𝑏, +∞) account for 5% of the total distribution, as
shown in figure 5.3. Using the 𝑧-transform we have that 𝑎 is 1.96
standard deviations below 𝜇, i.e., 𝑎 = 𝜇 − 1.96𝜎/√𝑛, and 𝑏 is 1.96
standard deviations above 𝜇, i.e., 𝑏 = 𝜇 + 1.96𝜎/√𝑛. Now, if we
consider an arbitrary observation 𝑥̅ , then this observation will lie in
the interval [𝑎, 𝑏] 95% of the time. That is, its distance from 𝜇 will be
less than 1.96𝜎/√𝑛 95% of the time. Or, otherwise stated, 95% of
the time 𝜇 will be less than 1.96𝜎/√𝑛 from 𝑥̅ . That is, 𝜇 will lie in
the confidence interval Ÿ𝑥̅ − 1.96𝜎/√𝑛, 𝑥̅ + 1.96𝜎/√𝑛 95% of the
time.

99
Simulation Techniques
0.25
Harry Perros
0.2

fX(t)
0.15

fX(t)
0.1

0.05

0 2 4 6
a 8 10

µ
12
b 14 16 18 20

2.5% 95% 2.5%

Figure 5.3: Points a and b in the normal distribution

In general, a confidence interval can be calculated for any confidence


level. Most typical confidence levels are 99%, 95% and 90%. For
each value, points 𝑎 and 𝑏 can be calculated from the table of the
standard normal distribution. The 𝑧 value at 90% and 99% confi-
dence is 1.645 and 2.56 respectively. We observe that a small confi-
dence interval comes at the expense of having lower confidence about
the result!

In order to construct a confidence interval of the sample mean 𝑥̅ , we


need to know the variance 𝜎 f . If it is not known, then we use the
sample variance 𝑠 f given by the familiar expression:
l
f
1
𝑠 = ž(𝑥R − 𝑥̅ )f .
𝑛−1
R¢\

Note that dividing the above summation ∑lR¢\(𝑥R − 𝑥̅ )f by 𝑛 − 1


gives an unbiased estimator of 𝜎 f , whereas dividing it by 𝑛 gives the
maximum likelihood estimator of 𝜎 f . Either denominators can be
used.

The above expression for 𝑠 f is correct when the observations are in-
dependent of each other. To compute the above expression, we need
to store all the observations first and then at the end of the simulation
compute the sample variance. The following equivalent expression

100
Harry Perros Simulation Techniques
allows the calculation on the fly without having to store all the obser-
vations.
f
1 l 1 l
𝑠 f = Éž 𝑥Rf − Ûž 𝑥R Ý Ê.
𝑛−1 R¢\ 𝑛 R¢\

That is, when the 𝑘th observation is obtained, we update the cumula-
tive sums ∑kR¢\ 𝑥\f and ∑kR¢\ 𝑥R , and total count of observations. At the
end of the simulation, the variance can be easily computed using the
above expression.

In the case where the standard deviation 𝜎 is not known and has to be
estimated from the sample data, the confidence interval is computed
using the 𝑡-student distribution with 𝑛 − 1 degrees of freedom. That
is:
𝑠 𝑠
Û𝑥̅ − 𝑡li\ , 𝑥̅ + 𝑡li\ Ý.
√𝑛 √𝑛
The 𝑡-student distribution is symmetric and bell-shaped, like
the normal distribution, but has heavier tails, meaning that it is more
prone to producing values that fall far from its mean. It is commonly
used instead of the 𝑧 value, which is calculated from the normal dis-
tribution, since the standard deviation 𝜎 is typically not known. The
𝑡-student distribution is also used if the sample size is small, less than
30. As the degrees of freedom increase, the 𝑡-distrubtion tends to the
normal distribution. It is calculated for different confidence level in
the same way as the 𝑧 values.

Quite frequently, the observations 𝑥\ , 𝑥f , … , 𝑥l that we obtain endog-


enously from a simulation model are correlated. For instance, the
down time of a machine 𝑖 depends on the down time of the machine 𝑗
in front of it in the repairman's queue. If the down time of 𝑗 is long,
then the down time of 𝑖 is also long. Likewise, if the down time of 𝑗 is
short, then the down time of 𝑖 is also short. In general, two random
variables are uncorrelated, or positively correlated or negatively cor-
related, as shown in figure 5.4.

101
Simulation Techniques Harry Perros

a) Uncorrelated, i.e., 𝒓𝑿𝒀 = 0

b) Positively correlated, i.e., 𝒓𝑿𝒀 > 0

c) Negatively correlated, i.e., 𝒓𝑿𝒀 < 0

Figure 5.4: The three cases of correlation

102
Harry Perros Simulation Techniques
In the presence of correlated observations, the above expression for
the variance does not hold. The expression for the mean holds for cor-
related and uncorrelated observations. The correct procedure, there-
fore, for obtaining the confidence interval of the sample mean is to
first check if the observations are correlated. If they are not, one can
proceed as described above. If the observations are correlated, then
one has to use a special procedure to get around this problem. Below,
we discuss the following four procedures for estimating the variance
of correlated observations:

a. Estimation of the autocorrelation function


b. Batch means
c. Replications

5.5.1. Estimation of the autocorrelation function (ACF)


Let 𝑋 and 𝑌 be two random variables. Then, covariance of X and Y
𝐶𝑜𝑣(𝑋, 𝑌) is given by the expression:

𝐶𝑜𝑣(𝑋, 𝑌) = 𝐸(𝑋𝑌) − 𝐸(𝑋)𝐸(𝑌)].

The covariance takes values in the space (−∞, +∞), and it is not di-
mensionless, which may make its interpretation difficult. In view of
this, we use the correlation 𝐶𝑜𝑟𝑟(𝑋, 𝑌), which is the normalized
𝐶𝑜𝑣(𝑋, 𝑌). This is a dimensionless metric, defined as follows:
𝐶𝑜𝑣(𝑋, 𝑌)
𝐶𝑜𝑟𝑟(𝑋, 𝑌) = .
Ï𝑉𝑎𝑟(𝑋)Ï𝑉𝑎𝑟(𝑌)

Let (𝑥\ , 𝑦\ ), (𝑥f , 𝑦f ), … , (𝑥l , 𝑦l ) be 𝑛 pairs of observations of (𝑋, 𝑌).


Then, the sample covariance 𝜌Ôð is given by the expression:

∑lR¢\(𝑥R − 𝑥̅ )(𝑦R − 𝑦µ)


𝜌Ôð = ,
𝑛−1
and the sample correlation 𝑟Ôð , obtained by normalizing the sample
covariance, is:

103
Simulation Techniques Harry Perros
∑lR¢\(𝑥R − 𝑥̅ )(𝑦R − 𝑦µ)
𝑟Ôð = .
Ï∑lñ¢\(𝑥R − 𝑥̅ )f Ï∑lñ¢\(𝑦R − 𝑦µ)f

It can be shown that −1 ≤ 𝑟Ôð ≤ 1. 𝑋 and 𝑌 are positively correlated


if 𝑟Ôð > 0, and they are strongly positively correlated if 𝑟Ôð = 1. 𝑋
and 𝑌 are negatively correlated if 𝑟Ôð < 0, and they are strongly
negatively correlated if 𝑟Ôð = −1. Finally, they are uncorrelated, i.e.,
independent from each other if 𝑟Ôð = 0.

Now, let us now consider 𝑛 observations 𝑥\ , 𝑥f , … , 𝑥l from a random


variable 𝑋. We form the following 𝑛 − 1 pairs of observations:
(𝑥\ , 𝑥f ), (𝑥f , 𝑥e ), … , (𝑥li\ , 𝑥l ). We regard the first observation in
each pair as coming from 𝑋 and the second observation as coming
from a random variable Y. Then, 𝑟Ôð is:

∑li\
R¢\ (𝑥R − 𝑥̅ )(𝑥R[\ − 𝑥̅ )
𝑟Ôð = ,
Ï∑li\ f li\
ñ¢\ (𝑥R − 𝑥̅ ) Ï∑ñ¢\ (𝑥R[\ − 𝑥̅ )
f

where
li\ l
1 1
𝑥̅ = ž 𝑥R and 𝑦µ = ž 𝑥R .
𝑛−1 𝑛−1
R¢\ R¢f

The above correlation 𝑟Ôð is called the lag 1 autocorrelation of a ran-


dom variable and we refer to it as 𝑟\ , where the subscript stands for
lag 1. If 𝑛 is large, it can be approximated as follows:

∑li\
R¢\ (𝑥R − 𝑥̅ )(𝑥R[\ − 𝑥̅ )
𝑟\ = ,
∑lñ¢\(𝑥R − 𝑥̅ )f

where 𝑥̅ is the sample mean of the 𝑛 observations. In a similar fash-


ion, we can obtain the lag 𝑘 autocorrelation, that is the correlation
between observations which are 𝑘 apart. This is given by the expres-
sion:

∑lik
R¢\ (𝑥R − 𝑥̅ )(𝑥R[k − 𝑥̅ )
𝑟k = , 𝑘 ≥ 1,
∑lñ¢\(𝑥R − 𝑥̅ )f
104
Harry Perros Simulation Techniques
where 𝑥̅ is the overall sample mean. The lag 𝑘 autocorrelations, 𝑘 ≥
1, are also referred to as the autocorrelation coefficients.

The autocorrelation can also be computed by first computing the au-


to-covariances 𝜌\ , 𝜌f , … , 𝜌l where 𝜌k is given by the expression:
lik
1
𝜌k = ž(𝑥Ri 𝑥̅ )( 𝑥R[k − 𝑥̅ ).
𝑛
R¢\

The lag 𝑘 autocorrelation is computed as the ratio:


𝜌k
𝑟k = ,
𝑠 f
where 𝑠 f is the sample variance. We note that 𝑟c = 1, since 𝜌c = 𝑠 f .
The set of sample autocorrelations computed from a given sample,
are typically presented in a graph, known as the correlogram. Two
examples of a correlogram are shown in figures 5.5 and 5.6.

+1

rk

lag k

Figure 5.5: A correlogram with short-term correlation

+1

rk

lag k

Figure 5.6: A alternating correlogram

105
Simulation Techniques Harry Perros
Let us now return to our estimation problem. Having obtained a sam-
ple of 𝑛 observations 𝑥\ , 𝑥f , … , 𝑥l , we calculate the autocorrelation
coefficients, and then the variance can be estimated using the expres-
sion:
l/{
𝑘
𝑠 f = 𝑠Ôf ò1 + 2 ž Û1 − 𝑟k Ýó ,
𝑛
k¢\

where 𝑠Ôf is the sample variance given by


\ \
𝑠 f = li\ ∑lR¢\(𝑥R − 𝑥̅ )f , where 𝑥̅ = l ∑lR¢\ 𝑥R .

That is, in the case of correlated observations, the sample variance s2


calculated under the assumption that the observations are uncorrelat-
ed, is modified by the above term in the square brackets.

!", !$ , … , !&, !&'" , !&'$ , … , !$& , … !()*")&'" , !()*")&'$ , … , !)&

Figure 5.7: The batch means method

5.5.2. The batch means method


This is a popular technique and very easy to implement. It involves
dividing successive observations into batches as shown in figure 5.7.
Each batch contains the same number of observations. Let the batch
size be equal to 𝑏. Then, batch 1 contains observations 𝑥\ , 𝑥f , … , 𝑥— ,
batch 2 observations 𝑥—[\ , 𝑥—[f , … , 𝑥f— , and so on. The total number
observations n should be selected so that 𝑛 = 𝑚𝑏. The observations
at the end of a batch maybe be correlated with those at the beginning
of the next batch. Let 𝑥̅R be the sample mean of the 𝑖th batch. If we
chose the batch size 𝑏 to be large enough, then the sequence of the 𝑚
sample means 𝑥̅\ , 𝑥̅f , … , 𝑥̅| , are approximately uncorrelated. Conse-
quently, we can calculate the standard deviation using the standard
expression. We have:

106
Harry Perros Simulation Techniques
|
1
𝑥̿ = ž 𝑥̅R
𝑚
R¢\

|
1
𝑠 f = ž(𝑥̅R − 𝑥̿ )f ,
𝑚−1
R¢\

and the confidence interval of 𝑥̿ is:


𝑠 𝑠
Û𝑥̿ − 𝑡c.õ² , 𝑥̿ + 𝑡c.õ² Ý .
√𝑚 √𝑚
An estimate of 𝑏 can be obtained by plotting out the correlogram of
all the 𝑚𝑏 observations, which can be obtained from a preliminary
simulation run. Statistical packages that plot a correlogram, also plot
two symmetric lines about the 𝑥-axis, i.e., one above the 𝑥-axis and
the other below it. These two lines give the confidence interval
bounds for the null hypothesis that the lag 𝑘 autocorrelation is zero.
In simple terms, if the lag 𝑘 autocorrelation is above the upper line or
below the lower line, then we assume that it is not zero. If these con-
fidence interval lines are not given, then the rule of thumb is to use a
cutoff of around 0.15. That is, if the absolute value of lag 𝑘 is less
than 0.15, then we assume that lag 𝑘 is zero. That is, the pairs
(𝑥R , 𝑥R[k ), 𝑖 ≥ 1, are not correlated. Going back to estimating 𝑏, we
select the first lag 𝑘 which falls within the confidence interval
bounds, and we fix 𝑏 so that it is at least 5 × 𝑘. The number of batch-
es 𝑚 is typically more than 30.

The batch size and the number of batches simulated may be a lot big-
ger than what was described above. In general, they are fixed so that
the confidence interval is very small. This is an important issue and it
is discussed further in section 5.6.

5.5.3. The replication method


Another approach to constructing a confidence interval of a mean is
to replicate the simulation several times. For each replication, we use
a different seed, which gives rise to a different sequence of pseudo-
random numbers, and consequently a different sequence of events. As
107
Simulation Techniques Harry Perros
a result, we get a different set of observations for each replication.
The difference between two successive seeds should be large.

Suppose we make m replications each with l observations, and we get


the following sets of observations:

Replication 1: 𝑥\\ , 𝑥\f , … , 𝑥\ö


Replication 2: 𝑥f\ , 𝑥ff , … , 𝑥fö

Replication 𝑚: 𝑥|\ , 𝑥|f , … , 𝑥|ö

Let 𝑥̅R be the sample mean of the 𝑖th replication, that is:

1 ö
𝑥̅R = ž 𝑥Ru , 𝑖 = 1,2, … , 𝑚.
𝑙 u¢\

The sample means 𝑥̅\ , 𝑥̅f , … , 𝑥̅ | are independent from each other and
therefore the confidence interval is:
𝑠 𝑠
Û𝑥̿ − 𝑡c.õ² , 𝑥̿ + 𝑡c.õ² Ý,
√𝑚 √𝑚
where
| |
1 1
𝑥̿ = ž 𝑥̅R and 𝑠 f = ž(𝑥̅R − 𝑥̿ )f .
𝑚 𝑚−1
R¢\ R¢\

For each replication, we allow the simulation to go through the warm


up period and then run it long enough so that to collect the 𝑙 observa-
tions. This procedure is repeated 𝑚 times.

The replication method appears to be similar to the batch means


method. However, in the batch means method the batch size is rela-
tively small and the number of batches is large. In the replications
method it is the other way around. That is, each a replication is very
large and the number of replications can be small, as little as 5.

108
Harry Perros Simulation Techniques
5.6. Estimation of the confidence interval of a percentile
Percentiles is an important performance metric used in service level
agreements (SLAs). They are often ignored in favor of the mean of a
random variable, which in general is not an adequate metric. We are
primarily interested in the 90th, 95th, and 99th percentile. Given a
random variable 𝑋, its 95th percentile (or any other percentile) is a
value 𝑥c.õ² such that 95% of the values of the random variable 𝑋 are
less than 𝑥c.õ² , i.e., 𝑃𝑟𝑜𝑏[𝑋 ≤ 𝑥c.õ² ] = 0.95. Correspondingly, 5%
of the values of the random variable 𝑋 are greater than 𝑥c.õ² , i.e.,
𝑃𝑟𝑜𝑏[𝑋 ≥ 𝑥c.õ² ] = 0.05.

These high percentiles give us an idea of the shape of the right-hand


side tail of the probability distribution. For, if 𝑥c.õ² is close to the
mean, then the tail is very short. On the other hand, if it is far away
from the mean, then the distribution has a long tail. We are interested
in the shape of the right-hand tail of the distribution as it provides a
probabilistic upper bound of a random variable.

Percentiles are calculated as follows. Let 𝑥\ , 𝑥f , … , 𝑥l , be a sample of


an endogenously created random variable X. The sample is sorted out
in an ascending order, and let 𝑦\ ≤ 𝑦f ≤ … ≤ 𝑦l be the sorted ob-
servations. Then, the 95th percentile (or any other percentile) of 𝑋 is
the value 𝑦k where k = ⌈0.95x𝑛⌉. (The function ⌈𝑦⌉ is the ceiling
function that maps a real number 𝑥 to the smallest integer not less
than 𝑥.) For instance, for a sample of 950 observations, 𝑘 =
⌈0.95x950⌉ = 903, and the 95th percentile is the value 𝑦õce .

The confidence interval of a percentile can be calculated easily using


the batch means method or the replication method. Let us assume that
we are interested in the 95th percentile. After 𝑚 batches or replica-
tions, we obtain a sample of 𝑚 independent estimates of the 95th per-
centile, i.e., 𝑥c.õ²,\ , 𝑥c.õ²,f , … , 𝑥c.õ²,| . Then, we have:
𝑠 𝑠
Û𝑥
‹0.95 − 𝑡c.õ² ,𝑥
‹0.95 + 𝑡c.õ² Ý
√𝑚 √𝑚
where

109
Simulation Techniques Harry Perros
𝑚 𝑚
1 1 2
𝑥̅c.õ² = ž 𝑥c.õ²,R and 𝑠2 = žŸ𝑥c.õ²,R − 𝑥̅c.õ² .
𝑚 𝑚−1
𝑖=1 𝑖=1

The estimation of high percentiles requires very long batches or repli-


cation runs, because we want to collect as many as possible observa-
tions in the right-hand tail of the distribution. If the batches are not
long, then the estimates will be inaccurate. The calculation of a per-
centile requires that we store the entire sample of observations until
the end of the simulation, and that we order the sample of observa-
tions in an ascending order. These two operations can be avoided by
constructing a frequency histogram of the random variable on the fly.
When an observation becomes available, it is immediately classified
into the appropriate bin of the histogram. Thus, it suffices to keep
track of how many observations fall within each bin. At the end of
each batch, the percentile can be easily picked out from the histo-
gram. Obviously, the accuracy of this implementation depends on the
chosen width of the bins of the histogram.

5.7. Estimation of the confidence interval of a probability


The estimation of the confidence interval of the probability 𝑝 that a
particular event 𝐸 occurs can be handled in the same way as the esti-
mation of the confidence interval of a mean or a percentile using the
batch means method or the replication method. For instance, let us
say that in the machine interference model we want to calculate the
probability that a machine that breaks down finds the repairman idle.
We introduce the variables 𝑁 and 𝐾 in the simulation; the first one
keeps count of the total number of machine that broke down, and the
second one keeps count of the total number of those machine that
broke down at a time that the repairman was idle. Then, 𝑝 = 𝐾/𝑁.
Using the batch means method or the replication method we calculate
𝑚 independent estimates of 𝑝, i.e., 𝑝\ , 𝑝f , … , 𝑝| . Then, we have:
𝑠 𝑠
Û𝑝̅ − 𝑡c.õ² , 𝑝̅ + 𝑡c.õ² Ý
√𝑚 √𝑚
where

110
Harry Perros Simulation Techniques
| |
1 1
𝑝̅ = ž 𝑝R and 𝑠 f = ž(𝑝R − 𝑝̅ )f .
𝑚 𝑚−1
R¢\ R¢\

5.8. Estimation techniques for transient-state simulation


The statistical behavior of a simulation during its transient state de-
pends on the initial condition. We can estimate a statistic of a random
variable 𝑋 during the transient state using the replication method, see
section 5.3.3. For each replication we start the simulation with the
same initial condition and run the simulation for the transient period.
This, of course, requires advanced knowledge of the length of the
transient period. Furthermore, the pseudo-random numbers used in a
replication have to be independent of those used in previous replica-
tions. This can be achieved by using a different seed for each replica-
tion. The difference between two seeds should be about 10,000.

5.9. Achieving a required accuracy


So far, we discussed techniques for generating confidence intervals
for various statistics of an endogenously generated random variable.
In general, the accuracy of an estimate of a statistic depends on the
width of the confidence interval 𝑡c.õ² Ÿ𝑠⁄√𝑛 , where 𝑛 is the number of
observations used. The width of the confidence interval is also re-
ferred to as the error. The smaller the width, the higher is the accura-
cy. For instance, if it is concluded in an opinion poll that 30% of the
population will vote for a candidate with an error of ±5%, then that
means that the true population percent of people who will vote for the
candidate lies in interval (25%, 35%) for a given level of confidence.
However, this is not the same as concluding that 30% will vote for
the candidate with an error of ±1%, i.e., the confidence interval is
(29%, 31%). Obviously, in the second case the accuracy is much
higher.

We note that the width 𝑡c.õ² Ÿ𝑠⁄√𝑛 is proportional to 1⁄√𝑛. There-


fore, the width can be reduced if we increase 𝑛. For instance, in order
to halve the width, 𝑛 has to be increased four times since 1⁄√4𝑛 =
(1/2)( 1⁄√𝑛). That is, we need to run the simulation long enough so
that to collect a total of 4𝑛 observations. A rule of thumb is that 𝑛

111
Simulation Techniques Harry Perros
should be fixed so that the width is about 10% or less of the estimat-
ed statistic. The typical approach is to run the simulation with increas-
ing values of 𝑛, until we get the desired result. Then, we can proceed
with the simulation experiments with the chosen value of 𝑛.

An alternative approach is to determine the desired simulation length


on the fly. That is, the simulation runs continuously and periodically
we check the width of the confidence interval. We continue until the
desired accuracy has been achieved. Let us assume that we want to
estimate a statistic 𝜃 of a random variable using independent replica-
tions or the batch means method, with a width of the confidence in-
terval approximately equal to 0.1𝜃þ, where 𝜃þ is a point estimate of 𝜃.
We first run the simulation with 𝑛 replications or batch means, and let
𝜃þ\ be the point estimate of 𝜃 and 𝛿\ the width of its confidence inter-
val. If 𝛿\ ≤ 0.1𝜃þ\ , then we stop. Otherwise, we run the simulation for
another 𝑛 replications or batches and calculate 𝜃þf and 𝛿f using the
2𝑛 replications. If 𝛿f ≤ 0.12, then we stop, else, we repeat the above
step.

.Finally, we note that the width 𝑡c.õ² Ÿ𝑠⁄√𝑛 can be reduced by reduc-
ing the variance 𝑠 f . For that, there are special variance reduction
techniques some of which are presented in Chapter 7.

Computer project
In this assignment you will enhance your program with a statistical
estimation procedure and then exercise it to obtain performance
curves. Use the code you developed in the computer project in Chap-
ter 3.

1. The machine interference problem. Implement a statistical estima-


tion procedure in your program and obtain performance curves as
described below.

a. Statistical estimation procedure


Remove the print statement that prints out a line of output each
time an event occurs. Setup a data structure to collect information

112
Harry Perros Simulation Techniques
regarding the amount of time each machine spends being broken
down, i.e., waiting in the queue and also being repaired.

Run your simulation for 1050 observations, i.e., repairs. (That is,
stop the simulation after the 1050th observation is generated and
ignore the customers remaining in the queues.) Discard the first
50 observations to account for the transient state. Use a statistical
package to obtain and graph the correlogram based on the above
1000 observations. Based on the correlogram, calculate the batch
size. Implement the batch means approach in your program and
run your program for 31 batches. Disregard the first batch and use
the other 30 batch means to construct a confidence interval of the
mean down time (queueing and being repaired) 𝑊.

Use the batch means method to estimate the 95th percentile of the
response time. For this, make sure that the batch size is very large,
around 1000 observatios. Below you will estimate both the mean
and the 95th percentile in a single run, and for this you should use
a large batch size of 1000 observations instead of the batch size
determined above.

b. Experimentation
You have now completed the development of the simulation
model. The reason we developed the simulation model is to use it
to obtain performance curves. In this simulation model, we have
two variables to play with, namely the mean repair time (𝑀𝑅𝑇)
and the mean operational time (𝑀𝑂𝑇).

Set 𝑀𝑅𝑇 = 10 and calculate the mean 𝑊 and the 95th percentile
𝑊c.õ² of the down time by varying 𝑀𝑂𝑇 from 10 to 50 in incre-
ments of 5. The idea is to get a good spread of values so that to
have a complete picture of how 𝑊 and 𝑊c.õ² vary as a function of
𝑀𝑂𝑇. Therefore, if needed, decrease or increase the range of val-
ues of 𝑀𝑂𝑇. You will observe that 𝑊c.õ² is much larger than 𝑊
since it is an estimate of the tail of the distribution, unless the dis-
tribution has very low variability in which case the two estimates
will be close. Graph your results and discuss the observed trends.

113
Simulation Techniques Harry Perros
Obtain similar curves by fixing 𝑀𝑂𝑇 and then vary 𝑀𝑅𝑇. Choose
values for 𝑀𝑂𝑇 and 𝑀𝑅𝑇 so that to obtain a complete picture of
the two graphs. Graph your results and discuss the observed
trends.

You can extend your simulation model to the case where the
number of machines is an input to the simulation. Then you can
repeat the above experiments by also varying the number of ma-
chines. For instance, you can get multiple graphs when you fix the
mean repair time 𝑀𝑅𝑇 and vary the mean operational time 𝑀𝑂𝑇,
each corresponding to a different number of machines. Again, you
need to select values for the 𝑀𝑅𝑇 and 𝑀𝑂𝑇 so that to get mean-
ingful curves.

You can also run a similar experiment by further extending your


simulation model to the case where the number of machines is al-
so an input to the simulation. Now, you can vary both the number
of repairmen and machines!

Note: Draw the confidence intervals at each simulated point as a


vertical line. You may find that for some values the confidence in-
tervals may be longer than others. For these cases, simply in-
crease the number of batches.

2. The token-based access scheme. Implement a statistical estimation


procedure in your program and obtain performance curves as de-
scribed below.

a. Statistical estimation procedure


Change the inter-arrival times to 20, 25, and 30 instead of 10,
15, and 20 that you used before which leads to an unstable sys-
tem, that is, the queues may become infinitely big. Remove the
print statement that prints out a line of output each time an event
occurs. Augment your simulation to record how much time each
packet spends in a queue, from the time when it joins the queue to
the time it is transmitted out. We will refer to this time as the re-
sponse time. Record this information in the order in which packets
arrive to the token-based system.

114
Harry Perros Simulation Techniques
Run your simulation for a total of 3100 packet departures from all
three queues. (That is, stop the simulation after the 3100th packet
has been transmitted. Ignore the remaining packets in the queues.)
Discard the first 100 observations to account for the transient
state. Use a statistical package to calculate and graph the correlo-
gram of the response times collected in the above task. Based on
the correlogram calculate the batch size. Implement the batch
means approach in your program and run your program for 31
batches. Disregard the first batch and use the other 30 batch
means to construct a confidence interval of the mean response
time.

Use the batch means method to estimate the 95th percentile of the
response time. For this, make sure that the batch size is very large,
around 1000 observatios. Below you will estimate both the mean
and the 95th percentile in a single run, and for this you should use
a large batch size of 1000 observations instead of the batch size
determined above.

b. Experimentation
You have now finished building the simulation model and you
can exercise it in order to study the performance of the system.
Specifically, we are interested to see how the response time is af-
fected by the packet inter-arrival time and by the time out 𝑇. The
transmission time of a packet and the switch over time also affect
the response time, but they will not be considered here. Also, in
order to simplify the experiments, we will assume that the mean
inter-arrival time of packets to all three queues has the same
mean, notated as 𝑀𝐼𝐴𝑇. Obtain the following results.

Estimate the mean response time 𝑅 and the 95th percentile


𝑅c.õ² of the response time by varying 𝑀𝐼𝐴𝑇 while 𝑇 is fixed equal
to 15. You will observe that 𝑅c.õ² is much larger than 𝑅 since it is
an estimate of the tail of the distribution, unless the distribution
has very low variability in which case the two estimates will be
close. Start with a large value of 𝑀𝐼𝐴𝑇, such as 𝑀𝐼𝐴𝑇 = 40, and
then decrease it by 5 each time you run the simulation. It is im-
portant that 𝑀𝐼𝐴𝑇 does not get smaller than a critical value
115
Simulation Techniques Harry Perros

𝑀𝐼𝐴𝑇 below which the queues become unstable. That is, they
continuously build up and they grow infinitely big as time goes
on. In this case you will observe your simulation program will
eventually run out of memory. Also, in this case there is no need
to simulate the system in order to estimate 𝑅 and 𝑅c.õ² since they
are both infinitely big. (In general, a system which uses queues
should operate within parameters so that the queues are always
stable.) The problem, however, is that it is not easy to calculate
𝑀𝐼𝐴𝑇 ∗ . In view of this, you need to proceed with caution as you
reduce 𝑀𝐼𝐴𝑇. Keep an eye on 𝑅 and 𝑅c.õ² and stop when they
start increasing rapidly. Graph your results and discuss the ob-
served trends.

Repeat the above experiment by varying 𝑇 for each value of the


𝑀𝐼𝐴𝑇 that you used. Vary 𝑇 sufficiently so that you can obtain a
complete picture of a trend. Graph your results of 𝑅 and 𝑅c.õ² ver-
sus 𝑇 for each value of 𝑀𝐼𝐴𝑇 and discuss the observed trends.
(You can also graph your results of 𝑅 and 𝑅c.õ² versus 𝑀𝐼𝐴𝑇 for
each value of 𝑇.)

Modify the simulation model so that the number of queues is an


input variable and obtain similar graphs as above for different
number of queues.

Note: Draw the confidence intervals at each simulated point as a


vertical line. You may find that for some values the confidence in-
tervals may be longer than others. For these cases, simply in-
crease the number of batches.

3. The two-stage manufacturing system. Implement a statistical es-


timation procedure in your program and obtain performance
curves as described below.

a. Statistical estimation procedure


Remove the print statement that prints out a line of output each
time an event occurs. Setup a data structure to collect information
regarding the amount of time a customer spends in the system, i.e.

116
Harry Perros Simulation Techniques
from the time it arrives to queue 1 to the time it departs from
server 2. We shall refer to this time as the production time.

Run your simulation until you have obtained 1050 production


times. (That is, stop the simulation after the 1050th customer de-
parts from server 2. Ignore the remaining customers in the
queues.) Discard the first 50 observations, i.e., production times,
to account for the transient state and use a statistical package to
calculate and graph the correlogram of the remaining 1000 obser-
vations. Calculate the batch size based on the correlogram. Im-
plement the batch means method in your program and run your
program for 31 batches. Disregard the first batch and use the other
30 batch means to construct a confidence interval of the mean
time a customer spends in the system.

Use the batch means method to estimate the 95th percentile of the
production time. For this, make sure that the batch size is very
large, around 1000 observatios. Below you will estimate both the
mean and the 95th percentile in a single run, and for this you
should use a large batch size of 1000 observations instead of the
batch size determined above.

Experimentation

You have now finished building the simulation model and you
can exercise it in order to study the performance of the system. Of
interest is how the production time of a customer is affected by
the parameters of the system. These are: the mean inter-arrival
time (𝑀𝐼𝐴𝑇) to the two-stage manufacturing system, the mean
service time (𝑀𝑆𝑇1), mean operational (𝑀𝑂𝑇1) and mean repair
time (𝑀𝑅𝑇1) of server 1, the mean service time (𝑀𝑆𝑇2), mean
operational (𝑀𝑂𝑇2) and mean repair time (𝑀𝑅𝑇2) of server 2,
and the buffer capacity 𝐵 of queue 2 (which includes the one in
service as well).

Estimate the mean production time 𝑃 and the 95th percentile


𝑃c.õ² by varying the mean inter-arrival time 𝑀𝐼𝐴𝑇 with the re-
maining parameters fixed as follows: 𝑀𝑆𝑇1 = 20, 𝑀𝑂𝑇1 =

117
Simulation Techniques Harry Perros
200, 𝑀𝑅𝑇1 = 50, 𝑀𝑆𝑇2 = 10, 𝑀𝑂𝑇2 = 300, 𝑀𝑅𝑇2 = 30, and
𝐵 = 4. You will observe that 𝑃c.õ² is much larger than 𝑃 since it
is an estimate of the tail of the distribution of the production time,
unless the distribution has very low variability in which case the
two estimates will be close. Start with a large value of 𝑀𝐼𝐴𝑇,
such as 𝑀𝐼𝐴𝑇 = 200, and then decrease it by 20 each time you
run the simulation. It is important that 𝑀𝐼𝐴𝑇 does not get smaller
than a critical value 𝑀𝐼𝐴𝑇 ∗ below which the first queue becomes
unstable. That is, it continuously builds up and grows infinitely
big as time goes on. (This is an undesirable situation since the
system should operate within parameters so that the first queue is
always stable. Note that the second queue will never become un-
stable since it has a finite buffer. Also, in this case your simula-
tion program may eventually run out of memory.) For the given
values of the input parameters, the critical value 𝑀𝐼𝐴𝑇 ∗ is ap-
proximately equal to 25. In view of this, exercise caution as you
reduce 𝑀𝐼𝐴𝑇. Keep an eye on 𝑃 and 𝑃c.õ² and stop when they
start increasing rapidly. Graph your results and discuss the ob-
served trends.

Recall that upon a service completion at server 1, the server gets


blocked if queue 2 is full. That is, the server cannot serve any
other customers that may be waiting in queue 1. The server will
remain blocked until a customer departs from server 2. In this
case, a space will become available in queue 2 and the served cus-
tomer in front of server 1 will move into queue 2 thus freeing
server 1 to serve other customer in queue 1. Obviously, the block-
ing of server 1 affects the production time since during that time it
cannot serve other customer in queue 1. The more frequently it
gets blocked, the longer is the production time. Estimate the mean
production time 𝑃 and the 95th percentile 𝑃c.õ² by varying the
mean service time 𝑀𝑆𝑇2 of server 2 while the remaining parame-
ters remain fixed as in the previous experiment, that is, 𝑀𝑆𝑇1 =
20, 𝑀𝑂𝑇1 = 200, 𝑀𝑅𝑇1 = 50, 𝑀𝑂𝑇2 = 300, 𝑀𝑅𝑇2 = 30, and
𝐵 = 4. For the mean inter-arrival time 𝑀𝐼𝐴𝑇 select a value
somewhere in the middle of the values you used in the previous
experiment. Start with an initial 𝑀𝑆𝑇2 equal to 10 and slowly in-
crease it each time you run the simulation. 𝑃 and 𝑃c.õ² increase as
118
Harry Perros Simulation Techniques
the 𝑀𝑆𝑇2 gets longer. Again, keep an eye on these two values
and stop when they start increasing rapidly. Vary 𝑀𝑆𝑇2 suffi-
ciently so that you can obtain a complete picture of how the pro-
duction time changes as a function of 𝑀𝑆𝑇2. Graph your results
of 𝑃 and 𝑃c.õ² versus 𝑀𝑆𝑇2 and discuss the observed trends. You
can repeat this experiment for various values of the 𝑀𝐼𝐴𝑇 so that
to get a family of graphs each corresponding to a different value
of 𝑀𝐼𝐴𝑇.

Modify the simulation model so that the number of servers at each


stage is an input variable and obtain similar graphs as above for
different number of servers.

Note: Draw the confidence intervals for each simulated point as a


vertical line. You may find that for some values the confidence in-
tervals may be longer than others. For these cases, simply in-
crease the number of batches.

119
Harry Perros Simulation Techniques
Chapter 6:
Validation of a simulation model
Validating a simulation model is often neglected despite the fact that
it is a very important part of the process of building a simulation
model. How accurately does a simulation model (or, for that matter,
any kind of model) reflect the operations of the real-life system under
study? How confident are you that the obtained simulation results are
accurate? These are important questions that need to be addressed be-
fore using the results to make decisions.

Let us consider an example involving a factory operating at nearly


full capacity. The management is considering various alternative con-
figurations for expanding the system's capacity. The question that
arises here is which of the alternatives is the best one. That is, it will
achieve the expected performance at minimum cost. Now, since
these alternative configurations do not exist, their performance can be
evaluated by using simulation or other mathematical models. The
standard method is to construct a simulation model of the existing
system. Then, modify the model so that to analyze each alternative.
The model of the existing system can be validated by comparing its
output against actual data obtained from the existing system. Howev-
er, there is no guarantee that when we modify the simulation to model
an alternative configuration, that this new simulation model correctly
represents the configuration under study!

Let us consider another example where the actual system does not
exist at all. For instance, let us consider a communications equipment
manufacturer who is currently designing a new communications de-
vice, such as a router. Obviously, the manufacturer would like to
know in advance if the new router has an acceptable performance.
Also, the manufacturer would like to know the performance of vari-
ous alternative configurations of the router so that to come up with a
good portfolio. In this case, the performance of the router can be es-
timated through simulation or other mathematical modeling. The
question that arises here is how does one make sure that the model
that will be constructed is a valid representation of the system under
study.
121
Simulation Techniques Harry Perros
The two examples presented above are typical situations that a mod-
eler has to deal with. The following is a list of checks for validating a
simulation model.

a. Check the pseudo-random number generator. A battery of sta-


tistical tests can be carried out to make sure that the pseudo-
random number generator used in the simulation program cre-
ates numbers that are uniformly distributed in [0,1] and statis-
tically independent.
b. Check the stochastic variate generators. Similar statistical
tests can be carried out for each random variate generator used
in a simulation model.
c. Check the logic of the simulation program. This is an arduous
task. One way of going about it is to print out the status varia-
bles, the event list, and other relevant data structures each time
an event takes place, similar to what you did in the early stag-
es of the development of your simulation. Then, check by
hand whether the data structures are updated appropriately.
This is a rather tedious task. However, using this method one
can discover logical errors and also get a good feel about the
simulation model.
d. Relationship validity. Verify that the assumptions you made
when building the simulation model are the same as in the
system under study.
e. Output validity. This is the most important validity checks. If
actual data are available then this data can be compared with
the output obtained from the simulation model. Otherwise,
there are various ways that the validity of the output can be
confirmed. A simple method is to consider special cases for
which you can predict the behavior of the results. For in-
stance, let us consider the machine interference model. As the
mean repair time gets smaller, the mean down time of a ma-
chine should also get smaller. This can be verified by running
the simulation model for different mean repair times that get
progressively smaller. An alternative situation can be created
by assuming that the mean operational time is very close to
zero. In this case, we should observe that all the machines are
broken down queueing in the repairman’s queue. In general,

122
Harry Perros Simulation Techniques
output validity tests can be carried out by obtaining graphs of
a particular measure of interest for different values of the in-
put parameters and then try to interpret these graphs intuitive-
ly.

Computer projects
Here we describe some validation tests that you can use to validate
your simulation code. Use the code developed in the previous Chap-
ter. You can also devise additional validation tests. If your validation
test fails, then you have to look into your code very carefully and try
to identify the error(s).

1. The machine interference problem. This problem can be also ana-


lyzed using queueing theory. Let 1/𝜆 and 1/𝜇 be the mean opera-
tional time of a machine and the mean repair time respectively.
Also, let 𝑁 be the number of machines. Then, the mean down
time 𝑊 of a machine (queueing and being repaired) is given by
the expression:

𝑊 = 𝐿 / 𝜆(𝛮 − 𝐿)

where

𝜆+𝜇
𝐿 = #𝑁 − + 1$ (1 − 𝑝c )
𝜆
and
%
𝑁! 𝜆 l
𝑝c = 1/ ž Û Ý .
(𝑁 − 𝑛)! 𝜇
l¢c

Set 1/𝜇 = 10 and calculate 𝑊 by varying 1/𝜆 from 10 to 50 in


increments of 5. (The idea is to get a good spread of values so that
to observe the behavior of 𝑊 as a function of 1/𝜆. If needed de-
crease or increase the range of values of 1/𝜆.)

For each value of 1/𝜆, obtain an estimate of the mean down time
and confidence interval using your simulation model with the

123
Simulation Techniques Harry Perros
batch means method. Graph both sets of results. The theoretical
results should lie within the confidence intervals.

2. The token-based access scheme. Carry out the following two vali-
dation tests.

a. Modify your code so that the token is always in queue 1 and it


never visits the other queues. You can do this by setting 𝑇 to a
very large number so that the time-out never occurs during the
life of the simulation. Also, do not let the token change queues
if queue 1 is empty. Then, queue 1 will be the only queue that
will be served, while the other queues will grow in length for
ever. Use the batch means method to estimate the mean wait-
ing time 𝑊 and its confidence interval in queue 1. Assume a
mean service time of 5 for all packets and vary the mean inter-
arrival time from 20 to 8 in increments of 1. (The idea is to
get a good spread of values, so you may have to de-
crease/increase the range of values of the mean inter-arrival
time so that to achieve this.) The only departures are from
queue 1, and consequently your estimation procedure will au-
tomatically give the mean waiting time of queue 1. Make sure
that the data structures for the other queues do not become
very large so that to cause memory problems. An easy way to
do that, is to take no action when there is an arrival to the oth-
er queues.

Under the conditions described above, 𝑊 can also be obtained


by analyzing queue 1 as an 𝑀/𝑀/1 queue using queueing
theory. In this case we have 𝑊 = 1/(µ-l) where 1/𝜆 and 1/µ
is the mean inter-arrival and mean service time respectively.
Calculate 𝑊 for the same values as in the simulation and
compare the results. The theoretical results should lie within
the confidence intervals obtained from the simulation.

b. Remove the changes you made in the code in the above task
so that to restore the code in its original state. Assume that 𝑇
is very small, i.e., 𝑇 = 0.001, and that the switch over time is
very small as well, i.e., it is equal to 0.001. Then, in this case
the queues will be served in a round-robin fashion where a
124
Harry Perros Simulation Techniques
maximum of one customer is served from each queue, and the
overhead time due to switch-over time is almost zero. Use the
simulation method with the batch means method to estimate
the mean waiting time 𝑊 in the system, assuming that the
mean service time is the same for all packets and it is equal to
5, and vary the inter-arrival time to each queue from 40 to 20
in increments of 2. (Again, if needed, increase or decrease the
range of values so that to get a full picture of the trend in the
graph.)

Under the conditions described above, the entire token-based


system behaves as an 𝑀/𝑀/1 queue and therefore 𝑊 =
1/(µ-l› ) where 1/µ is the mean service time and 1/l› is the
mean inter-arrival time to all three queues, that is, it is equal to
the mean inter-arrival time 1/𝜆 to a queue divided by 3, i.e.,
1/l› = 1/3𝜆. Set 1/µ = 5 and calculate 𝑊 by varying the
mean inter-arrival 1/𝜆 as above. The theoretical results should
lie within the confidence interval obtained from the simula-
tion.

3. The two-stage manufacturing system. As the buffer capacity of


the second queue increases, the probability that server 1 will get
blocked upon service completion due to queue 2 being full will
decrease, and eventually it will become zero. At that moment, the
two-stage manufacturing system becomes equivalent to two infi-
nite capacity queues linked in series. Now, if we assume that the
percent of time each machine is down is very small, i.e., the mean
downtime divided by the sum of the mean operational time and
the mean downtime is less than 2%, then the service time at each
queue can be safely approximated by an exponential distribution.
Therefore, each queue becomes an M/M/1 queue, and the mean
waiting time in the two-stage manufacturing system is equal to the
mean waiting time 𝑊\ = 1/(µ\ -l) in the first queue plus the
mean waiting time 𝑊f = 1/(µf -l), where 1/𝜆 is the mean inter-
arrival time to the first queue and 1/µ\ and 1/µf is the mean ser-
vice time in the first and second queue respectively. Use the ex-
pressions for 𝑊\ and 𝑊f to calculate the total mean waiting time
in the manugacturing system 𝑊\ + 𝑊f . Set 1/µ\ = 10 and 1/µf =
125
Simulation Techniques Harry Perros
15 and vary 1/𝜆 from 100 to 20 in increments of 10. (The idea is
to get a good picture of the trend in the graph, and so, if needed,
decrease or increase the range of values of 1/𝜆.)

Run your simulation model with the batch means method and es-
timate the total mean waiting time in the manufacturing system
and its confidence interval. The theoretical results obtained using
the 𝑀/𝑀/1 queue should lie within the confidence interval ob-
tained from the simulation.

126
Harry Perros Simulation Techniques
Chapter 7:
Variance reduction techniques

NC S U
Variance
Reduction

7.1. Introduction
In Chapter 6, it was mentioned that the accuracy of an estimate is
proportional to 1/√𝑛, where 𝑛 is the sample size. One way to in-
crease the accuracy of an estimate (i.e., reduce the width of its confi-
dence interval) is to increase 𝑛. For instance, the confidence interval
width can be halved if the sample size is increased to 4𝑛. That is, the
accuracy can be increased by running long simulations, which maybe
time consuming. An alternative way to increase the estimate's accura-
cy is to reduce its variance. If one can reduce the variance of an en-
dogenously created random variable without disturbing its expected
value, then the confidence interval width will be smaller, for the same
amount of simulation. Techniques aiming at reducing the variance of
a random variable are known as Variance Reduction Techniques.
Most of these techniques were originally developed in connection
with Monte Carlo Techniques (see section 3.7).

Variance reduction techniques require additional computation in or-


der to be implemented. Furthermore, it is not possible to know in ad-
vance whether a variance reduction technique will effectively reduce
the variance in comparison with straightforward simulation. It is a
good idea, therefore, to carry out pilot simulation runs in order to get
a feel of the effectiveness of a variance reduction technique and of the
additional computational cost required for its implementation.
127
Simulation Techniques Harry Perros
In this Chapter, we examine two variance reduction techniques,
namely, the antithetic variates technique and the control variates
technique. We note that the material in this Chapter is not necessary
for learning how to construct a simulation model, since in practice
accuracy is achieved by simply running very long simulations. How-
ever, it is worth studying it should you have the time and interest!

7.2. The antithetic variates technique


This is a very simply technique to use and it only requires a few addi-
tional lines of code in order to be implemented. However, there is no
guarantee that it is effective. Also, it is not possible to know in ad-
vance how much variance reduction can be achieved. Therefore, a
small pilot study may be useful in order to decide whether or not to
implement this technique.

Let us assume that we want to construct the mean and confidence in-
terval of an endogenously created random variable 𝑋. Let 𝑋(1) and
𝑋(2) be the random variable 𝑋 as observed in two separate simula-
tion runs 1 and 2, and let 𝑍 be a random variable defined as follows:

𝑋(1) + 𝑋(2)
𝑍= .
2
That is, let 𝑥\,\ , 𝑥\,f , … , 𝑥\,l be 𝑛 observations of 𝑋 obtained in a sim-
ulation run, and let 𝑥f,\ , 𝑥f,f , … , 𝑥f,l be 𝑛 observations of 𝑋 obtained
in a second simulation run. Then, the 𝑖th value of 𝑍 is:

Ÿ𝑥\,R + 𝑥f,R
𝑧R = , 𝑖 = 1,2, … , 𝑛.
2
As will be seen below, we use the random variable Z to construct the
confidence interval of 𝐸(𝑋). We have

𝑋(1) + 𝑋(2)
𝐸(𝑍) = 𝐸 É Ê
2

1
= '𝐸(𝑋(1)) + 𝐸Ÿ𝑋(2) ( = 𝐸(𝑋),
2
128
Harry Perros Simulation Techniques
since the expected value of 𝑋(1) or 𝑋(2) is that of 𝑋. Thus, the ex-
pected value of this new random variable 𝑍 is identical to that of 𝑋.
Now, let us examine its variance. We have

𝑋(1) + 𝑋(2)
𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟 É Ê
2
\
= { [𝑉𝑎𝑟(𝑋(1)) + 𝑉𝑎𝑟(𝑋(2)) + 2𝐶𝑜𝑣(𝑋(1), 𝑋(2))].

Since 𝑉𝑎𝑟(𝑋(1)) = 𝑉𝑎𝑟(𝑋(2)) = 𝑉𝑎𝑟(𝑋), we have

1
𝑉𝑎𝑟(𝑍) = [𝑉𝑎𝑟(𝑋) + 𝐶𝑜𝑣(𝑋(1), 𝑋(2)].
2
The covariance of two random variables 𝑋 and 𝑌 can be written as

𝐶𝑜𝑣(𝑋, 𝑌) = 𝐶𝑜𝑟(𝑋, 𝑌)Ï𝑉𝑎𝑟(𝑋)𝑉𝑎𝑟(𝑌) ,

where 𝐶𝑜𝑟(𝑋, 𝑌)is the correlation of 𝑋 and 𝑌. Therefore,

1
𝑉𝑎𝑟(𝑍) = Ÿ𝑉𝑎𝑟(𝑋) + 𝜌𝑉𝑎𝑟(𝑋)
2
1
= 𝑉𝑎𝑟(𝑋)(1 + 𝜌),
2
where 𝜌 is the correlation between 𝑋(1) and 𝑋(2).

We note that if 𝜌 < 0, then 𝑉𝑎𝑟(𝑍) < 𝑉𝑎𝑟(𝑋), and in this case we
can use 𝑍 in order to construct the mean and confidence interval of
𝑋, since 𝑍 and 𝑋 have the same mean and Z has a lower variance.

The antithetic variates technique attempts to introduce a negative cor-


relation 𝜌 between the results of the two simulation runs. For in-
stance, let us consider a simulation model of a single server M/M/1
queue, and let 𝑋 be an endogeneously created random variable that
indicates the waiting time of a customer in the queue, i.e., the time
from the moment it arrives to the moment it departs. We want to con-
struct an interval estimate of 𝑋. For this, we run the simulation to col-
129
Simulation Techniques Harry Perros
lect a sample of 𝑛 observations. Then, we modify the simulation and
run it again in order to collect a second sample which is negatively
correlated to the first one. This is done using the following simple
procedure.

We note that in order to simulate a customer 𝑖 we need to genarate


two pseudo-random numbers 𝑟R and 𝑣R . Using these two numbers we
obtain the customer’s interarrival time 𝑡R = 𝐹 i\ (𝑟R ) and service time
𝑠R = 𝐺 i\ (𝑣R ), where 𝐹(𝑡) and G(s) are the cumulative distribution of
the inter-arrival time and service time respectively. An indication of
whether the queue is tending to increase or decrease can be obtained
by examining the difference 𝑑R = 𝑡R − 𝑠R . This difference may be pos-
itive or negative indicating that the queue is going through a slack or
a busy period respectively. In the second run, we generate the pseudo-
random numbers 𝑟R› and 𝑣R› for the 𝑖th simulated customer, so that
𝑑R› = 𝑡R› − 𝑠R› , where 𝑡R› = 𝐹 i\ (𝑟R› )) and 𝑠R› = 𝐺 i\ (𝑣R› ), has the opposite
sign of 𝑑R . That is, if the queue was going through a slack (busy) pe-
riod in the first run at the time of the 𝑖th simulated customer, now it
goes through a busy (slack) period. In order to achieve this negative
correlation, we simply set 𝑟R› = 1 − 𝑟R and 𝑣R› = 1 − 𝑣R . An easy way
to implement this is to store 𝑟R and 𝑣R for each customer and then use
them in the second run for the same customer. A simpler way is to
generate both pseudo-random numbers 𝑟R and 𝑣R when we schedule
the arrival of a new customer 𝑖. In this way, we guarantee that the
same pair of pseudo-random numbers are associated with the same
customer.

Once we have generated the two sets of observations 𝑥R,\ , 𝑥R,f , … , 𝑥R,l ,
𝑖 = 1,2, we construct the mean and confidence interval estimate of
𝐸(𝑋) using 𝑍 as described above. That is, we calculate a new set of
observations 𝑧\ , 𝑧f , … , 𝑧l where 𝑧R = (𝑥R,\ + 𝑥R,f )/2, and then calcu-
late the mean 𝑧̅ and standard deviation 𝑠* , from which we obtain the
confidence interval of the mean of 𝑍 which is the same as that of 𝑋.

130
Harry Perros Simulation Techniques
Sample size 𝒏 Confidence interval
600 13.86 ± 3.46
900 13.03 ± 2.70
1200 13.11 ± 2.30
1500 12.82 ± 1.99
1800 12.86 ± 1.84

Table 7.1: Straight simulation of an M/M/1 queue

First run Sample


Original
Second runSample
Antithetic
Original Variates
55.0 ZMean
valuesof Two
Antithetic Samples
Variates
55.0 Mean of Two Samples

45.0
Per Customer

45.0
Per Customer

35.0
TimeTime

35.0
Waiting
Waiting

25.0
25.0

15.0
15.0

5.0

5.0
1 5 10 15 20 25 30
Simulated Number of Customers
1 5 10 15 20 25 30
Simulated Number of Customers

Figure 7.1: The antithetic variates technique applied to an M/M/1 queue

The antithetic variates technique, was implemented in a simulation of


an M/M/1 queue. The random variable 𝑋 is the time a customer
spends in the system. In both simulation runs the observations of 𝑋
were obtained by sampling every 10th customer, in order to avoid
131
Simulation Techniques Harry Perros
generating correlated observations. The results given in table 7.1 were
obtained using straight simulation. Using the antithetic variates tech-
nique, we obtained a confidence interval of 13.52 ± 1.76 using two
sets of 300 observations, i.e., a total of 600 observations. From table
7.1, we see that a similar confidence interval width of 1.84 was ob-
tained using a sample size of 𝑛 = 1800. Figure 7.2 shows a sample of
the values from the first run (straight simulation), the second run
(where we implemented the antithetic variates technique) and the
computed 𝑍 values. We observe that the samples from the first and
second run are nicely negatively correlated, and the 𝑍 values are all
close to the mean, indicating that their variance is small.

$ $
1.6 1.6
$ Straight$ Straight
SimulationSimulation
# Standard# Standard
Antithetic Antithetic
Variate Technique
Variate Technique

1.2 1.2
Standard Error

! ⁄ " Error

# #
$ $
Standard

$ $
0.8 0.8 $ $$ $
# # #$ #$#$ $
#
0.4 0.4

800 800 1600 16002400 24003200 3200


Sample Size
Sample Size

Figure 7.2: A comparison between straight simulation and the antithetic vari-
ates techniques for an M/M/2 queue

In the above example, the antithetic variates technique worked quite


well. However, this should not be construed that this method always
works well. We applied the same technique to an M/M/2 queue, i.e.,
an M/M/1 queue but with two servers, and as can be seen in figure 7.2
there is little benefit to be gained from using the antithetic variates
technique. The sample size on the axis is the total number of custom-
ers simulated in the straight simulation, and it is the total number of
customers simulated in both runs when implementing the antitheic
variates technique.
132
Harry Perros Simulation Techniques
7.3. The control variates technique
This method is also known as the method of concomitant information.
Let 𝑋 be an endogenously created random variable whose mean and
confidence interval we wish to estimate. Let 𝑌 be another endoge-
nously created random variable whose mean is known in advance and
it is correlated with 𝑋. This random variable is referred to as the con-
trol variable. We have the following two cases.

a. X and Y are negatively correlated

Define a new random variable 𝑍 = 𝑋 + 𝑌 − 𝐸(𝑌). We have

𝐸(𝑍) = 𝐸(𝑋 + 𝑌 − 𝐸(𝑌)) = 𝐸(𝑋),

𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟(𝑋) + 𝑉𝑎𝑟(𝑌) + 2 𝐶𝑜𝑣 (𝑋, 𝑌).

Since 𝑋 and 𝑌 are negatively correlated, we have that 𝐶𝑜𝑣(𝑋, 𝑌) < 0.


Therefore, if 𝑉𝑎𝑟(𝑌) − 2|𝐶𝑜𝑣(𝑋, 𝑌)| < 0 then, a reduction in the
variance of Z has been achieved.

b. X and Y are positively correlated

Define 𝑍 = 𝑋 − 𝑌 + 𝐸(𝑌). Then

𝐸(𝑍) = 𝐸(𝑋 − 𝑌 + 𝐸(𝑌)) = 𝐸(𝑋)

𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟(𝑋) + 𝑉𝑎𝑟(𝑌) − 2𝐶𝑜𝑣(𝑋, 𝑌).

Therefore, if 𝑉𝑎𝑟(𝑌) − 2|𝐶𝑜𝑣(𝑋, 𝑌)| < 0 then a reduction in the


variance of Z has been achieved.

As an example, let us consider a simulation of an M/M/1 queue, and


let 𝑋 and 𝑌 indicate the waiting time in the queue and the interarrival
time respectively. If 𝑌 is very small, then customers arrive faster and,
therefore, the queue size gets larger, which means that customers
have to wait longer, i.e. 𝑋 is large. On the other hand, if 𝑌 is large,
then customers arrive slower and, hence, the queue size gets smaller.
Obviously, the smaller the queue size, the less a customer has to wait

133
Simulation Techniques Harry Perros
in the queue, i.e., 𝑋 is small. Therefore, we see that X and Y can be
negatively correlated.

Let 𝑥\ , 𝑥f , … , 𝑥l be 𝑛 observations of 𝑋, and let 𝑦\ , 𝑦f , … , 𝑦 be 𝑛 ob-


servations of 𝑌, where 𝑦R is the inter-arrival time associated with the
𝑥R observation. Let

𝑧R = 𝑥R + 𝑦R − 𝐸(𝑌), 𝑖 = 1,2, … , 𝑛..

Then, the confidence interval of 𝐸(𝑋) is given by the expression


𝑠*
𝑧̅ ± 1.96
√𝑛
where
l
1
𝑧̅ = ž 𝑧R
𝑛
R¢\

and
l
1
𝑠* = − 1 ž(𝑧R − 𝑧̅)f .
𝑛
R¢\

More generally, the random variable 𝑍 can be be defined as

𝑍 = 𝑋 − 𝑎 (𝑌 − 𝐸(𝑌)),

where 𝑎 is a constant to be estimated and 𝑌 is positively or negatively


correlated to 𝑋. Again, we have that 𝐸(𝑍) = 𝐸(𝑋), and

𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟(𝑋) + 𝑎f 𝑉𝑎𝑟(𝑌) − 2𝑎 𝐶𝑜𝑣(𝑋, 𝑌).

𝑍 has a smaller variance than 𝑋 if

𝑎f 𝑉𝑎𝑟(𝑌) − 2𝑎 𝐶𝑜𝑣(𝑋, 𝑌) < 0.

134
Harry Perros Simulation Techniques
We select 𝑎 so that to minimize the left-hand side in the above ex-
pression. For this, we set the derivative of the left-hand side to zero.
We have

2𝑎 𝑉𝑎𝑟(𝑌) − 2 𝐶𝑜𝑣(𝑋, 𝑌) = 0

or

𝐶𝑜𝑣(𝑋, 𝑌)
𝑎 = .
𝑉𝑎𝑟(𝑌)

Now, substituting into the expression for 𝑉𝑎𝑟(𝑍) we have


f
𝐶𝑜𝑣(𝑋, 𝑌)
𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟(𝑋) + É Ê 𝑉𝑎𝑟(𝑌)
𝑉𝑎𝑟(𝑌)
𝐶𝑜𝑣(𝑋, 𝑌)
− 2 𝐶𝑜𝑣(𝑋, 𝑌)
𝑉𝑎𝑟(𝑌)

(𝐶𝑜𝑣(𝑋, 𝑌))f
= 𝑉𝑎𝑟(𝑋) −
𝑉𝑎𝑟(𝑌)

Since

𝐶𝑜𝑣(𝑋, 𝑌)
𝜌Ôð = ,
Ï𝑉𝑎𝑟(𝑋)Ï𝑉𝑎𝑟(𝑌)

We have
f ).
𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟(𝑋)(1 − 𝜌Ôð

Thus, we always get a reduction in the variance of 𝑍 for the optimal


value of 𝑎, provided that 𝑋 and 𝑌 are correlated. The determination of
𝑎 requires a priori knowledge of the 𝑉𝑎𝑟(𝑌) and 𝐶𝑜𝑣 (𝑋, 𝑌). Sample
estimates can be used in order to approximately obtain 𝑎.

The definition of Z can be further generalized using 𝑚 control varia-


bles as follows:

135
Simulation Techniques Harry Perros
|

𝑍 = 𝑋 − ž 𝑎R (𝑌R − 𝐸(𝑌R ),
R¢\

where 𝑎R , 𝑖 = 1,2, … , 𝑚, are any real numbers.

Computer assignments
Consider the machine interference problem. Carry out the following
tasks:

1. Implement the antithetic variance reduction technique.


2. Implement the control variates technique.
3. Compare the two variance reduction techniques against straight
simulation for various sample sizes.

136
Harry Perros Simulation Techniques
Chapter 8: Simulation projects
In this Chapter, we give three simulation projects to help you hone
your simulation skills. These projects are drawn from the area of
computer networks, but as you will see, you do not need to be famil-
iar with the underlying networking technology because each project is
described in a non-technical manner. These projects are:

• A simulation of a CPU priority scheduler


• A simulation of the flow of SIP messages
• A simulation of a multiplexer
Simulation models as well as all stochastic models are either continu-
ous-time or discrete-time models. That is, the underlying time is ei-
ther a continuous or a discrete variable. The three simulation projects
used in the previous Chapters are continuous-time models. In this
Chapter, the first two projects are continuous-time models and the
third one is a discrete-time model. As we have seen, only one event
can take place at any time in a continuous-time model. However, in a
discrete-time model it is possible that multiple events may occur at
the same time. Also, in a discrete-time simulation, the unit-time ad-
vance design is an obvious choice.

Note that the first simulation project is broken into three tasks in or-
der to facilitate its development, similar to the way that you devel-
oped your simulation model in the previous chapters. However, this is
not the case for the other two projects, though there are sufficient in-
structions as to how to build the models. Also, good validation data
are given in the first project so you can test the accuracy of your sim-
ulation model; a limited number of validation data is given in the sec-
ond project, and none in the third project. Note that the first part of
the third project may be of interest to the reader since it is a simple
project and at the same time it is a good example of a discrete-time
simulation, something that has not been discussed in this book so far.

8.1. A simulation of a CPU priority scheduler


We consider a simple Internet of Things (IoT) system that consists of
a large number of sensors and actuators, and a server. The sensors

137
Simulation Techniques Harry Perros
collect data which they send to the server in the form of messages.
The server processes the messages and issues commands that are sent
to the actuators. There are two types of messages, namely, real-time
(RT) and non real-time (nonRT). RT messages have to be processed
by the server as fast as possible since they represent tasks that need to
be executed in real time. For instance, in the case of a driverless car,
the breaks have to be activated immediately when the radar detects a
person in front of the car. NonRT messages represent non real-time
tasks which are not time constrained.

RT Queue
Arrivals of RT
messages Server

nonRT Queue
Arrivals of nonRT
messages

Figure 8.1: The CPU priority queueing system

The server maintains two queues, an RT queue and a nonRT queue,


as shown in figure 8.1. The server processes the messages using the
pre-emptive resume priority policy that guarantees that RT messages
always have a higher priority than nonRT messages. This policy is
commonly used for scheduling processes for execution on a CPU.
This scheduling policy works as follows:

• Each time the server completes a service, i.e., processing a


message, it checks the RT queue to see if there are any mes-
sages waiting. If there is a message waiting, then it starts pro-
cessing it.
• If the RT queue is empty, the server checks the nonRT queue.
If there is a message waiting, it starts processing it. If there are
no messages waiting, the server becomes idle.
• If during the time the server is processing a nonRT message,
an RT message arrives, the server interrupts the processing of
the nonRT message and starts processing the RT message.
(This is why the scheduling policy is called preemptive.) Up-

138
Harry Perros Simulation Techniques
on completion of processing the RT message, the server se-
lects the next message by going back to step 1.
• When the server processes an interrupted nonRT message, it
starts processing it from where it stopped when it was inter-
rupted. (This explains the “resume” term of the name of the
scheduling policy.)

In this simulation project, we are only concerned with the perfor-


mance of the CPU scheduling policy. In particular, you will develop
an event-based simulation model of the two-queueing system at the
server, with a view to calculating the 95th percentile of the response
time of RT and nonRT messages. The response time is defined as the
time elapsing from the time a message (RT or nonRT) joins its queue
to the moment it is fully processed by the server and departs from the
server.

For pedagogical purposes, the simulation project is broken into the


following three tasks:

1. Task 1 - Hand simulation: The objective is to make sure that


you understand the events, the event clocks, and how the sim-
ulation advances from one event to another.
2. Task 2 - Code basic simulation: The objective of this task is to
code the simulation logic and to make sure that the program
works correctly by carefully checking the output.
3. Task 3 - Statistical estimation: The objective here is to embel-
lish your simulation with additional code so that to estimate
the mean and 95th percentile of the response time of the RT
and nonRT messages.

8.1.1. The simulation logic


The state of the simulation model can be described by the following
parameters:

• Number of RT messages in the RT queue, 𝑛¶+


• Number of nonRT messages in the nonRT queue, 𝑛l–l¶+
• State of the server 𝑠: idle (𝑠 = 0), serving an RT message
(𝑠 = 1), serving a nonRT message (𝑠 = 2).
139
Simulation Techniques Harry Perros
There are other parameters too, but they are not critical to the devel-
opment of the simulation model. The simulation is based on tracking
the events that change the state of the system through time. These
events are:

1. An RT arrival occurs
2. A nonRT arrival occurs
3. The processing time of an RT or nonRT message is completed

Note, that the occurrence of one event may trigger the occurrence of
one or more events. Below, we discuss what happens when an event
occurs.

1. An RT arrival occurs

The message joins the RT queue, that is, the number of RT messages
𝑛¶+ currently in the queue is increased by 1. No further action is re-
quired, if there are other messages waiting in the queue. However, if
this message finds the queue empty, then it may be possible that it can
start its processing immediately. Check the status of the server and
schedule a new service time if it is idle. If it is busy serving an RT
message, then no further action is required. If it is busy serving a
nonRT message, then the service is interrupted, and the server starts
processing the RT message. The interrupted nonRT message returns
to the top of the nonRT queue and it will be processed at a later time
per the scheduling algorithm. Its processing will start from where it
stopped when it was pre-empted. Before leaving this part of the logic,
generate the next arrival time of an RT message.

2. A nonRT arrival occurs

The message joins the nonRT queue, that is, the number of nonRT
messages 𝑛l–l¶+ currently in the queue is increased by 1. No further
action is required, if there are other messages waiting in the queue.
On the other hand, if this message finds the queue empty, then it may
be possible that it can start its processing immediately. Check the sta-
tus of the server, and if idle schedule a new service time. No further
action is required if it is busy serving an RT or a nonRT message. Be-

140
Harry Perros Simulation Techniques
fore leaving this part of the logic, generate the next arrival time of an
nonRT message.

3. A service completion occurs

In this case, the server runs the scheduler to determine the next mes-
sage to process. Specifically, it checks first the RT queue. If the num-
ber of messages 𝑛¶+ in the queue is greater than 0, then at least an RT
message is waiting. The server starts processing the first message at
the top of the RT queue and 𝑛¶+ is reduced by one. Schedule the
time-of-service completion. If 𝑛¶+ = 0, then it checks the nonRT
queue. If the number of nonRT messages 𝑛¶+ currently in the queue
is greater than 0, then it starts processing the first message at the top
of the nonRT queue. Schedule the time-of-service completion and
reduce 𝑛l–l¶+ by 1. If the nonRT message has been pre-empted, the
service time is the left-over service at the time when the message was
pre-empted.

We are now ready to summarize the logic of the simulation program.

Each of the above three events is associated with a clock that gives
the time of completion of the event in the future. Let 𝑅𝑇𝐶𝐿,
𝑛𝑜𝑛𝑅𝑇𝐶𝐿, and 𝑆𝐶𝐿 be the clock for the event of an RT arrival, a
nonRT arrival, and a service completion respectively. In addition, let
𝑀𝐶 be the master clock that gives the current time in the simulation.

Each time an event has been serviced, the simulation logic checks all
the event clocks scheduled to occur in the future. It then advances to
the event with the smallest clock value. Depending on the event, it
takes the following action.

1. Arrival of an RT message

• Set 𝑀𝐶 to 𝑅𝑇𝐶𝐿.
• Message joins the RT queue. Increase 𝑛¶+ by 1.
• Generate the next inter-arrival time 𝐼𝐴𝑇 of an RT message and
set 𝑅𝑇𝐶𝐿 = 𝑀𝐶 + 𝐼𝐴𝑇.
• If 𝑛¶+ = 1, then check if server is idle.
o If yes, message begins processing at the server.
141
Simulation Techniques Harry Perros
o Determine the length of the service time 𝑆𝑇 and set 𝑆𝐶𝐿 =
𝑀𝐶 + 𝑆𝑇.
o Decrease 𝑛¶+ by 1.
o Set 𝑠 = 1. Recall that 𝑠 is the state of the server: 𝑠 = 0
(idle), 𝑠 = 1 (serving an RT message, 𝑠 = 2 (serving a
nonRT message).
• If not, check if the server is busy serving a nonRT message. If
yes, the nonRT message is preempted and the RT message
begins processing.
o If the remaining service time of the pre-empted nonRT
message is not zero, then store the remaining service time,
return the pre-empted message to the nonRT queue, and
increase 𝑛l–l¶+ by 1.
o If the remaining service time of the pre-empted nonRT
message is zero, then the nonRT message has completed
its service and it departs from the system. (Note: this con-
dition can only happen when the clocks are integers,
which is what we assume in tasks 1 and 2.1. In tasks 2.2
and 3 the clocks are defined as real variables, which
means that two event clocks can never be the same.)
o Determine the length of the service time 𝑆𝑇 and set 𝑆𝐶𝐿 =
𝑀𝐶 + 𝑆𝑇.
o Decrease 𝑛¶+ by 1.
o Set 𝑠 = 1.
• Go back to the beginning to locate the next event.

2. Arrival of a nonRT message

• Set 𝑀𝐶 to 𝑛𝑜𝑛𝑅𝑇𝐶𝐿.
• Message joins the nonRT queue. Increase 𝑛l–l¶+ by 1.
• Generate the next inter-arrival time 𝐼𝐴𝑇 of a nonRT message
and set 𝑛𝑜𝑛𝑅𝑇𝐶𝐿 = 𝑀𝐶 + 𝐼𝐴𝑇.
• If 𝑛l–l¶+ = 1, then check if server is idle. If yes, message be-
gins processing at the server.
o Determine the length of the service time 𝑆𝑇 and set 𝑆𝐶𝐿 =
𝑀𝐶 + 𝑆𝑇.
o Set 𝑠 = 2.
• Decrease 𝑛l–l¶+ by 1.
142
Harry Perros Simulation Techniques
• Go back to the beginning to locate the next event.

3. Service completion

• Set 𝑀𝐶 to 𝑆𝐶𝐿.
• Check the RT queue.
o If there is an RT message waiting in the queue, calculate
its service time 𝑆𝑇 and set 𝑆𝐶𝐿 = 𝑀𝐶 + 𝑆𝑇.
o Set 𝑠 = 1.
o Decrease 𝑛¶+ by 1.
• If RT queue is empty, check the nonRT queue.
o If there is a nonRT message waiting in the queue calculate
its service time 𝑆𝑇 and set 𝑆𝐶𝐿 = 𝑀𝐶 + 𝑆𝑇.
o Set 𝑠 = 2.
o Decrease 𝑛l–l¶+ by 1.
• If both queues are empty, the server becomes idle. Set 𝑠 = 0.
• Go back to the beginning to locate the next event.

8.1.2. Task 1: Hand simulation


We start with the hand simulation shown in table 8.1. For this, the
following simplifying assumptions have been made:

• The inter-arrival time of RT messages is constant equal to 10


• The inter-arrival time of nonR is constant equal to 5
• The service time of an RT message is constant equal to 2
• The service time of a nonRT message is constant equal to 4.

The following initial conditions at 𝑀𝐶 = 0 are used: 𝑅𝑇𝐶𝐿 = 3,


𝑛𝑜𝑛𝑅𝑇𝐶𝐿 = 5, 𝑛¶+ = 0, 𝑛l–l¶+ = 0, 𝑠 = 2, 𝑆𝐶𝐿 = 4.

Note that it is possible that an arrival event may occur at the same
time as a service completion event. In this case, first execute the ser-
vice completion event and show the resulting status variables in a
new line in the table and then execute the arrival event and show the
results in the next line. Alternatively, execute both events and show
the resulting status variables in a single line in the table. The problem
of simultaneously occurring events will go away in the subsequent
two tasks because all the clocks will be defined as real variables.
143
Simulation Techniques Harry Perros
Task 1.1: Continue the hand simulation started in table 8.1 until
𝑀𝐶 = 50. (The last column in the table gives the remaining service
time 𝑅𝑆𝑇 of a pre-empted nonRT message.) Check your hand simula-
tion table against the correct solution given in section 8.1.5 to make
sure that you have not made any errors.

𝑴𝑪 𝑹𝑻𝑪𝑳 𝒏𝒐𝒏𝑹𝑻𝑪𝑳 𝒏𝑹𝑻 𝒏𝒏𝒐𝒏𝑹𝑻 𝑺𝑪𝑳 𝒔 RST


0 3 5 0 0 4 2
3 13 5 0 1 5 1 1
5 13 10 0 2 5 1 1
5 13 10 0 1 6 2
6 13 10 0 0 10 2
10 13 15 0 1 10 2
10 13 15 0 0 14 2
13 23 15 0 1 15 1 1

Table 8.1: Hand simulation for task 1.1

𝑴𝑪 𝑹𝑻𝑪𝑳 𝒏𝒐𝒏𝑹𝑻𝑪𝑳 𝒏𝑹𝑻 𝒏𝒏𝒐𝒏𝑹𝑻 𝑺𝑪𝑳 𝒔 𝑹𝑺𝑻


0 3 5 0 0 4 2
3 8 5 0 1 7 1 s=1
5 8 15 0 2 7 1 s=1
7 8 15 0 1 8 2
8 13 15 0 1 12 1
12 13 15 0 0 14 2

Table 8.2: Hand simulation for task 1.2

Task 1.2: Do the hand simulation started in table 8.2 until 𝑀𝐶 = 20.
This is the same simulation as above but now we have switched the
input parameters as follows. The inter-arrival time of RT messages is
constant equal to 5, the inter-arrival time of nonR is constant equal to
10, the service time of an RT message is constant equal to 4, and the
service time of a nonRT message is constant equal to 2. The initial
conditions are the same. Compare your hand simulation table with the

144
Harry Perros Simulation Techniques
correct solution given in section 8.1.5 to make sure that you have not
made any errors.

8.1.3. Task 2: Write the basic simulation code


In this task, you will write the simulation code to reproduce the hand
simulation. Also, you will introduce randomness in your simulation
by assuming that the inter-arrival and service times are exponentially
distributed.

Task 2.1: Use a programming language of your choice to code your


hand simulation and reproduce the tables in tasks 1.1 and 1.2.
Run your code with the same input values as in task 1.1 and print out
the same table you produced in task 1.1. Check the output by hand to
verify that it is the same as in task 1.1. If it is not the same, go
through your code to identify the errors. Repeat the same for the input
values used in task 1.2.
Task 2.2: Introduce randomness in your simulation
Use the program you developed in task 2.1, with the same initial con-
ditions, but assume that the inter-arrival times and service times are
exponentially distributed. Make sure you define the clocks as real
variables.

Let 𝑀𝐼𝐴𝑇¶+ , 𝑀𝐼𝐴𝑇l–l¶+ , 𝑀𝑆𝑇¶+ and 𝑀𝑆𝑇l–l¶+ be the mean inter-


arrival time of RT messages, the mean inter-arrival time of nonRT
messages, the mean service time of an RT message, and the mean
service time of a nonRT message. The mean values are the same as
the input values used in task 1.1, i.e., 𝑀𝐼𝐴𝑇¶+ = 10, 𝑀𝐼𝐴𝑇l–l¶+ = 5,
𝑀𝑆𝑇¶+ = 2, and 𝑀𝑆𝑇l–l¶+ = 4. Each time you want to generate an
inter-arrival time or a service time, draw a pseudo-random number 𝑟
and then obtain the exponential variate using the expression:
−(𝑚𝑒𝑎𝑛)𝑙𝑜𝑔Ø (𝑟).

Print a line of output each time you handle an event, i.e., you advance
the master clock, with the same information as in the tables in task 1.
Run your simulation until 𝑀𝐶 exceeds 200.

145
Simulation Techniques Harry Perros
8.1.4. Task 3: Statistical estimation of the response time
The objective of this task is to use the simulation you developed in
task 2 in order to calculate the mean and 95th percentile and their
confidence intervals of the response time 𝑅¶+ and 𝑅l–l¶+ of the RT
and nonRT messages respectively. We recall that 𝑅¶+ is the time
elapsed from the moment an RT message arrives at the RT queue to
the moment it is fully processed and departs from the server. 𝑅l–l¶+
is defined similarly.

For this, you have to modify your program as follows:

• Stop printing a line of output each time you handle an event as


in tasks 1 and 2. In other words, do not print the hand simula-
tion table.
• Add a data structure to collect data on the response time.
o For this, you will have to implement a queue structure for
the RT and another one for the nonRT queues. Arrivals are
added to the end of the queue, and messages are served
from the top. Each data element represents a different
message and it should contain the time of arrival.
o When an RT message completes its service and departs
from the server, calculate the elapsed time for the message
by subtracting the current time from the time of its arrival.
Save it in the array 𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒.
o Same as above for nonRT messages. Save the elapsed
times in the array 𝑛𝑜𝑛𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒.
o After you simulate the required number of messages, use
the two arrays 𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 and 𝑛𝑜𝑛𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 to calculate
the required statistical estimates. If space is a problem,
you can use the 𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 and 𝑛𝑜𝑛𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 arrays to
store the response times from one batch, and calculate the
batch mean and batch 95th percentile after you have simu-
lated the total number in the batch. Save this information
in another array and reuse the two arrays for the next
batch.
• Implement the batch means method to construct the confi-
dence intervals.

146
Harry Perros Simulation Techniques
• Calculate the mean, 95th percentile and their confidence in-
terval of 𝑅¶+ and 𝑅l–l¶+ .

The length of the simulation is controlled by m, the number of batch-


es and the number of observations b within a batch. Set m = 51, and b
= 1,000. (A large batch size is required in order to estimate the per-
centile accurately.) In order to allow for the initial conditions, ignore
the results from the first batch, and calculate all your performance
metrics using the remaining 50 batches. Run your simulation until
you have mb observations collected in the arrays 𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 and
𝑛𝑜𝑛𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒. Then apply the batch means method to the
𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 and 𝑛𝑜𝑛𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 arrays separately. It is possible that
one array may have more than mb observations. In this case use only
the first mb observations.

Set 𝑀𝐼𝐴𝑇¶+ = 7, 𝑀𝑆𝑇¶+ = 2, 𝑀𝑆𝑇l–l¶+ = 4, and run your simula-


tion for different values of 𝑀𝐼𝐴𝑇l–l¶+ ranging from 10 to 40 in in-
crements of 5. For each value of 𝑀𝐼𝐴𝑇l–l¶+ obtain the mean, 95th
percentile, and confidence intervals of 𝑅¶+ and 𝑅l–l¶+ . Graph all
your results on the mean response times of 𝑅¶+ and 𝑅l–l¶+ including
the confidence intervals (drawn as vertical bars), as a function of
𝑀𝐼𝐴𝑇l–l¶+ . Likewise, graph all your results for the 95th percentile.
Comment on all your results.

The correct plots for the above results are given in section 8.1.5. Your
results may not match entirely these plots due to the randomness in
the simulation.

8.1.5. Correct simulation results


Tables 8.3 and 8.4 give the complete hand simulation for tasks 1.1
and 1.2 respectively, and figures 8.2 and 8.3 give the plots for tasks 3.
Use these results to verify that your simulation model is correct. Re-
member, however, that in practice you will not have such results
available and you will have to rely on the validation checks described
in Chapter 6.

147
Simulation Techniques Task 1.1
Harry Perros
MC RTCL nonRTCL n_RT n_nonRT SCL Status (s) Pre-empted time
0 3 5 0 Task 1.1 0 4 2 0
MC 3 RTCL 13 nonRTCL5 n_RT 0 n_nonRT1 SCL 5 Status (s)1 Pre-empted time 1
50 13
3 10
5 00 02 45 21 01
53 13
13 10
5 00 11 56 12 10
65 13
13 10
10 00 20 10
5 12 10
105 13
13 15
10 00 11 10
6 22 00
106 13
13 15
10 00 00 14
10 22 00
13
10 23
13 15
15 00 11 15
10 21 01
15
10 23
13 20
15 00 02 15
14 21 01
15
13 23 20
15 0 11 16
15 12 10
16
15 23 20 0 20 20
15 12 10
20
15 23 25
20 0 11 20
16 22 00
20
16 23 25
20 0 00 24
20 22 00
23
20 33
23 25 0 11 25
20 21 01
25
20 33
23 30
25 0 02 25
24 21 01
25
23 33 30
25 0 11 26
25 12 10
26
25 33 30 0 20 30
25 12 10
30
25 33 35
30 0 11 30
26 22 00
30
26 33 35
30 0 00 34
30 22 00
33
30 43
33 35 0 11 35
30 21 01
35
30 43
33 40
35 0 02 35
34 21 01
35
33 43 40
35 0 11 36
35 12 10
36
35 43 40 0 20 40
35 12 10
40
35 43 45
40 0 11 40
36 22 00
40
36 43 45
40 0 00 44
40 22 00
43
40 53
43 45 0 11 45
40 21 01
45
40 53
43 50
45 0 02 45
44 21 01
45
43 53 50
45 0 11 46
45 12 10
45
46 53 50 0 20 45
50 12 10
45
50 53 50
55 0 11 46
50 22 00
46
50 53 50
55 0 00 50
54 22 00
50 53 55 0 1 50 2 0
50 53 55 0 Task 1.2 0 54 2 0
MC RTCL Table 8.3: Correct
nonRTCL num_RT hand simulation
num_nonRT SCL forStatus
task 1.1Pre-empted time
0 3 5 0 Task 1.2 0 4 2 0
MC 3 RTCL 8 nonRTCL 5 num_RT 0 num_nonRT 1 SCL 7 Status 1 Pre-empted time 1
50 3
8 5
15 0 02 47 21 01
73 8 5
15 0 11 78 12 10
85 8
13 15 0 21 7
12 11 10
127 8
13 15 0 10 8
14 22 00
138 13
18 15 0 11 12
17 11 01
12
15 13
18 15
25 0 02 14
17 21 01
13
17 18 15
25 0 11 17
18 12 10
15
18 18
23 25 0 21 17
22 11 10
17
22 18
23 25 0 10 18
24 22 00
18 23 25 0 1 22 1 0
22 23 25 0 0 24 2 0

Table 8.4: Correct hand simulation for task 1.2


148
Harry
Graphs: Perros Simulation Techniques

!"#$%&%'(
Figure 8.2: Mean and confidence intervals for RT and nonRT messages

!"#$%&%'(
The mean for RT remains almost constant whereas for Non-RT is decreasing. This is because
in real-time jobs,Figure 8.3: 𝟗𝟓th
the interarrival percentile
time does andthe
not impact confidence intervals
response time due to the fact that
for RTstarts
once a new job arrives, it immediately andprocessing
nonRT messages
(except when the server is already
processing a real-time job) since they have higher priority. Hence, the response time remains
almost constant and nearer to the service time of a real-time job.

149
In the case of non real-time jobs, as the interarrival time increases the response time decreases
because the larger IAT allows more jobs in the queue to be processed and hence decreases the
pending queue length. The jobs do not need to wait for a long time to start processing (if there
are no jobs in RT queue) and hence are also completed quickly, thereby reducing the response
Simulation Techniques Harry Perros
8.2. A simulation of the flow of SIP messages
In order to make a voice or video call, many messages are exchanged
between the calling and the called devices prior to the moment where
we start to speak. Several specialized servers are also involved in this
message exchange to provide different functionalities, such as, au-
thentication and location of the called device. These messages are
used to setup the call, modify it, and finally tear it down when the us-
ers hang up. SIP is a very popular protocol that defines the structure
of these messages, the functionality of the various servers, and the
flow of messages between the two devices.

For those familiar with SIP, in this simulation project you will model
the flow of SIP messages through a P-CSCF, an S-CSCF, and an ap-
plication server (AS). One does not have to be familiar with the con-
tent of the SIP messages and the functionality of the SIP servers,
since in this simulation we abstract the problem into a simulation of
the flow of messages through a number of service stations, each rep-
resenting a different SIP server. Such a network of interconnected
service stations through which customers flow following different
paths is known as a queueing network. These queueing models are
technology independent and they are used to model many situations,
such as, the flow of jobs in a manufacturing process, the flow of que-
ries to a web server through the different web services that are in-
volved in the processing of a query, the flow of parcels ordered on-
line through a delivery system, and the flow of IP packets through a
computer network.

AU

!
Figure 8.4: The queueing system under study

150
Harry Perros Simulation Techniques
The queueing network under study consists of three service stations,
namely, P-CSCF, S-CSCF, and AS, as shown in figure 8.4. A cus-
tomer in this queueing network represents a SIP message, and the
flow of customers through the three service stations is depicted by the
circled numbers shown in figure 8.4. Each service station consists of
a queue where customers are waiting and a server that serves the cus-
tomers. (Here the word “server” means a service point and not a
computer server!) A customer arrives at the P-CSCF queue (step 1),
waits for its turn, gets served and then moves into the S-CSCF queue
(step 2). There it waits for its turn and gets processed by the server.
Upon service completion, the customer moves to the AS queue (step
3). After the AS processes the request, the customer goes back and
joins the S-CSCF queue (step 4), and then after it is processed it goes
back and joins the P-CSCF queue (step 5). Upon service completion,
it departs from the system (step 6). We observe that in the first two
queues we have new customers moving towards the AU server, and
recirculated ones going backwards. We need to distinguish between
these two types of customer, so we know how to route them.

The objective of this simulation is to estimate the end-to-end delay,


also known as the response time. This is the amount of time elapsed
from the moment a message arrives at the queueing network to the
moment it departs from it.

The inter-arrival time of new customers at the P-CSCF and the ser-
vice times at the three queues are assumed to be exponentially dis-
tributed. Let 𝐼𝐴𝑇 be the mean inter-arrival time at the P-CSCF queue,
and let 𝑀𝑆𝑃, 𝑀𝑆𝑆, 𝑀𝑆𝐴 be the mean service at the P-CSCS, S-CSCF
and AS queues, respectively. Customers are processed in each queue
in the order in which they arrive, that is, in first in first out (FIFO)
manner.

8.2.1. The simulation logic


The basic events that change the state of the system are:

1. Arrival of a new customer at the P-CSCF queue


2. Completion of a service time at the P-CSCF queue
3. Completion of a service time at the S-CSCF queue
151
Simulation Techniques Harry Perros
4. Completion of a service time at the AS queue.

For each event we define a clock, which gives the time when the
event will occur. Let 𝐴𝐶, 𝑆𝐶𝑃, 𝑆𝐶𝑆, and 𝑆𝐶𝐴 be the arrival clock,
service completion clock at the P-CSCF, service completion clock at
the S-CSCF, and service completion clock at the AU, respectively.
Also, let 𝑀𝐶 be the master clock.

For the simulation model, will we use the event-advance design. That
is, after completing servicing an event, we check all the future events
to see which of them will occur next. Subsequently, we advance
𝑀𝐶 to the time of occurrence of this event and take appropriate ac-
tion, and then we go back to locate the next event, and so on until a
stopping rule is satisfied. Below, we describe the actions taken when
an event occurs.

1. Arrival of a new customer to the P-CSCF queue:

The first task is to decide when the next arrival of a new customer
will occur. For this, we generate an exponential variate 𝑡 with a mean
𝐼𝐴𝑇 and update the arrival clock, i.e., 𝐴𝐶 = 𝑀𝐶 + 𝑡. Recall that to
generate an exponential variate 𝑡, we first generate a pseudo-random
number 𝑟 and then set 𝑡 = −(𝑚𝑒𝑎𝑛)𝑙𝑜𝑔Ø (𝑟), where 𝑚𝑒𝑎𝑛 is the
mean of the exponential distribution.

If the queue is empty and the server is idle, the new customer goes
into service and for this we generate an exponentially distributed ser-
vice time 𝑡 with mean 𝑀𝑆𝑃 and update the service completion clock
𝑆𝐶𝑃, i.e., 𝑆𝐶𝑃 = 𝑀𝐶 + 𝑡. If the server is busy, then the customer
joins the queue and no further action is taken.

2. Service completion at the P-CSCF

If the departing customer is a new customer, then it joins the S-CSCF


queue. If it is a recirculated customer that has come back from the S-
CSCF, then it leaves the system.

If it moves to the S-CSCF queue, then we follow the same logic as


above. That is, if there is no one in the queue and the S-CSCF server
152
Harry Perros Simulation Techniques
is idle, then the customer goes into service and we generate a new
service time and update the service completion clock 𝑆𝐶𝑆. If the serv-
er is busy, then the customer joins the queue and no further action is
taken.

In addition, if there are customers waiting in the P-CSCF queue, then


the next customer will start its service and for this we generate a new
service time and update the service completion clock 𝑆𝐶𝑃.

3. Service completion at the S-CSCF

If the departing customer arrived from the P-CSCF, then it joins the
AS queue. Otherwise, it joins the P-CSCF queue. The rest of the logic
is the same as above. That is, if the customer finds the P-CSCF or AS
server idle (i.e., it is not busy and no one is waiting in the queue),
then it starts a new service and the appropriate service completion
clock is updated. Otherwise, it just joins the queue. In addition, if
there are customers waiting in the S-CSCF queue, then the next in
line starts service.

4. Service completion at the AS

The departing customer joins the S-CSCF queue. If it finds the S-


CSCF server idle, i.e., it is not busy and no one is waiting in the
queue, then it starts a new service and the service completion clock
𝑆𝐶𝑆 is updated. Otherwise, it just joins the queue. In addition, if there
are customers waiting in the AS queue, then the next in line starts
service and the service completion clock 𝑆𝐶𝐴 is updated.

8.2.2. Implementation and results


The easiest way to implement the event list is to use a linear sequen-
tial array as described in section 4.4. Also, each queue should be im-
plemented as a linked list, see section 4.5, where each data element
represents a customer and contains the time it arrived at the P-CSCF
queue for the first time as a new customer and whether it is a new
customer or a recirculated one. When the customer departs from the
queueing network, calculate the total time it spent in the system and
save this information is a separate array.

153
Simulation Techniques Harry Perros
The objective of this simulation is to estimate the mean and the 95th
percentile and confidence intervals of the end-to-end delay. This is
the amount of time elapsed from the moment a customer arrives at the
P-CSCS queue as a new customer to the moment it departs from the
P-CSCF queue as a recirculated customer after it has visited the other
queues.

Start your simulation assuming that all the queues are empty. In order
to get the simulation going you need to pre-generate the time that the
first arrival will occur at the P-CSCF queue. In order to eliminate the
effects of the initial condition, run the simulation for 100 departures
first, and after that, start the batch method. Simulate 30 batches of
2000 observations, i.e., departures from the queueing network. For
each batch calculate the mean and the 95th percentile of the end-to-
end delay, and at the end of the simulation construct their confidence
intervals.

Set 𝑀𝑆𝑃 = 0.1 sec, 𝑀𝑆𝑆 = 0.2 sec, and 𝑀𝑆𝐴 = 0.5 sec, and run
your simulation for different values of the 𝐼𝐴𝑇, ranging from 0.67 to
2 in increments of 0.15. Plot your mean end-to-end delay and its con-
fidence interval as a function of 𝐼𝐴𝑇. Same for the 95th percentile.
Comment on your results. Which SIP server in your opinion may be-
come a bottleneck?

You can validate your simulation model using the following two data
points:

• 𝐼𝐴𝑇 = 1: Confidence interval for the mean end-to-end delay:


(1.89946, 1.94703). Confidence interval for the 95th per-
centile end-to-end delay: (4.04669, 4.24222).
• 𝐼𝐴𝑇 = 2: Confidence interval for the mean end-to-end delay:
(1.38601, 1.40044). Confidence interval for the 95th per-
centiles end-to-end delay: (2.85748, 2.89641).

Note that your results maybe slightly different due to the randomness
in the simulation.

154
Harry Perros Simulation Techniques
8.3. A simulation model of a multiplexer
A multiplexer is a device used in computer networks to merge many
different streams of packets originating from different locations into a
single stream that is transmitted out to a single destination. Figure 8.5
gives an example of a multiplexer with four arrival streams. The
packets, indicated by small colored orthogonals, join a common
queue from where they are transmitted out one at a time. The queue is
referred to as the buffer and its size is finite with capacity 𝐵. That is,
the number of packets waiting in the buffer plus the one being trans-
mitted out cannot exceed the value 𝐵. This means that if a packet ar-
rives at a time when the buffer is full, the packet is lost. Therefore, of
interest is how to size the buffer so that only a given percentage of
packets are lost.

Another feature of this particular multiplexer is that all the packets


have the same length. That is, they all consist of the same number of
bytes. This is not the case with the packets in the Internet currently
which have a variable length. However, back in the 90s, a novel net-
working architecture was developed, known as ATM (not related to
ATM machines!), which revolutionized the way computer networks
worked. This network is not in use anymore, but many of its scheme
are used in the Internet so that voice, video, and data can be all car-
ried simultaneously. Our interest in simulating this multiplexer is that
it is a good example of a discrete-time simulation, as opposed to all
the other simulation models in this book which are in continuous
time.

Stream 1

Stream 2

Stream 3

B
Stream 4
8 7 6 5 4 3 2 1
slots
Figure 8.5: The multiplexer with four arrival streams

155
Simulation Techniques Harry Perros
The time axis is slotted, and each slot is long enough to accommodate
the transmission of a packet. That is, it takes one slot to transmit out a
packet from the buffer, and one time slot for a packet to arrive at the
buffer. For simplicity, we will assume in this model that there are four
arriving streams of packets, as shown in figure 8.5. The arrival of
packets for all four streams is synchronized. That is, in slot 𝑖, we may
have an arrival from each stream, up to a total of four arrivals, and
also during the same slot, we may have a departure from the buffer.
For instance, let us consider time slot 1 in figure 8.5, and let us as-
sume that there are 3 packets in the buffer, one of which is being
transmitted out. Then at the end of the time slot 1, a packet has de-
parted from the buffer and 4 new packets have arrived. That is, now
the buffer contains 6 packets. At the end of time slot 2, we have one
arrival and one departure, which means that the buffer still contains 6
packets. At the end of slot 3, we have two arrivals and one departure,
and the buffer contains 7 packets, and so on.

In order to construct the simulation model, we also need to describe


how the packets of a single stream arrive at the buffer. In the continu-
ous-time simulation models we have assumed that the inter-arrival
time of customers is exponentially distributed. The equivalent of the
exponential distribution in discrete time is the geometric distribution,
see section 3.4.2. That is, for a given stream, let 𝑝 and 1 − 𝑝 be the
probability that a time slot contains a packet or it is empty. Then, the
probability that the interarrival time between two successive packets
is 𝑖 slots is: (1 − 𝑝)R 𝑝. In the simulation model, we do not have to
generate geometric variates, as there is a simpler way to determine
whether a packet from a given stream has arrived or not. That is, for
each slot we draw a pseudo-random number 𝑟, and if 𝑟 ≤ 𝑝, then the
slot contains a packet. Otherwise, it is empty.

8.3.1. Simulation logic


The simulation model under the assumptions described above is ex-
tremely simple. We use the unit-time design, where the unit is one
slot. That is, we advance the master clock one slot each time, and take
care of the events that occur at the end of the slot. Also, as will be
seen below, we do not need to keep an event list.

156
Harry Perros Simulation Techniques
At the end of each slot do the following:

• If the buffer has at least one packet, then one packet departs.
Update the total number of packets in the buffer.
• Draw a pseudo-random number 𝑟, and if 𝑟 ≤ 𝑝, then a packet
from stream 1 has arrived at the buffer. Else, no arrival from
stream 1.
• Repeat above step for streams 2, 3, and 4.
• Calculate how many of these arriving packets will be admitted
to the buffer, based on the current number of packets in the
buffer, and update the number of packets in the buffer.
• Keep a counter 𝑁 of the total number of arrivals and another
counter 𝐿 of the total number of lost packets.
For the initial conditions assume that the buffer is empty. There is no
need to make an assumption as to when the first packet in each stream
will arrive, as we have done in previous simulations.

8.3.2. Results
The objective of this simulation is to calculate the packet loss 𝑝ö–˜˜
for different values of 𝐵 and 𝑝. The packet loss is defined as the per-
centage of the arriving packets that do not enter the buffer, i.e.,
𝑝ö–˜˜ = 𝐿/𝑁. The estimation of the confidence interval can be done
using the batch means method as described in section 5.7, after we
allow for the warm-up period. That is, let 𝑝ö–˜˜,R be the packet loss
calculate in batch 𝑖, 𝑖 = 1,2, … 𝑚. Then, the confidence interval is:
𝑠 𝑠
Û𝑝̅ö–˜˜ − 𝑡c.õ² , 𝑝̅ö–˜˜ + 𝑡c.õ² Ý
√𝑚 √𝑚
where
| |
1 1 f
𝑝̅ö–˜˜ = ž 𝑝ö–˜˜,R and 𝑠 f = žŸ𝑝ö–˜˜,R − 𝑝̅ .
𝑚 𝑚−1
R¢\ R¢\

Run your simulation for different values of 𝐵 and 𝑝 in order to see


how the packet loss probability varies as a function of these two pa-
rameters. Specifically, estimate the packet loss for 𝑝, 0 ≤ 𝑝 ≤ 1, for
157
Simulation Techniques Harry Perros
𝐵 = 1. Then repeat this process for 𝐵 = 2, and so on. Stop when you
see a small difference between the results for two different values of
𝐵. Plot all your results on the same graph, a different plot for each
value of 𝐵, and discuss your conclusions.

Stream 1
voice

Stream 2
video

Stream 3
data
Stream 4
8 7 6 5 4 3 2 1
slots
B
Figure 8.6: The multiplexer with priorities

8.3.3. An extension of the simulation model


In this section, we describe a slightly more complicated version of the
above model. So far, we have assumed that all packets in the buffer
have the same priority. However, this is not the case as typically dif-
ferent packets are given different priorities for transmission out of the
buffer. In figure 8.6, we see that the buffer consists of three queues:
one for packets carrying voice, a separate one for packets carrying
video, and a third one for packets carrying data. The priority scheme
is the same as in the simulation project described in section 8.1, but
without pre-emption. The voice queue has priority over the video
queue, which has priority over the data queue. Each time the server
completes a service, i.e., it completes a transmission of a packet, it
checks the voice queue, and if it is not empty it starts serving the
packet at the top of the queue. Otherwise, it checks the video queue.
If it is not empty, it starts serving the packet at the top of the queue.
Otherwise, it checks the data queue and if it is not empty, it starts
serving the packet at the top of the queue. Otherwise, and the server
become idle. This procedure is repeated each time a packet completes
its service.

As before, we assume four arrival streams, and a slotted time struc-


ture. It takes one slot for a packet to arrive and one slot for a packet to
158
Harry Perros Simulation Techniques
be served, i.e., transmitted out of the buffer. Each incoming stream
consists of 20% voice packets, 20% video pckets, and 60% data
packets. That is, a slot in a stream contains a packet with probability
𝑝, and this packet is a voice, video, or data packet with probability
0.20, 0.20, and 0.60 respectively.

The three queues share the total buffer space 𝐵. In order to make sure
that each queue 𝑖 does not grow too big so that it takes up most of the
buffer space 𝐵, we impose a low bound 𝐿R and an upper bound 𝑈R .
The upper bound is used to limit how big the 𝑖th queue can grow, and
it is selected so that the sum of the upper bounds of the four queues is
larger than 𝐵. The lower bound 𝐿R can be seen part of the duffer that
is dedicated permanently to the 𝑖th queue. That is, the 𝐿R buffer spac-
es are only used to store packets belonging to the 𝑖th queue. The sum
of the lower bounds of the four queues is less than 𝐵. Therefore, the
total number of buffer spaces that can be shared by the three queues is
𝐵 − 𝐿.

In order to clarify further how the upper and lower bounds work, let
us consider the 𝑖th queue and let us assume that it is empty. The first
packet that arrives to the 𝑖th queue is stored in the buffer, and the total
number of d spaces in the dedicated buffer of the 𝑖th queue is reduced
by one. This continues until all dedicated spaces for the 𝑖th queue are
used. When a packet arrives to the 𝑖th queue at a time when all dedi-
cated spaces have been used, the packet will be accepted if the fol-
lowing two conditions should be satisfied:

• The total number of packets in the 𝑖th queue is less than its
upper bound 𝑈R .
• For each queue 𝑖, calculate the number of packets 𝑥R which are
over its lower bound 𝐿R . If the number of packets in the 𝑖th
queue is less or equal to 𝐿R then 𝑥R = 0. Then, the sum 𝑥\ +
𝑥f + 𝑥e has to be less than 𝐵 − 𝐿.
The simulation logic is the same as in the simulation model described
above with the following additions. First, we have to decide which of
the three queues a new arrival will join, and whether it will be saved
in the buffer or it will be rejected, i.e., it will be lost. Also, we have to

159
Simulation Techniques Harry Perros
implement the priority scheme described above. The main logic of the
model is summarized below.

At the end of a time slot, do the following:

• Select the next queue to serve, and if it is not empty then one
packet departs. Update the total number of packets in the
queue. If all queues are empty, then no departure occurs.
• Draw a pseudo-random number 𝑟, and if 𝑟 ≤ 𝑝, then a packet
from stream 1 has arrived at the buffer. Else, no arrival from
stream 1. If we have an arrival, then:
o Draw a pseudo-random number r.
o If 𝑟 ≤ 0.20, then the new arrival is a voice packet.
o If 0.20 < 𝑟 ≤ 0.40, then the new arrival is a video
packet.
o If 0.40 < 𝑟 ≤ 1, then the new arrival is a data packet.
Once the type of packet has been identified determine whether
the packet is accepted or rejected.
• Repeat above step for streams 2, 3, and 4.
• Keep a counter 𝑁R of the total number of arrivals and another
counter 𝐾R of the total number of lost packets for each queue
𝑖, 𝑖 = 1,2,3.
At the end of the simulation, the packet loss per queue 𝑖 is determined
using the expression: 𝐿R /𝐾R , 𝑖 = 1,2,3. The confidence interval of the
packet loss per queue can be estimated using the batch means method
as explained above in section 8.3.2. Notice that the batch means
method has to be applied for each queue separately. As before, ignore
the first 100 arrivals so that to account for the effects of the initial
conditions. Then, run your simulation until you have generated the
required number of arrivals, i.e., observations, for each queue. As a
result, you will generate more arrivals for a queue than you need. You
can ignore the additional arrivals or simply use them to create addi-
tional batches.

The performance question of interest regarding this multiplexer, and


any other multiplexer for that matter, is to determine the size of 𝐵 and
lower and upper bounds for each queue so that the packet loss per
queue is less than a predefined number for a given a traffic load. The
160
Harry Perros Simulation Techniques
traffic load is expressed in terms of the probability 𝑝, 0 < 𝑝 ≤ 1. The
higher its value, the more packets arrive. Also, the predefined packet
loss varies from one queue to another. Typically, we want a very low
packet loss for the voice and video queues and a slightly higher one
for the data queue. In real life, the packet loss should be about 1% for
the voice and video packets and about 5% for the data packets. In or-
der to simplify the simulation experiments, we will assume 10%, 10%
and 20% packet loss for the voice, video, and data packets respective-
ly. So, the question you need to address using this simulation model is
as follows. Determine the values for 𝐵, 𝐿\ , 𝑈\ , 𝐿f , 𝑈f , 𝐿e , 𝑈e so that
the packet loss satisfies the above requirements for a given value of 𝑝.

The values of the above parameters can be determined by running a


grid search. That is, we create a set of different values for each of the
unknown parameters, and then run a simulation for each combination
of the values of the parameters. The best combination or combina-
tions that satisfy the desired packet loss can then be selected. This
approach will obviously result in a huge number of simulations. In
order to reduce the number of simulations, we can create a range of
values for each parameter using large increments, which will result to
a coarse grid of different combinations of values of these parameters.
Then, we run a simulation for each of these combinations with a view
to locating an area of the grid, i.e., a set of combinations, where the
best value(s) may lie. Subsequently, we carry out a refined grid
search within that area using small increments.

For this problem, we will further simplify the experimentation by as-


suming that 𝐿\ = 𝐿f = 𝐿e = 0.20𝐵, and 𝑈\ = 𝑈f = 𝑈e = 0.40𝐵. So,
now the search is reduced to simply determine the value of 𝐵 for
which the packet loss constraints are satisfied. Repeat this experiment
for different values of 𝑝, and plot the best value of 𝐵 versus 𝑝. Dis-
cuss your results.

161

You might also like