discrete event simulation - uc3m

24
Discrete Event Simulation Ignacio Cascos 2019 Ignacio Cascos Discrete Event Simulation 2019 1 / 24

Upload: others

Post on 11-Apr-2022

32 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Discrete Event Simulation - UC3M

Discrete Event Simulation

Ignacio Cascos

2019

Ignacio Cascos Discrete Event Simulation 2019 1 / 24

Page 2: Discrete Event Simulation - UC3M

Outline

3.1 Poisson processes (R5.4, R5.5 )

3.2 Gaussian processes

3.3 Discrete event simulation: queues, inventories, and collectiverisk (R7)

Ignacio Cascos Discrete Event Simulation 2019 2 / 24

Page 3: Discrete Event Simulation - UC3M

Poisson processes

A counting process is a stochastic process {N(t) : t ≥ 0} that iscontinuous in the space of time and discrete in the space of states and suchthat

N(t) ≥ 0.N(t) is an integer.If t2 ≥ t1, then N(t2) ≥ N(t1).

If t2 > t1, the increment N(t2)− N(t1) is the number of events during theinterval [t1, t2].

We can characterise the counting process either by means of the(increasing) sequence of rvs T1,T2, . . . where Ti is the time at which thei-th event occurs, or by the sequence of times between two consequtiveevents, Wi = Ti − Ti−1.

Ignacio Cascos Discrete Event Simulation 2019 3 / 24

Page 4: Discrete Event Simulation - UC3M

Homogeneous Poisson process

If the increments are independent and rv N(t) follows a Poisson distribution,then {N(t) : t ≥ 0} is a Poisson process.

A counting propcess follows is a (homogeneous) Poisson process with rate(intensity) λ > 0 if

N(0) = 0.N(t2)− N(t1) is independent of N(s2)− N(s1) for every0 ≤ t1 < t2 ≤ s1 < s2 (independent increments).N(t2)− N(t1) ∼ P(λ(t2 − t1)) (stationary increments with a Poissondistribution).

We have Wi ∼ Exp(λ) for every i and independent, andTi =

∑ir=1 Wr ∼ Gamma(i , λ).

Ignacio Cascos Discrete Event Simulation 2019 4 / 24

Page 5: Discrete Event Simulation - UC3M

Simulation of homogeneous Pois. process until k-th event1 Set T0 = 0.2 Generate W1,W2, . . . ,Wk Exponential rvs with parameter λ.3 For 1 ≤ i ≤ k, set Ti = Ti−1 + Wi and stop.

k <- 50 ; lmbd <- 0.5 ; set.seed(1)T <- cumsum(rexp(k,rate=lmbd))N <- 0:kplot(stepfun(T,N),do.points=F,xlab="t",ylab="N(t)")

0 20 40 60 80 100

010

2030

4050

stepfun(T, N)

t

N(t

)

Ignacio Cascos Discrete Event Simulation 2019 5 / 24

Page 6: Discrete Event Simulation - UC3M

Simulation of homogeneous Poisson process in [0, tl ]

Conditional on the total number of events in [t1, t2], the (unordered) eventtimes are distributioned as uniform rvs in the interval (t1, t2).

1 Generate k = N(tl ) a Poisson rv with rate λtl .2 Generate U1,U2, . . . ,Uk random numbers in (0, 1).3 Sort the previous sample as U(1) < U(2) < · · · < U(k).4 For 1 ≤ i ≤ k, set Ti = tlU(i) and stop.

Ignacio Cascos Discrete Event Simulation 2019 6 / 24

Page 7: Discrete Event Simulation - UC3M

Simulation of homogeneous Poisson process in [0, tl ]

tl <- 100 ; lmbd <- 0.5 ; set.seed(2)k <- rpois(1,lambda=lmbd*tl)u <- runif(k) ; T <- tl*sort(u) ; N <- 0:kplot(stepfun(T,N),do.points=F,xlab="t",ylab="N(t)")

0 20 40 60 80 100

010

2030

40

stepfun(T, N)

t

N(t

)

Ignacio Cascos Discrete Event Simulation 2019 7 / 24

Page 8: Discrete Event Simulation - UC3M

Nonhomogeneous Poisson process

A counting process is a nonhomogeneours Poisson process with intensityfunction λ : R+ 7→ R+ if

N(0) = 0.N(t2)− N(t1) is independent of N(s2)− N(s1) for every0 ≤ t1 < t2 ≤ s1 < s2 (independent increments).N(t2)− N(t1) ∼ P

(∫ t2t1λ(s) ds

)(nonstationary increments with a

Poisson distribution).

We denote m(t) =∫ t

0 λ(s) ds and λm ≥ λ(s) for every 0 ≤ s ≤ tl .

Ignacio Cascos Discrete Event Simulation 2019 8 / 24

Page 9: Discrete Event Simulation - UC3M

Simulation of nonhomogeneous Pois. proc. until k-th event

The cdf of rv time until next event from time t is

Ft(r) = P(Wi ≤ r |Ti−1 = t) = 1− P(Wi > r |Ti−1 = t)

= 1− P(N(t + r)− N(t) = 0) = 1− exp(−∫ t+r

tλ(s) ds

)

We can use the inverse transform technique to simulate the first k events ofa nonhomogenoeus Poisson process as:

1 Set T0 = 0.2 Generate U1,U2, . . . ,Uk random numbers in (0, 1).3 For 1 ≤ i ≤ k, set Ti = Ti−1 + F −1

Ti−1(Ui ) and stop.

Ignacio Cascos Discrete Event Simulation 2019 9 / 24

Page 10: Discrete Event Simulation - UC3M

Simulation of nonhomogeneous Pois. proc. until k-th event(thinning)

The previous algorithm requires an explicity expression for F −1t (for every t).

An alternative approach is the thinning technique that consists on rejectingsome of the simulated events.

1 Set T0 = 0, T ∗ = 0, i = 0.2 Generate X Exponential rv with rate λm.3 Set T ∗ = T ∗ + X .4 Generate U random number in (0, 1).5 If U > λ(T ∗)/λm, set i = i + 1 and Ti = T ∗

6 If i = k Stop.7 Go to Step 2.

Ignacio Cascos Discrete Event Simulation 2019 10 / 24

Page 11: Discrete Event Simulation - UC3M

Simulation of nonhomogeneous Poisson process in [0, tl ]

When simulating a nonhomogenous Poisson process on a fixed time interval,we build a dmf out of the intensity function by scaling it.

1 Generate k = N(tl ) a Poisson rv with rate m(tl ) =∫ tl

0 λ(s) ds2 Generate V1,V2, . . . ,Vk independent random numbers with densityλ/m(tl ).

3 Sort the previous sample as V(1) < V(2) < · · · < Vk .4 For every 1 ≤ i ≤ k, set Ti = V(i) and stop.

Ignacio Cascos Discrete Event Simulation 2019 11 / 24

Page 12: Discrete Event Simulation - UC3M

More about Poisson and counting processes

Superposition principle of Poisson processes. If {N1(t)} and{N2(t)} are two independent Poisson processes with respectiveintensity functions λ1 and λ2, then {N1(t) + N2(t)} is a Poissonprocess with intensity function λ1 + λ2.

Simulation: Generate the path of a Poisson process with intensityfunction λ1 + λ2. If an event occurs at time t, it corresponds to thePoisson process {N1(t)} with probability λ1(t)/(λ1(t) + λ2(t)).

Compound Poisson process. We can associate an independent rv Xito each event occurring in a Poisson process N(t) and set thecompount Poisson process S(t) =

∑N(t)k=0 Xk .

Simulation: Generate the path of the Poisson process N(t), at eachevent, generate a rv with the distribution of X and update S(t).

Ignacio Cascos Discrete Event Simulation 2019 12 / 24

Page 13: Discrete Event Simulation - UC3M

More about Poisson and counting processes

Mixed Poisson process. The intensity (rate) is not a function oftime, but a structural rv instead, so N(t) ∼ P(Λt) with Λ rv.

Simulation: Generate an observation of Λ and simulate thehomogeneous Poisson process.

Doubly stochastic Cox process. The intensity function is astochastic process itself.

Simulation: Generate a path of the stochastic process Λ(t) andsimulate the nonhomogeneous Poisson process.

Renewall process. The times between events are not exponentiallydistributed.

Simulation: As the simulation of the homogeneous Poisson processuntil k-th event, but not the time between two consecutive events is notexponentially distributed.

Ignacio Cascos Discrete Event Simulation 2019 13 / 24

Page 14: Discrete Event Simulation - UC3M

3.2 Gaussian processes

A stochastic process X (t) is Gaussian if all its marginal distributions arenormal.

Every random vector (X (t1),X (t2), . . . ,X (tn))t is normaly distributed.Completely determined by mean function µX (t) = E[X (t)] andautocovariance CX (t1, t2) = Cov(X (t1),X (t2)).

Ignacio Cascos Discrete Event Simulation 2019 14 / 24

Page 15: Discrete Event Simulation - UC3M

Brownian motion

A Gaussian process B(t) is a brownian motion (Wiener process) if

B(0) = 0.Given 0 ≤ t1 < t2 < . . . < tn, the increments(B(t2)−B(t1)), (B(t3)−B(t2)), . . . , (B(tn)−B(tn−1)) are indepenent.If s > 0, B(t + s)− B(t) ∼ N(0,

√s)

Has continuous paths, that is, B(t) is a continuous function of t.

A brownian motion with drift µ and volatility σ is given by

X (t) = µt + σB(t),

so X (t) ∼ N(µt, σ√

t).

Ignacio Cascos Discrete Event Simulation 2019 15 / 24

Page 16: Discrete Event Simulation - UC3M

Simulating a Brownian motion1 Set B0 = 0.2 Generate n normals Xi ∼ N(0,√ti − ti−1).3 Set Bti = Bti−1 + Xi .

n <- 100 ; tl <- 10 ; t <- seq(0,tl,by=1/n) ; set.seed(1)B <- append(0,cumsum(rnorm(n*tl,mean=0,sd=1/sqrt(n))))plot(t,B,type="l",ylab="B(t)")

0 2 4 6 8 10

−1

01

2

t

B(t

)

Ignacio Cascos Discrete Event Simulation 2019 16 / 24

Page 17: Discrete Event Simulation - UC3M

Geometric Brownian motion

The geometric (or exponential) brownian motion is a continuous-timestochastic process that is used to model stock prices.

S(t) = S(0) exp(

(µ− σ2/2)t + σB(t))

where S(0) is the stock price at time 0. The univariate marginals of ageometric brownian motion follow a log-normal distribution.

If X ∼ N(µ, σ), then eX ∼ lnorm(meanlog = µ,sdlog = σ),

E[eX ] = exp{µ+ σ2/2} , Var[eX ] = (exp{σ2} − 1) exp{2µ+ σ2}.

Ignacio Cascos Discrete Event Simulation 2019 17 / 24

Page 18: Discrete Event Simulation - UC3M

3.3 Discrete event simulation

Discrete event simulation models a system as a discrete sequence ofevents in time.

We must simulate the times at wich the events ocurr and some randomamounts associated with the events themselves.

The relevant variables are:

Time variable, t, elapsed amount of (simulated) time.Counter variables, number of times certain events have occurred bytime t.Systems statate variable, SS, state of the system at time t.

Whenever an event occurs the variables are updated.

Ignacio Cascos Discrete Event Simulation 2019 18 / 24

Page 19: Discrete Event Simulation - UC3M

Single-server queuing system

Customers arrive to a service station in accordance with a nonhomogeneousPoisson process with intensity function λ.

Upon arrival each customer either enters service is server is free or joins thewaiting queue if server is busy.

When the server completes serving a customer, either begins serving newcustomer (the one that has been waiting for longer if FIFO queue) if there acustomers in the queue or remains free until next customer arrives if thereare no waiting customers.

The amount of time used in the service of a customer is a random variable(independent of the other service times) with some known probabilitydistribution.

There is a final time tl after which no additional arrivals are allowed in thesystem.

Ignacio Cascos Discrete Event Simulation 2019 19 / 24

Page 20: Discrete Event Simulation - UC3M

Single-server queuing system

Relevant variables

Time variable, t.Counter variables, NA and ND number of arrivals and departures bytime t.Systems statate variable, n, number of customers in the system bytime t.

Events: arrivals and departures, occurring at times tA and tD.

The output variables to consider here are: Ai arrival time of i-th customer,Di departure time of i-th customer, and Tp the time past the final time tlthat the last customer departs.

Ignacio Cascos Discrete Event Simulation 2019 20 / 24

Page 21: Discrete Event Simulation - UC3M

Inventory model

A shop stocks a particular type of good that sells at price r per unit.

Customers demanding the good appear in accordance with a Poissonprocess with rate λ, and the amount of good demanded by each of them isa rv with distribution G .

The shopkeeper keeps an amount of stock S on hand, and whenever thestock becomes low orders new units from the distributor. Using a (s1, s2)ordering policy, where s1 < s2, if the stock S goes below s1, the shopkeeperorders s2 − S units.

The cost of ordering y units c(y) is specified, it takes L units of time untilthe order is delivered. The payment is made upon delivery and the shoppays an inventory holding cost of h per unit of item in stock.

If a customer demands more of the product than is presently available, theamound on hand is sold, and the customer buys the rest somewhere else(the shopkeeper misses the chance to sell that amount of product).

Ignacio Cascos Discrete Event Simulation 2019 21 / 24

Page 22: Discrete Event Simulation - UC3M

Inventory model

Relevant variables

Time variable, t.Counter variables, C ,H,R total amount of ordering costs, ofinventory holding costs, and revenue earned by time t.Systems statate variable, x amount of inventory on hand, and yamount on order by time t.

Events: arrivals of either customers (next customer arrives at time tc) ororders (next order arrives at time to). The updating is performed at thesmallest value between tc and to.

The output variable to consider here is the shop’s revenue.

Ignacio Cascos Discrete Event Simulation 2019 22 / 24

Page 23: Discrete Event Simulation - UC3M

Collective risk model

The policyholders of an insurance company generate claims according toindependent Poisson processes with common rate λ (NC (t)). These claimsare independent and their amount follows distribution X ∼ F . Clients enrolthe company according to a Poisson process with rate ν (NA(t)). Clientsstay in the company for an exponentially distributed time with rate µ(ND(t) for the process of departures).

Each policyholder pays a fixed amoung c per unit of time in the company,the company starts with n0 clients, and initial capital a0 ≥ 0.

The company’s capital at time t is given by

A(t) = a0 + ct(n0 + NA(t)− ND(t))−NC (t)∑i=1

Xi ,

and P(inf{A(t) : 0 ≤ t ≤ tl} < 0) is the ruin probability over period [0, tl ].

Ignacio Cascos Discrete Event Simulation 2019 23 / 24

Page 24: Discrete Event Simulation - UC3M

Collective risk model

Relevant variables

Time variable, t.System state variables, n number of policy holders and a firm’scapital by time t.

Events: new policyholder, lost policyholder, and new claim.

At time t with n policy holders, next event with occur in an exponentialtime with rate ν + nλ+ nµ and it will be a new client (with probabilityν/(ν + nλ+ nµ)), a claim, or a lost client with each of the correspondingprobabilities. The (system state variables), firm’s capital and number ofpolicy holders is to be updated.

The output variable to consider here is and indicator variable that the firm’scapital is nonnegative throughout [0, tl ]

Ignacio Cascos Discrete Event Simulation 2019 24 / 24