|
| 1 | +program example_normal_cdf |
| 2 | + use stdlib_random, only: random_seed |
| 3 | + use stdlib_stats_distribution_normal, only: norm_cdf => cdf_normal, & |
| 4 | + norm => rvs_normal |
| 5 | + |
| 6 | + implicit none |
| 7 | + real, dimension(2, 3, 4) :: x, mu, sigma |
| 8 | + real :: xsum |
| 9 | + complex :: loc, scale |
| 10 | + integer :: seed_put, seed_get, i |
| 11 | + |
| 12 | + seed_put = 1234567 |
| 13 | + call random_seed(seed_put, seed_get) |
| 14 | + |
| 15 | + ! standard normal cumulative probability at x=0.0 |
| 16 | + print *, norm_cdf(0.0, 0.0, 1.0) |
| 17 | + ! 0.500000000 |
| 18 | + |
| 19 | + ! cumulative probability at x=2.0 with mu=-1.0 sigma=2.0 |
| 20 | + print *, norm_cdf(2.0, -1.0, 2.0) |
| 21 | + ! 0.933192849 |
| 22 | + |
| 23 | + ! cumulative probability at x=1.0 with mu=1.0 and sigma=-1.0 (out of range) |
| 24 | + print *, norm_cdf(1.0, 1.0, -1.0) |
| 25 | + ! NaN |
| 26 | + |
| 27 | + ! standard normal random variates array |
| 28 | + x = reshape(norm(0.0, 1.0, 24), [2, 3, 4]) |
| 29 | + |
| 30 | + ! standard normal cumulative array |
| 31 | + mu(:, :, :) = 0.0 |
| 32 | + sigma(:, :, :) = 1.0 |
| 33 | + print *, norm_cdf(x, mu, sigma) |
| 34 | + ! 0.713505626 0.207069695 0.486513376 0.424511284 0.587328553 |
| 35 | + ! 0.335559726 0.401470929 0.806552052 0.866687536 0.371323735 |
| 36 | + ! 0.866228044 0.898046613 0.198435277 0.141147852 0.681565762 |
| 37 | + ! 0.206268221 0.627057910 0.580759525 0.190364420 7.27325380E-02 |
| 38 | + ! 7.08068311E-02 0.728241026 0.522919059 0.390097380 |
| 39 | + |
| 40 | + ! cumulative probability array where sigma<=0.0 for certain elements |
| 41 | + print *, norm_cdf([1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 0.0, -1.0]) |
| 42 | + ! 0.500000000 NaN NaN |
| 43 | + |
| 44 | + ! `cdf_normal` is pure and, thus, can be called concurrently |
| 45 | + xsum = 0.0 |
| 46 | + do concurrent (i=1:size(x,3)) |
| 47 | + xsum = xsum + sum(norm_cdf(x(:,:,i), mu(:,:,i), sigma(:,:,i))) |
| 48 | + end do |
| 49 | + print *, xsum |
| 50 | + ! 11.3751936 |
| 51 | + |
| 52 | + ! complex normal cumulative distribution at x=(0.5,-0.5) with real part of |
| 53 | + ! mu=1.0, sigma=0.5 and imaginary part of mu=0.0, sigma=1.0 |
| 54 | + loc = (1.0, 0.0) |
| 55 | + scale = (0.5, 1.0) |
| 56 | + print *, norm_cdf((0.5, -0.5), loc, scale) |
| 57 | + ! 4.89511043E-02 |
| 58 | + |
| 59 | +end program example_normal_cdf |
| 60 | + |
0 commit comments