5 random variates
TRANSCRIPT
-
8/9/2019 5 Random Variates
1/33
1
ICS 2307ICS 2307Modelling and SimulationModelling and Simulation
Random VariatesRandom Variates
-
8/9/2019 5 Random Variates
2/33
1
2
Random VariatesRandom Variates
There are several techniques for generating randomThere are several techniques for generating randomvariatesvariates
These include:These include:
Inverse MethodInverse Method
Convolution MethodConvolution Method
Acceptance-rejection methodAcceptance-rejection method
The inverse method is articularl! suited for anal!ticall!The inverse method is articularl! suited for anal!ticall!tracta"le ro"a"ilit! densit! functions#tracta"le ro"a"ilit! densit! functions#
The remaining methods deal $ith more com le% casesThe remaining methods deal $ith more com le% casessuch as the normalsuch as the normal
-
8/9/2019 5 Random Variates
3/33
3
The Inverse MethodThe Inverse Method
Method a lica"le to cases $here the C&' can "eMethod a lica"le to cases $here the C&' can "einversed anal!ticall!inversed anal!ticall!
Assume that we wish to generate stochasticAssume that we wish to generate stochasticvariates from a probability density function pdfvariates from a probability density function pdf
f(x)f(x)!et "(x) be the cumulative density function!et "(x) be the cumulative density function
"(x) is de#ned in the region $%&'"(x) is de#ned in the region $%&'
"irst we generate a random variable r which we"irst we generate a random variable r which weset to be e ual to "(x) i e "(x) *rset to be e ual to "(x) i e "(x) *r
+he uantity x is obtained by inverting " i e x*" +he uantity x is obtained by inverting " i e x*" ,,'' (r)(r)
+he inverse function is applied on both sides of +he inverse function is applied on both sides of
-
8/9/2019 5 Random Variates
4/33
4
The Inverse MethodThe Inverse Method
ExampleExample
Assume that we wish to generate random variates withAssume that we wish to generate random variates withthe pdf f(x)=2x, 0the pdf f(x)=2x, 0 ≤≤ xx≤≤ 11
We first generate an expression for the CDFWe first generate an expression for the CDF
∫ = x
tdt x F 0
2)( xt x F 0
2 ][)( =2)( x x F =
!ow if we "now that F(x)=r, then!ow if we "now that F(x)=r, then
xx22=r=r
r x =
-
8/9/2019 5 Random Variates
5/33
5
The Inverse MethodThe Inverse Method
r is o"tained "! generating a random varia"le "et$een 0r is o"tained "! generating a random varia"le "et$een 0and ( i)e) sam ling the C&'and ( i)e) sam ling the C&'
Remem"er that the C&' for an! df ta*es valuesRemem"er that the C&' for an! df ta*es valuesuniforml! distri"uted in the interval 0 to (uniforml! distri"uted in the interval 0 to (
This is illustrated in the figures "elo$This is illustrated in the figures "elo$
"ig ' .ampling A continuous
-
8/9/2019 5 Random Variates
6/33
6
The Inverse MethodThe Inverse Method
"ig / .ampling a discretedistribution
-
8/9/2019 5 Random Variates
7/33
7
The Inverse MethodThe Inverse Method
1)1) Sampling from a uniform distributionSampling from a uniform distribution
-
8/9/2019 5 Random Variates
8/33
8
The Inverse MethodThe Inverse Method
,et the inverse of the C&' e% ression,et the inverse of the C&' e% ression
aba x
x F r −−== )(
aabr x +−= )(
-
8/9/2019 5 Random Variates
9/33
9
The Inverse MethodThe Inverse Method
2)2) Sampling from an exponential distributionSampling from an exponential distribution
+e get the C&' e% ression+e get the C&' e% ression
-
8/9/2019 5 Random Variates
10/33
10
The Inverse MethodThe Inverse Method
,et the inverse of the C&' e% ression,et the inverse of the C&' e% ression
t
et F λ −
−=1)(t et F r λ −−== 1)(
λ
λ
λ
λ
λ
)1ln(
)1ln(
)ln()1ln(
1
1
r t
t r
er
er
er
t
t
t
−−=
−=−=−
=−
−=
−
−
−
-ecause r is a com lement of (.r $e have-ecause r is a com lement of (.r $e have
λ
)log( r t −=
-
8/9/2019 5 Random Variates
11/3311
Convolution MethodConvolution Method
3)3) Sampling from an Erlang distributionSampling from an Erlang distribution
The m./rlang random varia"le is the statistical sumThe m./rlang random varia"le is the statistical sumconvolutions1 of m inde endent and identicall!convolutions1 of m inde endent and identicall!
distri"uted e% onential random varia"lesdistri"uted e% onential random varia"les
If ! re resents the m./rlang random varia"le# tenIf ! re resents the m./rlang random varia"le# ten! !! ! (( !! 22 4 !4 ! mm
$here !$here ! i#i#i (#2#4#m are inde endent and identicall!i (#2#4#m are inde endent and identicall!
distri"uted e% onential random varia"les $hosedistri"uted e% onential random varia"les $hosero"a"ilit! densit! function is defined asro"a"ilit! densit! function is defined as
-
8/9/2019 5 Random Variates
12/3312
Convolution MethodConvolution Method
i y
i e y f
λ λ −=)( y i>0, i=1,2,…,m
The iThe i thth e% onential sam le is given "!e% onential sam le is given "!
mir yii
..1),ln(1
=−=
λ
The m./rlang sam le is com uted asThe m./rlang sam le is com uted as
{ }
)...ln(1
)ln(...)ln()ln(1
21
21
m
m
r r r y
r r r y
××−=
+++−=
λ
λ
-
8/9/2019 5 Random Variates
13/3313
Convolution MethodConvolution Method
4)4) The Normal DistributionThe Normal Distribution 5 random varia"le % $ith df 5 random varia"le % $ith df
is said to have a normal distri"ution $ith meanis said to have a normal distri"ution $ith mean µµ andand
standard deviationstandard deviation σσ The e% ectation and variance areThe e% ectation and variance are µµ andand σσ22 res ectivel!res ectivel!
IfIf µµ 0 and0 and σσ (# then the varia"le has a standard normal(# then the varia"le has a standard normaldistri"utiondistri"ution
-
8/9/2019 5 Random Variates
14/3314
Convolution MethodConvolution Method
5 random varia"le % follo$s a normal distri"ution $ith 5 random varia"le % follo$s a normal distri"ution $ithmeanmean µµ and standard deviationand standard deviation σσ then the randomthen the randomvaria"le 6varia"le 6
σ µ −= x z
follo$s a standard normal distri"utionfollo$s a standard normal distri"ution
In order to generate variates from the normal distri"utionIn order to generate variates from the normal distri"ution$ith arameters$ith arameters µµ andand σσ# $e use the central limit theorem# $e use the central limit theorem
-
8/9/2019 5 Random Variates
15/3315
Convolution MethodConvolution Method
The central limit theorem states thatThe central limit theorem states thatif xif x '' & x& x// & 0x& 0x nn are n independent random variatesare n independent random variateswith parameters meanwith parameters mean µµ and stdand std σσ then& and wethen& and wehave y de#ned ashave y de#ned as
∑=n
i x y 1y approaches a normal distribution as n becomesy approaches a normal distribution as n becomeslargelarge
1(y)*n1(y)*n µµ2ar(y)*n2ar(y)*n σσ//
-
8/9/2019 5 Random Variates
16/3316
Convolution MethodConvolution Method
The rocedure for generating random varia"les from theThe rocedure for generating random varia"les from thenormal distri"ution $ill this require generation of n valuesnormal distri"ution $ill this require generation of n valuesof the uniform distri"utionof the uniform distri"ution
The random varia"les associated $ith these values haveThe random varia"les associated $ith these values have
mean ( 2 and variance ( (2mean ( 2 and variance ( (2
Therefore $hen the varia"le got from a summation of n ofTherefore $hen the varia"le got from a summation of n ofthese varia"les $ill have a normal distri"ution $iththese varia"les $ill have a normal distri"ution $ith
µµ n 2 andn 2 and σσ22
n (2n (2i)e ! has the normal distri"ution given "elo$i)e ! has the normal distri"ution given "elo$
)12
,2
()( nn
N y f =
-
8/9/2019 5 Random Variates
17/3317
Convolution MethodConvolution Method
! can "e ma ed into the standard normal distri"ution to! can "e ma ed into the standard normal distri"ution toroduce a ne$ varia"le 6 usingroduce a ne$ varia"le 6 using
σ µ −= x z
Thus 6 is given "!Thus 6 is given "!
12/
2/
n
n y z
−=
8o$ if $e consider the normal distri"ution $e $ant to8o$ if $e consider the normal distri"ution $e $ant toroduce $ith arametersroduce $ith arameters µµ andand σσ# $e have# $e have
unn y
x
u xnn y
+−
=
−=−
12/))2/((
12
2/
σ
σ
-
8/9/2019 5 Random Variates
18/3318
Convolution MethodConvolution Method
If $e ta*e n (2# this sim lifies toIf $e ta*e n (2# this sim lifies to
u y x +−= σ )6(In summar!# this means thatIn summar!# this means that
(1(1 +e generate ! "! generating and summing (2 random+e generate ! "! generating and summing (2 randomvaria"les r varia"les r (( 4r 4r (2(2 1# ! r 1# ! r (( 4 r 4 r (2(2
2121 +e su"tract 9 from !#+e su"tract 9 from !#
3131 Multi l! the difference in t$o $ith the standard deviationMulti l! the difference in t$o $ith the standard deviationfor the distri"ution $e $ant to sam lefor the distri"ution $e $ant to sam le
11 5dd the mean 5dd the mean µµ to the roduct of 3 to get our randomto the roduct of 3 to get our randomvariate %variate %
-
8/9/2019 5 Random Variates
19/3319
Convolution MethodConvolution Method
This method is slo$ "ecause of the need to generate (2This method is slo$ "ecause of the need to generate (2random valuesrandom values
5 more efficient rocedure calls for using the 5 more efficient rocedure calls for using thetransformationtransformation
),2sin())ln(2(),2cos())ln(2(
212
211
r r z and r r z
π π
−=−=
This method is -o%.Mueller rocedure Taha#20031This method is -o%.Mueller rocedure Taha#20031
T$o values in the $ith the standard normal distri"utionT$o values in the $ith the standard normal distri"utionare generated using t$o random varia"lesare generated using t$o random varia"les
These can "e ma ed into the distri"ution of interest $ithThese can "e ma ed into the distri"ution of interest $ithmean 8mean 8 µµ##σσ11
-
8/9/2019 5 Random Variates
20/3320
Convolution MethodConvolution Method
5)5)
Poisson DistributionPoisson Distribution
If the distri"ution of the time "et$een the occurrence ofIf the distri"ution of the time "et$een the occurrence ofsuccessive events is e% onential# then the distri"ution ofsuccessive events is e% onential# then the distri"ution ofthe num"er of events er unit time is ;oissonthe num"er of events er unit time is ;oisson
This relationshi s is used to sam le the ;oissonThis relationshi s is used to sam le the ;oissondistri"utiondistri"ution
If the ;oisson distri"ution has a mean value ofIf the ;oisson distri"ution has a mean value of λλ eventseventser unit time#er unit time#
The time "et$een events is e% onential $ith mean (The time "et$een events is e% onential $ith mean ( λλ time unitstime units
This means that a ;oisson sam le#n# $ill occur during tThis means that a ;oisson sam le#n# $ill occur during t
time units iff time units iff
-
8/9/2019 5 Random Variates
21/3321
Convolution MethodConvolution Method
The time "et$een events is e% onential $ith mean (The time "et$een events is e% onential $ith mean (
λλ
time unitstime units
This means that a ;oisson sam le#n# $ill occur during tThis means that a ;oisson sam le#n# $ill occur during ttime units iff time units iff
3eriod until event n occurs3eriod until event n occurs ≤≤t4period until eventt4period until eventn5' occursn5' occurs
this is translated tothis is translated to
t t 11 +t +t 22 +…+t +…+t nn ≤ ≤ t < t t < t 11 +t +t 22 +…+t +…+t n+1 ,n+1 , n>0n>000 ≤ ≤ t
-
8/9/2019 5 Random Variates
22/3322
Convolution MethodConvolution Method
The follo$ing discussion sho$s ho$ this is im lementedThe follo$ing discussion sho$s ho$ this is im lemented
-
8/9/2019 5 Random Variates
23/3323
Convolution MethodConvolution Method
In summar!# this means thatIn summar!# this means that
λλ is the num"er of events er unit time#is the num"er of events er unit time#
5nd $e $ant a sam le of ho$ man! events $ill occur in a 5nd $e $ant a sam le of ho$ man! events $ill occur in atime ttime t
'irst $e o"tain the value of e'irst $e o"tain the value of e .. λλtt
ase 1ase 1
If $e generate the first random value r If $e generate the first random value r (( and it is less thanand it is less thanee .. λλtt# then $e have a sam le of 0# I)e) our ;oisson random# then $e have a sam le of 0# I)e) our ;oisson randomvaria"le n ta*es the value 0)varia"le n ta*es the value 0)
8ote that if e8ote that if e .. λλtt is greater than (# $e $ill never get ais greater than (# $e $ill never get a
sam le of 0 co6 the relevant inequalit! $ill never "esam le of 0 co6 the relevant inequalit! $ill never "esatisfiedsatisfied
-
8/9/2019 5 Random Variates
24/3324
Convolution MethodConvolution Method
ase 2 ase 2
i)i) +e generate a series of random varia"les r +e generate a series of random varia"les r ii until theuntil theroduct of the the random varia"les $e have generated isroduct of the the random varia"les $e have generated is
less than eless than e .. λλtt# these varia"le $ill "e r # these varia"le $ill "e r (( 4r 4r m (m (
ii)ii) +e then chec* if the roduct of r +e then chec* if the roduct of r (( 4r 4r mm is greater or equalis greater or equalto eto e .. λλtt# if so# then out ;oisson random varia"le n ta*es the# if so# then out ;oisson random varia"le n ta*es thevalue m)value m)
iii)iii) IfIf ii ii
is not satisfied# $e have to discard the m values andis not satisfied# $e have to discard the m values andstart generating ne$ r start generating ne$ r ii valuesvalues
-
8/9/2019 5 Random Variates
25/3325
5cce tance Re>ection Method 5cce tance Re>ection Method
!)!) Sampling the binomial distributionSampling the binomial distribution
is the ro"a"ilit! of success and q is the ro"a"ilit! of is the ro"a"ilit! of success and q is the ro"a"ilit! offailurefailure
The C&' for one trial is as sho$n "elo$The C&' for one trial is as sho$n "elo$
"ig 6 C7" of a binomialdistribution
-
8/9/2019 5 Random Variates
26/3326
5cce tance Re>ection Method 5cce tance Re>ection Method
If $e assume 0 corres onds to a success and ( to aIf $e assume 0 corres onds to a success and ( to afailure from the diagram1failure from the diagram1
To generate a random sam le# * assuming that $e haveTo generate a random sam le# * assuming that $e haven trialsn trials
i)i) +e set * to 0+e set * to 0
ii)ii) If $e generate a random num"er r If $e generate a random num"er r ii "et$een 0 and (1 and"et$een 0 and (1 andit is less or equal to # set * * (# else * doesn?t changeit is less or equal to # set * * (# else * doesn?t change
iii)iii) Re eat from ii until !ou have generated n random valuesRe eat from ii until !ou have generated n random valuesThe value of * $ill hold the value of the "inomial variateThe value of * $ill hold the value of the "inomial variate
This is an e%am le of acce tance re>ection methodThis is an e%am le of acce tance re>ection method
-
8/9/2019 5 Random Variates
27/3327
5cce tance Re>ection Method 5cce tance Re>ection Method
The acce tance.re>ection method is designed forcom le% dfs that are hard to deal $ith anal!ticall!
The general idea is to re lace this com le% df f %1 $ith amore anal!ticall! managea"le ro%! df h %1
Sam les from h %1 can then "e used to sam le theoriginal df
Pro"edurePro"edure
&efine the ma>ori6ing g %1 such that it dominates f %1 in&efine the ma>ori6ing g %1 such that it dominates f %1 inits entire rangeits entire range
g %1g %1≥≥f %1# .f %1# .∞∞@%@.@%@.∞∞
-
8/9/2019 5 Random Variates
28/33
-
8/9/2019 5 Random Variates
29/3329
5cce tance Re>ection Method 5cce tance Re>ection Method
ExampleExample
#se the a$$eptan$e%re&e$tion method to generate at one#se the a$$eptan$e%re&e$tion method to generate at oneva'ue from the fo''owing distri utionva'ue from the fo''owing distri ution
f(x)= x(1%x), 0f(x)= x(1%x), 0≤≤xx ≤≤11
Assume the fo''owing random va'ues are generatedAssume the fo''owing random va'ues are generated
0*+0*+
-..-..
/-/-
/+/+
.1
.1
-
8/9/2019 5 Random Variates
30/3330
Sam ling 'rom an /m irical &istri"utionSam ling 'rom an /m irical &istri"ution
Buite often# an em irical ro"a"ilit! distri"ution ma! notBuite often# an em irical ro"a"ilit! distri"ution ma! not"e a ro%imated $ell "! one of the $ell *no$n"e a ro%imated $ell "! one of the $ell *no$ntheoretical distri"utionstheoretical distri"utions
In such a case# $e have to generate variates from theIn such a case# $e have to generate variates from the
em irical distri"utionem irical distri"utionSampling from a discrete probability distributionSam pling from a discrete probability distribution
et % "e a discrete random varia"le ta*ing the valueset % "e a discrete random varia"le ta*ing the values%%(( #%#%22 #4%#4%nn # $ith ro"a"ilities # $ith ro"a"ilities (( # # 22 #4##4# nn
et ; %et ; % ≤≤%%ii1 ;1 ; ii# i 0# (#4#n Cumulative ;ro"a"ilit!1# i 0# (#4#n Cumulative ;ro"a"ilit!1
et r "e a random varia"le generated from the uniformet r "e a random varia"le generated from the uniformdistri"ution D0#(Edistri"ution D0#(E
-
8/9/2019 5 Random Variates
31/3331
Sam ling 'rom an /m irical &istri"utionSam ling 'rom an /m irical &istri"ution
Then % %Then % %ii $hen ;$hen ;
i.(i.(@r @r ≤≤;;
ii
"ig 8 .ampling a discreteempirical distribution "rom the
#gure& x*x /
-
8/9/2019 5 Random Variates
32/3332
Sam ling 'rom an /m irical &istri"utionSam ling 'rom an /m irical &istri"ution
Sampling from a continuous probability distributionSam pling from a continuous probability distribution
Sometimes $e need to grou the data into classesSometimes $e need to grou the data into classeses eciall! $hen the sam les ta*e continuous values1es eciall! $hen the sam les ta*e continuous values1
/ach sam le $ill have the lo$er class "oundar! and the/ach sam le $ill have the lo$er class "oundar! and the
u er class "oundar!u er class "oundar!+e $ill use the u er class "oundar! of each class to+e $ill use the u er class "oundar! of each class to
lot the C&'#lot the C&'#
et us assume that the u er class "oundar! values areet us assume that the u er class "oundar! values are%%(( # %# %22 # 4%# 4% nn $here %$here % i.(i.( is the lo$er class "oundar! for theis the lo$er class "oundar! for theclass $ith the u er "oundar! %class $ith the u er "oundar! % ii
ets also assume that the %ets also assume that the % ≤≤%%ii1 ;1 ; ii
-
8/9/2019 5 Random Variates
33/33
Sam ling 'rom an /m irical &istri"utionSam ling 'rom an /m irical &istri"ution
If $e generate a random value r in the interval D0#(EIf $e generate a random value r in the interval D0#(E
Then the associated value % is o"tained using theThen the associated value % is o"tained using thefollo$ing formulafollo$ing formula
1
111 )(
−
−−−
−
−−+=
ii
iiii
P P
P r x x x x
+here r is greater than ;+here r is greater than ; i.(i.( "ut less or equal to ;"ut less or equal to ; ii