Documente Academic
Documente Profesional
Documente Cultură
+
=
=
Simulation 11
Random Generators in NS-2
Default random generator : defaultRNG
Create new random generator:
set newRNG [new RNG]
$newRNG seed 2
The generation of random variables uses a seed
seed=0 - a new random variables sequence is generated
each time the simulation is run
seed !=0 the same sequence of random variables is
generated each time the simulation is run
Different random generators with the same seed
generates the same rv sequences
$defaultRNG seed 0
$defaultRNG seed 2
Simulation 12
Generating Random Variates
Have: Desired input distribution for model (fitted
or specified in some way), and RNG (UNIF (0, 1))
Want: Transform UNIF (0, 1) random numbers
into draws from the desired input distribution
Method: Mathematical transformations of random
numbers to deform them to the desired
distribution
Specific transform depends on desired distribution
Details in online Help about methods for all
distributions
Do discrete, continuous distributions separately
Simulation 13
Generating from Discrete Distributions
Example: probability mass function
2
0 3
Divide [0, 1] into subintervals of length 0.1, 0.5, 0.4
Generate U ~ UNIF (0, 1)
See which subinterval its in
Return X = corresponding value
Simulation 14
Generating from Discrete Distributions :
Another View
Plot cumulative distribution function; generate U
and plot on vertical axis; read across and down
Inverting the
CDF
Equivalent to
earlier
method
Simulation 15
Generating from Continuous Distributions
Inverse Transform Method
Example: EXPO (5) distribution
PDF
CDF
General algorithm (see proof later):
1. Generate a random number U ~ UNIF(0, 1)
2. Set U = F(X) and solve for X = F
1
(U)
Solving analytically for X may or may not be simple (or
possible)
Sometimes use numerical approximation to solve
Simulation 16
Generating from Continuous Distributions
(contd.)
Solution for EXPO (5) case:
Set U = F(X) = 1 e
X/5
e
X/5
= 1 U
X/5 = ln (1 U)
X = 5 ln (1 U)
Picture (inverting the CDF, as in discrete case):
More Us will hit F(x)
where its steep
This is where the density
f(x) is tallest, and we
want a denser
distribution of Xs
Simulation 17
). ( )) ( ( ) ) ( ( ) (
1
x F x F U P x U F P x X P = = =
Inverse Transform Method: Proof
Algorithm:
1. Generate a random number U ~ UNIF(0, 1)
2. Set U = F(X) and solve for X = F
1
(U)
Proof:
1. We need to show that for any real
number x, P(X x)=F(x).
2. Since F is invertible, we have
Simulation 18
Inverse Transform Method: When is it Appropriate (1/2)
When a closed form inverse for CDF exists
Simple example: Triangular distribution
The inverse transform formula can
then be determined to be:
Simulation 19
Inverse Transform Method: When is it Appropriate (2/2)
When a good approximation for the CDF inverse can be
found
Example: simple approx. for Gaussian distribution
Approximation with at least one decimal-place accuracy
for
20
Example: Pareto Distribution
CDF
is shape parameter.
Random Variable x
where u is drawn from uniform distribution U(0,1).
Simplified Formula
1
( )
0
X
k
x k
F x
x
x k
| |
|
=
\
1
(1 )
k
x
u
=
1
u
k
x =
Simulation 21
Acceptance Rejection Method
Gamma Distribution
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0 2 4 6 8 10
x
f ( x )
A closed form for F(X) does not exist
Use another distribution for which we do know how to
calculate the CDF and its inverse.
We pick a function t(x) that is larger than f(x) for all x.
Technically we say that t(x) majorizes f(x)
Gamma(2,1) distribution.
Simulation 22
Acceptance Rejection Method
Acceptance-Rejection Example
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.5
0 2 4 6 8 10
x
f
(
x
)
t(x) = 0.4
PDF for t : r(x) = 0.4/4
CDF for t :
R(x) = 0.1x
Simulation 23
Acceptance Rejection Method
Acceptance-Rejection Example
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.5
0 2 4 6 8 10
x
f
(
x
)
t(x) = 0.4
If we threw darts that could land
only on the line x = 3, then the
probability that a dart hitting inside
the distribution would be
f(X=3)/t(X=3).
Simulation 24
Acceptance Rejection Method: Algorithm
Acceptance-Rejection Example
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.5
0 2 4 6 8 10
x
f
(
x
)
t(x) = 0.4
1. Generate a random value x with uniform density in [0,10]
2. With probability f(x)/t(x), return x as random value for f(x)
To check the probability, generate another r.v. which is uniformly
distributed between 0 and 1
3. If failed, return to step 1
Simulation 25
Acceptance Rejection Method: Example
Two calls to the function to return a random variate for the gamma(2,1)
distribution. In this example, the value 0.3064 would be returned.
No. X f(X=x) t(X=x) Accept if U<= U~U(0,1) Accept?
1 8.1074 0.002 0.4 0.0061 0.911 FALSE
2 0.3064 0.226 0.4 0.5638 0.453 TRUE
Comparison A-R vs Known Gamma
0.00
0.02
0.04
0.06
0.08
0.10
0.12
0.14
0.16
0.18
0.20
0
.
0
1
.
0
2
.
0
3
.
0
4
.
0
5
.
0
6
.
0
7
.
0
8
.
0
9
.
0
1
0
.
0
x
p
(
x
)
A-R
Known Gamma(2,1)
A-R method with t(x) =0.4 against a histogram from a known Gamma (2,1) distribution.
n = 1000
Simulation 26
Acception-Rejection Method: Intuitive Explanation
f(Y)/t(Y) is small most of Y-s rejected
f(Y)/t(Y) is largemost of Y-s accepted
The concentration of Ys from t(Y) is altered to agree f(Y)
Simulation 27
Random Variables Examples in NS-2