





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






We want to be able to perform an experiment with a given set of probabilities. Starting point:
Random number generators (rng) produce a stream of numbers that look like realizations of independent standard uniform variables U 1 , U 2 , U 3 ,...
Usually, these numbers are not completely random , but pseudo random. This way, we ensure repeatability of an experiment. (Note: even the trick to link the system’s rand() function to the internal clock, gives you only pseudo random numbers, since the same time will give you exactly the same stream of random numbers.) There are hundreds of methods that have been proposed for doing this - some (most?) are pretty bad. A good method - and, in fact, current standard in most operating systems, is:
Definition 1.1.1 (Linear Congruential Sequence) For integers a, c, and m a sequence of “random numbers” xn is defined by:
xi ≡ (axi− 1 + c) mod m for i = 1, 2 ,...
Note: this sequence still depends on the choice of x 0 , the so-called seed of the sequence. Choosing different seeds yields different sequences. That way, we get a sequence with elements in [0, m − 1]. We define ui := x mi. The choice of the parameters a, c and m is crucial! obviously, we want to get as many different numbers as possible - therefore m needs to be as large as possible and preferably prime (that way we get rid of small cycles).
Example 1.1.1 rng examples Status-quo in industry is the so called Minimal standard generator. It fulfills the common requirements of a rng and at the same time is very fast. Its parameters are:
c = 2 a = 16807 m = 231 − 1
An example for a terrible random number generator is the RANDU, with
c = 0 a = 65539 m = 231
It was widely used, before people discovered how bad it actually is: Knowing two successive random numbers gives you the possibility to predict the next number pretty well.... that’s not, how rng s are supposed to work.
For more information about random number generators and different techniques, how to produce and check them, look at http://crypto.mat.sbg.ac.at/results/karl/server/ State of the art at the moment is the Marsaglia-Multicarry-RNG:
#define znew ((z=36969(z&65535)+(z>>16))<<16) #define wnew ((w=18000(w&65535)+(w>>16))&65535) #define IUNI (znew+wnew) #define UNI (znew+wnew)*2.328306e- static unsigned long z=362436069, w=521288629; void setseed(unsigned long i1,unsigned long i2){z=i1; w=i2;}
Whenever you need random integers or random reals in your C program, just insert those six lines at (near?) the beginning of the program. In every expression where you want a random real in [0,1) use UNI, or use IUNI for a random 32-bit integer. No need to mess with ranf() or ranf(lastI), etc, with their requisite overheads. Choices for replacing the two multipliers 36969 and 18000 are given below. Thus you can tailor your own in-line multiply-with-carry random number generator.
This section is expressed as a C comment, in case you want to keep it filed with your essential six lines: */
/* Use of IUNI in an expression will produce a 32-bit unsigned random integer, while UNI will produce a random real in [0,1). The static variables z and w can be reassigned to i1 and i by setseed(i1,i2); You may replace the two constants 36969 and 18000 by any pair of distinct constants from this list: 18000 18030 18273 18513 18879 19074 19098 19164 19215 19584 19599 19950 20088 20508 20544 20664 20814 20970 21153 21243 21423 21723 21954 22125 22188 22293 22860 22938 22965 22974 23109 23124 23163 23208 23508 23520 23553 23658 23865 24114 24219 24660 24699 24864 24948 25023 25308 25443 26004 26088 26154 26550 26679 26838 27183 27258 27753 27795 27810 27834 27960 28320 28380 28689 28710 28794 28854 28959 28980 29013 29379 29889 30135 30345 30459 30714 30903 30963 31059 31083 (or any other 16-bit constants k for which both k*2^16-
A faster definition than the one above is X = d 6 · U e.
Consider a continuous density f with distribution function F. We know (cf. fig. 1.2) that F : (x 0 , ∞) 7 → (0, 1) (x 0 could be −∞) has an inverse function:
F −^1 : (0, 1) 7 → (x 0 , ∞)
x
FX
xo
Figure 1.2: Starting at some value x 0 any continuous distribution function has an inverse. In this example, x 0 = 1.
General Method:
For a given standard uniform variable U ∼ U(0,1) we define
X := F (^) X− 1 (U )
Then X has distribution FX.
Why? For a proof of the above statement, we must compute the distribution function X has. Remember, the distribution function of X, FX at value x is the probability that X is x or less:
P (X ≤ x) trick = apply FX to both sides of the inequality = P (FX (X) ≤ FX (x)) dfn of U = = P (U ≤ FX (x)) = U is a standard uniform variable,P (U ≤ t) = t = F (x).
Therefore, X has exactly the distribution, we wanted it to have. 2
Example 1.1.3 Simulate from Expλ Suppose, we want to simulate a random variable X that has an exponential distribution with rate λ. How do we do this based on a standard uniform variable U ∼ U(0,1)? The distribution function for Expλ is
Expλ(x) =
0 for x ≤ 0 1 − e−λx^ for x ≥ 0
So, Expλ : (0, ∞) 7 → (0, 1) has an inverse:
Let u be a positive real number:
u = 1 − e−λx ⇐⇒ 1 − u = e−λx ⇐⇒ ln(1 − u) = −λx
⇐⇒ x = −
λ
ln(1 − u) =: F (^) X− 1 (u)
then X := − (^) λ^1 ln(1 − U ) has an exponential distribution with rate λ. In fact, since 1 − U is uniform, if U is uniform, we could also have used X := − (^1) λ ln U
For specific densities there are a lot of different special tricks for simulating observations: For all of the next sections, let’s assume that we have a sequence of independent standard uniform variables U 1 , U 2 , U 3 ,...
1.1.2.1 Simulating Binomial & Geometric distributions
Let p be the rate of success for a single Bernoulli trial. define:
Xi =
0 if ui ≥ p 1 if ui < p
Then
X :=
∑^ n
i=
Xi ∼ Bn,p
and W := # of Xi until the first is 1 W ∼ Geometricp
1.1.2.2 Simulating a Poisson distribution
With given U , we know, X = − (^) λ^1 ln U has an exponential distribution with rate λ. Define
Y := largest index j such that
∑^ j
i=
Xi ≤ 1 and
∑^ j+
i=
Xi > 1
then Y ∼ P oλ
1.1.2.3 Simulating a Normal distribution
To simulate a normal distribution, we need two sequences of standard uniform variables. Let U 1 and U 2 be two independent standard uniform variables. Define
Z 1 := [−2 ln U 1 ]−^1 /^2 cos(2πU 2 ) Z 2 := [−2 ln U 1 ]−^1 /^2 sin(2πU 2 )
Then both Z 1 and Z 2 have a standard normal distribution and are independent,
Z 1 , Z 2 ∼ N (0, 1)
and X := μ + σZi ∼ N (μ, σ^2 )
What should we expect for R, the overall resistance? The following lines are R output from a simulation of 1000 values of R:
R1 <- rnorm (1000, mean=100, sd = 2) R2 <- rnorm (1000, mean=100, sd = 2) R3 <- rnorm (1000, mean=100, sd = 2)
R <- R1 + R2*R3/(R2 + R3)
mean(R) > [1] 149. sd(R) > [1] 2.
sum(R<146)/ > [1] 0.
Example 1.2.2 at MacMall Assume, you have a summer job at MacMall, your responsibility are the Blueberry IMacs in stock. At the start of the day, you have 20 Blueberry IMacs in stock. We know: X = # of IMacs ordered per day is Poisson with mean 30 Y = # of IMacs received from Apple is Poisson with mean 15 a day Question: What is the probability that at the end of the day you have inventory left in the stock. Let I be the number of Blueberry IMacs in stock at the end of the day.
I = 20 − X + Y
Asked for is the probability that I ≥ 1). Again, we use R for simulating I:
u1 <- runif(33000) e1 <- -1/30*log(u1) sum(e1) [1] 1099.
E1 <- cumsum(e1) E1[25:35] [1] 0.7834028 0.7926534 0.7929962 0.7959631 0.8060001 0.8572329 0. [8] 0.8947401 1.0182220 1.0831698 1. E1 <- floor(E1) E1[25:35] [1] 0 0 0 0 0 0 0 0 1 1 1
X <- table(E1) X[1:10] 0 1 2 3 4 5 6 7 8 9 32 26 31 32 17 27 31 33 32 31
X <- X[1:1000]
mean(X) [1] 30. var(X) [1] 29.
Y <- rpois(1000,15)
I <- 20 - X + Y