Simulation Discrete-Event System Simulation Dr. Mesut Gne Computer - - PowerPoint PPT Presentation

simulation
SMART_READER_LITE
LIVE PREVIEW

Simulation Discrete-Event System Simulation Dr. Mesut Gne Computer - - PowerPoint PPT Presentation

Computer Science, Informatik 4 Communication and Distributed Systems Simulation Discrete-Event System Simulation Dr. Mesut Gne Computer Science, Informatik 4 Communication and Distributed Systems Chapter 5 Random-Number Generation


slide-1
SLIDE 1

Computer Science, Informatik 4 Communication and Distributed Systems

Simulation

“Discrete-Event System Simulation”

  • Dr. Mesut Güneş
slide-2
SLIDE 2

Computer Science, Informatik 4 Communication and Distributed Systems

Chapter 5

Random-Number Generation

slide-3
SLIDE 3
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 3 Chapter 5. Random-Number Generation

Purpose & Overview Discuss the generation of random numbers. Introduce the subsequent testing for randomness:

  • Frequency test
  • Autocorrelation test.
slide-4
SLIDE 4
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 4 Chapter 5. Random-Number Generation

Properties of Random Numbers

  • Two important statistical properties:
  • Uniformity
  • Independence.
  • Random Number, Ri, must be independently drawn from a uniform

distribution with pdf:

Figure: pdf for random numbers

⎩ ⎨ ⎧ ≤ ≤ =

  • therwise

, 1 , 1 ) ( x x f 2 1 2 ) (

1 2 1

= = =∫ x xdx R E

slide-5
SLIDE 5
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 5 Chapter 5. Random-Number Generation

Generation of Pseudo-Random Numbers

  • “Pseudo”, because generating numbers using a known method

removes the potential for true randomness.

  • Goal: To produce a sequence of numbers in [0,1] that simulates, or

imitates, the ideal properties of random numbers (RN).

  • Important considerations in RN routines:
  • Fast
  • Portable to different computers
  • Have sufficiently long cycle
  • Replicable
  • Closely approximate the ideal statistical properties of uniformity and

independence.

slide-6
SLIDE 6
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 6 Chapter 5. Random-Number Generation

Techniques for Generating Random Numbers

  • Linear Congruential Method (LCM).
  • Combined Linear Congruential Generators (CLCG).
  • Random-Number Streams.
slide-7
SLIDE 7
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 7 Chapter 5. Random-Number Generation

Linear Congruential Method

  • To produce a sequence of integers, X1, X2, … between 0 and m-1

by following a recursive relationship:

  • The selection of the values for a, c, m, and X0 drastically affects

the statistical properties and the cycle length.

  • The random integers are being generated [0,m-1], and to convert

the integers to random numbers:

,... 2 , 1 , , mod ) (

1

= + =

+

i m c aX X

i i

The multiplier The increment The modulus

,... 2 , 1 , = = i m X R

i i

slide-8
SLIDE 8
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 8 Chapter 5. Random-Number Generation

Linear Congruential Method – Example

  • Use X0 = 27, a = 17, c = 43, and m = 100.
  • The Xi and Ri values are:

X1 = (17*27+43) mod 100 = 502 mod 100 = 2, R1 = 0.02; X2 = (17*2+43) mod 100 = 77, R2 = 0.77; X3 = (17*77+43) mod 100 = 52, R3 = 0.52; X4=(17*52+43) mod 100 = 27, R3 = 0.27; …

slide-9
SLIDE 9
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 9 Chapter 5. Random-Number Generation

Characteristics of a Good Generator

  • Maximum Density
  • Such that he values assumed by Ri, i = 1,2,…, leave no large gaps on

[0,1]

  • Problem: Instead of continuous, each Ri is discrete
  • Solution: a very large integer for modulus m
  • Approximation appears to be of little consequence
  • Maximum Period
  • To achieve maximum density and avoid cycling.
  • Achieve by: proper choice of a, c, m, and X0.
  • Most digital computers use a binary representation of numbers
  • Speed and efficiency are aided by a modulus, m, to be (or close to) a

power of 2.

slide-10
SLIDE 10
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 10 Chapter 5. Random-Number Generation

Random-Numbers in Java Defined in java.util.Random

private final static long multiplier = 0x5DEECE66DL; private final static long addend = 0xBL; private final static long mask = (1L << 48) - 1; protected int next(int bits) { long oldseed, nextseed; ...

  • ldseed = seed.get();

nextseed = (oldseed * multiplier + addend) & mask; ... return (int)(nextseed >>> (48 - bits)); }

slide-11
SLIDE 11
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 11 Chapter 5. Random-Number Generation

Combined Linear Congruential Generators

  • Reason: Longer period generator is needed because of the

increasing complexity of simulated systems.

  • Approach: Combine two or more multiplicative congruential

generators.

  • Let Xi,1, Xi,2, …, Xi,k, be the i-th output from k different multiplicative

congruential generators.

  • The j-th generator:
  • Has prime modulus mj and multiplier aj and period is mj -1
  • Produces integers Xi,j is approx ~ Uniform on integers in [1, mj –1]
  • Wi,j = Xi,j -1 is approx ~ Uniform on integers in [0, mj -2]
slide-12
SLIDE 12
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 12 Chapter 5. Random-Number Generation

Combined Linear Congruential Generators

  • Suggested form:
  • The maximum possible period is:

1 mod ) 1 (

1 1 , 1

− ⎟ ⎟ ⎠ ⎞ ⎜ ⎜ ⎝ ⎛ − = ∑

= −

m X X

k j j i j i

⎪ ⎪ ⎩ ⎪ ⎪ ⎨ ⎧ = − > = , 1 , Hence,

1 1 1 i i i i

X m m X m X R

1 2 1

2 ) 1 )...( 1 )( 1 (

− − − =

k k

m m m P

The coefficient: Performs the subtraction Xi,1-1

slide-13
SLIDE 13
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 13 Chapter 5. Random-Number Generation

Combined Linear Congruential Generators

  • Example: For 32-bit computers, combining k = 2 generators with

m1 = 2,147,483,563, a1 = 40,014, m2 = 2,147,483,399 and a2 = 20,692. The algorithm becomes:

Step 1: Select seeds

  • X1,0 in the range [1, 2,147,483,562] for the 1st generator
  • X2,0 in the range [1, 2,147,483,398] for the 2nd generator.

Step 2: For each individual generator, X1,j+1 = 40,014 X1,j mod 2,147,483,563 X2,j+1 = 40,692 X1,j mod 2,147,483,399.

Step 3:

Xj+1 = (X1,j+1 - X2,j+1 ) mod 2,147,483,562.

Step 4: Return Step 5: Set j = j+1, go back to step 2.

  • Combined generator has period: (m1 – 1)(m2 – 1)/2 ~ 2 x 1018

⎪ ⎪ ⎩ ⎪ ⎪ ⎨ ⎧ = > =

+ + + +

, 563 2,147,483, 562 2,147,483, , 563 2,147,483,

1 1 1 1 j j j j

X X X R

slide-14
SLIDE 14
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 14 Chapter 5. Random-Number Generation

Random-Numbers in Excel 2003 In Excel 2003 new Random Number Generator It is stated that this method produces more than 10^13 numbers

1.0 mod 30323 30307 Y 30269 X R 30323 mod 170 Z Z 30307 mod 172 Y Y 30269 mod 171 X X 00} {1,...,300 Z Y, X, ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ + + = ⋅ = ⋅ = ⋅ = ∈ Z

slide-15
SLIDE 15
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 15 Chapter 5. Random-Number Generation

Random-Numbers Streams

  • The seed for a linear congruential random-number generator:
  • Is the integer value X0 that initializes the random-number sequence.
  • Any value in the sequence can be used to “seed” the generator.
  • A random-number stream:
  • Refers to a starting seed taken from the sequence X0, X1, …, XP.
  • If the streams are b values apart, then stream i could defined by starting seed:
  • Older generators: b = 105; Newer generators: b = 1037.
  • A single random-number generator with k streams can act like k distinct

virtual random-number generators

  • To compare two or more alternative systems.
  • Advantageous to dedicate portions of the pseudo-random number sequence to

the same purpose in each of the simulated systems.

⎣ ⎦

b P i b i

i X S , , 2 , 1

) 1 (

K = =

slide-16
SLIDE 16
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 16 Chapter 5. Random-Number Generation

Tests for Random Numbers

  • Two categories:
  • Testing for uniformity:

H0: Ri ~ U[0,1] H1: Ri ~ U[0,1]

  • Failure to reject the null hypothesis, H0, means that evidence of non-

uniformity has not been detected.

  • Testing for independence:

H0: Ri ~ independently H1: Ri ~ independently

  • Failure to reject the null hypothesis, H0, means that evidence of

dependence has not been detected.

  • Level of significance α, the probability of rejecting H0 when it is true:

α = P( reject H0 | H0 is true)

/ /

slide-17
SLIDE 17
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 17 Chapter 5. Random-Number Generation

Tests for Random Numbers

  • When to use these tests:
  • If a well-known simulation languages or random-number generators is

used, it is probably unnecessary to test

  • If the generator is not explicitly known or documented, e.g., spreadsheet

programs, symbolic/numerical calculators, tests should be applied to many sample numbers.

  • Types of tests:
  • Theoretical tests: evaluate the choices of m, a, and c without actually

generating any numbers

  • Empirical tests: applied to actual sequences of numbers produced.
  • Our emphasis.
slide-18
SLIDE 18
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 18 Chapter 5. Random-Number Generation

Frequency Tests

  • Test of uniformity
  • Two different methods:
  • Kolmogorov-Smirnov test
  • Chi-square test
slide-19
SLIDE 19
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 19 Chapter 5. Random-Number Generation

Kolmogorov-Smirnov Test

  • Compares the continuous cdf, F(x), of the uniform distribution with

the empirical cdf, SN(x), of the N sample observations.

  • We know:
  • If the sample from the RN generator is R1, R2, …, RN, then the empirical

cdf, SN(x) is:

  • Based on the statistic: D = max | F(x) - SN(x)|
  • Sampling distribution of D is known
  • A more powerful test, recommended.

1 , ) ( ≤ ≤ = x x x F

N x R R x S

i i N

where

  • f

Number ) ( ≤ =

slide-20
SLIDE 20
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 20 Chapter 5. Random-Number Generation

Kolmogorov-Smirnov Test

  • The test consists of the following steps
  • Step 1: Rank the data from smallest to largest

R(1)≤R(2)≤... ≤R(N)

  • Step 2: Compute
  • Step 3: Compute D = max(D+, D-)
  • Step 4: Get Dα for the significance level α
  • Step 5: If D ≤ Dα accept, otherwise reject H0

⎭ ⎬ ⎫ ⎩ ⎨ ⎧ − − = ⎭ ⎬ ⎫ ⎩ ⎨ ⎧ − =

≤ ≤ − ≤ ≤ +

N i R D R N i D

i N i i N i

1 max max

) ( 1 ) ( 1

slide-21
SLIDE 21
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 21 Chapter 5. Random-Number Generation

Kolmogorov-Smirnov Test

  • Example: Suppose N=5 numbers: 0.44, 0.81, 0.14, 0.05, 0.93.

Step 1: Step 2: Step 3: D = max(D+, D-) = 0.26 Step 4: For α = 0.05, Dα = 0.565 > D Hence, H0 is not rejected.

Arrange R(i) from smallest to largest D+ = max {i/N – R(i)} D- = max {R(i) - (i-1)/N} 5 4 3 2 1 i 0.13 0.21 0.04

  • 0.05

R(i) – (i-1)/N 0.07

  • 0.16

0.26 0.15 i/N – R(i) 1.00 0.80 0.60 0.40 0.20 i/N 0.93 0.81 0.44 0.14 0.05 R(i)

slide-22
SLIDE 22
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 22 Chapter 5. Random-Number Generation

Chi-square test

  • Chi-square test uses the sample statistic:
  • Approximately the chi-square distribution with n-1 degrees of freedom
  • For the uniform distribution, Ei, the expected number in each class is:
  • Valid only for large samples, e.g. N >= 50

=

− = Χ

n i i i i

E E O

1 2 2

) (

n

  • bservatio
  • f

# total the is N where , n N Ei =

n is the # of classes Oi is the observed # in the i-th class Ei is the expected # in the i-th class

slide-23
SLIDE 23
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 23 Chapter 5. Random-Number Generation

Chi-square test

  • Example
  • 100 numbers from [0,1]
  • α=0.05
  • 10 intervals
  • X2

0.05,9=16.9

  • Accept, since
  • X2

0=11.2 < X2 0.05,9

11.2 100 100 S 1.6 16 4 10 14 1.0 10 10 10 0.9 9 0.9 9

  • 3

10 7 0.8 8 10 10 0.7 7 0.9 9 3 10 13 0.6 6 3.6 36 6 10 16 0.5 5 1.6 16

  • 4

10 6 0.4 4 2.5 25

  • 5

10 5 0.3 3 0.1 1

  • 1

10 9 0.2 2 10 10 0.1 1 (Oi-Ei)^2/Ei (Oi-Ei)^2 Oi-Ei Ei Oi Upper Limit Interval

X2

0=11.2

slide-24
SLIDE 24
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 24 Chapter 5. Random-Number Generation

Tests for Autocorrelation Autocorrelation is concerned with dependence between numbers in a sequence Example: Numbers at 5-th, 10-th, 15-th, ... are very similar Numbers can be

  • Low
  • High
  • Alternating

0.87 0.69 0.36 0.19 0.58 0.95 0.43 0.05 0.49 0.68 0.88 0.75 0.27 0.60 0.41 0.91 0.35 0.33 0.15 0.99 0.93 0.83 0.28 0.64 0.31 0.89 0.28 0.23 0.01 0.12

slide-25
SLIDE 25
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 25 Chapter 5. Random-Number Generation

Tests for Autocorrelation

  • Testing the autocorrelation between every m numbers (m is a.k.a.

the lag), starting with the i-th number

  • The autocorrelation ρim between numbers: Ri, Ri+m, Ri+2m, Ri+(M+1)m
  • M is the largest integer such that
  • Hypothesis:
  • If the values are uncorrelated:
  • For large values of M, the distribution of the estimator of ρim, denoted

is approximately normal.

N )m (M i ≤ + + 1

im

ρ ˆ dependent are numbers if t independen are numbers if

, : , :

1

≠ =

im im

H H ρ ρ

slide-26
SLIDE 26
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 26 Chapter 5. Random-Number Generation

Tests for Autocorrelation

  • Test statistics is:
  • Z0 is distributed normally with mean = 0 and variance = 1, and:
  • If ρim > 0, the subsequence has positive autocorrelation
  • High random numbers tend to be followed by high ones, and vice versa.
  • If ρim < 0, the subsequence has negative autocorrelation
  • Low random numbers tend to be followed by high ones, and vice versa.

im

im

Z

ρ

σ ρ

ˆ

ˆ ˆ =

) (M M σ . R R M ρ

im

ρ M k )m (k i km i im

1 12 7 13 ˆ 25 1 1 ˆ

1

+ + = − ⎥ ⎦ ⎤ ⎢ ⎣ ⎡ ⋅ + =

= + + +

slide-27
SLIDE 27
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 27 Chapter 5. Random-Number Generation

Example

  • Test whether the 3rd, 8th, 13th, and so on, for the numbers on Slide

24.

  • Hence, α = 0.05, i = 3, m = 5, N = 30, and M = 4
  • z0.025 = 1.96 hence, the hypothesis is not rejected.

516 . 1 1280 . 1945 . 128 . 1 4 12 7 ) 4 ( 13 1945 . 25 ) 36 . )( 05 . ( ) 05 . )( 27 . ( ) 27 . )( 33 . ( ) 33 . )( 28 . ( ) 28 . )( 23 . ( 1 4 1 ˆ

ˆ 35

35

− = − = = + + = − = − ⎥ ⎦ ⎤ ⎢ ⎣ ⎡ + + + + + = Z ) ( σ . ρ

ρ

slide-28
SLIDE 28
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 28 Chapter 5. Random-Number Generation

Shortcomings

  • The test is not very sensitive for small values of M, particularly when

the numbers being tested are on the low side.

  • Problem when “fishing” for autocorrelation by performing numerous

tests:

  • If α = 0.05, there is a probability of 0.05 of rejecting a true hypothesis.
  • If 10 independence sequences are examined,
  • The probability of finding no significant autocorrelation, by chance

alone, is 0.9510 = 0.60.

  • Hence, the probability of detecting significant autocorrelation when it

does not exist = 40%

slide-29
SLIDE 29
  • Dr. Mesut Güneş

Computer Science, Informatik 4 Communication and Distributed Systems 29 Chapter 5. Random-Number Generation

Summary

  • In this chapter, we described:
  • Generation of random numbers
  • Testing for uniformity and independence
  • Caution:
  • Even with generators that have been used for years, some of which still

in used, are found to be inadequate.

  • This chapter provides only the basic
  • Also, even if generated numbers pass all the tests, some underlying

pattern might have gone undetected.