Skip to content

Commit 49d269b

Browse files
authored
Merge pull request #273 from Jim-215-Fisher/Distribution-Normal
Probability Distribution and Statistical Functions -- Normal Distribution Module
2 parents 100f760 + 078e5a7 commit 49d269b

File tree

8 files changed

+895
-1
lines changed

8 files changed

+895
-1
lines changed

doc/specs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This is and index/directory of the specifications (specs) for each new module/fe
2525
- [sorting](./stdlib_sorting.html) - Sorting of rank one arrays
2626
- [stats](./stdlib_stats.html) - Descriptive Statistics
2727
- [stats_distributions_uniform](./stdlib_stats_distribution_uniform.html) - Uniform Probability Distribution
28+
- [stats_distributions_normal](./stdlib_stats_distribution_normal.html) - Normal Probability Distribution
2829
- [string\_type](./stdlib_string_type.html) - Basic string support
2930
- [strings](./stdlib_strings.html) - String handling and manipulation routines
3031
- [stringlist_type](./stdlib_stringlist_type.html) - 1-Dimensional list of strings
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
---
2+
title: stats_distribution_normal
3+
---
4+
5+
# Statistical Distributions -- Normal Distribution Module
6+
7+
[TOC]
8+
9+
## `rvs_normal` - normal distribution random variates
10+
11+
### Status
12+
13+
Experimental
14+
15+
### Description
16+
17+
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.
18+
19+
Without argument the function returns a standard normal distributed random variate N(0,1).
20+
21+
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.
22+
23+
With three arguments, the function returns a rank one array of normal distributed random variates.
24+
25+
Note: the algorithm used for generating normal random variates is fundamentally limited to double precision.
26+
27+
### Syntax
28+
29+
`result = [[stdlib_stats_distribution_normal(module):rvs_normal(interface)]]([loc, scale] [[, array_size]])`
30+
31+
### Class
32+
33+
Function
34+
35+
### Arguments
36+
37+
`array_size`: optional argument has `intent(in)` and is a scalar of type `integer`.
38+
39+
`loc`: optional argument has `intent(in)` and is a scalar of type `real` or `complex`.
40+
41+
`scale`: optional argument has `intent(in)` and is a scalar of type `real` or `complex`.
42+
43+
`loc` and `scale` arguments must be of the same type.
44+
45+
### Return value
46+
47+
The result is a scalar or rank one array, with a size of `array_size`, and as the same type of `scale` and `loc`.
48+
49+
### Example
50+
51+
```fortran
52+
program demo_normal_rvs
53+
use stdlib_random, only: random_seed
54+
use stdlib_stats_distribution_normal, only: norm => rvs_normal
55+
56+
implicit none
57+
real :: a(2,3,4), b(2,3,4)
58+
complx :: loc, scale
59+
integer :: seed_put, seed_get
60+
61+
seed_put = 1234567
62+
call random_seed(seed_put, seed_get)
63+
64+
print *, norm( ) !single standard normal random variate
65+
66+
! 0.563655198
67+
68+
print *, norm(1.0, 2.0)
69+
!normal random variate mu=1.0, sigma=2.0
70+
71+
! -0.633261681
72+
73+
print *, norm(0.0, 1.0, 10) !an array of 10 standard norml random variates
74+
75+
! -3.38123664E-02 -0.190365672 0.220678389 -0.424612164 -0.249541596
76+
! 0.865260184 1.11086845 -0.328349441 1.10873628 1.27049923
77+
78+
a(:,:,:) = 1.0
79+
b(:,:,:) = 1.0
80+
print *, norm(a,b) ! a rank 3 random variates array
81+
82+
!0.152776539 -7.51764774E-02 1.47208166 0.180561781 1.32407105
83+
! 1.20383692 0.123445868 -0.455737948 -0.469808221 1.60750175
84+
! 1.05748117 0.720934749 0.407810807 1.48165631 2.31749439
85+
! 0.414566994 3.06084275 1.86505437 1.36338580 7.26878643E-02
86+
! 0.178585172 1.39557445 0.828021586 0.872084975
87+
88+
loc = (-1.0, 2.0)
89+
scale = (2.0, 1.0)
90+
print *, norm(loc, scale)
91+
!single complex normal random variate with real part of mu=-1, sigma=2;
92+
!imagainary part of mu=2.0 and sigma=1.0
93+
94+
! (1.22566295,2.12518454)
95+
96+
end program demo_normal_rvs
97+
```
98+
99+
## `pdf_normal` - normal distribution probability density function
100+
101+
### Status
102+
103+
Experimental
104+
105+
### Description
106+
107+
The probability density function (pdf) of the single real variable normal distribution:
108+
109+
$$f(x) = \frac{1}{\sigma \sqrt{2}} e^{-\frac{1}{2}(\frac{x-\mu}{\sigma})^{2}}$$
110+
111+
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):
112+
113+
$$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}]}$$
114+
115+
### Syntax
116+
117+
`result = [[stdlib_stats_distribution_normal(module):pdf_normal(interface)]](x, loc, scale)`
118+
119+
### Class
120+
121+
Elemental function
122+
123+
### Arguments
124+
125+
`x`: has `intent(in)` and is a scalar of type `real` or `complex`.
126+
127+
`loc`: has `intent(in)` and is a scalar of type `real` or `complex`.
128+
129+
`scale`: has `intent(in)` and is a scalar of type `real` or `complex`.
130+
131+
All three arguments must have the same type.
132+
133+
### Return value
134+
135+
The result is a scalar or an array, with a shape conformable to arguments, and as the same type of input arguments.
136+
137+
### Example
138+
139+
```fortran
140+
program demo_normal_pdf
141+
use stdlib_random, only : random_seed
142+
use stdlib_stats_distribution_normal, only : norm_pdf=>pdf_normal, &
143+
norm => rvs_normal
144+
145+
implicit none
146+
real :: x(3,4,5),a(3,4,5),b(3,4,5)
147+
complx :: loc, scale
148+
integer :: seed_put, seed_get
149+
150+
seed_put = 1234567
151+
call random_seed(seed_put, seed_get)
152+
153+
print *, norm_pdf(1.0,0.,1.) !a probability density at 1.0 in standard normal
154+
155+
! 0.241970733
156+
157+
print *, norm_pdf(2.0,-1.0, 2.0)
158+
!a probability density at 2.0 with mu=-1.0 sigma=2.0
159+
160+
!6.47588000E-02
161+
162+
x = reshape(norm(0.0, 1.0, 60),[3,4,5])
163+
! standard normal random variates array
164+
165+
a(:,:,:) = 0.0
166+
b(:,:,:) = 1.0
167+
print *, norm_pdf(x, a, b) ! standard normal probability density array
168+
169+
! 0.340346158 0.285823315 0.398714304 0.391778737 0.389345556
170+
! 0.364551932 0.386712372 0.274370432 0.215250477 0.378006011
171+
! 0.215760440 0.177990928 0.278640658 0.223813817 0.356875211
172+
! 0.285167664 0.378533930 0.390739858 0.271684974 0.138273031
173+
! 0.135456234 0.331718773 0.398283750 0.383706540
174+
175+
loc = (1.0, -0.5)
176+
scale = (1.0, 2.)
177+
print *, norm_pdf((1.5,1.0), loc, scale)
178+
! a complex normal probability density function at (1.5,1.0) with real part
179+
! of mu=1.0, sigma=1.0 and imaginary part of mu=-0.5, sigma=2.0
180+
181+
! 5.30100204E-02
182+
183+
end program demo_normal_pdf
184+
```
185+
186+
## `cdf_normal` - normal distribution cumulative distribution function
187+
188+
### Status
189+
190+
Experimental
191+
192+
### Description
193+
194+
Cumulative distribution function of the single real variable normal distribution:
195+
196+
$$F(x)=\frac{1}{2}\left [ 1+erf(\frac{x-\mu}{\sigma \sqrt{2}}) \right ]$$
197+
198+
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):
199+
200+
$$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}})]$$
201+
202+
### Syntax
203+
204+
`result = [[stdlib_stats_distribution_normal(module):cdf_normal(interface)]](x, loc, scale)`
205+
206+
### Class
207+
208+
Elemental function
209+
210+
### Arguments
211+
212+
`x`: has `intent(in)` and is a scalar of type `real` or `complex`.
213+
214+
`loc`: has `intent(in)` and is a scalar of type `real` or `complex`.
215+
216+
`scale`: has `intent(in)` and is a scalar of type `real` or `complex`.
217+
218+
All three arguments must have the same type.
219+
220+
### Return value
221+
222+
The result is a scalar or an array, with a shape conformable to arguments, as the same type of input arguments.
223+
224+
### Example
225+
226+
```fortran
227+
program demo_norm_cdf
228+
use stdlib_random, only : random_seed
229+
use stdlib_stats_distribution_normal, only : norm_cdf => cdf_normal, &
230+
norm => rvs_normal
231+
232+
implicit none
233+
real :: x(2,3,4),a(2,3,4),b(2,3,4)
234+
complx :: loc, scale
235+
integer :: seed_put, seed_get
236+
237+
seed_put = 1234567
238+
call random_seed(seed_put, seed_get)
239+
240+
print *, norm_cdf(1.0, 0.0, 1.0) ! a standard normal cumulative at 1.0
241+
242+
! 0.841344714
243+
244+
print *, norm_cdf(2.0, -1.0, 2.0)
245+
! a cumulative at 2.0 with mu=-1 sigma=2
246+
247+
! 0.933192849
248+
249+
x = reshape(norm(0.0, 1.0, 24),[2,3,4])
250+
! standard normal random variates array
251+
252+
a(:,:,:) = 0.0
253+
b(:,:,:) = 1.0
254+
print *, norm_cdf(x, a, b) ! standard normal cumulative array
255+
256+
! 0.713505626 0.207069695 0.486513376 0.424511284 0.587328553
257+
! 0.335559726 0.401470929 0.806552052 0.866687536 0.371323735
258+
! 0.866228044 0.898046613 0.198435277 0.141147852 0.681565762
259+
! 0.206268221 0.627057910 0.580759525 0.190364420 7.27325380E-02
260+
! 7.08068311E-02 0.728241026 0.522919059 0.390097380
261+
262+
loc = (1.0,0.0)
263+
scale = (0.5,1.0)
264+
print *, norm_cdf((0.5,-0.5),loc,scale)
265+
!complex normal cumulative distribution at (0.5,-0.5) with real part of
266+
!mu=1.0, sigma=0.5 and imaginary part of mu=0.0, sigma=1.0
267+
268+
!4.89511043E-02
269+
270+
end program demo_norm_cdf
271+
272+
```

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(fppFiles
3030
stdlib_stats_moment_mask.fypp
3131
stdlib_stats_moment_scalar.fypp
3232
stdlib_stats_distribution_uniform.fypp
33+
stdlib_stats_distribution_normal.fypp
3334
stdlib_stats_var.fypp
3435
stdlib_quadrature.fypp
3536
stdlib_quadrature_trapz.fypp

src/Makefile.manual

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SRCFYPP = \
3232
stdlib_stats_moment_mask.fypp \
3333
stdlib_stats_moment_scalar.fypp \
3434
stdlib_stats_distribution_uniform.fypp \
35+
stdlib_stats_distribution_normal.fypp \
3536
stdlib_stats_var.fypp \
3637
stdlib_math.fypp \
3738
stdlib_math_linspace.fypp \
@@ -167,6 +168,11 @@ stdlib_stats_distribution_uniform.o: \
167168
stdlib_kinds.o \
168169
stdlib_error.o \
169170
stdlib_random.o
171+
stdlib_stats_distribution_normal.o: \
172+
stdlib_kinds.o \
173+
stdlib_error.o \
174+
stdlib_random.o \
175+
stdlib_stats_distribution_uniform.o
170176
stdlib_random.o: \
171177
stdlib_kinds.o \
172178
stdlib_error.o

0 commit comments

Comments
 (0)