2G1503 Simulation and Modeling Exercise #1-2 Ali Ghodsi aligh@imit.kth.se 1 Linear Congruential Method • Used to generate random numbers • Recursive definition: – Xn+1=a*Xn+c mod m • If c=0 then – Multiplicative LCM • else – Mixed LCM 2 Mixed LCG • Let X0 = 27, a = 8, c = 47, and m = 100 • Exercise 1: Generate three numbers! X0 = 27 X1 = 8*27 + 47 mod 100 = 63 X2 = 8*63 + 47 mod 100 = 51 X3 = 8*51 + 47 mod 100 = 55 3 Multiplicative LCG • Let X0 = 847, a = 56, and m = 1000 • Exercise 2: Generate four numbers! X0 = 847 X1 = 56*847 mod 1000 = 432 X2 = 56*432 mod 1000 = 192 X3 = 56*192 mod 1000 = 752 X4 = 56*752 mod 1000 = 112 4 Random Numbers and Integers • The random integers from the LGC can be converted to random numbers by the formula: Rn = Xn / m for all n • Example: – R1 = 847/1000 = 0.847 – R2 = 432/1000 = 0.432 – R3 = 192/1000 = 0.192 5 Testing for Uniformity • Use the Kolmogorov-Smirnov test with alpha = 0.05 to determine if the hypothesis that the numbers are uniformly distributed on the interval [0,1] can be rejected. • Numbers: 0.54, 0.73, 0.98, 0.11, 0.68 6 Sorting the numbers N: Numbers: Sorted: 5 0.54, 0.73, 0.98, 0.11, 0.68 0.11, 0.54, 0.68, 0.73, 0.98 – Make N intervals and compute D+ and D- 7 Kolmogorov Smirnov Test of Uniformity i 1 2 3 4 5 Ri 0.11 0.54 0.68 0.73 0.98 i/N 0.2 0.4 0.6 0.8 1.0 (i-1)/N 0.0 0.2 0.4 0.6 0.8 i/N–Ri 0.09 - - 0.07 0.02 D+=0.07 Ri–(i-1)/N 0.11 0.34 0.28 0.13 0.18 D-=0.34 D=0.34 8 Null-hypothesis • H0: The sample has a uniform distribution • H1: The sample does not have a uniform distribution 9 Significance Level • The significance level α gives the probability that the null-hypothesis is rejected even though it is true. • P(H0 rejected|H0 is true) 10 Critical Values for KS-test N D0.10 D0.05 D0.01 1 0.950 0.975 0.995 … … … .. 5 0.510 0.565 0.669 … … … … 11 Testing for Independence Use the run-ups and run-downs test with alpha = 0.05 to determine if the hypothesis that the numbers are independent holds. 12 Testing for Independence 0.34 0.83 0.96 0.47 0.79 0.90 0.76 0.99 0.30 0.71 0.25 0.79 0.77 0.17 0.23 0.89 0.64 0.67 0.82 0.19 0.87 0.70 0.56 0.56 0.82 0.44 0.81 0.41 0.05 0.93 0.12 0.94 0.52 0.45 0.65 0.21 0.74 0.73 0.31 0.37 0.46 0.22 0.99 0.78 0.39 0.67 0.74 0.02 0.05 0.42 13 Testing for Independence + 0.83 0.96 0.47 0.79 0.90 0.76 0.99 0.30 0.71 0.25 0.79 0.77 0.17 0.23 0.89 0.64 0.67 0.82 0.19 0.87 0.70 0.56 0.56 0.82 0.44 0.81 0.41 0.05 0.93 0.12 0.94 0.52 0.45 0.65 0.21 0.74 0.73 0.31 0.37 0.46 0.22 0.99 0.78 0.39 0.67 0.74 0.02 0.05 0.42 14 Testing for Independence + 0.83 0.96 0.47 0.79 0.76 0.99 0.30 0.71 0.25 0.79 0.77 0.17 0.23 0.89 0.64 0.67 0.82 0.19 0.87 0.70 0.56 0.56 0.82 0.44 0.81 0.41 0.05 0.93 0.12 0.94 0.52 0.45 0.65 0.21 0.74 0.73 0.31 0.37 0.46 0.22 0.99 0.78 0.39 0.67 0.74 0.02 0.05 0.42 15 Testing for Independence + 0.83 0.96 0.47 0.79 0.76 0.99 0.30 0.71 + 0.79 0.77 0.17 0.23 0.89 0.64 0.67 0.82 0.19 0.87 0.70 0.56 0.56 0.82 0.44 0.81 0.41 0.05 0.93 0.12 0.94 0.52 0.45 0.65 0.21 0.74 0.73 0.31 0.37 0.46 0.22 0.99 0.78 0.39 0.67 0.74 0.02 0.05 0.42 16 Testing for Independence + + - + - + + - + + + + + + + - + + - + + + + + + + + + + + 17 Testing for Indepedence • N=50 (numbers) a=27 (runs) • We know how to calculate σa, µa: σa = (2N-1) / 3 => 100-1 / 3 = 33 µ2a = (16N–29) / 90 => 800-29 / 90 = 8.57 µa = 8.570.5=2.93 • Converting to standard N(0,1): Z0 = a-σa / µa = 27-33 / 2.93 = -2.05 18 Standard Normal Distribution Acceptance region for hypothesis of independence -Za/2 ≤ Z0 ≤ Za/2 α/2 α/2 -Zα / 2 fail to reject Zα / 2 19 Critical Values for run-ups/run-downs Zα 0.05 0.06 0.07 0.0 0.51994 0.52392 0.52790 … … … .. 1.9 0.97491 0.97500 0.97558 … … … … 20 Testing the Hypothesis • Z0.025 = 1.96 for N(0,1) • Z0=-2.05 < -Z0.025 hence H0 is rejected 21