

header <napatool.hdr>
header <Function/random.hdr>                 // a few functions are not yet referenced in "tool.hdr" as not yet debugged
header <Resource/mathconst.hdr>              // Euler constant

#* directive ENFORCE_ODEH_EVANS_ALGORITHM    // to substitute to Box-Muller if you prefer

fs        1.0e6

#define ANALYZE 4
#ask    ANALYZE  "what analysis 0..12 ?"


ivar      npts    1000000

comment "Generator                  Range (x)     Mean             Variance                       "
comment "                                                                                         "
comment "x = rand_01()                0 < x < 1     1/2             1/12                          "
comment "x = rand_chisquare(n)        x > 0         n               2*n                           "
comment "x = rand_erlang(n,b)         x > 0         n*b             n*b*b                         "
comment "x = rand_exponential(m)      x > 0         m               m*m                           "
comment "x = rand_gumbel1(m,b)        b > 0                                                       "
comment "x = rand_gumbel2(m,b)        b > 0                                                       "
comment "x = rand_halfnormal(t)       x > 0         t*sqrt(2/Pi)    t*t*(1-2/Pi)                  "
comment "x = rand_lognormal(m,s)      x > 0         exp(m+0.5*s*s)  (exp(s*s)-1)*exp(2*m+s*s)     "
comment "x = rand_normal(m,s)         all x         m               s*s                           "
comment "x = rand_rayleigh(b)         x > 0         b*sqrt(Pi/2)    b*b*(4-Pi)/2                  "
comment "x = rand_student(n)          all x         0    (n > 1)    n/(n-2)    (n > 2)            "
comment "x = rand_uniform(a,b)        a < x < b     (a+b)/2         (b-a)*(b-a)/12                "
comment "x = rand_weibull(k,l)        x > 0         l*gamma(1+1/k)  l*l*gamma(1+2/k)-m*m          "
comment "                                                                                         "

#if ANALYZE == 0
## 01
node (b0) dalgebra  rand_01()
dvar mean           0.5                          &export
dvar sigma          1.0/sqrt(12.0)               &export

tool     statval "stat_b0.out"  b0 1.0 npts      // OK
tool   histogram "hist_b0.out"  b0 1.0 npts
#endif

#if ANALYZE == 1
comment "uniform"
title "UNIFORM"
dvar  a   1.0
dvar  b   5.0
node (b1) dalgebra  rand_uniform(a, b)
dvar mean           (a+b)/2.0                    &export
dvar sigma          (b-a)/sqrt(12.0)             &export

tool     statval "stat_b1.out"  b1 1.0 npts      // OK
tool   histogram "hist_b1.out"  b1 1.0 npts
#endif

#if ANALYZE == 2
comment "exponential"
title "EXPONENTIAL"
dvar  m   2.0
node (b2) dalgebra  rand_exponential(m)
dvar mean           m                            &export
dvar sigma          m                            &export

tool     statval "stat_b2.out"  b2 1.0 npts      // OK
tool   histogram "hist_b2.out"  b2 1.0 npts
#endif

#if ANALYZE == 3
comment "erlang"
title "ERLANG"
ivar  n   2
dvar  b   0.7
node (b3) dalgebra  rand_erlang(n, b)
dvar mean           n*b                          &export
dvar sigma          b*sqrt(n)                    &export

tool     statval "stat_b3.out"  b3 1.0 npts      // OK
tool   histogram "hist_b3.out"  b3 1.0 npts
#endif

#if ANALYZE == 4
comment "normal"

title "NORMAL"
dvar  m   1.5
dvar  s   0.4
node (b4) dalgebra  rand_normal(m, s)
dvar mean           m                            &export
dvar sigma          s                            &export

tool     statval "stat_b4.out"  b4 1.0 npts      // OK
tool   histogram "hist_b4.out"  b4 1.0 npts
#endif

#if ANALYZE == 5
comment "lognormal"
title "LOGNORMAL"
dvar  m   0.5
dvar  s   1.5
node (b5) dalgebra  rand_lognormal(m, s)
dvar mean           exp(m+0.5*s*s)                         &export
dvar sigma          sqrt((exp(s*s)-1.0) * exp(2.0*m+s*s))  &export

tool     statval "stat_b5.out"  b5 1.0 npts      // OK
tool   histogram "hist_b5.out"  b5 1.0 npts
#endif

#if ANALYZE == 6
comment "chisquare"
title "CHISQUARE"
ivar  n   2
node (b6) dalgebra  rand_chisquare(n)
dvar mean           n                            &export
dvar sigma          sqrt(2.0*n)                  &export

tool     statval "stat_b6.out"  b6 1.0 npts      // OK
tool   histogram "hist_b6.out"  b6 1.0 npts
#endif

#if ANALYZE == 7
comment "student"
title "STUDENT"
ivar  n  3
node (b7) dalgebra  rand_student(n)
dvar mean           0.0                          &export  //  (n > 1)
dvar sigma          sqrt(n/(n-2.0))              &export  //  (n > 2)

tool     statval "stat_b7.out"  b7 1.0 npts      // OK
tool   histogram "hist_b7.out"  b7 1.0 npts
#endif

#if ANALYZE == 8
comment "rayleigh"
title "RAYLEIGH"
dvar  b  3.0
node (b8) dalgebra  rand_rayleigh(b)
dvar mean           b*sqrt(_pi2_)                &export
dvar sigma          b*sqrt(b*(4.0-_pi_)/2.0)     &export

tool     statval "stat_b8.out"  b8 1.0 npts      // OK
tool   histogram "hist_b8.out"  b8 1.0 npts
#endif

#if ANALYZE == 9
comment "weibull"
title "WEIBULL"
dvar  k  0.5
dvar  l  1.2
dvar  mean          l*gamma(1.0+(1.0/k))               &export
dvar  sigma         l*sqrt(gamma(1.0+2.0/k)-mean*mean) &export
node (b9) dalgebra  rand_weibull(k, l)

tool     statval "stat_b9.out"  b9 1.0 npts
tool   histogram "hist_b9.out"  b9 1.0 npts
#endif

#if ANALYZE == 10
comment "Gumbel1"
title "GUMBEL1"
dvar  m  1.5
dvar  b  3.0
dvar  mean           m-Euler_Mascheroni*b        &export
dvar  sigma          b*_pi_/sqrt(6.0)            &export
node (b10) dalgebra  rand_gumbel1(b, m)

tool      statval "stat_b10.out"  b10 1.0 npts
tool    histogram "hist_b10.out"  b10 1.0 npts
#endif

#if ANALYZE == 11
comment "Gumbel2"
title "GUMBEL2"
dvar  m  1.5
dvar  b  3.0
dvar  mean           m-Euler_Mascheroni*b        &export
dvar  sigma          b*_pi_/sqrt(6.0)            &export
node (b11) dalgebra  rand_gumbel2(b, m)

tool      statval "stat_b11.out"  b11 1.0 npts
tool    histogram "hist_b11.out"  b11 1.0 npts
#endif

#if ANALYZE == 12
comment "halfnormal"
title "HALFNORMAL"
dvar  t  2.0
dvar mean           t*sqrt(2.0/_pi_)             &export
dvar sigma          t*sqrt(1.0-(2.0/_pi_))       &export
node (b12) dalgebra  rand_halfnormal(t)

tool     statval "stat_b12.out"  b12 1.0 npts    // OK
tool   histogram "hist_b12.out"  b12 1.0 npts
#endif

#if ANALYZE > 12
error  "wrong analysis number!"
#endif

terminate  1 <= TOOL_INDEX


ping stderr