stats_distribution_normal

统计分布 -- 正态分布模块

rvs_normal - 正态分布随机变量

状态

实验性

描述

正态连续随机变量分布,也称为高斯分布或拉普拉斯-高斯分布。位置loc指定均值或期望值 (). scale指定标准差 ().

无参数时,函数返回一个标准正态分布的随机变量.

有两个参数时,函数返回一个正态分布的随机变量. 对于复数参数,实部和虚部彼此独立。

有三个参数时,函数返回一个秩为1的正态分布随机变量数组。

注意

用于生成指数随机变量的算法从根本上限制为双精度。1

语法

result = rvs_normal ([loc, scale] [[, array_size]])

类别

元素函数(传递locscale)。

参数

loc:可选参数具有intent(in),是类型为realcomplex的标量。

scale:可选参数具有intent(in),是类型为realcomplex的正标量。

array_size:可选参数具有intent(in),是类型为integer的标量。

locscale参数必须具有相同的类型。

返回值

结果是一个标量或秩为1的数组,大小为array_size,与scaleloc的类型相同。如果scale是非正数,则结果为NaN

示例

program example_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)
  complex :: 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 example_normal_rvs

pdf_normal - 正态分布概率密度函数

状态

实验性

描述

单实变量正态分布的概率密度函数 (pdf)

对于复变量具有独立的实部和虚部部分,联合概率密度函数是相应实部和虚部边缘pdf的乘积:2

语法

result = pdf_normal (x, loc, scale)

类别

元素函数

参数

x:具有intent(in),是类型为realcomplex的标量。

loc:具有intent(in),是类型为realcomplex的标量。

scale:具有intent(in),是类型为realcomplex的正标量。

所有三个参数必须具有相同的类型。

返回值

结果是一个标量或数组,其形状与参数一致,并且与输入参数的类型相同。如果scale是非正数,则结果为NaN

示例

program example_normal_pdf
  use stdlib_random, only: random_seed
  use stdlib_stats_distribution_normal, only: norm_pdf => pdf_normal, &
                                              norm => rvs_normal

  implicit none
  real, dimension(3, 4, 5) :: x, mu, sigma
  real :: xsum
  complex :: loc, scale
  integer :: seed_put, seed_get, i

  seed_put = 1234567
  call random_seed(seed_put, seed_get)

  ! probability density at x=1.0 in standard normal
  print *, norm_pdf(1.0, 0., 1.) 
  ! 0.241970733

  ! probability density at x=2.0 with mu=-1.0 and sigma=2.0 
  print *, norm_pdf(2.0, -1.0, 2.0)
  ! 6.47588000E-02

  ! probability density at x=1.0 with mu=1.0 and sigma=-1.0 (out of range) 
  print *, norm_pdf(1.0, 1.0, -1.0)
  ! NaN

  ! standard normal random variates array
  x = reshape(norm(0.0, 1.0, 60), [3, 4, 5])

  ! standard normal probability density array
  mu(:, :, :) = 0.0
  sigma(:, :, :) = 1.0
  print *, norm_pdf(x, mu, sigma)  
  !  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

  ! probability density array where sigma<=0.0 for certain elements 
  print *, norm_pdf([1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 0.0, -1.0])
  ! 0.398942292  NaN NaN

  ! `pdf_normal` is pure and, thus, can be called concurrently 
  xsum = 0.0
  do concurrent (i=1:size(x,3))
    xsum = xsum + sum(norm_pdf(x(:,:,i), mu(:,:,i), sigma(:,:,i)))
  end do
  print *, xsum
  ! 18.0754433

  ! complex normal probability density function at x=(1.5,1.0) with real part
  ! of mu=1.0, sigma=1.0 and imaginary part of mu=-0.5, sigma=2.0
  loc = (1.0, -0.5)
  scale = (1.0, 2.)
  print *, norm_pdf((1.5, 1.0), loc, scale)
  ! 5.30100204E-02

end program example_normal_pdf

cdf_normal - 正态分布累积分布函数

状态

实验性

描述

单实变量正态分布的累积分布函数

对于复变量具有独立的实部和虚部部分,联合累积分布函数是相应实部和虚部边缘cdf的乘积:2

语法

result = cdf_normal (x, loc, scale)

类别

元素函数

参数

x:具有intent(in),是类型为realcomplex的标量。

loc:具有intent(in),是类型为realcomplex的标量。

scale:具有intent(in),是类型为realcomplex的正标量。

所有三个参数必须具有相同的类型。

返回值

结果是一个标量或数组,其形状与参数一致,并且与输入参数的类型相同。如果scale是非正数,则结果为NaN

示例

program example_normal_cdf
  use stdlib_random, only: random_seed
  use stdlib_stats_distribution_normal, only: norm_cdf => cdf_normal, &
                                              norm => rvs_normal

  implicit none
  real, dimension(2, 3, 4) :: x, mu, sigma
  real :: xsum
  complex :: loc, scale
  integer :: seed_put, seed_get, i

  seed_put = 1234567
  call random_seed(seed_put, seed_get)

  ! standard normal cumulative probability at x=0.0
  print *, norm_cdf(0.0, 0.0, 1.0)
  ! 0.500000000

  ! cumulative probability at x=2.0 with mu=-1.0 sigma=2.0
  print *, norm_cdf(2.0, -1.0, 2.0)
  ! 0.933192849

  ! cumulative probability at x=1.0 with mu=1.0 and sigma=-1.0 (out of range) 
  print *, norm_cdf(1.0, 1.0, -1.0)
  ! NaN

  ! standard normal random variates array
  x = reshape(norm(0.0, 1.0, 24), [2, 3, 4])

  ! standard normal cumulative array
  mu(:, :, :) = 0.0
  sigma(:, :, :) = 1.0
  print *, norm_cdf(x, mu, sigma)        
  !  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

   ! cumulative probability array where sigma<=0.0 for certain elements 
  print *, norm_cdf([1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 0.0, -1.0])
  ! 0.500000000  NaN NaN

  ! `cdf_normal` is pure and, thus, can be called concurrently 
  xsum = 0.0
  do concurrent (i=1:size(x,3))
    xsum = xsum + sum(norm_cdf(x(:,:,i), mu(:,:,i), sigma(:,:,i)))
  end do
  print *, xsum
  ! 11.3751936

  ! complex normal cumulative distribution at x=(0.5,-0.5) with real part of
  ! mu=1.0, sigma=0.5 and imaginary part of mu=0.0, sigma=1.0
  loc = (1.0, 0.0)
  scale = (0.5, 1.0)
  print *, norm_cdf((0.5, -0.5), loc, scale)
  ! 4.89511043E-02

end program example_normal_cdf

  1. Marsaglia, George 和 Wai Wan Tsang。“生成随机变量的锯齿形方法。”《统计软件杂志》5 (2000):1-7。 

  2. Miller, Scott 和 Donald Childers。《概率与随机过程:及其在信号处理和通信中的应用》。学术出版社,2012 年(第 197 页)。