Professional Documents
Culture Documents
Numbers
(2/3)
Non-uniform
Distribu/ons
Sebas4ano
Pila4
spila&@ictp.it
Room:
245
2nd
Floor
/afs/ictp/public/s/spila&/Lecture_RN2.pdf
PROGRAM TESTOPTIONALARGUMENTS!
USE TESTFUNCTION!
IMPLICIT NONE!
REAL :: mya,myc!
INTEGER :: myb!
!
!
mya = 2.0!
myb = 2!
myc = 3.5!
!
PRINT*,
PRINT*,
PRINT*,
PRINT*,
PRINT*,
"f(a)
"f(a,b)
"f(a,c)
"f(a,b,c)
"f(a,b,c)
=
=
=
=
=
",
",
",
",
",
testfunc(mya)!
testfunc(mya,
testfunc(mya,
testfunc(mya,
testfunc(mya,
!
if (present(b)) then!
foo = a**b!
else!
foo = a!
end if!
myb)!
c = myc)!
myb, myc)!
c = myc, b = myb)!
!
END PROGRAM!
user$./a.out
f(a)
=
5.000000
f(a,b)
=
7.000000
f(a,c)
=
5.500000
f(a,b,c)
=
7.500000
f(a,b,c)
=
7.500000
if (present(c)) then!
foo = foo + c!
else!
foo = foo + 3.0!
end if!
end function testfunc!
!
END MODULE!
PROGRAM IntrinsicRNG!
IMPLICIT NONE!
INTEGER :: i, usedsize!
INTEGER, ALLOCATABLE, DIMENSION(:) :: myseed, copyseed !
seed for random number generator!
REAL
:: R
!
random number
!
REAL, ALLOCATABLE, DIMENSION(:)
:: Rarray
!
random array!
!
DO i = 1, 4!
CALL RANDOM_NUMBER( R )!
WRITE(6,"(A20,I3,A6,F12.8)") "Random Number ",i," is = ",
R!
END DO!
!
ALLOCATE(copyseed(usedsize))!
CALL RANDOM_SEED( GET = copyseed ) !query current seed!
!
! write on screen the current seed!
!
END PROGRAM!
Transforma4on
method
Create
random
numbers
with
distribu&on
f(x)
f(x)dx:
probability
of
producing
r.n.
between
x
and
x+dx
We
need
a
rela&on
in
the
form
x
=
G(u)
u
is
a
uniform
random
number
[0:1)
Take
the
inverse,
u
=
G-1(x)
and
dieren&ate,
du
=
[G-1(x)]
dx
=
f(x)
dx
Note:
inverse
=>
G-1(G(x))
=
x
Transforma4on
method
x
1
Integrate, u = G (x) =
f ( x)dx
x min
Transforma4on
method
Example
1:
Exponen&al
distribu&on:
x
f (x) = e x
u=
f ( x)dx = 1 e x
Inver&ng
Sample
uniform
r.n
in
[0:1):
u
(e.g.,
using
linear
congruent
generator
or
beger)
Calculate:
x
=
-log(1-u)
Inver&ng
Transforma4on
method
Example
3:
Gaussian
distribu&on
(Box-Muller
method)
Impossible
to
invert
u=G-1(x)
in
1-d
but
possible
in
2-d
2D
Gaussian
distribu&on:
in
polar
coordinates:
R 2
1
f (x, y)dxdy =
2 exp
2 RdRd
2
2
R 2
integra&ng
from
0
to
R
(and
over
):
u = exp 2 +1
2
inver&ng,
we
nd:
R = 2 2 log(1 u)
R = 2 2 log(1 u)
= 2u'
x = Rsin
x
and
y
are
Gaussian
random
variables
y = Rcos
Rejec4on
method
If
the
inverse
of
the
integral
cannot
be
found
Less
ecient
(because
of
rejec&ons)
works
only
if
the
func&on
has
a
nite
bound
1)
generate
a
random
number
x
uniformly
distributed
in
[xmin,xmax).
2)
generate
another
r.n.,
call
it
r,
in
[0,1)
3)
If:
r
<
f(x)
/fmax
accept
x
otherwise:
reject
and
go
to
step
1
fmax= max(f(x))
NTOT = H[i]
i=1
Homework
Write
a
program
with:
1) A
func&on
that
generates
Gaussian
random
numbers
with
=
0.5
(and
mean
=
0)
using
transforma&on
method
2) The
main
program:
ini&alizes
the
seed
Generates
an
histogram
with
60
bins,
x
in
the
range
[-3:3].
Sample
200000
points.
Write
the
histogram
in
output
le
(60
lines)
Then,
plot
the
data
using
gnuplot,
save
the
plot
on
a
postscript
le
Send
to
spila&@ictp.it:
the
program
code
and
the
postscript
le
(or
send
the
histogram
le
with
60
lines
instead
of
the
postscript)
Gnuplot:
On
terminal
enter
gnuplot
Plowng
a
func&on:
gnuplot>
f(x)
=exp(x)
gnuplot>
plot
f(x)
Plowng
from
a
data
le:
gnuplot>
plot
data.dat
u
1:2
w
linespoints,
f(x)
Crea&ng
output
ps
le:
gnuplot>
set
terminal
postscript
gnuplot>
set
output
data.ps
gnuplot>
replot
Program
output
1:2
2/(22gaussian.out
2)u1/2
e
xp(-
x
)/(2
exp(-x**2 / (2.*0.5**2)) /(0.5*(2.*acos(-1.))**0.5)
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-3
-2
-1
Rejec4on
method
Example:
number
of
agempts
required
to
return
a
valid
value.
Probability
to
generate
a
number
between
x
and
x+dx:
Probability
that
it
is
accepted:
Total calls =
Rejec4on
method
Example:
Gaussian
distribu&on
-rst
need
to
choose
a
nite
range,
e.g.
6
standard
devia&ons
from
the
middle
For
=1,
Total
calls=