Modelng and Smulaton NETW 707 Lecture 5 Tests for Random Numbers Course Instructor: Dr.-Ing. Magge Mashaly magge.ezzat@guc.edu.eg C3.220 1
Propertes of Random Numbers Random Number Generators (RNGs) must satsfy propertes of Random Numbers: 1. Unformty 2. Independence But what f you are gven some numbers generated by any RNG How to guarantee that they are Unform & Independent random numbers??? Usng Tests for Random Numbers 2
Tests for Random Numbers There are two types of tests: 1. Frequency Tests - Compare the dstrbuton of the set of numbers to a unform dstrbuton - Examples for frequency tests: 1. Kolmogorov-Smrnov Test (KS) 2. Ch-Square Test 2. Autocorrelaton Test Tests the correlaton between numbers and compares the sample correlaton to the expected correlaton of zero 3
Kolmogorov Smrnov Test The KS test compares a contnuous CDF F(x) to an emprcal CDF S N (x) of the sample of N observatons. If the sample from the random number generator s R 1, R 2,, R N, then the emprcal CDF S N (x) s gven by: S N number of R 1, R2, ( x) N, R N whch are x 4
5 Kolmogorov Smrnov Test How to perform the test? 1. Rank the data from smallest to largest: 2. Compute: R N R R 2 1 N R N D 1 max N R D N 1 max 1
Kolmogorov Smrnov Test 3. Compute D=max(D +,D - ) 4. Locate the crtcal value D α n the Kolmogorov Smrnov Crtcal Values Table for the specfed sgnfcance level α and the gven sample sze N 5. Compare: If D D α Accept: No dfference between S N (x) and F (x ) If D > D α Reject: Dfference exsts between S N (x) and F (x) 6
Kolmogorov Smrnov Test Kolmogorov Smrnov Crtcal Values Table 7
Kolmogorov Smrnov Test: Example The fve numbers: 0.44, 0.81, 0.14, 0.05, 0.93 were generated and t s requred to test for unformty usng the Kolmogorov-Smrnov Test wth the level of sgnfcance α = 0.05. 8
Kolmogorov Smrnov Test: Example Soluton: R N N R R 1 n 0.05 0.14 0.44 0.81 0.93 0.2 0.4 0.6 0.8 1 0.15 0.26 0.16 -- 0.07 0.05 -- 0.04 0.21 0.13 D=max(D +,D - ) = max(0.26,0.21) = 0.26 In KS crtcal values table, For α = 0.05 and N = 5, the crtcal value D α = 0.565 Snce D < D α, no dfference has been detected between the true dstrbuton of {R 1, R 2,, R N } and the unform dstrbuton 9
Kolmogorov Smrnov Test: Example Soluton: 10
Ch-square Test The Ch-square test uses the sample statstc: 2 0 Where: O = the number of observatons n the -th class E = the expected number n the -th class n = the number of classes. n 1 O E E 2 11
Ch-square Test 1. Rank the data from smallest to largest: R 1 R 2 R N 2. Dvde the Range R N -R 1 n n equdstant ntervals such that each nterval has at least 5 observatons. 3. Calculate: 2 0 n 1 O E E 2 12
Ch-square Test 4. For sgnfcance level α, utlze the table of (Percentage ponts of the ch square dstrbuton wth ν degrees of freedom) to determne χ α,n-1 5. Compare If χ 0 2 χ 2 α,n-1 Accept: No dfference between S N (x) and F(x) If χ 0 2 > χ 2 α,n-1 Reject: Dfference exsts between S N (x) and F(x) 13
Ch-square Test Percentage ponts of the CHI SQUARE dstrbuton wth ν degrees of freedom 14
Ch-square Test: Example Use the ch-square test wth α=0.05 to test whether the data shown next are unformly dstrbuted. 0.34 0.90 0.25 0.89 0.87 0.44 0.12 0.21 0.46 0.67 0.83 0.76 0.79 0.64 0.70 0.81 0.94 0.74 0.22 0.74 0.96 0.99 0.77 0.67 0.56 0.41 0.52 0.73 0.99 0.02 0.47 0.30 0.17 0.82 0.56 0.05 0.45 0.31 0.78 0.05 0.79 0.71 0.23 0.19 0.82 0.93 0.65 0.37 0.39 0.42 0.99 0.17 0.99 0.46 0.05 0.66 0.10 0.42 0.18 0.49 0.37 0.51 0.54 0.01 0.81 0.28 0.69 0.34 0.75 0.49 0.72 0.43 0.56 0.97 0.30 0.94 0.96 0.58 0.73 0.05 0.06 0.39 0.84 0.24 0.40 0.64 0.40 0.19 0.79 0.62 0.18 0.26 0.97 0.88 0.64 0.47 0.60 0.11 0.29 0.78 15
Ch-square Test: Example Soluton: Interval O E O E 2 1 8 10-2 4 0.4 2 8 10-2 4 0.4 3 10 10 0 0 0 4 9 10-1 1 0.1 5 12 10 2 4 0.4 6 8 10-2 4 0.4 7 10 10 0 0 0 8 14 10 4 16 1.6 9 10 10 0 0 0 10 11 10 1 1 0.1 100 100 0 3.4 O E O E 2 E 16
Ch-square Test: Example Soluton: The test uses n=10 ntervals of equal length, namely [0,0.1[, [0.1,0.2[,, [0.9,1] The value of χ 02 =3.4 From table (Percentage ponts of the CHI SQUARE dstrbuton wth ν degrees of freedom), the crtcal value of χ 0.05,9 =16.9 Snce χ 02 < χ 0.05,9, the hypothess of unform dstrbuton s not rejected. 17
Notes on Unformty Tests Both the Kolmogorov-Smrnov test and the ch-square test are acceptable for testng the unformty of sample data provded that the sample sze s large. The KS test can be appled to small sample szes, whereas the ch-square test s vald only for large samples, e.g.: N 50. The KS test s more powerful and s recommended 18
Test for Auto-correlaton The tests for auto-correlaton are concerned wth the dependence between numbers n a sequence. Example: 0.12 0.01 0.23 0.28 0.89 0.31 0.64 0.28 0.83 0.93 0.99 0.15 0.33 0.35 0.91 0.41 0.60 0.27 0.75 0.88 0.68 0.49 0.05 0.43 0.95 0.58 0.19 0.36 0.69 0.87 Examnaton of the 5 th, 10 th, 15 th,,etc. ndcates a large number n that poston. 19
Test for Auto-correlaton The test requres the computaton of the autocorrelaton between every m numbers (m s known as the lag), startng wth the th number: The autocorrelaton ρ m of nterest shall be between numbers: R, R +m, R +2m, R +(M+1)m M s the largest nteger such that +(M+1)m N If the values are uncorrelated: For large values of M, the dstrbuton of the estmator of ρ m, denoted s approxmately normal. 20
Test for Auto-correlaton Test statstc s: Where: ˆ m ˆ m 1 M 1 13M 7 12 M 1 k 0 Z 0 s dstrbuted normally wth mean = 0 and varance = 1. Z M 0 R ˆ km m ˆ m R k 1 m 0.25 21
Test for Auto-correlaton Test Steps: 1. Compute Z 0 2. The hypothess of ndependence s not rejected f: z Z z / 2 0 / 2 Where α s the level of sgnfcance and z α/2 s obtaned from the standard normal dstrbuton table. / 2 -Z / 2 Z / 2 / 2 22
Test for Auto-correlaton The standard normal dstrbuton table 23
Test for Auto-correlaton Test Steps: 3. If numbers are correlated, determne the type of correlaton If ρ m > 0, the subsequence has postve autocorrelaton Hgh random numbers tend to be followed by hgh ones, and vce versa. If ρ m < 0, the subsequence has negatve autocorrelaton Low random numbers tend to be followed by hgh ones, and vce versa. 24
Test for Auto-correlaton: Example Test whether the 3 rd, 8 th, 13 th, random varables are correlated for the followng output usng α = 0.05. 0.12 0.01 0.23 0.28 0.89 0.31 0.64 0.28 0.83 0.93 0.99 0.15 0.33 0.35 0.91 0.41 0.60 0.27 0.75 0.88 0.68 0.49 0.05 0.43 0.95 0.58 0.19 0.36 0.69 0.87 25
Test for Auto-correlaton: Example Soluton: = 3, m = 5, N = 30, 3+(M+1)5 30 M = 4 M 1 ˆ 1 m R kmr M k 0 ˆ ˆ 35 35 ˆ 1 4 1 0.1945 35 13M 12 k 1 m 0.25 0.230.28 0.280.33 0.330.27 0.270.05 0.050.36 7 13 4 M 1 124 1 7 0.128 0.25 26
Test for Auto-correlaton: Example Soluton: The test statstc s gven by: ˆ m 0.1945 Z 0.1280 From the standard normal dstrbuton table, the crtcal value s: Snce 0 ˆ z m 0.05/ 2 z0.025 z 0.1516 1.96 0.025 Z0 z0.025 The hypothess of ndependence cannot be rejected 27
Test for Auto-correlaton: Example Soluton: The test statstc s gven by: ˆ m 0.1945 Z 0.1280 From the standard normal dstrbuton table, the crtcal value s: Snce 0 ˆ z m 0.05/ 2 z0.025 z 0.1516 1.96 0.025 Z0 z0.025 The hypothess of ndependence cannot be rejected 28
References NETW 707 Lectures sldes by A. Prof. Tallal El-Shabrawy, 2016 & 2017 NETW 707 Lectures sldes by Dr. Akram Al, 2014 Smulaton Modelng and Analyss by Averll M. Law, 5 th Edton, 2015 F. J. Massey, The Kolmogorov-Smrnov Test for Goodness of Ft, The Journal of the Amercan Statstcal Assocaton, Vol. 46, 1951, p.70 R. J. Wonnacolt, T.H. Wonnacolt, Statstcs: Dscoverng Its Power, New Yor,: John Wley and Sons, 1982, p.352 29