+ All Categories
Home > Documents > Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a...

Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a...

Date post: 30-Apr-2020
Category:
Upload: others
View: 13 times
Download: 0 times
Share this document with a friend
21
1 Topic 7 Random Number Generation Contents 7.1 Introduction ...................................... 2 7.2 The Linear Congruential Method .......................... 5 7.3 Tests For Random Numbers ............................. 5 7.3.1 Frequency Test ................................ 6 7.3.2 Runs Test ................................... 6 7.3.3 Poker Test ................................... 7 7.4 Random Variate Generation ............................. 7 7.4.1 Inverse Transform Method .......................... 8 7.4.2 Acceptance/Rejection Method ....................... 10 7.5 Empirical Distributions ................................ 18 7.6 Summary ....................................... 20
Transcript
Page 1: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

1

Topic 7

Random Number Generation

Contents

7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

7.2 The Linear Congruential Method . . . . . . . . . . . . . . . . . . . . . . . . . . 5

7.3 Tests For Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

7.3.1 Frequency Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

7.3.2 Runs Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

7.3.3 Poker Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

7.4 Random Variate Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

7.4.1 Inverse Transform Method . . . . . . . . . . . . . . . . . . . . . . . . . . 8

7.4.2 Acceptance/Rejection Method . . . . . . . . . . . . . . . . . . . . . . . 10

7.5 Empirical Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

7.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Page 2: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

2 TOPIC 7. RANDOM NUMBER GENERATION

7.1 Introduction

In this unit we talk about random numbers. In particular, we will address the followingquestions:� What are random numbers?� How do we generate random numbers (for example for simulations)?� How do we test numbers for being random?

As a warm-up to this topic we look at a number of examples that claim to be randomnumbers. Have a look at the following, and try to decide for yourself whether you wouldcall those sequences of numbers random or not:��� ;� 01314 51333 6� �������� ������� � ������ �������� ����������������� ������� �������� �������� ������������������ �������� ��������� ��� ����� ������ ���������� � ������ ��������� ������� � �������� �������� �������� �������� �������� � ����� �������� �������� ������� � �������������������������� �� ����� � ������ ��� ����� ����������������� ������� � �������� � ������� ��������

c�

HERIOT-WATT UNIVERSITY 2004

Page 3: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

7.1. INTRODUCTION 3

� 3141592653 5897932384 6264338327 9502884197 16939937510582097494 4592307816 4062862089 9862803482 53421170679821480865 1328230664 7093844609 5505822317 25359408128481117450 2841027019 3852110555 9644622948 95493038196442881097 5665933446 1284756482 3378678316 52712019091456485669 2346034861 0454326648 2133936072 60249141273724587006 6063155881 7488152092 0962829254 09171536436789259036 0011330530 5488204665 2138414695 19415116094330572703 6575959195 3092186117 3819326117 93105118548074462379 9627495673 5188575272 4891227938 18301194912983367336 2440656643 0860213949 4639522473 71907021798609437027 7053921717 6293176752 3846748184 67669405132000568127 1452635608 2778577134 2757789609 17363717872146844090 1224953430 1465495853 7105079227 96892589235420199561 1212902196 0864034418 1598136297 74771309960518707211 3499999983 7297804995 1059731732 81609631859502445945 5346908302 6425223082 5334468503 52619311881710100031 3783875288 6587533208 3814206171 77669147303598253490 4287554687 3115956286 3882353787 59375195778185778053 2171226806 6130019278 7661119590 92164201989380952572 0106548586 3278865936 1533818279 68230301952035301852 9689957736 2259941389 1249721775 28347913151557485724 2454150695 9508295331 1686172785 58890750983817546374 6493931925 5060400927 7016711390 09848824012858361603 5637076601 0471018194 2955596198 9467678374

Ideally, you have looked for patterns in these sets of numbers, or you might have thoughtabout how these numbers where created.

The data of the previous activity were generated as follows:� 0;� my office phone number;� random data from a normal distribution;� random data from an exponential distribution;� the first 1250 digits of � .

Let us first try to figure out what random means. Possible synonyms are typical,representative, arbitrary, accidental, scrambled, aimless, patternless, senseless,causeless, independent, incidental, indeterminate, indescribable, undirected,uncontrolled, uncertain, unrelated, unordered, unpredictable.

An informal definition of randomness could be the following: Something is random if it isnot possible to describe it more briefly than to reproduce it. To be more precise we willuse the following working definition for random numbers:A (long) sequence of numbersis random if the shortest computer program printing out the sequence has about the size(length) of the sequence.

A formal definition of randomness for infinite sequences is the following:An infinite

c�

HERIOT-WATT UNIVERSITY 2004

Page 4: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

4 TOPIC 7. RANDOM NUMBER GENERATION

sequence of numbers is random if, for every block length � , the probability that aparticular block of length � is observed is �!�� .

For example for a sequence of numbers between 1 and 6 to be random it is not enoughthat each number occurs with probability 1/6. We also require that each tuple ofnumbers11, 12, 14, 15, 16, 21, 22, 23, 24, 25, 26, 31,..., 66 occurs with probability1/36, and that each triple of numbers like 111, 123, 461,... occurs with probability 1/216,etc.

We first consider random numbers for the uniform distribution on the interval [0,1]. Recallthat for this distribution mean and variance are given by"$#&%(')+*-,�* #/. 0 *2143 ') # 0 and5 2 # % '

0* 2 ,�*76 " 1 # . 8 *-9 3 ') 6 : # 0

In practice it is hard to generate true random numbers, in particular on a computer.Such numbers will always have patterns (in particular repetitions), and the aim is thusto reduce such patterns.

Among others the following problems occur with computer generated ’random’ numbers:; The numbers may not be uniformly distributed.; The values are usually discrete, and not continuous.; The (sample) mean may differ from 12 .; There may be cyclic variations, including

– autocorrelation between numbers;

– numbers successively lower or higher than adjacent numbers;

– a couple of numbers below the mean followed by a couple of numbers abovethe mean.

In practice we will have to apply tests to sequences of such numbers to check whetherthey are sufficiently random or not.

There are also some more practical issues, even if we accept that we can only generatepseudo-random numbers:; The method (program) generating pseudo-random numbers should be fast.; The pseudo-random numbers should have a long cycle. If 100,000 numbers are

needed for a simulation the cycle length should be at least that big.; The random numbers should be replicable (yes, indeed (!)). This feature is neededfor debugging and comparison.; The generated numbers should approximate the uniform distribution (or any otherdistribution for which we generate random numbers).

c<

HERIOT-WATT UNIVERSITY 2004

Page 5: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

7.2. THE LINEAR CONGRUENTIAL METHOD 5

7.2 The Linear Congruential Method

The linear congruential method is the most common method to produce pseudo-randomnumbers on a computer. The method produces integers between 0 and =?>$@ accordingto the rule xn + 1 ACBEDGFIHKJMLN mod m, F-O , D , L and = are given numbers.These numbersare then used to represent the numbers yn AQF2H�R = in the unit interval [0,1]. The valueF O is called the seed, D the constant multiplier, and L the increment. The choice of valuesF-O , D , L , = has big impact on the quality of the random numbers. For = common valuesare 231 >S@ and 248.

For = a power of 2 the period (cycle length) = can be achieved with LUTA 0 relativelyprime to = (i.e., the greatest common divisor gcd( L , = ) = 1) and DWV 1 mod 4.

If LXA+Y then the largest possible period is = /4, which can be achieved with DZV 3 mod 8or DWV 5 mod 8, and F[O odd.

Example

Problem:

We consider the values D\A @] , LXA+Y , = A_^�`aA&b c and F-O = 1, 2, 3, 4.

Solution:d Y @ ^ ] c e b f g h @ Y @�@ @ ^ @] @ c @ e @ bF O A @ @ @] c @ ^ @ @ f ^�h e�f ] f ]�] c�e h e ] c�h b @ ^�e e @F-OaA&^ ^ ^�b @ g c�^ ] c e�g eY @ Y ^F-OaA ] ] ] h e�h b ] e @ ^ ] c ] cGf ] e f ^�f ]�@ @ h e�e @�@ @ e ]F-OaAic c e�^ ] b ^Y cThese numbers show how vital a good choice of the parameters is. Also note the largegaps between the pseudo-random numbers. Even with the period length of 16 the gapsbetween the random numbers are quite large.

7.3 Tests For Random Numbers

There are a number of tests that we can perform on sequences of pseudo-randomnumbers to ensure that the sequences have the desired properties. Among them arethe following:j Simple frequency test. This test uses the chi-square test to compare the (uniform)

distribution against the set of numbers in the sequence.j Autocorrelation test.This tests for correlation between the numbers and comparesit to the expected correlation 0.j Runs tests.These simple tests test for ups and downs, or for values above andbelow the mean.j Gap test.This test counts the number of digits that appear between repetitions of

ck

HERIOT-WATT UNIVERSITY 2004

Page 6: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

6 TOPIC 7. RANDOM NUMBER GENERATION

a particular digit, and then uses the Kolmogorov-Smirnov test to compare with theexpected size of gaps.l Poker test.Treats numbers grouped together as a poker hand. These hands arecompared to what is expected using the chi-square test.

We will treat some of these tests with the following set of data, created using the C++random number generator:m�npo q m�npo�r m�nsro m�nutv m�npw q m�npx�v m�npo�r m�npw�x m�npym m�npz�zm�npv�x m�npz�o m�npw�t�m�npvm m�npv q m�npz�r m�n{q�t|m�npz�r m�nsrx m�npx�rm�nutz m�npv�w m�npv�o m�npxm m�npo�x m�npx�t�m�npv�x m�npz q m�npv�o m�npx�vm�npxm m�n}m�x m�npz�x m�npy�w m�nsr m m�npv�o m�npw q m�npy�r m�nutz m�n{q�xm�npxm m�nsr�q m�npz�z m�npzm m�npo�y m�npx�o m�npw�w m�npz�y m�nutv m�nsr�tOur level of significance will always be ~�� m�n}m�y .7.3.1 Frequency Test

The first test is a frequency test. We sub-divide the interval [0,1] into �S� t�mintervals

of equal length (0,0.1),n�n�n

,(0.9,1) and count the number ��� of observed numbers in eachinterval � . The statistics �������Q����u� �����-�E�I�2������2� has a chi-square distribution with 10 - 1degrees of freedom. Here ��� is the expected number in each interval, which is �K���� ���� � y .We test the null hypothesis that the data are from a uniform distribution. The hypothesisis accepted if � �� � � � ¡ ¢ � tv�npw�tw

.

We find the following datat z x q y v r o w t�m� � t q w o z x o y y y���-�E�I�I��� ��I� x�npz m�npz x�npz tnpo tnpo m�npo tnpo m m mand � 2 � tz�npo

. Thus, the hypothesis is accepted.

This frequency test is usually applied if there is enough data so that each �£�¥¤ y.

7.3.2 Runs Test

The first runs test is about ups and downs, i.e., whether the next number in the listof random numbers is above or below the current number. The following sequence isobtained from our data set.

+ - - + - + + - - + - + - + -+ - + - - + - - + - + - + - -- + + + - + - - + - + - - + -+ - - +

A consecutive sequence of ¦ ’s or § ’s is called a run. If the sequence is truly random

the mean and variance of the number of runs are given by ¨ = 2N - 13 and ©ª�¬« ��­¯® � � ¢¢ �

where ° is the total number of random numbers. If ° is sufficiently large this distributionof runs is approximately normal, and we can use standard hypothesis testing.

We test the null hypothesis that the data come from a uniform normal distribution at

HERIOT-WATT UNIVERSITY 2004

Page 7: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

7.4. RANDOM VARIATE GENERATION 7

significance level ²�³+´�µ}´�¶ .We observed 37 runs, and calculate· ³¹¸�º¼»�½¾ ³¿¸�º¼»À¸�¸Á  µp¶�Ã�à ³Zĵ ¸ Ã�Ã�Ã. For the two-tailed test the hypothesis is rejected if z < - z0.05 ÅÇÆ or if z > z0.05 ÅÇÆ . Thevalue for z0.05 ÅÇÆ is 1.960, and the null hypothesis that the data is from a uniformdistribution is accepted.

A different runs test tests for values above and below the mean. For this test we get thefollowing data

+ + + - + - + + + - + - + + +- - - + - - + + - + - + - + -- - - + + + + + - - - + - - +- + - - +

If ÈÊÉ and È Æ are the numbers of observations below and above the mean (here Ë Ì andË�à ), and Í the total number of runs (here Ë�Î ), then Í is approximately normally distributedwith mean and standard deviation½ ³ ÆÇÏGÐÑÏ�ÒÓ Ô ÉÆ and ¾ ³WÕ ÆÇÏ Ð Ï Ò4Ö ÆÇÏ Ð Ï ÒØ× ÓÚÙÓ Ò Ö Ó × É Ùwhere Û is again the number of random numbers.

The distribution z = b - ÜÝ is approximately a standard normal distribution, and we findz = 29 - 25.45Þ É Æ ß ÆáàÇâ¯ã ³Zĵ}´�ÄÃ�ÄSince » · à ß à Éåä ÆKæ ĵ}´�ÄÃ�Ä æ · à ß à Éåä Æ the null hypothesis is accepted.

7.3.3 Poker Test

We test for patterns in individual numbers. We generated random numbers of two digitseach.

If the numbers are truly random then 10100 Ä�´�´ % will have twice the same digit, while

90100 Ä�´�´ % will have different digits.

We use again the chi-square test with random variable ç Æ ³¬èiéuê Éáë Æ Ö�ì-íE×Iî2í Ù Òî2í where ï éare the observed frequencies, and ð é the expected frequencies.

With our data of 50 random numbers we findï é ð é Ö�ì-íE×Iî2í Ù Òî2íTwo different digits Ì º Ì�¶ ´�µ}´ Â� Î

Two like digits ¸ ¶ ´�µ ÂSince ç Æ ³+´�µ Â�Â� Πæ ç à ß àáñ ë Æ ³&¶�µpÎ�Î�Ä the null hypothesis is accepted.

7.4 Random Variate Generation

Next we want to generate random numbers for more general probability distributions. Wewill discuss two methods, the inverse transform method and the acceptance/rejection

HERIOT-WATT UNIVERSITY 2004

Page 8: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

8 TOPIC 7. RANDOM NUMBER GENERATION

method.

7.4.1 Inverse Transform Method

The following result gives in theory a method to generate random numbers for anyprobability distribution:

Let ó be a (continuous) random variable with cumulative distribution ôöõ÷ó[ø . Then therandom variable ùûú+ô7õ÷óüø is a uniform random variable over the interval ý\þÿù þ � .Indeed, ù takes values between 0 and 1. Moreover, for every fixed ù�� , �7õ÷ù þÿù��ø�ú��öõ÷ó$þó � ø úQô7õ÷ó � øÚú?ù � (as shown on the next graph) so that the cumulative distribution for ùis

� ù if ý þÿù þ �ý else

Figure 7.1

Taking the derivative of ô with respect to ù shows that the density function is indeed� ù if ý\þÿù þ �ý else

This result yields the following algorithm for generating random numbers for a probabilitydistribution with density function �[õ÷ó[ø and cumulative distribution ô7õ÷óüø ú�� �� �-õ÷óüø���� :� Draw uniformly random numbers ��� between 0 and 1.� Substitute into ��� ú+ô7õ÷ó���ø and solve for ó�� .In practice the last step is often difficult or even impossible and we have to look forapproximations of the function ó��üú+ô �� õ����Ñø .

c�

HERIOT-WATT UNIVERSITY 2004

Page 9: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

7.4. RANDOM VARIATE GENERATION 9

Examples

1.

Problem:

We look at the exponential distribution with density function�! �"!#%$'&)(+*-,/.0 if "21433 elseand cumulative distribution (for x 1 0)5 �"�#7698;:<�= �� �>?#A@�>7698�:B & (+C.0 @�>76ED/FHG (IC.KJ :B 6MLNFOG (+*.

Solution:

If we let P 6MLQFOG < :�R 0 and solve for " then "S6TFVU ln WLQF P #Thus, drawing uniformly random numbers P+X between 0 and 1 gives random numbers " Xthen "Y6ZFHU ln WL[F P�X # for the exponential probability distribution. Note that in practiceLQF P is replaced by P .Figure 7.2 shows a frequency plot of 1000 random numbers P X drawn using the C++random number generator. Here we used the inverse transform to get random numbersfor the exponential distribution. The interval length is 0.1.

Figure 7.2: Interval length 0.1

2.

Problem:

We look at the uniform distribution on the interval \ ]�^)_)` . We generated uniformly randomnumbers P on the unit interval \ 3 ^ L ` . To get random numbers for the uniform distribution

on \ ]�^)_)` recall that the density function for this distribution is a bc <�d if a e " ef_3 elseso that

the cumulative distribution is 0 if " eg] , 1 if "h1 _ , and5 �"�#76�ij �> e "�#76M8 =<�= �� �>?#W@k>l68 :d bc <�d @�>76 : <�dc <�d for a e x e b.

cm

HERIOT-WATT UNIVERSITY 2004

Page 10: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

10 TOPIC 7. RANDOM NUMBER GENERATION

Solution:

So we solve n2oqp+r�st r�s for u and get uvoxwzyM{}|H~Yw���n . Thus drawing random numbersn�� for the uniform distribution on � ������� and calculating u���o�w�yT{}|H~Yw���n�� gives randomnumbers for the uniform distribution on the interval � w��)|)� .The above mentioned technique works well if the cumulative distribution has a closedform and if we can calculate the inverse of this function, i.e., solve for u . However, formany important distributions, notably the normal distribution, this is not possible. Inpractice we have to approximate the function u�oE� r�� {�nk� . A simple and reasonablygood approximation for the inverse of the cumulative distribution of the standard normaldistribution is the function u�o�� r�� {�nk��o��-�A� ���/� r����-r �-���A� ���/���  �¢¡?£W¤ . The approximation gives atleast one correct decimal of the inverse for n taking values between 0.00135 and 0.9986(see Schmeiser, Approximations to the inverse cumulative normal function for use onhand calculators, Applied Statistics 28:179-176, 1979).

Another (much better) approximation (Odeh, Evans, Algorithm AS 70: percentage pointsof the normal distribution, Applied Statistics 23:96-97, 1974) isu¥o�¦§y©¨zª«­¬ ��® «�¯ «¨ ª«°¬ ��± «�¯ « where ¦Vo³² ~H´�µ·¶;{W�Q~¸nk� and ¹ � , º � are the following values:¹ � oT~H��»½¼I´I´I´I¼I´�¾¿�À��ÁIÁ º � o���»Â��ÃIÃI¼�¾kÁ�¾kÄI´+��Ĺ � oT~Å� º � o���»½ÆIÁIÁIÆIÁ���Æ�Ç��+¾kÃIƹ�È�oT~É��»½¼�¾k´I´�¾k´+��ÁIÁIÆ�¾ÊÇ º�È�o���»½ÆI¼��I�À��¼�¾kÄI´I¼IÄIĹ�Ë�oT~É��»Â��´+�+¾k´I¼���´��À��´�¾kÆ º�ËÉo���»·�À��¼IÆI¼�ÇIÇ+ÆI´IÁIƹ�Ì�oM~É��»Â�I�I�I�+¾kÆI¼IÄ�¾k´I´��À�¿�;kÁ ºÀÌÉo���»Â�I��¼IÁIÆIÄ+�kÇ��I��ÄI¼�¾This approximation is accurate up to about 6 digits for ��»½ÆÏÎ ngÎ � . Symmetry ofthe normal distribution allows to extend this to the interval �ÐÎ nÑÎ 0.5 using thetransformation nÓÒÔ ��~Õn and uSÒÔ ~Õu .

7.4.2 Acceptance/Rejection Method

Another method to generate random variate is the acceptance/rejection method. Thismethod is often used in practice.

Suppose we have a density function Ö�{�u!� and we want to generate random data withthis density. Since Ö!{�u!� may be difficult to compute, or since it may be impossible tocompute the inverse of the cumulative distribution, we assume the existence of anotherdensity function × which is easy to compute and which satisfies Ö�{�u��HÎ9Ø�» ×�{�u!� for someconstant Ø (and all u ). The algorithm then works as follows:Ù Generate Ú randomly for the uniform density on the interval {Û�������Ù Generate Ü randomly for the density × .Ù If ÚhÎYÖ!{�Ü��-Ý+ØÞ×�{�Ü¿� accept Ü , otherwise reject Ü .Ù Repeat.

A sequence of numbers Ük� accepted by this algorithm is a sequence of random numbersfor the density Ö!{�u!� .The idea behind the algorithm is that a sequence of random numbers Ü for the density ×is ’thinned’ to give a sequence of random numbers for the density Ö!{�u!� .

HERIOT-WATT UNIVERSITY 2004

Page 11: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

7.4. RANDOM VARIATE GENERATION 11

We note that the literature shows various modifications of this algorithm to increasespeed.

Examples

1.

Problem:

We consider the density functionà�á�â�ã7äæåçéèSêë â 0 ì â ì ëíelse

where the graph is simply a straight line.

Figure 7.3: Density function of îSolution:

The uniform density function on the interval ï í�ð)ë�ñ has density function ò á�â!ã�äó'ôõ í ì â ì ë0 else

We do not have that î á�â!ã ìÏò á�â�ã for all â , but we do have î á�â!ã ì ë ò á�â!ãfor all â . Thus we can use the uniform distribution on ï í�ð)ë�ñ to generate random numbersfor the density ò as follows:ö Generate ÷ randomly for the uniform density on á í�ð ê ã .ö Generate ø randomly for the density ò , which is the uniform density on ï í�ð)ë�ñ .ö Accept ø if ÷ùìYî á ø ã)ú ë ò á ø ã , i.e., if ÷hì á ôõ ø ã-û;á ëýü ôõ ã7ä ôõ ø otherwise reject.

The following is a list of 20 random numbers ÷�þ uniformly distributed on the interval á í�ð ê ã ,of 20 random numbers ø þ , uniformly distributed on the interval ï í�ð)ë�ñ , and the verdict ’a’or ’r’ for acceptance or rejection:

cÿ

HERIOT-WATT UNIVERSITY 2004

Page 12: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

12 TOPIC 7. RANDOM NUMBER GENERATION

��� ��� ������ � ? a � r0.9501 0.9931 n r0.2311 1.7995 y a0.6068 1.6433 y a0.4860 1.2898 y a0.8913 1.6359 n r0.7621 1.3205 n r0.4565 0.6839 n r0.0185 0.5795 y a0.8214 0.6824 n r0.4447 1.0682 y a0.6154 1.4542 y a0.7919 0.6189 n r0.9218 1.1361 n r0.7382 0.7408 n r0.1763 1.4055 y a0.4057 0.8898 y a0.9355 1.3891 n r0.9169 1.2426 n r0.4103 1.0931 y a0.8936 1.5896 n r

Thus, a sequence of random numbers for the density � is ���������� ���������� ���������� ����������� ���������� �� ������� �� ������� ����������� ��������� Of course, it would be much easier to use the inverse transform technique for this simpleexample.

2.

Problem:

Consider the density function !#"%$'&)( * ,+.-+ -0/ � $21 /4365 � � $ � ��else

which is plotted in

Figure 7.4.

c7

HERIOT-WATT UNIVERSITY 2004

Page 13: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

7.4. RANDOM VARIATE GENERATION 13

Figure 7.4: Density function of 8#9%:';We want to generate random numbers for this distribution using the acceptance/rejectionmethod.

Solution:

The easiest way would be to use again the uniform distribution on < =�>@?BA . However, withthis method we would reject many numbers. We will try to do better by using a piece-wise linear function that approximates 8C9%:D; .Figure 7.5 shows our density function plus the two lines given by the equationsEGF ?�HJIH ILKNM :EGF ?�HPOH I KNM 9Q? K :';CR SH I KTM

cU

HERIOT-WATT UNIVERSITY 2004

Page 14: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

14 TOPIC 7. RANDOM NUMBER GENERATION

Figure 7.5: Density function and 2 lines

The first line is the tangent of V#W%X'Y through W[Z�\]Z�Y , the second is the tangent throughW_^�\@VCW_^6Y,Y with the constant term V#WQ`�Yba cd.egfDh added. These lines were chosen since byinspection both dominate the graph of VCW%X'Y in the interval i Z�\@`Bj .To simplify calculations we look at the two approximationsk al`nm�Z�o�p�o�Xk aNqrZ�m�pBs�t�u�Xwv)^�m�u�p�o�x

Figure 7.6: Graph of V and 2 linesThe two lines meet in the point Xzy|{}Z�m�u�x�s~p and k y�{�^�m�^Pu�Z�s . Thus we look at thepiece-wise linear function

c�

HERIOT-WATT UNIVERSITY 2004

Page 15: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

7.4. RANDOM VARIATE GENERATION 15

�2�%�'��� �� � �n��������� �if ��� � � ����� ���������~�

- 0.7495x + 1.5736 if�D� � � �N�� else

This function will play the role of �g� in the algorithm. Note that���%�D�

is not a densityfunction. Even though it is everywhere positive it does not satisfy ���� � ���%�D�������¡ Indeed, that integral is just the area under the piece-wise linear function

�, which is¢ �� � �2�%�'�����£� ¢¥¤P¦� �n��������� �§���©¨ ¢¥ª¤P¦¬« �����B��­�� �G¨®  ��������� ���

� �n���������� ��ª�¯¯¯¯ ¤P¦� « �����B��­��� ��ª�¯¯¯¯ ª¤ ¦ ¨°  ��������� �²± ª¤P¦� �n���������� ��ª� « �����B��­��� � � « �2ª� �C¨)  ��������� � � « ���6��³  ���������Call this value ´ (for area), then � �%�'�µ�·¶¸ �2�%�'�

is a density function and for � � ´ we havethat �g� �%�D�

dominates ¹ �%�D�.

Thus the algorithm becomes as follows:º Draw » uniformly on� ��¼  6�

.º Draw ½ with density � .º If » � ¹ � ½ �]¾ �g� � ½ �²� ¹ � ½ �]¾��2� ½ �then accept ½ , otherwise reject ½ .

To draw ½ with density � let us use the inverse transform method.

With � �%�D��� ¶¶,¿ ª �_À]� ���%�D�and the cumulative distributionÁ �%�'�²� ¢ ¤� � � �%Â]����Â

we have� �%�D��� �� �   ����­���� �if �G� � � �2�

- 0.6285x + 1.3070 if� � � � �N�� else

depicted in Figure 7.7:

HERIOT-WATT UNIVERSITY 2004

Page 16: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

16 TOPIC 7. RANDOM NUMBER GENERATION

-0.20

1.80 0.6 0.8

0.2

0.4

0.6

0.8

1.4

1.2

1

0.2 21.21 1.60.4 2.21.4

Figure 7.7: Densities Ä2Å%Æ'Ç and È#Å%Æ'ÇFor ÉGÅ%ÆDÇ we have that ÉGÅ%ÆDÇËÊ)Ì if Æ£ÍTÌ , and ÉGÅ%Æ'DzÊÏÎ if Æ£ÐNÑ . If Ì�ÍÒÆÓÍNÑ we calculateÉÔÅ%Æ'ÇËÊ�Õ×ÖØ�Ù Ä2Å%Ú]Ç�Û�Ú

Ê Õ ÖÜ Î�Ý�Þ�ß�Ñ�Ñ�Ú�Û�ÚÊ Î�Ý�Þ�ß�Ñ�ÑÑ Ú�à�áááá Ö ÜÊlÌ�Ý�â�ã�Þ�ÎäÆ à

If Æ Ü ÍÒÆ£ÍNÑ thenÉÔÅ%Æ'ÇËÊ Õ×ÖØ�Ù Ä2Å%Ú]Ç�Û�ÚÊ Õ ÖPåÜ g(t) Û�Úzæ Õ ÖÖPå g(t) Û�ÚÊ)Ì�Ý�â�ã�Þ�ÎäÆ2àÜèçÌ�Ý�Þ�Ñ�â�ã Ñ Ú�à áááá ÖÖ å æ°Î�Ý�é�Ì�ê�Ì�Ú�ë ÖÖ åÊ ç Ì�Ý�é�Î6êBÆ à æ)Î�Ý�é�Ì�ê�Ì�Æ ç Ì�Ý�é�Þ�â�Î

Thus

ÉGÅ%ÆDÇ�Ê ìííî ííïÌ if ÆÓÍTÌ

0.8461x2 if 0 ÍÒÆðÍÒÆ Üç Ì�Ý�é�Îäã�Ñ�Æ à æ)Î�Ý�é�Ì�ê�Ì�Æ ç Ì�Ý�é�Þ�â�Î if Æ Ü ÍÒÆÓÍNÑ1 else

The graphs of the density Ä and its cumulative distribution É are pictured in the graphbelow. The gap in the graph stems from the fact that we made several rounding errors.

HERIOT-WATT UNIVERSITY 2004

Page 17: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

7.4. RANDOM VARIATE GENERATION 17

Figure 7.8: Graph of ò and its cumulative distribution óWith ôBõ�ö÷óÔø%ù�õPú�ö)û�ü�ý�þ�ÿ�� the inverse ó����6ø[ô~ú is the function given byó ��� ø%ùDú�ö � � �õ � ��� � if û��Tô��TôBõ

1.3070 - � 1.30702 � ��� õ � � ���� õ ����� ��� ����� õ � � ��� if ô6õ��Tô�� �where the second part simplifies to

� �,õ�!_õ �#" � � ��$�� ��� ��$���� �õ ����� �Note again that the calculations are not entirely accurate due to rounding errors.

Now we can finally write down the algorithm to draw random numbers for the density % :& Draw ' uniformly on ( û*)+�, .& Draw ô uniformly on ( û*)+�, .& If û��Tô-�Tû�ü�ý�þ�ÿ�� let . be

� �õ � ��� � , else .Gö � �,õ�!_õ � " � � ��$�� ��� ��$���� �õ ����� � .& If '0/1%Cø2.4ú�354�ø2.4ú accept . , otherwise reject.

c6

HERIOT-WATT UNIVERSITY 2004

Page 18: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

18 TOPIC 7. RANDOM NUMBER GENERATION

7.5 Empirical Distributions

We treat empirical distributions by way of example.

Example

Problem:

Suppose that data is collected about repair times (in hours):

Frequency Rel.Freq Cum.Freq

0 7 x 7 0.5 31 0.31 0.310.5 7 x 7 1.0 10 0.1 0.411.0 7 x 7 1.5 25 0.25 0.661.5 7 x 7 2 34 0.34 1

The true cumulative probability function is unknown to us.

Figure 7.9: Cumulative distribution

Solution:

The inverse transform technique applies here directly. If for example 8:9 , a randomnumber from the uniform distribution, is 0.83, then we can read off the ; 9 value fromthe graph as 1.75 as in Figure 7.10.

c<

HERIOT-WATT UNIVERSITY 2004

Page 19: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

7.5. EMPIRICAL DISTRIBUTIONS 19

Figure 7.10

If we need more random numbers we will use a more systematic approach.

If =0>@?A>@=*BDC the cumulative probability function is EGFIHJ KL�MNHHJ OMNH ?PFQ=*BDR�S5? or ?TFVUWBDR*U�E .Here E takes values between 0 ( ?XFY= ) and 0.31 ( ?ZF[=*BDC ).If =*BDC1>\?]>^U the cumulative function has slope HJ _�L�MNHJ KLL�J HMNHJ O F`=*BDS and the intercept isfound by solving the equation =*BbacUdF]=*BDSdefUWBg=ihkj using the fact that the point l UWBg=*m�=*BbacUonis on the line. Thus, the intercept is =*BDS*U and the cumulative function has the equationE�FY=*BDS5?�hk=*BDS*U and E takes values between 0.31 and 0.41. The inverse is the function

?XF U=*BDS l2Eqpr=*BDS*UonFiC5EsptUWBg=uCIn general, if we have a line through l2? H m�E H n and l2? L m�E L n then the slope of the line is v�w M v�xy w M y xand the intercept is found by solving, for example, E H F\v�w M v�xy w M y x ? H hzj so that j{F y w|v�x M y x�v�wy w M y xThis way we get the following table describing the inverse of the cumulative distribution:

=*Bg=�=->}E~>t=*BD�*U ? F UWBDR*U�E=*BD�*Ud>}E~>t=*BbacU ? F C5Eqp1UWBg=uC=*BbacUd>}E~>t=*BDR�R ? F S5EihA=*B�U+�=*BDR�R�>}E~> UWBg=�= ? F UWBbaf��Edhk=*BDC��

c�

HERIOT-WATT UNIVERSITY 2004

Page 20: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

20 TOPIC 7. RANDOM NUMBER GENERATION

Figure 7.11

This function can now be used to generate random numbers for our empirical distributionusing the inverse transform method.

7.6 Summary� Complex systems are often simulated using computers. For this we need randomnumbers distributed according to various distributions.

� One basic problem arises that random numbers generated by a computer areonly pseudo-random. Nevertheless we have to design methods to generate suchnumbers which come close to being random.

� In practice it is enough to implement a random number generator for the uniformdistribution on the unit interval � �*�+�� . Usually the linear congruential method isused. Random numbers for other distributions can be generated using the inversetransform technique. This technique also applies to empirical distributions.

� In practice the acceptance/rejection method is often used to generate randomnumbers for density functions where the inverse transform method does not apply.

� Tests are employed to check whether sequences of numbers can be consideredrandom or not.

c�

HERIOT-WATT UNIVERSITY 2004

Page 21: Random Number Generation - Heriotjphillips/DAS/Topic7.pdf · 6 TOPIC 7. RANDOM NUMBER GENERATION a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the

GLOSSARY 21

Glossary

acceptance/rejection method

A method to generate random numbers for a density � by using random numbersfrom another density � such that �~����� for some constant � . Some of the randomnumbers for � are accepted to become random numbers for � , some are rejected.

frequency test

The frequency test uses the chi-square distribution to test how the frequencydistribution of a sequence of pseudo-random numbers compares to randomnumbers of the uniform distribution.

linear congruential method

The linear congruential method is the most common method to produce pseudo-random numbers on a computer. The method produces integers between 0 and�\��� according to the rule xn + 1 �����f�N��� �+� where � � , � , ¡ , � and � are givennumbers.

poker test

The poker test for sequence of pseudo random numbers treats numbers groupedtogether as a poker hand. These hands are compared to what is expected usingthe chi-square test.

pseudo random number

Pseudo random numbers are sequences of numbers generated by a device (oftena computer) that are supposed to model truly random numbers.

random numbers

An infinite sequence of numbers is random if, for every block length ¢ , theprobability that a particular block of length ¢ is observed is �o£ ¢ .

runs test

The simple runs tests test for ups and downs, or for values above and below themean in a sequence of pseudo-random numbers.

seed

The seed is the starting value in a method to generate random numbers.

HERIOT-WATT UNIVERSITY 2004


Recommended