-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomdistmod.f90
More file actions
487 lines (327 loc) · 10 KB
/
Copy pathrandomdistmod.f90
File metadata and controls
487 lines (327 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
module randomdistmod
use parametersmod, only : i4,sp
implicit none
public :: ranu
public :: ranur
public :: ran_seed
public :: ran_normal
public :: ran_gamma
private :: refill
private :: random_gamma1
private :: random_gamma2
private :: random_exponential
!----------------
!module variables
integer(i4), parameter :: qsiz = 10 !41265_i4
integer(i4), parameter :: cmul = 69609_i4
integer(i4), parameter :: coffs = 123_i4
real(sp), parameter :: rng1 = 1. / (2. * real(huge(i4))) !scales the random integer to -0.5,0.5
real(sp), parameter :: rng2 = 1. / real(huge(i4)) !scales the random integer to -1,1
real(sp), parameter :: one = 1.
real(sp), parameter :: half = 0.5
real(sp), parameter :: vsmall = tiny(1.)
real(sp), parameter :: zero = 0.
type randomstate
integer(i4), dimension(qsiz) :: q
integer(i4) :: carry = 362_i4
integer(i4) :: xcng = 1236789_i4
integer(i4) :: xs = 521288629_i4 !default seed
integer(i4) :: indx = qsiz + 1
logical :: have = .false.
end type randomstate !5+qsiz elements = 15 elements
contains
!-----------------------------------------------------------------------
real(sp) function ranur(state)
!generate a single precision random real on the interval [0,1]
implicit none
type(randomstate), target, intent(inout) :: state
!-----
ranur = rng1 * real(ranu(state)) + half
end function ranur
!-----------------------------------------------------------------------
integer(i4) function ranu(state)
!Generates a uniformly distributed random 4 byte integer with the range (-huge(i4),+huge(i4))
!based on the 32-bit super KISS random number generator by George Marsaglia, published online
!and translated to Fortran 90 by user "mecej4" and Marsaglia, http://forums.silverfrost.com/viewtopic.php?t=1480
!Further modifications to pass the complete state of the generator as an argument by J.O. Kaplan, 2011
implicit none
type(randomstate), target, intent(inout) :: state
integer(i4) :: supr
integer(i4), pointer :: indx
integer(i4), pointer :: xcng
integer(i4), pointer :: xs
integer(i4), pointer, dimension(:) :: q
!---------------------
indx => state%indx
q => state%q
xcng => state%xcng
xs => state%xs
!---
if (indx <= qsiz) then !reset the generator
supr = q(indx)
indx = indx + 1
else
supr = refill(state)
end if
!---
xcng = xcng * cmul + coffs
xs = ieor(xs,ishft(xs, 13))
xs = ieor(xs,ishft(xs,-17))
xs = ieor(xs,ishft(xs, -5))
ranu = xcng + xs + supr
end function ranu
!-----------------------------------------------------------------------
function refill(state) result(s)
implicit none
type(randomstate), target, intent(inout) :: state
integer(i4) :: s
integer(i4) :: z
integer(i4) :: h
integer :: i
integer(i4), pointer :: indx
integer(i4), pointer :: carry
integer(i4), pointer, dimension(:) :: q
!---------------------
indx => state%indx
carry => state%carry
q => state%q
!---
do i = 1,qsiz
h = iand(carry,1_i4)
z = ishft(ishft(q(i),9),-1) + ishft(ishft(q(i),7),-1) + ishft(carry,-1)
carry = ishft(q(i),-23) + ishft(q(i),-25) + ishft(z,-31)
q(i) = not(ishft(z,1)+h)
end do
indx = 2
s = q(1)
end function refill
!-----------------------------------------------------------------------
subroutine ran_seed(sval,state)
implicit none
integer(i4), intent(in) :: sval
type(randomstate), target, intent(inout) :: state
integer :: i
integer(i4), pointer :: xcng
integer(i4), pointer :: xs
integer(i4), pointer, dimension(:) :: q
!---------------------
q => state%q
xcng => state%xcng
xs => state%xs
xs = sval
!---
do i = 1,qsiz
xcng = xcng * cmul + coffs
xs = ieor(xs,ishft(xs, 13))
xs = ieor(xs,ishft(xs,-17))
xs = ieor(xs,ishft(xs, -5))
q(i) = xcng + xs
end do
end subroutine ran_seed
!-----------------------------------------------------------------------
subroutine ran_normal(state,nval)
!Sampler for the normal distribution based on Marsaglia polar method
implicit none
type(randomstate), intent(inout) :: state
real(sp), intent(out) :: nval
!---
real(sp), dimension(2) :: vals
integer(i4), dimension(2) :: u
real(sp), dimension(2) :: v
real(sp) :: s
real(sp) :: a
!---------------------
if (state%have) then
state%have = .false.
nval = vals(2)
else
do
u(1) = ranu(state)
u(2) = ranu(state)
v = real(u) * rng2 !convert integer (-huge,+huge) to (-1,+1)
s = sum(v**2)
if (s < 1.) exit
end do
a = sqrt(-2. * log(s) / s)
vals = v * a
nval = vals(1)
state%have = .true.
end if
end subroutine ran_normal
!----------------------------------------------------------------------------------------------------------
!The functions below are for randomly sampling the gamma distribution
!adapted from code by Alan J. Miller, URL,
!http://users.bigpond.net.au/amiller/random.html
!----------------------------------------------------------------------------------------------------------
subroutine ran_gamma(state,s,b,first,fn_val)
! Adapted from Fortran 77 code from the book:
! Dagpunar, J. 'Principles of random variate generation'
! Clarendon Press, Oxford, 1988. ISBN 0-19-852202-9
! function generates a random gamma variate.
! calls either random_gamma1 (s > 1.0)
! or random_exponential (s = 1.0)
! or random_gamma2 (s < 1.0).
! s = shape parameter of distribution (0 < real)
! b = scale parameter
implicit none
!arguments
type(randomstate), intent(inout) :: state
real(sp), intent(in) :: s !shape parameter of the Gamma distribution (alpha, unitless)
real(sp), intent(in) :: b !scale parameter of the Gamma distribution (Beta)
logical, intent(in) :: first !flag if this is the first call to the distribution
real(sp), intent(out) :: fn_val
!--------
if (s <= zero) then
write(0, *) 'shape parameter value must be positive'
stop
end if
if (s > one) then
call random_gamma1(state,s,first,fn_val)
else if (s < one) then
call random_gamma2(state,s,first,fn_val)
else
call random_exponential(state,fn_val)
end if
!scale the random variable with Beta
fn_val = b * fn_val
end subroutine ran_gamma
!--------------------------------------------------------------------
subroutine random_gamma1(state,s,first,fn_val)
! Uses the algorithm in
! Marsaglia, G. and Tsang, W.W. (2000) `A simple method for generating
! gamma variables', Trans. om Math. Software (TOMS), vol.26(3), pp.363-372.
! Generates a random gamma deviate for shape parameter s > 1.
implicit none
!arguments
type(randomstate), intent(inout) :: state
real(sp), intent(in) :: s
logical, intent(in) :: first
real(sp), intent(out) :: fn_val
!local variables
!real(sp), save :: c
!real(sp), save :: d
real(sp) :: c
real(sp) :: d
real(sp) :: u
real(sp) :: v
real(sp) :: x
!--------
if (first) then
d = s - one / 3.
c = one / sqrt(9. * d)
end if
! start of main loop
do
! generate v = (1+cx)^3 where x is random normal; repeat if v <= 0.
do
call ran_normal(state,x)
v = (one + c * x)**3
if (v > zero) exit
end do
! generate uniform variable u in the range (0,1)
u = real(ranu(state)) * rng1 + half
if (u < one - 0.0331 * x**4) then
fn_val = d*v
exit
else if (log(u) < half * x**2 + d*(one - v + log(v))) then
fn_val = d*v
exit
end if
end do
end subroutine random_gamma1
!--------------------------------------------------------------------
subroutine random_gamma2(state,s,first,fn_val)
! Adapted from Fortran 77 code from the book:
! Dagpunar, J. 'Principles of random variate generation'
! Clarendon Press, Oxford, 1988. ISBN 0-19-852202-9
! function generates a random variate in [0,infinity) from
! a gamma distribution with density proportional to
! gamma2**(s-1) * exp(-gamma2),
! using a switching method.
! s = shape parameter of distribution
! (real < 1.0)
implicit none
!arguments
type(randomstate), intent(inout) :: state
real(sp), intent(in) :: s
logical, intent(in) :: first
real(sp), intent(out) :: fn_val
!local variables
real(sp) :: r
real(sp) :: x
real(sp) :: w
!real(sp), save :: a
!real(sp), save :: p
!real(sp), save :: c
!real(sp), save :: uf
!real(sp), save :: vr
!real(sp), save :: d
real(sp) :: a
real(sp) :: p
real(sp) :: c
real(sp) :: uf
real(sp) :: vr
real(sp) :: d
!--------
if (s <= zero .or. s >= one) then
write(0, *) 'shape parameter value outside permitted range'
stop
end if
if (first) then ! initialization, if necessary
a = one - s
p = a / (a + s * exp(-a))
if (s < vsmall) then
write(0,*) 'shape parameter value too small'
stop
end if
c = one / s
uf = p * (vsmall / a)**s
vr = one - vsmall
d = a * log(a)
end if
do
r = real(ranu(state)) * rng1 + half !ranu(state) !value between (0,1)
if (r >= vr) then
cycle
else if (r > p) then
x = a - log((one - r)/(one - p))
w = a * log(x) - d
else if (r > uf) then
x = a * (r / p)**c
w = x
else
fn_val = zero
return
end if
r = real(ranu(state)) * rng1 + half !ranu(state)
if (one - r <= w .and. r > zero) then
if (r * (w + one) >= one) cycle
if (-log(r) <= w) cycle
end if
exit
end do
fn_val = x
end subroutine random_gamma2
!--------------------------------------------------------------------
subroutine random_exponential(state,fn_val)
! Adapted from Fortran 77 code from the book:
! Dagpunar, J. 'Principles of random variate generation'
! Clarendon Press, Oxford, 1988. ISBN 0-19-852202-9
! function generates a random variate in [0,infinity) from
! a negative exponential distribution wlth density proportional
! to exp(-random_exponential), using inversion.
implicit none
!arguments
type(randomstate), intent(inout) :: state
real(sp), intent(out) :: fn_val
!local variable
real(sp) :: r
!--------
do
r = real(ranu(state)) * rng1 + half !ranu(state)
if (r > zero) exit
end do
fn_val = -log(r)
end subroutine random_exponential
!-------------------------------
end module randomdistmod