-
Notifications
You must be signed in to change notification settings - Fork 191
Probability Distribution and Statistical Functions -- Normal Distribution Module #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jvdp1
merged 57 commits into
fortran-lang:master
from
Jim-215-Fisher:Distribution-Normal
Dec 9, 2021
Merged
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
979902d
initial commit
Jim-215-Fisher 658aa44
Delete stdlib_stats_distribution_normal.fypp
Jim-215-Fisher 51fb947
initial commit
Jim-215-Fisher ce87314
initial commit
Jim-215-Fisher 86aa8ce
initial commit
Jim-215-Fisher 849c278
Update CMakeLists.txt
Jim-215-Fisher 448fbdb
Update Makefile.manual
Jim-215-Fisher 7f2bb28
Update stdlib_stats_distribution_normal.md
Jim-215-Fisher 2e399ea
Update stdlib_stats_distribution_normal.md
Jim-215-Fisher 8da3064
Update CMakeLists.txt
Jim-215-Fisher 9d3b2b8
Add files via upload
Jim-215-Fisher 6795c34
Add files via upload
Jim-215-Fisher 8f9e681
Update stdlib_stats_distribution_normal.fypp
Jim-215-Fisher 80bd8ac
Add files via upload
Jim-215-Fisher 1a1dfe4
Update Makefile.manual
Jim-215-Fisher 8457bbe
Update Makefile.manual
Jim-215-Fisher f8881a0
Update Makefile.manual
Jim-215-Fisher 256b815
Update Makefile.manual
Jim-215-Fisher 75ee05e
Update stdlib_stats_distribution_uniform.fypp
Jim-215-Fisher 86f4f3a
Update stdlib_stats_distribution_normal.md
Jim-215-Fisher 374c4b9
chn. complex number with kinds
Jim-215-Fisher a605a89
chg. complex number with kinds
Jim-215-Fisher 9e0f2b9
Update Makefile.manual
Jim-215-Fisher e662e64
Update Makefile.manual
Jim-215-Fisher d83fe37
Update CMakeLists.txt
Jim-215-Fisher d47d532
Update Makefile.manual
Jim-215-Fisher 7d24bf6
Update Makefile.manual
Jim-215-Fisher d7ab9b3
Merge pull request #5 from Jim-215-Fisher/master
Jim-215-Fisher 9b2c7c1
Update Makefile.manual
Jim-215-Fisher 7b8fbc2
Update CMakeLists.txt
Jim-215-Fisher be8f7fb
Update Makefile.manual
Jim-215-Fisher 6d30e3e
Merge pull request #13 from Jim-215-Fisher/master
Jim-215-Fisher 156c802
Merge pull request #15 from Jim-215-Fisher/master
Jim-215-Fisher 0c54889
Update Makefile.manual
Jim-215-Fisher da63c88
Add files via upload
Jim-215-Fisher 5158fe1
Add files via upload
Jim-215-Fisher d6140cd
Update stdlib_stats_distribution_normal.md
Jim-215-Fisher 7817320
Update Makefile.manual
Jim-215-Fisher 848c5d5
Merge remote-tracking branch 'upstream/master'
eb596a9
Merge remote-tracking branch 'upstream/master'
e0d6dde
change .f90 to .fypp
2ad9b06
Merge branch 'master' into Distribution-Normal
7b27e63
add normal distribution
180da5e
add vn variable in zigset subroutine
e786bd5
change freq array size to array_size in test file
11bef2f
Update doc/specs/stdlib_stats_distribution_normal.md
Jim-215-Fisher 62f02a8
Update doc/specs/stdlib_stats_distribution_normal.md
Jim-215-Fisher 314492d
Update src/stdlib_stats_distribution_normal.fypp
Jim-215-Fisher c4d1391
some modification
d2a611e
Merge remote-tracking branch 'upstream/master'
ed7c2cb
modify for complex case
4e06727
Merge branch 'master' into Distribution-Normal
7194bfd
add xdp in test file
09f8fd2
add precisions for pdf and cdf
915d143
add limitation note in specs
6dfe477
pdf formula change to prevent overflow
078e5a7
Update stdlib_stats_distribution_normal.md
Jim-215-Fisher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
--- | ||
title: stats_distribution_normal | ||
--- | ||
|
||
# Statistical Distributions -- Normal Distribution Module | ||
|
||
[TOC] | ||
|
||
## `rvs_normal` - normal distribution random variates | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
A normal continuous random variate distribution, also known as Gaussian, or Gauss or Laplace-Gauss distribution. The location `loc` specifies the mean or expectation. The `scale` specifies the standard deviation. | ||
|
||
Without argument the function returns a standard normal distributed random variate N(0,1). | ||
|
||
With two arguments, the function returns a normal distributed random variate N(loc, scale^2). For complex arguments, the real and imaginary parts are independent of each other. | ||
|
||
With three arguments, the function returns a rank one array of normal distributed random variates. | ||
jvdp1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Note: the algorithm used for generating normal random variates is fundamentally limited to double precision. | ||
|
||
### Syntax | ||
|
||
`result = [[stdlib_stats_distribution_normal(module):rvs_normal(interface)]]([loc, scale] [[, array_size]])` | ||
|
||
### Class | ||
|
||
Function | ||
|
||
### Arguments | ||
|
||
`array_size`: optional argument has `intent(in)` and is a scalar of type `integer`. | ||
|
||
`loc`: optional argument has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
||
`scale`: optional argument has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
||
`loc` and `scale` arguments must be of the same type. | ||
|
||
### Return value | ||
|
||
The result is a scalar or rank one array, with a size of `array_size`, and as the same type of `scale` and `loc`. | ||
|
||
### Example | ||
|
||
```fortran | ||
program demo_normal_rvs | ||
use stdlib_random, only: random_seed | ||
use stdlib_stats_distribution_normal, only: norm => rvs_normal | ||
|
||
implicit none | ||
real :: a(2,3,4), b(2,3,4) | ||
complx :: loc, scale | ||
integer :: seed_put, seed_get | ||
|
||
seed_put = 1234567 | ||
call random_seed(seed_put, seed_get) | ||
|
||
print *, norm( ) !single standard normal random variate | ||
|
||
! 0.563655198 | ||
|
||
print *, norm(1.0, 2.0) | ||
!normal random variate mu=1.0, sigma=2.0 | ||
|
||
! -0.633261681 | ||
|
||
print *, norm(0.0, 1.0, 10) !an array of 10 standard norml random variates | ||
|
||
! -3.38123664E-02 -0.190365672 0.220678389 -0.424612164 -0.249541596 | ||
! 0.865260184 1.11086845 -0.328349441 1.10873628 1.27049923 | ||
|
||
a(:,:,:) = 1.0 | ||
b(:,:,:) = 1.0 | ||
print *, norm(a,b) ! a rank 3 random variates array | ||
|
||
!0.152776539 -7.51764774E-02 1.47208166 0.180561781 1.32407105 | ||
! 1.20383692 0.123445868 -0.455737948 -0.469808221 1.60750175 | ||
! 1.05748117 0.720934749 0.407810807 1.48165631 2.31749439 | ||
! 0.414566994 3.06084275 1.86505437 1.36338580 7.26878643E-02 | ||
! 0.178585172 1.39557445 0.828021586 0.872084975 | ||
|
||
loc = (-1.0, 2.0) | ||
scale = (2.0, 1.0) | ||
print *, norm(loc, scale) | ||
!single complex normal random variate with real part of mu=-1, sigma=2; | ||
!imagainary part of mu=2.0 and sigma=1.0 | ||
|
||
! (1.22566295,2.12518454) | ||
|
||
end program demo_normal_rvs | ||
``` | ||
|
||
## `pdf_normal` - normal distribution probability density function | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
The probability density function (pdf) of the single real variable normal distribution: | ||
|
||
$$f(x) = \frac{1}{\sigma \sqrt{2}} e^{-\frac{1}{2}(\frac{x-\mu}{\sigma})^{2}}$$ | ||
Jim-215-Fisher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
For complex varible (x + y i) with independent real x and imaginary y parts, the joint probability density function is the product of corresponding marginal pdf of real and imaginary pdf (ref. "Probability and Random Processes with Applications to Signal Processing and Communications", 2nd ed., Scott L. Miller and Donald Childers, 2012, p.197): | ||
|
||
$$f(x + y \mathit{i}) = f(x) f(y) = \frac{1}{2\sigma_{x}\sigma_{y}} e^{-\frac{1}{2}[(\frac{x-\mu}{\sigma_{x}})^{2}+(\frac{y-\nu}{\sigma_{y}})^{2}]}$$ | ||
|
||
### Syntax | ||
|
||
`result = [[stdlib_stats_distribution_normal(module):pdf_normal(interface)]](x, loc, scale)` | ||
|
||
### Class | ||
|
||
Elemental function | ||
|
||
### Arguments | ||
|
||
`x`: has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
||
`loc`: has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
||
`scale`: has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
||
All three arguments must have the same type. | ||
|
||
### Return value | ||
|
||
The result is a scalar or an array, with a shape conformable to arguments, and as the same type of input arguments. | ||
|
||
### Example | ||
|
||
```fortran | ||
program demo_normal_pdf | ||
use stdlib_random, only : random_seed | ||
use stdlib_stats_distribution_normal, only : norm_pdf=>pdf_normal, & | ||
norm => rvs_normal | ||
|
||
implicit none | ||
real :: x(3,4,5),a(3,4,5),b(3,4,5) | ||
complx :: loc, scale | ||
integer :: seed_put, seed_get | ||
|
||
seed_put = 1234567 | ||
call random_seed(seed_put, seed_get) | ||
|
||
print *, norm_pdf(1.0,0.,1.) !a probability density at 1.0 in standard normal | ||
|
||
! 0.241970733 | ||
|
||
print *, norm_pdf(2.0,-1.0, 2.0) | ||
!a probability density at 2.0 with mu=-1.0 sigma=2.0 | ||
|
||
!6.47588000E-02 | ||
|
||
x = reshape(norm(0.0, 1.0, 60),[3,4,5]) | ||
! standard normal random variates array | ||
|
||
a(:,:,:) = 0.0 | ||
b(:,:,:) = 1.0 | ||
print *, norm_pdf(x, a, b) ! standard normal probability density array | ||
|
||
! 0.340346158 0.285823315 0.398714304 0.391778737 0.389345556 | ||
! 0.364551932 0.386712372 0.274370432 0.215250477 0.378006011 | ||
! 0.215760440 0.177990928 0.278640658 0.223813817 0.356875211 | ||
! 0.285167664 0.378533930 0.390739858 0.271684974 0.138273031 | ||
! 0.135456234 0.331718773 0.398283750 0.383706540 | ||
|
||
loc = (1.0, -0.5) | ||
scale = (1.0, 2.) | ||
print *, norm_pdf((1.5,1.0), loc, scale) | ||
! a complex normal probability density function at (1.5,1.0) with real part | ||
! of mu=1.0, sigma=1.0 and imaginary part of mu=-0.5, sigma=2.0 | ||
|
||
! 5.30100204E-02 | ||
|
||
end program demo_normal_pdf | ||
``` | ||
|
||
## `cdf_normal` - normal distribution cumulative distribution function | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
Cumulative distribution function of the single real variable normal distribution: | ||
|
||
$$F(x)=\frac{1}{2}\left [ 1+erf(\frac{x-\mu}{\sigma \sqrt{2}}) \right ]$$ | ||
Jim-215-Fisher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
For the complex variable (x + y i) with independent real x and imaginary y parts, the joint cumulative distribution function is the product of corresponding marginal cdf of real and imaginary cdf (ref. "Probability and Random Processes with Applications to Signal Processing and Communications", 2nd ed., Scott L. Miller and Donald Childers, 2012, p.197): | ||
|
||
$$F(x+y\mathit{i})=F(x)F(y)=\frac{1}{4} [1+erf(\frac{x-\mu}{\sigma_{x} \sqrt{2}})] [1+erf(\frac{y-\nu}{\sigma_{y} \sqrt{2}})]$$ | ||
|
||
### Syntax | ||
|
||
`result = [[stdlib_stats_distribution_normal(module):cdf_normal(interface)]](x, loc, scale)` | ||
|
||
### Class | ||
|
||
Elemental function | ||
|
||
### Arguments | ||
|
||
`x`: has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
||
`loc`: has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
||
`scale`: has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
||
All three arguments must have the same type. | ||
|
||
### Return value | ||
|
||
The result is a scalar or an array, with a shape conformable to arguments, as the same type of input arguments. | ||
|
||
### Example | ||
|
||
```fortran | ||
program demo_norm_cdf | ||
use stdlib_random, only : random_seed | ||
use stdlib_stats_distribution_normal, only : norm_cdf => cdf_normal, & | ||
norm => rvs_normal | ||
|
||
implicit none | ||
real :: x(2,3,4),a(2,3,4),b(2,3,4) | ||
complx :: loc, scale | ||
integer :: seed_put, seed_get | ||
|
||
seed_put = 1234567 | ||
call random_seed(seed_put, seed_get) | ||
|
||
print *, norm_cdf(1.0, 0.0, 1.0) ! a standard normal cumulative at 1.0 | ||
|
||
! 0.841344714 | ||
|
||
print *, norm_cdf(2.0, -1.0, 2.0) | ||
! a cumulative at 2.0 with mu=-1 sigma=2 | ||
|
||
! 0.933192849 | ||
|
||
x = reshape(norm(0.0, 1.0, 24),[2,3,4]) | ||
! standard normal random variates array | ||
|
||
a(:,:,:) = 0.0 | ||
b(:,:,:) = 1.0 | ||
print *, norm_cdf(x, a, b) ! standard normal cumulative array | ||
|
||
! 0.713505626 0.207069695 0.486513376 0.424511284 0.587328553 | ||
! 0.335559726 0.401470929 0.806552052 0.866687536 0.371323735 | ||
! 0.866228044 0.898046613 0.198435277 0.141147852 0.681565762 | ||
! 0.206268221 0.627057910 0.580759525 0.190364420 7.27325380E-02 | ||
! 7.08068311E-02 0.728241026 0.522919059 0.390097380 | ||
|
||
loc = (1.0,0.0) | ||
scale = (0.5,1.0) | ||
print *, norm_cdf((0.5,-0.5),loc,scale) | ||
!complex normal cumulative distribution at (0.5,-0.5) with real part of | ||
!mu=1.0, sigma=0.5 and imaginary part of mu=0.0, sigma=1.0 | ||
|
||
!4.89511043E-02 | ||
|
||
end program demo_norm_cdf | ||
|
||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.