32
Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7 Dr. David Walshaw March 14, 2007 Dr. David Walshaw MAS1302:Computational Probability and Statistics:Week 7

MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

MAS1302:Computational Probability and Statistics:

Week 7

Dr. David Walshaw

March 14, 2007

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 2: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

3. Continuous Random Behaviour (cont.)

3.7 Checking for Randomness

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 3: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

3. Continuous Random Behaviour (cont.)

3.7 Checking for RandomnessIf we are going to use random number generators to simulatesystems such as queues, birth–death processes, etc. it is importantthat our psseudo–random number generators are not producingsequences of numbers which are non–random in any systematicway.

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 4: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

3. Continuous Random Behaviour (cont.)

3.7 Checking for RandomnessIf we are going to use random number generators to simulatesystems such as queues, birth–death processes, etc. it is importantthat our psseudo–random number generators are not producingsequences of numbers which are non–random in any systematicway.

Here we consider the issues involved in checking the ‘randomness’of a random number generator. This sounds quite straightforward.However, as we shall see, a sequence of pseudo–random numberscan exhibit non–random behaviour in a surprising variety of ways.

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 5: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

3. Continuous Random Behaviour (cont.)

3.7 Checking for RandomnessIf we are going to use random number generators to simulatesystems such as queues, birth–death processes, etc. it is importantthat our psseudo–random number generators are not producingsequences of numbers which are non–random in any systematicway.

Here we consider the issues involved in checking the ‘randomness’of a random number generator. This sounds quite straightforward.However, as we shall see, a sequence of pseudo–random numberscan exhibit non–random behaviour in a surprising variety of ways.

Using a variety of examples, we will consider ways in which asequence of supposedly independent U(0, 1) random numbers candeviate.

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 6: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.1: Checking the marginal distribution

The marginal distribution simply refers to the distribution ofindividual simulated values.

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 7: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.1: Checking the marginal distribution

The marginal distribution simply refers to the distribution ofindividual simulated values.

Suppose we simulate 10,000 observations from a U(0, 1)distribution. We might check the marginal distribution by tallyingthe frequencies in intervals of width 0.2.

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 8: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.1 (cont.)

a) Suppose we obtain the observed frequencies below:

Range (0,0.2) [0.2,0.4) [0.4,0.6) [0.6, 0.8) [0.8, 1.0)Probability 0.2 0.2 0.2 0.2 0.2

Expected Frequency 2000 2000 2000 2000 2000Observed Frequency 1783 1992 2274 1948 2003

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 9: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.1 (cont.)

a) Suppose we obtain the observed frequencies below:

Range (0,0.2) [0.2,0.4) [0.4,0.6) [0.6, 0.8) [0.8, 1.0)Probability 0.2 0.2 0.2 0.2 0.2

Expected Frequency 2000 2000 2000 2000 2000Observed Frequency 1783 1992 2274 1948 2003

Does it look as though these have the correct marginaldistribution? Let’s have a look at a histogram...

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 10: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Solution (Slide 1 of 1)

Range0

0.2 0.4 0.6 0.8 1.0

2000

0

Frequency

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 11: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Solution (Slide 1 of 1)

Range0

0.2 0.4 0.6 0.8 1.0

2000

0

Frequency

In fact these observed frequencies are much too far from theexpected frequencies - this random number generator is very poor!

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 12: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.1 (cont.)

b) Now suppose we obtain:

Range (0,0.2) [0.2,0.4) [0.4,0.6) [0.6, 0.8) [0.8, 1.0)Observed Frequency 2001 1998 1999 1999 2003

Do these look OK?

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 13: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Solution (Slide 1 of 1)

2000

00.2 0.4 Range0.6 0.8 1.00

Frequency

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 14: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Solution (Slide 1 of 1)

2000

00.2 0.4 Range0.6 0.8 1.00

Frequency

These observed frequencies are much too close to the expectedones! Again, a poor random number generator!

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 15: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.1 (cont.)

c) Now suppose we obtain:

Range (0,0.2) [0.2,0.4) [0.4,0.6) [0.6, 0.8) [0.8, 1.0)Observed Frequency 1954 2022 1977 2031 2016

What about these?

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 16: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Solution (Slide 1 of 1)

00 0.2 0.4 0.6 0.8 1.0 Range

Frequency

2000

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 17: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Solution (Slide 1 of 1)

00 0.2 0.4 0.6 0.8 1.0 Range

Frequency

2000

In fact these are typical of what we would see from a genuineU(0, 1) random variable. This generator looks OK . . . for themarginal distribution at least!

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 18: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.2 Checking for dependence: runs test

Even if the marginal distribution is OK, other things can go wrong.

a) E.g. consider the following sequence:

0.134, 0.768, 0.233, 0.984, 0.746, 0.865, 0.321, 0.432, 0.250, 0.777

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 19: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.2 Checking for dependence: runs test

Even if the marginal distribution is OK, other things can go wrong.

a) E.g. consider the following sequence:

0.134, 0.768, 0.233, 0.984, 0.746, 0.865, 0.321, 0.432, 0.250, 0.777

Here the consecutive numbers increase, decrease, increase, ... andso on. It looks as though they are not independent.

One way of checking this is via a runs test.

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 20: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.2 (cont.)

b) E.g. consider the following sequence:

(0.134, 0.279, 0.886), (0.197), (0.011, 0.923, 0.990), (0.876)

Here each run of ascending numbers has been identified inbrackets. We get:

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 21: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.2 (cont.)

b) E.g. consider the following sequence:

(0.134, 0.279, 0.886), (0.197), (0.011, 0.923, 0.990), (0.876)

Here each run of ascending numbers has been identified inbrackets. We get:

# of ‘run–ups’ with length 1 is R1 = 2# of ‘run–ups’ with length 2 is R2 = 0# of ‘run–ups’ with length 3 is R3 = 2

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 22: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.2 (cont.)

b) E.g. consider the following sequence:

(0.134, 0.279, 0.886), (0.197), (0.011, 0.923, 0.990), (0.876)

Here each run of ascending numbers has been identified inbrackets. We get:

# of ‘run–ups’ with length 1 is R1 = 2# of ‘run–ups’ with length 2 is R2 = 0# of ‘run–ups’ with length 3 is R3 = 2

Now in a long sequence of n ‘random’ numbers, let Rk be thenumber of ‘run–ups’ of length k. It is possible to calculate theexpected values of Rk and compare with the observed values.

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 23: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.2 (cont.)

c) E.g. for n = 5000:

k 1 2 3 4 5 ≥ 6

Expected Rk 833 1042 458 132 29 6

Observed Rk 824 1074 440 113 42 7

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 24: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.2 (cont.)

c) E.g. for n = 5000:

k 1 2 3 4 5 ≥ 6

Expected Rk 833 1042 458 132 29 6

Observed Rk 824 1074 440 113 42 7

If the observed frequencies differ greatly from the expectedvalues,then this would indicate dependence in the sequence, whichshouldn’t be there.

In the table for c) above, however, the discrepancies are notparticularly large, so there is no real evidence for non–randomness.

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 25: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.2 (cont.)

c) E.g. for n = 5000:

k 1 2 3 4 5 ≥ 6

Expected Rk 833 1042 458 132 29 6

Observed Rk 824 1074 440 113 42 7

If the observed frequencies differ greatly from the expectedvalues,then this would indicate dependence in the sequence, whichshouldn’t be there.

In the table for c) above, however, the discrepancies are notparticularly large, so there is no real evidence for non–randomness.

Exercise: Think what would happen for a sequence like the one ina) above.

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 26: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.3: Checking for dependence: scatterplots

Now consider a plot of the n + 1th number against the nth number.I.e. we plot the pairs (U1,U2), (U2,U3), (U3,U4), . . .

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 27: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Example 3.7.3: Checking for dependence: scatterplots

Now consider a plot of the n + 1th number against the nth number.I.e. we plot the pairs (U1,U2), (U2,U3), (U3,U4), . . .

For a good random number generator, these points should berandomly scattered in the unit square. We consider two ways inwhich a scatterplot can deviate from this . . .

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 28: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Solution (Slide 1 of 2)

10

0 U

U

1

n+1

n

+

+

+

+

++ +

++++ +

+

++

++

++ +

++

+

+

+

+

+

+

+

+++

++++

++

+

+

++

++

++

+

+

+ +

+

+

+

Here the points are ‘clustered’:— the mean separation betweenpoints is less than would be expected under randomness.

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 29: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Solution (Slide 2 of 2)

10

0

++

+

+

U

U

1

n+1

n

+

+

++

+

+

+

++ +

+

+ +

+

+

+

+

+ +

+

+

+

++

+

+

Here the points are too evenly spread:— the mean separationbetween points is greater than would be expected underrandomness.

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 30: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Summary

Summary

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 31: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Summary

Summary

All of the ideas above involve comparing some observed behaviourin our pseudo–random sequence with the expected behaviour ifrandomness and independence hold.

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7

Page 32: MAS1302: Computational Probability and Statistics: Week 7ndw/week7.pdf · Simulating Continuous Random Behaviour (cont.) MAS1302: Computational Probability and Statistics: Week 7

Simulating Continuous Random Behaviour (cont.)

Summary

Summary

All of the ideas above involve comparing some observed behaviourin our pseudo–random sequence with the expected behaviour ifrandomness and independence hold.

In order to check whether the observed behaviour is too far fromwhat we’d expect (or too close!), we would need to carry out anappropriate hypothesis test for goodness of fit. You will studythese in Stage 2!

Dr. David Walshaw

MAS1302:Computational Probability and Statistics:Week 7