57
Simulation Where real stuff starts

Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

  • View
    217

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Simulation

Where real stuff starts

Page 2: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

ToC

1. What, transience, stationarity2. How, discrete event, recurrence

3. Accuracy of output4. Monte Carlo

5. Random Number Generators6. How to sample

2

Page 3: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

1 What is a simulation ?

An experiment in computerImportant differences

Simulated vs real timeSerialization of events

3

Page 4: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Stationarity

A simulation can be terminating or not

For a terminating simulation you should make sure it is stationary

4

Page 5: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Information server, scenario 1

5

Page 6: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Information server, scenario 2

6

Page 7: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 8: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 9: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 10: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 11: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Non-Stationary (Time Dependent Inputs)

11

Page 12: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 13: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

2 Simulation Techniques

Discrete event simulationsRecurrences

Stochastic recurrences

13

Page 14: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 15: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 16: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 17: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 18: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Main program

18

Page 19: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Stochastic Recurrence

An alternative to discrete event simulationfaster but requires more work on the modelnot always applicable

Defined by iteration:

19

Page 20: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Example: random waypoint mobility model

20

Page 21: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

21

Page 22: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

22

Page 23: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Queuing System implemented as Stochastic Recurrence

23

Page 24: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

24

Page 25: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 26: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Results of 30 Independent Replications

26

Page 27: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Do They Look Normal ?

27

Page 28: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Bootstrap Replicates

28

Page 29: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Confidence Intervals

29

Mean, normal approx

Median

Mean, bootstrap

Page 30: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 31: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

The Linear Congruential Generator of ns2

31

uniform qq plot

autocorrelation

Page 32: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Lag Diagram, 1000 points

32

Page 33: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Period of RNG

RNG is in fact periodicPeriod should be much larger than maximum number of uses

33

Page 34: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Two Parallel Streams with too simple a RNG

34

Page 35: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Impact of RNG

35

Page 36: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 37: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 38: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

CDF Inversion

Applies to real or integer valued RV

38

Page 39: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

39

Page 40: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Example: integer valued RV

40

Page 41: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

41

Page 42: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 43: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Rejection Sampling for Conditional Distribution

This is the main idea

How can you apply this to the example in the previous slide ?

43

Page 44: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Rejection Sampling for General Distributions

44

Page 45: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

45

Page 46: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

A Sample from a Weird Distribution

46

Page 47: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

47

Page 48: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Another Sample from a Weird Distribution

48

Page 49: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 50: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

A Sample from a 2d- Normal Vector

50

Page 51: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 52: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

52

Page 53: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

53

Page 54: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

54

Page 55: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

55

Page 56: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

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

Page 57: Simulation Where real stuff starts. ToC 1.What, transience, stationarity 2.How, discrete event, recurrence 3.Accuracy of output 4.Monte Carlo 5.Random

Conclusion

Simulating well requires knowing the concepts ofTransienceConfidence intervalsSampling methods

57