PowerPoint Presentation
Chapter 24: Generating & Using Random Numbers
Copyright By PowCoder代写 加微信 powcoder
What’s a random number?
Coin toss: +1 if Heads, -1 if Tails
Die toss: {1,2,3,4,5,6}
Uniform random: Evenly distributed on interval (0,1)
Multi-dimensional random: 2-dimenional, 3-dimensional, ….
Stock return? Normally distributed?
Lehmer (famous mathematician, Berkeley):
“A random sequence is a vague notion embodying the idea of a sequence in which each term is unpredictable to the uninitiated and whose digits pass a certain number of tests, traditional with statisticians and depending somewhat on the uses to which the sequence is to be put.”
Read about the Derrick and : http://bancroft.berkeley.edu/Exhibits/Math/dhla.html
What is a random number generator?
Wikipedia, article on “Pseudorandomness”
Lehmer invented one of the main methods for creating pseudorandom numbers.
Pseudo random number generator
Excel’s (pseudo)random number generators
In VBA: Rnd
Both Rand() and Rnd produce
a number between 0 and 1
Students with birthdates on odd-numbered days:
Flip coin 100 times, record results
Students with birthdates on even-numbered days:
Imagine 100 coin flips, record results
(1937 – 1996) was one of the founders on mathematical psychology and non-expected utility theory. His colleague (good read: Thinking Fast and Slow) won the for economics in 2002. There is no doubt that Tversky would have also won the Nobel, had he been alive.
Which student born on an even day?
John is flipping a real coin: Lots of runs.
Anoradha is only imagining—after a few “Heads,” she’ll imagine some “Tails.”
Message: Hard to imagine randomness
We don’t understand the world as well as we think we do and tend to be fooled by false patterns, mistake luck for skills (the fooled by randomness effect), overestimate knowledge about rare events ( ), as well as human understanding, something that has been getting worse with the increase in complexity. So I am interested in a systematic program of how to live in that opaque world, be less fragile to a certain class of errors (“robustification program”).
The Correlation of Tail Events
Big sigma (or tail) events are low probability, high loss (or gain) events
Big sigma events are more highly correlated than we think
If you have one crisis—likely to have another
If you have one boom—ditto
Maybe there is an underlying cause?
Maybe the world is not rational?
Use VBA to produce a list of random numbers
Use Frequency to test Rand()
Use Frequency to test Rand()
Is this really random? Remember Tversky!
Run VBA’s Rnd Many Times
Sub UniformRandom()
‘Puts random numbers into bins
Range(“E3”) = Time
N = Range(“B2”).Value ‘the number of random draws
Dim distribution(10) As Long ‘bins
For k = 1 To N
draw = Rnd
distribution(Int(draw * 10) + 1) = _
distribution(Int(draw * 10) + 1) + 1
For Index = 1 To 10
Range(“B5”).Cells(Index, 1) = distribution(Index)
Next Index
Range(“E4”) = Time
Sub NormStandardRandom3()
‘Box-Muller for producing standard normal deviates
Application.ScreenUpdating = False
For Index = 1 To N
Static rand1, rand2, S1, S2, X1, X2
rand1 = 2 * Rnd – 1
rand2 = 2 * Rnd – 1
S1 = rand1 ^ 2 + rand2 ^ 2
If S1 > 1 Then GoTo start
S2 = Sqr(-2 * Log(S1) / S1)
X1 = rand1 * S2
X2 = rand2 * S2
Producing normally-distributed random numbers: Box-Muller method
Keeping Same Series of Random Numbers
Throw a die
How do Rand() and Rnd work?
Uniform random on (0,1) is basis of most random number generators
Excel uses congruence method due to Lehmer
For normally-distributed random numbers:
Box- .S.Inv(Rand())
How do Rand() and Rnd work?
Here is a random-number generator you can make yourself:
Start with some number, Seed
Let X1 = Seed + . Let X2 = exp(5+ln(X1))
The first random number is Random = X2 – Integer(X2), where Integer(X2) is the integer part of X2
Repeat the process, letting Seed = Random
Implement this random-number generator in a VBA program similar to randomlist and produce a list of 50 random numbers.
Congruency (Lehmer)
Define AmodB as the remainder when A is divided by B. For example, 36mod25 = 11. Excel has this function; it is written Mod(A,B)
Here is another random-number generator:
Let X0 = 1
Let Xn+1 = (7*Xn)mod108
Let Un+1 = Xn+1/108
The list of numbers U1, U2, … are the pseudo-random numbers generated by this random-number generator
This is one of the many random-number generators given in Abramowitz and Stegun 1972
http://office-watch.com/kb/?828795
The basic idea is that if you take three random numbers on [0,1] and sum them, the fractional part of the sum is itself a random number on [0,1]. The critical statements in the Fortran code listing from the original Wichman and Hill article are:
C IX, IY, IZ SHOULD BE SET TO INTEGER VALUES BETWEEN 1 AND 30000 BEFORE FIRST ENTRY
IX = MOD(171 * IX, 30269)
IY = MOD(172 * IY, 30307)
IZ = MOD(170 * IZ, 30323)
RANDOM = AMOD(FLOAT(IX) / 30269.0 + FLOAT(IY) / 30307.0 + FLOAT(IZ) / 30323.0, 1.0)
Therefore IX, IY, IZ generate integers between 0 and 30268, 0 and 30306, and 0 and 30322 respectively. These are combined in the last statement to implement the simple principle that was expressed earlier: if you take three random numbers on [0,1] and sum them, the fractional part of the sum is itself a random number on [0,1].
Because RAND produces pseudo-random numbers, if a long sequence of them is produced, eventually the sequence will repeat itself. Combining random numbers as in the Wichman-Hill procedure guarantees that more than 10^13 numbers will be generated before the repetition begins. Several of the Diehard tests produced unsatisfactory results with earlier versions of RAND because the cycle before numbers started repeating was unacceptably short.
Excel’s random number
Producing normal “deviates”
Normal “deviate”—numbers that are standard normal distributed
Use Norm.S.Inv(probability, mean, sigma)
Correlated normals
Create two normal z1 and z2
z3 and z1 are correlated with correlation r
Cholesky decomposition: Creating many correlated normals
Cholesky (1875-1918): any symmetric positive-definite matrix S (think variance-covariance matrix) can be written as the product of a lower-triangular matrix L and its transpose LT
Cholesky: Creating n standard normal deviates with given var-cov structure
S is the var-cov matrix
L is the Cholesky decomposition
Create a column x of n standard normal
Multiply L * x
Do this enough times—create normal, correlated
0.92620.34720.87480.5535<-- =RAND()
0.47200.67290.32770.3396
0.56840.48990.48400.4362
0.50690.77680.43060.4967
0.05240.19320.17570.3397
0.25840.96810.14370.9565
0.62540.93930.52610.5105
0.94160.37000.63330.3913
0.19070.63250.71680.8445
Each cell contains the function Rand(). Each time you
update the spreadsheet or press F9 the block of cells will
produce a new set of randomnumbers.
USING EXCEL'S RAND() FUNCTION
Anoradha John
HT<-- =IF(RAND()>0.5,”H”,”T”)
ANORADHA AND JOHN
0.91096 Sub RandomList()
0.22687 ‘Produces a simple list of random numbers
0.69512 For Index = 1 To 10
0.98000 Range(“A3”).Cells(Index, 1) = Rnd
0.24393 Next Index
0.53387 End Sub
USING VBA’S RND FUNCTION TO
PRODUCE A LIST OF RANDOM
RandomList Macro
BinFrequency
0.9663<-- =RAND() 0.1 0
0.2434 0.2 1
0.6057 0.3 3
0.8821 0.4 0
0.5881 0.5 0 <-- =FREQUENCY(A3:A12,D3:D12)
0.2118 0.6 1
0.2367 0.7 1
0.1524 0.8 0
0.9459 0.9 1
USING EXCEL'S FREQUENCY FUNCTION
TO TEST THE DISTRIBUTION OF RAND()
Each cell in the range A3:A12 contains the formula
. Pressing F9 will produce a new set of
random numbers and frequencies.
BinFrequency
0.8998<-- =RAND() 0.111
0.1561 0.2 9
0.8147 0.311
0.9732 0.4 6
0.9119 0.5 9 <-- =FREQUENCY(A3:A102,D3:D12)
0.5901 0.6 9
0.3528 0.715
0.6060 0.8 9
0.2608 0.913
0.7374 Check 100<-- =SUM(E3:E12)
USING EXCEL'S FREQUENCY FUNCTION
TO TEST THE DISTRIBUTION OF RAND()
Cells in column A contain the formula Rand(). Pressing F9 will produce a new set of
random numbers and frequencies.
024681012141612345678910
ABCDEFGHIJK
Runs 10,000,000
Starttime 12:06:28
BinOutput Stoptime 12:06:31
1999,709 Elapsed 0:00:03
41,000,081
51,000,282
71,001,419
81,000,377
Uniformly Distributed Random Numbers
998,000998,500999,000999,5001,000,0001,000,5001,001,0001,001,5001,002,00012345678910
Frequency Distribution of 10,000,000 runs of VBA's Rnd function
UniformRandom Macro
0.437880.29708
0.646550.70653
0.872280.65463
0.827360.96848
0.502830.48999
0.541790.72373
0.166290.06518
0.256100.60034
0.534230.25382
0.744710.70398
: to see the effect of the "Run Random_Same" button,
erase the cells B5:B14. Changing the seed in cell B2
changes the output in column B.
PRODUCING LISTS OF RANDOM
Run Random_EachDifferentRun Random_Same
3<-- =VLOOKUP(RAND(),$D$3:$E$9,2)
3<-- =VLOOKUP(RAND(),$D$3:$E$9,2) 0.000000 1
4 0.166667 2
2 0.333333 3
5 0.500000 4
1 0.666667 5
4 0.833333 6
4 1.000000
6 Mean 3.680<-- =AVERAGE(A:A)
1 Sigma 1.568<-- =STDEVP(A:A)
6 Mean 3.5000<-- =(1+2+3+4+5+6)/6
6 Variance 2.9167<-- =SUM((E3:E8-E16)^2)/6
5 Sigma 1.7078<-- =SQRT(E17)
Vlookup table
Theoretical mean/sigma
Sample mean/sigma
THROW A DIE
0.4000.0300.0200.000
0.0300.2000.000-0.060
0.0200.0000.3000.030
0.000-0.0600.0300.100
0.6320.0000.0000.000<-- {=cholesky(A2:D5)}
0.0470.4450.0000.000
0.032-0.0030.5470.000
0.000-0.1350.0540.281
0.4000.0300.0200.000<-- {=MMULT(A8:D11,TRANSPOSE(A8:D11))}
0.0300.2000.000-0.060
0.0200.0000.3000.030
0.000-0.0600.0300.100
Check: Multiply above matrix by its
Cholesky decomposition, L
Variance-covariance matrix, S
0.400.030.020.01 5%
0.030.300.00-0.06 6%
0.020.000.300.03 7%
0.01-0.060.030.10 8%
Cholesky decomposition
0.63250.00000.00000.0000<-- {=cholesky(varcov)}
0.04740.54570.00000.0000
0.0316-0.00270.54680.0000
0.0158-0.11130.05340.2907
Generating four random normals
-0.9598-0.4066-0.88920.02930.01200.9766-1.45711.14631.3323
1.0962-0.39291.22010.6670-1.13190.13622.0134-0.3954-0.0339
-0.3851-0.34161.6217-0.38000.5393-0.62380.8208-0.7077-0.1519
0.09630.5969-0.01011.20752.07380.70380.79441.86830.4852
Generating multinomial normal output
-0.55705-0.20718-0.512410.068550.057590.66768-0.871540.774960.89263
0.61261-0.173660.683590.42535-0.557050.180631.08950-0.101390.10467
-0.17393-0.128590.92527-0.138720.36840-0.240600.46720-0.279650.02918
-0.049770.272590.013740.336920.837850.251560.107570.647480.23778
Variance-covariance matrix
Cells B15:B18 contain the formula
=NORMSINV(RAND())
Cells B21:B25 contain the formula
{=MMULT($A$9:$D$12,B$15:B$18)+$F$3:$F
MULTIVARIATE NORMAL SIMULATION WITH NON-ZERO MEANS
simulations
220<-- =COUNT(15:15)
-1.16%<-- =AVERAGE(21:21)
3.84%<-- =AVERAGE(22:22)
6.15%<-- =AVERAGE(23:23)
7.61%<-- =AVERAGE(24:24)
From varcov
simulation
0.400.4076<-- =VAR.P(21:21,21:21)
0.300.3256<-- =VAR.P(22:22,22:22)
0.300.2904<-- =VAR.P(23:23,23:23)
0.100.1007<-- =VAR.P(24:24,24:24)
Covar(1,2)
0.030.0329<-- =COVARIANCE.P($21:$21,22:22)
Covar(1,3)
0.020.0214<-- =COVARIANCE.P($21:$21,23:23)
Covar(1,4)
0.010.0183<-- =COVARIANCE.P($21:$21,24:24)
Covar(2,3)
0.00-0.0293<-- =COVARIANCE.P($22:$22,23:23)
Covar(2,4)
-0.06-0.0887<-- =COVARIANCE.P($22:$22,24:24)
Covar(3,4)
0.030.0379<-- =COVARIANCE.P(23:23,24:24)
/docProps/thumbnail.jpeg
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com