0% found this document useful (0 votes)
13 views

Random Numbers and Monte-Carlo Methods

Uploaded by

vardankarki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Random Numbers and Monte-Carlo Methods

Uploaded by

vardankarki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

RANDOM NUMBERS

RANDOM NO. GENERATION

• Truly random phenomena are found in nature

• Ex:- Radioactivity, diffusion, Brownian motion

• Process is said to be random/stochastic if the outcome varies over many


possible outcomes.

• A random no. can be thought of as a value taken on by a random variable.

• RN generation - generation of a sequence of no.s that can’t be predicted better


than by random chance
PSEUDO RANDOM NUMBERS

• A study of random number generation techniques.

• Pseudo Random Numbers: Numbers generated by a deterministic process that


appears to be random, but the sequence can be reproduced if the initial
conditions (seed) are known.

• Pseudorandom Number Generators (PRNG): They are algorithms that generate


sequences of numbers that mimic the properties of random numbers.

PRNG - CHARACTERISTICS
• Deterministic: Given the same initial conditions, the sequence of numbers will always be the
same.

• Periodicity: After a certain number of iterations (period), the sequence will start repeating itself.
Cycle length should be long.

• Uniform Distribution: Ideally, the numbers should be uniformly distributed over a range. Eg: [0,1)

• Unpredictability: Despite being deterministic, PRNs should be difficult to predict without knowing
the algorithm or seed.

• Memory Storage: Should NOT require lot of storage.


PRNG – NEED?

• Efficiency: PRNGs can quickly generate large quantities of random numbers,


which is crucial in many applications.

• Reproducibility: In simulations or testing, using a specific seed allows


researchers to reproduce results consistently.

• Statistical Quality: Well-designed PRNGs can produce numbers that


statistically resemble true randomness, making them suitable for applications
like Monte Carlo simulations.

MID-SQUARE METHOD (VON NEUMANN METHOD)

• Developed by John von Neumann in 1946.

• The method generates random numbers by squaring the current number


(seed) and extracting the middle digits of the result.
MID-SQUARE METHOD (VON NEUMANN METHOD)
Procedure:
1.Start with an initial seed (n-digit number).

2.Square the seed. (if needed append zeroes to the left)

3.Extract the middle n digits from the result.

4.Use these digits as the next seed.

5.Repeat the process for the desired number of iterations.

MID-SQUARE METHOD (VON NEUMANN METHOD)


Algorithm:
Input:
Seed: An initial n-digit number X0. Digits (n): Number of digits for both the seed and
middle digits extraction. N: The number of random numbers to generate.
For each iteration i=1 to N, do the following steps:
Square the seed:
X1=X02
Pad the result if needed to ensure it has at least 2n digits. If the result has fewer than 2n
digits, add leading zeroes.
Extract the middle n digits:
Find the middle digits of the squared result.
These middle n digits become the next random number and the new seed X0.
Output the middle digits as the next pseudo-random number.
Repeat the process for N iterations or until the desired number of random numbers is
generated.
End For.
LINEAR CONGRUENCE (LCG) METHOD
• Xn+1 = (a Xn + c) mod m. a – multiplier; c – increment;

• a, c < m
• Modulus (m): Determines the range of possible random numbers.
Typically, a power of 2 or a large prime number is used.
• Multiplier (a): Should be carefully chosen for the best randomness.
• Increment (c): Introduces a constant shift. If c=0, the generator is called
a multiplicative LCG.
• Sequence begins with seed X0.

LINEAR CONGRUENCE METHOD


• Choice of a, c, m are correlated; It is known that to obtain a long period.

• m > 0;
• a, c < m; & X0 < m

Example: Assume X0 = 0 , a = 5, c = 3 and m = 7. Use this to generate PRN.


-------------- Additional Info ----------------
• The algorithm can be improved further by

• Shuffling

• Modifications of algorithm to avoid serial correlation

DESIRABLE PROPERTIES OF PRNG

• Fast, Simple, and Uniformly distributed in the given range.


• Long period
• Since the total no. of numbers that can be represented by a computer are
limited, the total no. of random no.s that can be generated are, in fact,
limited.
• Thus, after a certain period, the random numbers will start repeating
themselves
• No correlation amongst number patterns
• A certain pattern in one subset of random numbers must not be found in
another subset of the same set.
• Eg. (……0.111,0.222, ……. 0.444, 0.555, ……. 0.777, 0.888, …….)
TESTS FOR RANDOMNESS
• Test for Uniformity: Chi-Squared Test.
1. Applied to evaluate the quality of a pseudo-random number generator
(PRNG) to ensure that its output behaves like uniformly distributed random
numbers in a given range (usually between 0 and 1).
2. This test compares the observed frequency of random numbers falling into
predefined intervals (bins) with the expected frequency, assuming a uniform
distribution.
3. If the observed frequencies significantly deviate from the expected
frequencies, it suggests that the numbers may not be uniformly distributed.

Chi-Squared Test: Steps

• Generate the Random Numbers: Let the size of the sample be N.


• Divide the Range into Bins: Divide the interval [0, 1], into k equally spaced
intervals (bins).
• Count the Observed Frequencies: Count how many numbers from the
generated sequence fall into each bin. (Oi) is frequency of ith bin.
• Calculate the Expected Frequencies: If the numbers are uniformly
distributed, the expected frequency Ei for each bin should be the same.
• Ei = N / k; N  total number of RNs. k  bins
Chi-Squared Test: Steps
𝟐
𝒌 (𝑶𝒊 −𝑬𝒊 )
• Quantitatively, we calculate 𝝌𝟐𝒄 = 𝒊=𝟏 𝑬 𝒊

• If the observed no.s are significantly different from expected no.s then
we conclude that the PRNG is a poor one.
• Compare with the Critical Value: Compare the calculated chi-square
statistic 𝜒𝑐2 with a standard Chi-Squared distribution with ‘k-1’ degrees
of freedom (𝜒𝑘−1
2
) at a chosen significance level α
• If 𝝌𝟐 ≤ 𝝌𝟐𝒄  Numbers are uniformly distributed. (Null-hypothesis is
valid). That is no correlation between the RNs.
• Else, Number are not uniformly distributed. (Null-hypothesis is invalid)

Chi-Squared Test - Example


• Suppose you generate 100 random numbers and divide the interval [0, 1]
into 5 equal bins. The expected number of numbers in each bin would be: 20
• [0, 0.2), [0.2,0.4), [0.4,0.6), [0.6,0.8), [0.8,1)
• If observed frequencies are 18, 22, 21, 19, 20 in each of the 5 bins
• 𝝌𝟐 = 0.2+0.2+0.05+0.05 = 0.5
• Degrees of Freedom (DOF) = 5 – 1 = 4  (k – 1) Because, k = 5 (bins)
• 𝜶 = 0.05  This is assumed to balance the risk of making errors in
statistical conclusions
• 𝜒𝑐2 = 9.49  This value is found using a Chi-Square distribution table.
CHI-SQUARE DISTRIBUTION TABLE
𝜶

Serial Auto-Correlation Test


• Aim: It checks whether the current number in the sequence is correlated with
the previous number or with other numbers at a fixed lag.
• In random number sequences, the numbers should ideally be independent of
each other, meaning that no number should be predictable from the others.
• The correlation coefficient between a set of random no.s X1, X2, … Xn:

• For an uncorrelated sequence, it is expected that rk lies within


[𝜇 − 2𝜎, 𝜇 + 2𝜎] 95% of the time
Serial Auto-Correlation Test
• Let X1,X2,X3,…, XN be a sequence of N random numbers generated between
0 and 1 (from a PRNG).
• Choose a lag k, which represents the distance between numbers being
comparedThe correlation coefficient between a set of random no.s
• X1, X2, … Xn: Example: Lag 1 means Compare Xi with Xi+1,

• 𝑋  is the mean of the random numbers.


• Find rk.  close to 0 if the numbers are independent
• For an uncorrelated sequence, it is expected that rk lies within
[𝜇 − 2𝜎, 𝜇 + 2𝜎] 95% of the time. Where 𝝁 is mean and 𝝈 is std. deviation.

Serial Auto-Correlation Test : Example

Mean is calculated

Set the lag k = 1: Each number is compared with immediate next number

Calculate

The serial correlation coefficient r1= − 0.158 suggests that there is a slight
Interpret the result negative correlation between successive random numbers, but this value is
close to 0, which typically indicates no strong evidence of correlation.

You might also like