Simulation
Where real stuff starts
ToC
1. What, transience, stationarity2. How, discrete event, recurrence
3. Accuracy of output4. Monte Carlo
5. Random Number Generators6. How to sample
2
1 What is a simulation ?
An experiment in computerImportant differences
Simulated vs real timeSerialization of events
3
Stationarity
A simulation can be terminating or not
For a terminating simulation you should make sure it is stationary
4
Information server, scenario 1
5
Information server, scenario 2
6
Stationarity
In scenario 1:Transient phase, followed by “typical” (=stationary) phase You want to measure things only in the stationary phase
Otherwise: non reproduceable, non typical
In scenario 2:There is no stationary regime“walk to infinity”
you should not do a non terminating simulation with this scenario
7
Definition of Stationarity
A property of a stochastic model Xt
Let Xt be a stochastic model that represents the state of the simulator at time t. It is stationary iff
for any s>0
(Xt1, Xt2, …, Xtk) has same distribution as (Xt1+s, Xt2+s, …, Xtk+s)
i.e. simulation does not get “old”
8
Classical Cases
Markov modelsState Xt is sufficient to draw the future of the simulation
Common case for all simulations
For a Markov model, over a discrete state spaceIf you run the simulation long enough it will either walk to infinity (unstable) or converge to stationary
Ex: queue with >1: unstablequeue with <1: becomes stationary after transient
If the state space is strongly connected (any state can be reached from any state) then there is 0 or 1 stationary regime
Ex: queue
Else, there may be several distinct stationary regimes Ex: system with failure modes
9
Stationarity and Transience
Knowing whether a model is stationary is sometimes a hard problemWe will see important models where this is solvedEx: Solved for single queues, not for networksEx: time series models
Reasoning about your system may give you indicationsDo you expect growth ? Do you expect seasonality ?
Once you believe your model is stationary, you should handle transientsRemove (how ? Look at your output and guess)Sometimes it is possible to avoid transients at all (perfect simulation)
10
Non-Stationary (Time Dependent Inputs)
11
Typical Reasons For Non Stationarity
Obvious dependency on timeSeasonality, growthCan be ignored at small time scale (minute)
Non Stability: ExplosionQueue with utilization factor >1
Non Stability: Freezing Simulation System becomes slower with time (aging)
Typically because there are rare events of large impact (« Kings »)The longer the simulation, the larger the largest king
Ex: time between regeneration points has infinite mean
We’ll come back to this in the chapter « Importance of the View Point »
12
2 Simulation Techniques
Discrete event simulationsRecurrences
Stochastic recurrences
13
Discrete event simulationUses an Event Scheduler
Example:Information system modelled as a single server queueThree event classes
arrivalserviceDeparture
One event scheduler(global system clock)
14
0 t1 t2 t3
queuelength
1
2
t4
arrival service departure
Scheduler: Timeline
15
departuret=t2
servicet=t2
arrivalt=t4
servicet=t2
departuret=t3
arrivalt=t4
departuret=t3
arrivalt=t4
servicet=0
arrivalt=t1
arrivalt=0
arrivalt=t1
departuret=t2
servicet=0
departuret=t2
arrivalt=t4
arrivalt=t1
arrivalt=0
initialization
0 t1 t2 t3
queuelength
1
2
t4
Stat
e of
sch
edul
erE
ven
t bei
ng
exec
ute
d
Step 1 Step 2 Step 3 Step 4 Step 5 Step 6
Statistical Counters
Assume we want to output: mean queue length and mean response time. How do we do this ?
Note the difference betweenEvent based statisticTime based statistic
16
A Classical Organization of Simulation Code
Events contain specific codeA main loop advances the state of the schedulerExample: in the code of a departure event
17
Main program
18
Stochastic Recurrence
An alternative to discrete event simulationfaster but requires more work on the modelnot always applicable
Defined by iteration:
19
Example: random waypoint mobility model
20
21
22
Queuing System implemented as Stochastic Recurrence
23
24
3 Accuracy of Simulation Output
A stochastic simulation produces a random output, we need confidence intervalsMethod of choice: independent replicationsRemove transients
For non terminating simulations
Be careful to have truly random seedsEx: use computer time as seed
25
Results of 30 Independent Replications
26
Do They Look Normal ?
27
Bootstrap Replicates
28
Confidence Intervals
29
Mean, normal approx
Median
Mean, bootstrap
5 Random Number Generator
A stochastic simulation does not use truly random numbers but pseudo-random numbers
Produces a random number » U(0,1)Example (obsolete but commonly used, eg in ns2: )
Output appears to be random (see next slides)
30
The Linear Congruential Generator of ns2
31
uniform qq plot
autocorrelation
Lag Diagram, 1000 points
32
Period of RNG
RNG is in fact periodicPeriod should be much larger than maximum number of uses
33
Two Parallel Streams with too simple a RNG
34
Impact of RNG
35
Take Home Message
Be careful to have a RNG that has a period orders of magnitude larger than what you will ever use in the simulationSerialize the use of the RNG rather than parallel streams
36
6 Sampling From A Distribution
Pb is: Given a distribution F(), and a RNG, produce some sample X that is drawn from this distribution
A common task in simulationMatlab does it for us most of the time, but not alwaysTwo generic methods
CDF inversionRejection sampling
37
CDF Inversion
Applies to real or integer valued RV
38
39
Example: integer valued RV
40
41
Rejection SamplingApplies more generally, also to joint n-dimensional distributionsExample 1: conditional distribution on this areaStep1 :
Can you sample a point uniformly in the bounding rectangle ?
Step 2:How can you go from there to a uniform sample inside the non convex area ?
42
1000 samples
Rejection Sampling for Conditional Distribution
This is the main idea
How can you apply this to the example in the previous slide ?
43
Rejection Sampling for General Distributions
44
45
A Sample from a Weird Distribution
46
47
Another Sample from a Weird Distribution
48
6.3 Ad-Hoc Methods
Optimized methods exist for some common distributionsOptimization = reduce computing time
If implemented in your tool, use them !Example: simulating a normal distribution
Inversion method is not simple (no closed form for F-1)Rejection method is possibleBut a more efficient method exists, for drawing jointly 2 independent normal RV
There are also ad-hoc methods for n-dimensional normal distributions (gaussian vectors)
49
A Sample from a 2d- Normal Vector
50
4 Monte Carlo Simulation
A simple method to compute integrals of all kindsIdea: interpret the integral as Assume you can simulate as many independent samples of X as you want
51
52
53
54
55
Take Home Message
Most hard problems relative to computing a probability or an integral can be solved with Monte Carlo
Braindumb but why notRun time may be large -> importance sampling techniques (see later in this lecture)
56
Conclusion
Simulating well requires knowing the concepts ofTransienceConfidence intervalsSampling methods
57
Top Related