Mode Sim

Embed Size (px)

Citation preview

  • 8/3/2019 Mode Sim

    1/39

    GENERATING

    RANDOM NUMBERSAND RANDOM

    VARIABLESPresented By:

    Dionisio, Adrian

    Delos Reyes, Ma. Victorina

    Dizon, Xaraenielle

    Santos, Jeamaica

  • 8/3/2019 Mode Sim

    2/39

    GENERATING

    RANDOM

    NUMBERS

  • 8/3/2019 Mode Sim

    3/39

    WHAT ARE RANDOM NUMBERS

    Random numbers are a necessary basic

    ingredient in the simulation of almost all discrete

    systems. Most computer languages have a subroutine,

    object, or function that will generate a random

    number. Similarly simulation languages generate

    random numbers that are used to generate event

    limes and other random variables.

    It is impossible to produce an arbitrarily long

    string of random digits and prove it is random.

    Strangely, it is also very difficult for humans toproduce a string of random digits, and computer

    programs can be written which, on average, actually

    predict some of the digits humans will write down

    based on previous ones.

  • 8/3/2019 Mode Sim

    4/39

    Intuitively, we can list a number of criteria that a

    sequence of numbers must fulfill to pass as a random

    number sequence:

    unpredictability,

    independence,

    without pattern.

    These criteria appear to be the minimum request foran algorithm to produce random numbers. More precisely

    we can formulate:

    uniform distribution,

    uncorrelated,

    passes every test of randomness,

    large period before the sequence repeats ,

    sequence repeatable and possibility to vary starting

    values,

    fast algorithm.

  • 8/3/2019 Mode Sim

    5/39

    Two important statistical properties:

    y Uniformity

    y Independence

    PROPERTIES OF RANDOM NUMBERS

  • 8/3/2019 Mode Sim

    6/39

    Pseudo means false, so false random numbers

    are being generated. The goal of any generation

    scheme is to produce a sequence of numbers between

    zero(0) and one(1) which simulates, or initiates, the

    ideal properties of uniform distribution and

    independence as closely as possible.

    When generating pseudo-random numbers,

    certain problems or errors can occur. These errors, or

    departures from ideal randomness, are all related tothe properties stated previously. Some examples

    include the following:

    PSEUDORANDOM NUMBERS

  • 8/3/2019 Mode Sim

    7/39

    1. The generated numbers may not be uniformly

    distributed.

    2. The generated numbers may be discrete - valued

    instead continuous valued.

    3. The mean of the generated numbers may be too high

    or too low.

    4. The variance of the generated numbers may be toohigh or low

    5. There may be dependence. The following are examples:

    (a) Autocorrelation between numbers.(b) Numbers successively higher or lower than

    adjacent numbers.

    (c) Several numbers above the mean followed by

    several numbers below the mean.

  • 8/3/2019 Mode Sim

    8/39

    Usually, random numbers are generated by a

    digital computer as part of the simulation.

    Numerous methods can be used to generate thevalues. In selecting among these methods, or

    routines, there are a number of important

    considerations.

    Fast

    Portable to different computers

    Have sufficiently long cycle

    Replicable

    Closely approximate the ideal statistical

    properties of uniformity and independences.

  • 8/3/2019 Mode Sim

    9/39

    Linear Congruential Method (LCM)

    Combined Linear Congruential Generators(CLCG)

    Random Number Streams

    TECHNIQUES FOR GENERATING RANDOM

    NUMBERS

  • 8/3/2019 Mode Sim

    10/39

    The linear congruential method, initially proposed by

    Lehmer [1951], produces a sequence of integers, X1, X2,...between zero and m 1 according to the following recursive

    relationship:

    Xi+1 = (a Xi + c) mod m, i = 0,1, 2,...

    The initial valueX0

    is called the seed, a is called the

    constant multiplier, c is the increment, and m is the modulus.

    The selection of the values for a, c, m, andX0 drastically

    affects the statistical properties and the cycle length.

    LINEAR CONGRUENTIAL METHOD (LCM)

    multiplier modulusincrement

  • 8/3/2019 Mode Sim

    11/39

    If c 0, the form is called

    the mixed congruential method. When c =0, the

    form is known as the multiplicativecongruential method. The selection of the values

    for a, c, m and X 0 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:

    X

    Ri = ---- , i = 1,2,3

    m

  • 8/3/2019 Mode Sim

    12/39

    Example:

    Use the linear congruential method to

    generate a sequence of random numbers with

    X0 = 27, a = 17, c = 43, and m = 100. Here, the

    integer values generated will all be betweenzero and 99 because of the value of the

    modulus. These random integers should

    appear to be uniformly distributed the

    integers zero to 99. Random numbers betweenzero and 1 can be generated by:

    Ri

    = Xi

    / m, i=1,2,3.

  • 8/3/2019 Mode Sim

    13/39

    The sequence of Xi and subsequent Ri values is

    computed as follows:

    X0 = 27

    X1 = (17 27 + 43) mod 100 = 502 mod 100 = 2

    R1 = 2 100 = 0.02

    X2= (17 2 + 43) mod 100 = 77 mod 100 = 77

    R2 = 77 100 = 0.77

    X3 = (1777+ 43) mod 100 = 1352 mod 100 = 52R3 = 52 100 = 0.52

    X4 = (17 52 + 43) mod 100 = 927 mod 100 = 27

    R4 = 27 100 = 0.27

  • 8/3/2019 Mode Sim

    14/39

    Example 2:

    Find the sequence of random numbers given a = 13, c = 0,

    m = 31, X0 = 1.

    X0 = 1

    X1 = (13 * 1 + 0) mod 31 = 13 mod 31 = 13

    X2 = (13 * 13 + 0) mod 31 = 169 mod 31 = 14X3 = (13 * 14 + 0) mod 31 = 182 mod 31 = 27

    X4 = (13 * 27 + 0) mod 31 = 351 mod 31 = 10

    X5 = (13 * 10 + 0) mod 31 = 130 mod 31 = 6

    X6 = (13 * 6 + 0) mod 31 = 78 mod 31 = 16

    X7 = (13 * 16 + 0) mod 31 = 208 mod 31 = 22

    X8 = (13 * 22 + 0) mod 31 = 286 mod 31 = 7

    X9 = (13 * 7 + 0) mod 31 = 91 mod 31 = 29

    X10 = (13 * 29 + 0) mod 31 = 377 mod 31 = 5

    X11 = (13 * 5 + 0) mod 31 = 65 mod 31 = 3

  • 8/3/2019 Mode Sim

    15/39

    The sequence begins with:

    1, 13, 14, 27, 10, 6, 16, 22, 7, 29, 5, 3,

    What's the next value? Well, it looks

    pretty unpredictable, but you've been

    initiated. So you can compute. The first 30

    terms in the sequence are a permutation of theintegers from 1 to 30 and then the sequence

    repeats itself. It has a period equal to m - 1.

  • 8/3/2019 Mode Sim

    16/39

    If a pseudorandom integer sequence

    with values between 0 and m is scaled bydividing by m, the result is floating-point

    numbers uniformly distributed in the interval

    [0, 1]. Our simple example begins with:

    0.0323, 0.4194, 0.4516, 0.8710, 0.3226, 0.1935,

    0.5161, .

    There are only a finite number of values,

    30 in this case. The smallest value is 1/31 and

    the largest is 30/31.

  • 8/3/2019 Mode Sim

    17/39

    Seatwork:

    Use the linear congruential

    method to generate a sequence

    of random numbers with X0 = 9,a = 5, c = 11, and m = 16. Find

    the list of random numbers that

    will be generated by the givenvalues before an occurrence of

    any random number in the list

    will occur. Show your solution.

  • 8/3/2019 Mode Sim

    18/39

    The desirable properties of random numbersare uniformity and independence. To insure that

    these desirable properties are achieved, a number oftests can be performed. The first entry in the listbelow concerns testing for uniformity. The secondthrough fifth entries concern testing forindependence.

    The five types of tests are:

    1. Frequency Test

    2. Runs Test3. Autocorrelation Test

    4. Gap Test

    5. Poker Test

    TESTING RANDOM NUMBER RANDOMNESS

  • 8/3/2019 Mode Sim

    19/39

    When to Use These Tests:

    If a well-known simulation languagesor random-number generators isused, it is probably unnecessary totest

    If the generator is not explicitlyknown or documented, e.g.,spreadsheet programs,symbolic/numerical calculators, testsshould be applied to many samplenumbers.

  • 8/3/2019 Mode Sim

    20/39

    In testing for uniformity, thehypotheses are as follows:

    H0: Ri ~ U[0,1]

    H1: Ri ~ U[0,1]

    The null hypothesis, H0 reads that thenumbers are distributed uniformly on theinterval [0, 1]. Failure to reject the nullhypothesis means that no evidence of non-

    uniformity has been detected on the basisof this test. This does not imply thatfurther testing of the generator foruniformity is unnecessary.

  • 8/3/2019 Mode Sim

    21/39

    In testing for independence, the

    hypotheses are as follows:

    H0: Ri ~ independently

    H1: Ri ~ independently

    The null hypothesis, H0 reads that the

    numbers are independent. Failure to reject

    the null hypothesis means that no evidence

    of dependence has been detected on thebasis of this test. This does not imply that

    further testing of the generator for

    independence is unnecessary.

  • 8/3/2019 Mode Sim

    22/39

    For each test, a level of

    significance must be stated. The

    level is the probability of rejecting

    the null hypothesis given that the

    null hypothesis is true, or

    = P (reject H0|H0 true)

    The decision maker sets the

    value of for any test. Frequently,

    is set to 0.01 or 0.05.

  • 8/3/2019 Mode Sim

    23/39

    If several tests are conducted on

    the same set of numbers, the

    probability of rejecting the nullhypothesis on at least one test, by

    chance alone [i.e., making a Type I

    (a) error], increases. Say that =0.05 and those five different tests

    are conducted on a sequence of

    numbers. The probability ofrejecting the null hypothesis on at

    least one test, by chance alone, may

    be as large as 0.25.

  • 8/3/2019 Mode Sim

    24/39

    A basic test that should always be

    performed to validate a new generator isthe test of uniformity. Two different

    methods of testing are available. They are

    the Kolmogorov-Smirnov and the chi-

    square test. Both of these tests measure thedegree of agreement between the

    distribution of a sample of generated

    random numbers and the theoretical

    uniform distribution. Both tests are based

    on the null hypothesis of no significant

    difference between the sample distribution

    and the theoretical distribution.

    FREQUENCY TEST

  • 8/3/2019 Mode Sim

    25/39

    This test compares the continuous

    cdf, F(x), of the uniform distribution

    with the empirical cdf, SN(x), of the N

    sample observations. By definition:

    F(x) = x, 0 x 1

    KOLMOGOROV-SMIRNOV TEST

  • 8/3/2019 Mode Sim

    26/39

    If the sample from the RN generator

    is R1, R2, , RN, then the empirical cdf,SN(x) is:

    As N becomes larger, SN(x) should

    become a better approximation toF(x), provided that the null hypothesis is

    true.

  • 8/3/2019 Mode Sim

    27/39

    The Kolmogorov-Smirnov test is

    based on the largest absolute deviationbetween F(x) and SN(x) over the range of

    the random variable. That is, it is based

    on the statistic:

    D = max| F(x) - SN(x)|

    that is the absolute value of the

    differences.

  • 8/3/2019 Mode Sim

    28/39

    Here D is a random variable.

    If the calculated value is greaterthan the ones listed in the table,

    the hypothesis (no disagreement

    between the samples and the

    theoretical value) should be

    rejected; otherwise, we don'thave enough information to

    reject it.

  • 8/3/2019 Mode Sim

    29/39

    Following steps are taken to perform this

    test:

    1. Rank the data from smallest to largest

    R(1)R(2)... R(N)2. Compute

  • 8/3/2019 Mode Sim

    30/39

    3. Compute D = max(D+, D-)

    4. Get D

    for the significance

    level

    5. If D D accept, otherwisereject H0

  • 8/3/2019 Mode Sim

    31/39

  • 8/3/2019 Mode Sim

    32/39

    Example:

    Suppose 5generated numbers are 0.44, 0.81,

    0.14, 0.05, 0.93.

    Step 1: Arrange R(i) from smallest to largest.

    Step 2: Compute D+ and D-.

    D+ = 0.26

    D- = 0.21

    R(i) 0.05 0.14 0.44 0.81 0.93

    i/ N 0.20 0.40 0.60 0.80 1.00

    D+ = max{i/N R(i)} 0.15 0.26 0.16 - 0.07

    i/ N 0.05 - 0.04 0.21 0.13

  • 8/3/2019 Mode Sim

    33/39

    Step 3: D = max(D+

    , D-

    ).D = 0.26

    Step 4: Get D for the significance level .

    For = 0.05,

    D = 0.565 > D

    Step 5: H0 is not rejected.

  • 8/3/2019 Mode Sim

    34/39

    The chi-square test looks at

    the issue from the same anglebut uses different method.

    Instead of measure the

    difference of each point between

    the samples and the true

    distribution, chi-square checksthe ``deviation'' from the

    ``expected'' value.

    CHI-SQUARE TEST

  • 8/3/2019 Mode Sim

    35/39

    where n is the number ofclasses (e.g. intervals), O

    iis the

    number of samples observed inthe interval or the observed # inthe i-th class, E

    iis expected

    number of samples in theinterval or expected # in the i-thclass.

  • 8/3/2019 Mode Sim

    36/39

    For the uniform distribution,Ei, the expected number in each

    class is:

    where N is the total # of observation.

    This test is valid only on largesamples, e.g. N>=50.

  • 8/3/2019 Mode Sim

    37/39

    Example:

    100 numbers from [0,1]

    = 0.05

    10 intervals

    X20.05,9 = 16.9

  • 8/3/2019 Mode Sim

    38/39

    Interval Upper Limit Oi Ei Oi Ei (Oi Ei)2 (Oi Ei)

    2/Ei

    1 0.1 10 10 0 0 0

    2 0.2 9 10 -1 1 0.1

    3 0.3 5 10 -5 25 2.5

    4 0.4 6 10 -4 16 1.6

    5 0.5 16 10 6 36 3.6

    6 0.6 13 10 3 9 0.9

    7 0.7 10 10 0 0 0

    8 0.8 7 10 -3 9 0.9

    9 0.9 10 10 0 0 0

    10 1.0 14 10 4 16 1.6

    S 100 100 0 0 11.2

    Accept, since

    X

    2

    0 = 11.2 < X

    2

    0.05,9

  • 8/3/2019 Mode Sim

    39/39

    Seatwork:

    Suppose there are 10

    generated numbers which

    are, 0.95, 0.06, 0.09, 0.92, 0.76,0.13, 0.43, 0.65, 0.63, 0.59. Test

    if these numbers are

    uniformly distributed form

    the interval [0,1].