Random Numbers All stochastic simulations need to “generate” IID U(0,1) “random numbers” Other random variates coming from other distribution can be generated.

Slides:



Advertisements
Similar presentations
RSLAB-NTU Lab for Remote Sensing Hydrology and Spatial Modeling 1 An Introduction to R Pseudo Random Number Generation (PRNG) Prof. Ke-Sheng Cheng Dept.
Advertisements

Generating Random Numbers
Random Number Generation. Random Number Generators Without random numbers, we cannot do Stochastic Simulation Most computer languages have a subroutine,
 1  Outline  generation of random variates  convolution  composition  acceptance/rejection  generation of uniform(0, 1) random variates  linear.
Random Numbers. Two Types of Random Numbers 1.True random numbers: True random numbers are generated in non- deterministic ways. They are not predictable.
Random number generation Algorithms and Transforms to Univariate Distributions.
Random Number Generators. Why do we need random variables? random components in simulation → need for a method which generates numbers that are random.
1 Random Number Generation H Plan: –Introduce basics of RN generation –Define concepts and terminology –Introduce RNG methods u Linear Congruential Generator.
Pseudorandom Number Generators
Random Number Generation
Distinguishing Features of Simulation Time (CLK)  DYNAMIC focused on this aspect during the modeling section of the course Pseudorandom variables (RND)
CSCE Monte Carlo Methods When you can’t do the math, simulate the process with random numbers Numerical integration to get areas/volumes Particle.
Properties of Random Numbers
APPENDIX D RANDOM NUMBER GENERATION
Random Number Generation Fall 2013
Fall 2011 CSC 446/546 Part 6: Random Number Generation.
A quadratic equation is a second degree polynomial, usually written in general form: The a, b, and c terms are called the coefficients of the equation,
Random Number Generation Pseudo-random number Generating Discrete R.V. Generating Continuous R.V.
ETM 607 – Random Number and Random Variates
Random Number Generators CISC/QCSE 810. What is random? Flip 10 coins: how many do you expect will be heads? Measure 100 people: how are their heights.
MATH 224 – Discrete Mathematics
Random-Number Generation Andy Wang CIS Computer Systems Performance Analysis.
CS433 Modeling and Simulation Lecture 15 Random Number Generator Dr. Anis Koubâa 24 May 2009 Al-Imam Mohammad Ibn Saud Islamic University College Computer.
© 2003, Carla Ellis Simulation Techniques Overview Simulation environments emulation exec- driven sim trace- driven sim stochastic sim Workload parameters.
CPSC 531: RN Generation1 CPSC 531:Random-Number Generation Instructor: Anirban Mahanti Office: ICT Class Location:
Chapter 7 Random-Number Generation
Basic Concepts in Number Theory Background for Random Number Generation 1.For any pair of integers n and m, m  0, there exists a unique pair of integers.
Modeling and Simulation Random Number Generators
Random Number Generators 1. Random number generation is a method of producing a sequence of numbers that lack any discernible pattern. Random Number Generators.
Monte Carlo Methods.
APPENDIX D R ANDOM N UMBER G ENERATION Organization of chapter in ISSO* – General description and linear congruential generators Criteria for “good” random.
Validating a Random Number Generator Based on: A Test of Randomness Based on the Consecutive Distance Between Random Number Pairs By: Matthew J. Duggan,
The man who knew 28% of mathematics. John Von Neumann ( ) Hans Bethe: Academic seminars (10.
R ANDOM N UMBER G ENERATORS Modeling and Simulation CS
EMIS 7300 SYSTEMS ANALYSIS METHODS FALL 2005 Dr. John Lipp Copyright © 2005 Dr. John Lipp.
0 Simulation Modeling and Analysis: Input Analysis 7 Random Numbers Ref: Law & Kelton, Chapter 7.
Chapter 3 Generating Uniform Random Variables. In any kind of simulation, we need data, or we have to produce them. Especially in Monte Marco simulation.
MONTE CARLO METHOD DISCRETE SIMULATION RANDOM NUMBER GENERATION Chapter 3 : Random Number Generation.
Random Numbers All stochastic simulations need to “generate” IID U(0,1) “random numbers” Other random variates coming from other distribution can be generated.
UNIFORM RANDOM NUMBER GENERATION Chapter 7 (first half) The goal is to generate a sequence of The goal is to generate a sequence of Uniformly distributed.
1.  How does the computer generate observations from various distributions specified after input analysis?  There are two main components to the generation.
Step 1: Specify a null hypothesis
3. Random Number Generator
Applied Discrete Mathematics Week 11: Relations
Generating Random Numbers
Linear-Time Sorting Continued Medians and Order Statistics
Random Number Generators
Virtual University of Pakistan
Remember that our objective is for some density f(y|) for observations where y and  are vectors of data and parameters,  being sampled from a prior.
Random-Number Generation
Chapter 7 Random Number Generation
CS 475/575 Slide set 4 M. Overstreet Old Dominion University
Chapter 7 Random-Number Generation
Properties of Random Numbers
Discrete Event Simulation - 4
Lecture 2 – Monte Carlo method in finance
Honors Statistics From Randomness to Probability
STOCHASTIC HYDROLOGY Random Processes
Cryptography and Network Security Chapter 7
CS202 - Fundamental Structures of Computer Science II
Random Number and Random Variate Generation
Validating a Random Number Generator
Pseudorandom number, Universal Hashing, Chaining and Linear-Probing
Computer Simulation Techniques Generating Pseudo-Random Numbers
Random Number Generation
What we learn with pleasure we never forget. Alfred Mercier
Generating Random and Pseudorandom Numbers
Where real stuff starts
Generating Random and Pseudorandom Numbers
Linear Time Sorting.
Presentation transcript:

Random Numbers All stochastic simulations need to “generate” IID U(0,1) “random numbers” Other random variates coming from other distribution can be generated using U(0,1) random numbers

Uniform Random Numbers

Uniform Random Numbers

Early Methods Physical Mechanical Electrical Other Hamamaker’s Die Having no access to existing tables of random variables during world war II, the late well-known statistics professor H.C. Hamamaker at Philips Research Laboratories in Eindhoven, invented this die and the dice-throwing technique to produce random sampling numbers. The technique has been tested in various ways on series of up to 40,000 throws without showing evidence of bias. Physical Dice Cards Lotteries Mechanical Spinning disks Electrical Electronic Random Number Indicator Equipment (ERNIE, used by British Post Office to pick winners in a lottery (1959)) RAND Corporation Tables (A million random digits (1955)) Other Pick digits randomly from phone directories Decimals in expansion of π to 100,000 places

Algorithmic and Sequential Computer Methods Sequential means that the next “random” number is determined by one or several of its predecessors according to a fixed mathematical formula The midsquare method: (von Neumann and Metropolis, 1945) Start with Z0 = 4-digit positive integer Z1 = middle 4 digits of Z02 (append 0s if necessary to left of Z02 to get exactly 8 digits); U1 = Z1, with decimal point at left Z2 = middle 4 digits of Z12; U2 = Z2, with decimal point at left Z3 = middle 4 digits of Z22; U3 = Z3, with decimal point at left So on ... Problems the with midsquare method Not really “random” since the entire sequence is determined by Z0 If a Zi ever reappears, the entire sequence will be recycled (This will occur, since the only choices for 4-digit positive integers are 0000, 0001, 0002, ..., 9999) “Design” generators so Ui’s “appear” to be IID U(0,1) and cycle length is long

The Midsquare Method

Can We Generate “Truly” Random Numbers? “True” randomness Only possible with physical experiment having U(0,1) output Counting gamma rays from space Problems Not reproducible Impractical for computers Practical approach: produce stream of numbers that appear to be IID U(0,1) Use theoretical properties as far as possible Perform empirical tests

Criteria for Random Number Generators Appear to be distributed uniformly on [0, 1] and independent Fast, low memory requirement Should be able to reproduce a particular stream of random numbers This makes debugging easier One can use identical random numbers to simulate alternative system configurations for better comparison Should have provision in the generator for a large number of separate (nonoverlapping) streams of random numbers (usually such streams are just carefully chosen subsequences of the larger overall sequence) Beware: There are many RNGs in use (and in software) that have extremely poor statistical properties

Linear Congruential Generators Specify four parameters (all nonnegative integers): Z0 = seed (or starting value) m = modulus (or divisor) a = multiplier c = increment Then, Z1, Z2, Z3, ... are recursively generated by Zi = (aZi-1 + c) (mod m) Zi is the remainder of dividing aZi-1 + c by m Thus, 0 ≤ Zi ≤ m – 1, so the random number is defined as Ui = Zi/m Note that 0 ≤ Ui < 1 c = 0 (Multiplicative LCG), c > 0 (Mixed LCG)

Example of a “Toy” LCG Questions: m = 16, a = 5, c = 3, Z0 = 7 Formula Zi Ui 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2 ---- 0.375 0.063 0.500 0.688 0.625 0.313 0.750 0.938 0.875 0.563 0.000 0.188 0.125 0.813 0.250 0.438 m = 16, a = 5, c = 3, Z0 = 7 Formula Zi = 5Zi-1 + 3 (mod 16) Cycle length (or period) here is 16 = m (the longest possible) Questions: Can the period be “predicted” in advance? Can a full period be guaranteed?

Objections to LCGs Not really “random”. Indeed, an explicit formula for Zi is Cycles when a previous Zi reappears (there are only m choices, so cycle length is ≤ m) Ui’s can only take the discrete values 0/m, 1/m, 2/m, ..., (m-1)/m , but they’re supposed to be continuous For this reason, pick m ≥ 109 or more in practice

Full-Period Theorem (Hull and Dobell, 1966) The LCG Zi = (aZi-1 + c) (mod m) has full period (m) if and only if all three of the following conditions hold: Both c and m are relatively prime (i.e., the only positive integer that divides both c and m is 1) If q is any prime number that divides m, then q also divides a – 1 If 4 divides m, then 4 also divides a – 1 Choice of initial seed Z0 is irrelevant Checking these conditions for a “real” generator may be difficult Condition 1 says that if c = 0, then full period is impossible (since m divides both m and c = 0); but m – 1 is possible Theorem is about period only, it says nothing about uniformity of a subcycle, independence, or other statistical properties

Desirable Properties of LCGs Fast Low storage requirement Reproducible (remember Z0) Restart in middle (remember last Zi, use as Z0 next time) Multiple streams (save separated seeds) May have good statistical properties (depends on choice of parameters)

Implementation/Portability Issues LCGs (and other generators) generally deal with very large integers Usually, m ≥ 2.1 billion ≈ 2.1 × 109 ≈ 231 Take care in coding, especially in high level languages (FORTRAN, C) To avoid need to store and operate on large integers: use sophisticated abstract-algebra tricks to “break up” the arithmetic on large integers, then reassemble

Other Kinds of Generators More General Congruences Zi = g(Zi-1, Zi-2, ...) (mod m), Ui = Zi/m LCG: g(Zi-1) = a Zi-1 + c Multiplicative recursive generator: g(Zi-1, Zi-2, ..., Zi-q) = a1Zi-1 + ... + aq Zi-q Quadratic congruential generator: g(Zi-1) = a’Zi-12 + aZi-1 + c Fibonacci (bad): g(Zi-1, Zi-2) = Zi-1 + Zi-2

Composite Generators Shuffling Differencing LCGs Wichmann/Hill Fill a vector of length 128 (say) from generator 1 Use generator 2 to pick one of the 128 in the vector Fill the hole with the next value from generator 1, use generator 2 to pick one of the 128 in the vector, etc. Shuffling a bad generator improves it, but shuffling a good generator doesn’t gain much Differencing LCGs Get Z1i and Z2i from LCGs with different moduli Let Zi = (Z1i – Z2i ) (mod m); Ui = Zi / m They have very long periods (like 1018) and very good statistical properties Wichmann/Hill Use three LCGs to get U1i , U2i , and U3i sequences Let Ui = fractional part of U1i + U2i + U3i They have long period and good statistical properties (equivalent to a LCG!)

Testing Random Number Generators Since RNGs are completely deterministic, we need to test them to see if they appear to be random and IID uniform on [0, 1] General advice: be wary of “canned” RNGs in software that is not specifically simulation software, especially if they are not thoroughly documented; perhaps even test them before using Two types of testing procedures Empirical statistical tests Chi-square Kolmogorov-Smirnov Serial test Runs test Correlation test Theoretical procedures Full period values Lattice structure

Empirical Statistical Tests Use trial generator to generate some U’s and apply statistical tests This gives information only on that part of a generator’s cycle examined (local) Chi-square, K-S tests for U(0,1) (all parameters known) Serial test: generalized chi-square test to higher dimensions Two dimensions, subdivide unit square into subcells Provides indirect test of independence

Empirical Statistical Tests Correlation test: perform correlation analysis using the correlation coefficient of lag j to test H0: independence

Checking the Excel RAND() generator Data Characteristic Value Source file <edited> Observation type Real valued Number of observations 100 Minimum observation 0.03580 Maximum observation 0.99638 Mean 0.50290 Median 0.50590 Variance 0.06893 Coefficient of variation 0.52207 Skewness 0.04632

Checking the Excel RAND() generator

Checking the Excel RAND() generator

Checking the Excel RAND() generator

Checking the Excel RAND() generator

Checking the Excel RAND() generator

Some General Guidelines Beware of “canned” generators; especially in non-simulation software, and especially if it is poorly documented Insist on documentation of the generator, check with “tried and true” ones Don’t “seed” the generator with the square root of the clock or any other such scatterbrained scheme, we want to get reproducibility of the random-number stream Reasonably safe ideas Use one of the generators from the book Look for highly portable generators See if they provide default streams with controlled, known spacing Take care to avoid overlapping the streams