Class | Statistics |
In: |
statistics.f90
|
統計解析関係のルーチン集
Subroutine : | |||
nx : | integer, intent(in)
| ||
x(nx) : | real, intent(in)
| ||
anor(nx) : | real, intent(inout)
| ||
error : | real, intent(in), optional
|
1 次元データ配列の偏差を返す
subroutine Anomaly_1d( nx, x, anor, error ) ! 1 次元データ配列の偏差を返す implicit none integer, intent(in) :: nx ! データの要素数 real, intent(in) :: x(nx) ! データ real, intent(inout) :: anor(nx) ! 各 x(i) に対応する偏差 anor(i) real, intent(in), optional :: error ! 欠損値が存在するデータセットの場合の欠損値 integer :: i real :: ave if(present(error))then call Mean_1d( nx, x, ave, error ) do i=1,nx if(x(i)==error)then anor(i)=0.0 else anor(i)=x(i)-ave end if end do else call Mean_1d( nx, x, ave ) do i=1,nx anor(i)=x(i)-ave end do end if end subroutine Anomaly_1d
Subroutine : | |||
nx : | integer, intent(in)
| ||
ny : | integer, intent(in)
| ||
x(nx,ny) : | real, intent(in)
| ||
anor(nx,ny) : | real, intent(inout)
| ||
error : | real, intent(in), optional
|
2 次元データ配列の偏差を返す
subroutine Anomaly_2d( nx, ny, x, anor, error ) ! 2 次元データ配列の偏差を返す implicit none integer, intent(in) :: nx ! データの要素数 1 integer, intent(in) :: ny ! データの要素数 2 real, intent(in) :: x(nx,ny) ! データ real, intent(inout) :: anor(nx,ny) ! 各 x(i,j) に対応する偏差 anor(i,j) real, intent(in), optional :: error ! 欠損値が存在するデータセットの場合の欠損値 integer :: i, j real :: ave if(present(error))then call Mean_2d( nx, ny, x, ave, error ) do j=1,ny do i=1,nx if(x(i,j)==error)then anor(i,j)=0.0 else anor(i,j)=x(i,j)-ave end if end do end do else call Mean_2d( nx, ny, x, ave, error ) do j=1,ny do i=1,nx anor(i,j)=x(i,j)-ave end do end do end if end subroutine Anomaly_2d
Subroutine : | |||
nx : | integer, intent(in)
| ||
ny : | integer, intent(in)
| ||
nz : | integer, intent(in)
| ||
x(nx,ny,nz) : | real, intent(in)
| ||
anor(nx,ny,nz) : | real, intent(inout)
| ||
error : | real, intent(in), optional
|
3 次元データ配列の偏差を返す
subroutine Anomaly_3d( nx, ny, nz, x, anor, error ) ! 3 次元データ配列の偏差を返す implicit none integer, intent(in) :: nx ! データの要素数 1 integer, intent(in) :: ny ! データの要素数 2 integer, intent(in) :: nz ! データの要素数 3 real, intent(in) :: x(nx,ny,nz) ! データ real, intent(inout) :: anor(nx,ny,nz) ! 各 x(i,j,k) に対応する偏差 anor(i,j,k) real, intent(in), optional :: error ! 欠損値が存在するデータセットの場合の欠損値 integer :: i, j, k real :: ave if(present(error))then call Mean_3d( nx, ny, nz, x, ave, error ) do k=1,nz do j=1,ny do i=1,nx if(x(i,j,k)==error)then anor(i,j,k)=0.0 else anor(i,j,k)=x(i,j,k)-ave end if end do end do end do else call Mean_3d( nx, ny, nz, x, ave, error ) do k=1,nz do j=1,ny do i=1,nx anor(i,j,k)=x(i,j,k)-ave end do end do end do end if end subroutine Anomaly_3d
Subroutine : | |||
nx : | integer, intent(in)
| ||
x(nx) : | real, intent(in)
| ||
y(nx) : | real, intent(in)
| ||
cc : | real, intent(inout)
| ||
error : | real, intent(in), optional
|
2 データの相関係数を計算するルーチン
subroutine Cor_Coe( nx, x, y ,cc, error ) ! 2 データの相関係数を計算するルーチン implicit none integer, intent(in) :: nx ! データ個数 real, intent(in) :: x(nx) ! データ要素 1 real, intent(in) :: y(nx) ! データ要素 2 real, intent(inout) :: cc ! 相関係数 real, intent(in), optional :: error ! 欠損値 integer :: i real :: cov, anor1, anor2 if(present(error))then call covariance( nx, x, y, cov, error ) call stand_vari( nx, x, anor1, error ) call stand_vari( nx, y, anor2, error ) else call covariance( nx, x, y, cov ) call stand_vari( nx, x, anor1 ) call stand_vari( nx, y, anor2 ) end if cc=cov/(sqrt(anor1)*sqrt(anor2)) end subroutine Cor_Coe
Subroutine : | |||
nx : | integer, intent(in)
| ||
nx : | integer, intent(in) | ||
x(nx) : | real, intent(in)
| ||
y(nx) : | real, intent(in)
| ||
slope : | real, intent(inout)
| ||
intercept : | real, intent(inout)
|
最小二乗法による傾きと切片計算
subroutine LSM( nx, x, y, slope, intercept ) ! 最小二乗法による傾きと切片計算 implicit none integer, intent(in) :: nx ! データ数 real, intent(in) :: x(nx) ! データ要素 1 real, intent(in) :: y(nx) ! データ要素 2 real, intent(inout) :: slope ! 最適な傾き real, intent(inout) :: intercept ! 最適な切片 real :: u(nx), v(nx) integer :: i, j, k real :: a, b, c, d a=0.0 b=0.0 c=0.0 d=0.0 !$omp parallel do shared(u, v, x, y) private(i) do i=1,nx u(i)=x(i)*x(i) v(i)=x(i)*y(i) end do !$omp end parallel do call summ(nx,v,a) call summ(nx,x,b) call summ(nx,y,c) call summ(nx,u,d) slope=(nx*a-b*c)/(nx*d-b**2) intercept=(c*d-a*b)/(nx*d-b**2) contains subroutine summ(nx,z,add) implicit none integer, intent(in) :: nx real, intent(in) :: z(nx) real, intent(inout) :: add integer :: i add=0.0 do i=1,nx add=add+z(i) end do end subroutine summ end subroutine LSM
Subroutine : | |||
nx : | integer, intent(in)
| ||
x(nx) : | real, intent(in)
| ||
ave : | real, intent(inout)
| ||
error : | real, intent(in), optional
|
1 次元配列平均値計算ルーチン
subroutine Mean_1d( nx, x, ave, error ) ! 1 次元配列平均値計算ルーチン implicit none integer, intent(in) :: nx ! データの要素数 real, intent(in) :: x(nx) ! データ real, intent(inout) :: ave ! 計算する平均値 real, intent(in), optional :: error ! 欠損値が存在するデータセットの場合の欠損値 integer :: i, nt real :: summ summ=0.0 nt=0 if(present(error))then do i=1,nx if(x(i)/=error)then summ=summ+x(i) nt=1+nt end if end do if(nt/=0)then ave=summ/nt else ave=0.0 end if else do i=1,nx summ=summ+x(i) end do ave=summ/nx end if end subroutine Mean_1d
Subroutine : | |||
nx : | integer, intent(in)
| ||
ny : | integer, intent(in)
| ||
x(nx,ny) : | real, intent(in)
| ||
ave : | real, intent(inout)
| ||
error : | real, intent(in), optional
|
2 次元配列平均値計算ルーチン
subroutine Mean_2d( nx, ny, x, ave, error ) ! 2 次元配列平均値計算ルーチン implicit none integer, intent(in) :: nx ! データの要素数 1 integer, intent(in) :: ny ! データの要素数 2 real, intent(in) :: x(nx,ny) ! データ real, intent(inout) :: ave ! 計算する平均値 real, intent(in), optional :: error ! 欠損値が存在するデータセットの場合の欠損値 integer :: i, j, nt real :: summ, tmp summ=0.0 nt=0 if(present(error))then do j=1,ny do i=1,nx if(x(i,j)/=error)then summ=summ+x(i,j) nt=1+nt end if end do end do if(nt/=0)then ave=summ/nt else ave=0.0 end if else do j=1,ny do i=1,nx summ=summ+x(i,j) end do end do ave=summ/(nx*ny) end if end subroutine Mean_2d
Subroutine : | |||
nx : | integer, intent(in)
| ||
ny : | integer, intent(in)
| ||
nz : | integer, intent(in)
| ||
x(nx,ny,nz) : | real, intent(in)
| ||
ave : | real, intent(inout)
| ||
error : | real, intent(in), optional
|
3 次元配列平均値計算ルーチン
subroutine Mean_3d( nx, ny, nz, x, ave, error ) ! 3 次元配列平均値計算ルーチン implicit none integer, intent(in) :: nx ! データの要素数 1 integer, intent(in) :: ny ! データの要素数 2 integer, intent(in) :: nz ! データの要素数 2 real, intent(in) :: x(nx,ny,nz) ! データ real, intent(inout) :: ave ! 計算する平均値 real, intent(in), optional :: error ! 欠損値が存在するデータセットの場合の欠損値 integer :: i, j, k, nt real :: summ, tmp summ=0.0 nt=0 if(present(error))then do k=1,nz do j=1,ny do i=1,nx if(x(i,j,k)/=error)then summ=summ+x(i,j,k) nt=1+nt end if end do end do end do if(nt/=0)then ave=summ/nt else ave=0.0 end if else do k=1,nz do j=1,ny do i=1,nx summ=summ+x(i,j,k) end do end do end do ave=summ/(nx*ny*nz) end if end subroutine Mean_3d
Subroutine : | |||
nx : | integer, intent(in)
| ||
x(nx) : | real, intent(in)
| ||
y(nx) : | real, intent(in)
| ||
slope : | real, intent(inout)
| ||
intercept : | real, intent(inout)
|
LSM を用いて回帰直線の傾き slope と切片 intercept を計算するルーチン
subroutine Reg_Line( nx, x, y, slope, intercept ) ! LSM を用いて回帰直線の傾き slope と切片 intercept を計算するルーチン implicit none integer, intent(in) :: nx ! データ数 real, intent(in) :: x(nx) ! データ要素 1 real, intent(in) :: y(nx) ! データ要素 2 real, intent(inout) :: slope ! 最適な傾き real, intent(inout) :: intercept ! 最適な切片 real :: u(nx), v(nx) call Anomaly_1d( nx, x, u ) call Anomaly_1d( nx, y, v ) call LSM( nx, u, v, slope, intercept ) end subroutine
Subroutine : | |||
nx : | integer, intent(in)
| ||
x(nx) : | real, intent(in)
| ||
y(nx) : | real, intent(in)
| ||
cov : | real, intent(inout)
| ||
error : | real, intent(in), optional
|
2 つの 1 次元データの共分散を計算 共分散$sigma $の定義は, $$sigma =sum^{nx}_{i=1}{(x-\bar{x})(y-\bar{y})} $$
subroutine covariance( nx, x, y, cov, error ) ! 2 つの 1 次元データの共分散を計算 ! 共分散$\sigma $の定義は, ! $$\sigma =\sum^{nx}_{i=1}{(x-\bar{x})(y-\bar{y})} $$ implicit none integer, intent(in) :: nx ! データ数 real, intent(in) :: x(nx) ! データ 1 real, intent(in) :: y(nx) ! データ 2 real, intent(inout) :: cov ! 標準偏差 real, intent(in), optional :: error ! 欠損値 integer :: i real :: an1(nx), an2(nx) cov=0.0 if(present(error))then call Anomaly_1d( nx, x, an1, error ) call Anomaly_1d( nx, y, an2, error ) do i=1,nx if(x(i)/=error)then cov=cov+an1(i)*an2(i) end if end do else call Anomaly_1d( nx, x, an1 ) call Anomaly_1d( nx, y, an2 ) do i=1,nx cov=cov+an1(i)*an2(i) end do end if end subroutine covariance
Subroutine : | |||
x(:) : | real, intent(in)
| ||
point : | real, intent(in)
| ||
i : | integer, intent(inout)
|
漸増配列(要素数が増えるごとに値が大きくなる配列)のなかで, point の前に来る要素番号を出力する.
subroutine interpo_search_1d( x, point, i ) ! 漸増配列(要素数が増えるごとに値が大きくなる配列)のなかで, ! point の前に来る要素番号を出力する. implicit none real, intent(in) :: x(:) ! 漸増配列 real, intent(in) :: point ! この点 integer, intent(inout) :: i ! point の値を越えない最大の値をもつ要素番号 integer :: nx, j nx=size(x) if(x(1)>point.or.x(nx)<point)then write(*,*) "****** ERROR ******" write(*,*) "searching point was not found." write(*,*) "Abort. Stop.!!!" stop end if do j=1,nx if(x(j)<=point)then i=j else exit end if end do end subroutine interpo_search_1d
Subroutine : | |||
x(:) : | real, intent(in)
| ||
y(:) : | real, intent(in)
| ||
pointx : | real, intent(in)
| ||
pointy : | real, intent(in)
| ||
i : | integer, intent(inout)
| ||
j : | integer, intent(inout)
|
漸増配列(要素数が増えるごとに値が大きくなる配列)のなかで, point の前に来る要素番号を出力する.
subroutine interpo_search_2d( x, y, pointx, pointy, i, j ) ! 漸増配列(要素数が増えるごとに値が大きくなる配列)のなかで, ! point の前に来る要素番号を出力する. implicit none real, intent(in) :: x(:) ! 漸増配列 x real, intent(in) :: y(:) ! 漸増配列 y real, intent(in) :: pointx ! この点 x real, intent(in) :: pointy ! この点 y integer, intent(inout) :: i ! pointx の値を越えない最大の値をもつ要素番号 integer, intent(inout) :: j ! pointy の値を越えない最大の値をもつ要素番号 call interpo_search_1d( x, pointx, i ) call interpo_search_1d( y, pointy, j ) end subroutine interpo_search_2d
Subroutine : | |||
x(:) : | real, intent(in)
| ||
y(:) : | real, intent(in)
| ||
z(:) : | real, intent(in)
| ||
pointx : | real, intent(in)
| ||
pointy : | real, intent(in)
| ||
pointz : | real, intent(in)
| ||
i : | integer, intent(inout)
| ||
j : | integer, intent(inout)
| ||
k : | integer, intent(inout)
|
漸増配列(要素数が増えるごとに値が大きくなる配列)のなかで, point の前に来る要素番号を出力する.
subroutine interpo_search_3d( x, y, z, pointx, pointy, pointz, i, j, k ) ! 漸増配列(要素数が増えるごとに値が大きくなる配列)のなかで, ! point の前に来る要素番号を出力する. implicit none real, intent(in) :: x(:) ! 漸増配列 x real, intent(in) :: y(:) ! 漸増配列 y real, intent(in) :: z(:) ! 漸増配列 z real, intent(in) :: pointx ! この点 x real, intent(in) :: pointy ! この点 y real, intent(in) :: pointz ! この点 z integer, intent(inout) :: i ! pointx の値を越えない最大の値をもつ要素番号 integer, intent(inout) :: j ! pointy の値を越えない最大の値をもつ要素番号 integer, intent(inout) :: k ! pointz の値を越えない最大の値をもつ要素番号 call interpo_search_1d( x, pointx, i ) call interpo_search_1d( y, pointy, j ) call interpo_search_1d( z, pointz, k ) end subroutine interpo_search_3d
Subroutine : | |||
tmin : | real, intent(in)
| ||
tmax : | real, intent(in)
| ||
xmin : | real, intent(in)
| ||
xmax : | real, intent(in)
| ||
point : | real, intent(in)
| ||
val : | real, intent(inout)
|
1 次の線形内挿ルーチン
subroutine interpolation_1d( tmin, tmax, xmin, xmax, point, val ) ! 1 次の線形内挿ルーチン implicit none real, intent(in) :: tmin ! 内挿点の左端 real, intent(in) :: tmax ! 内挿点の右端 real, intent(in) :: xmin ! tmin での値 real, intent(in) :: xmax ! tmax での値 real, intent(in) :: point ! 内挿点 real, intent(inout) :: val ! 内挿点での値 real :: fd, dt dt=point-tmin fd=(xmax-xmin)/(tmax-tmin) val=xmin+dt*fd end subroutine interpolation_1d
Subroutine : | |||
x(2) : | real, intent(in)
| ||
y(2) : | real, intent(in)
| ||
z(2,2) : | real, intent(in)
| ||
point(2) : | real, intent(in)
| ||
val : | real, intent(inout)
|
2 次の重線形内挿ルーチン 本ルーチンは直線直交座標空間でのみ使用可能.
subroutine interpolation_2d( x, y, z, point, val ) ! 2 次の重線形内挿ルーチン ! 本ルーチンは直線直交座標空間でのみ使用可能. implicit none real, intent(in) :: x(2) ! 内挿の空間点 x 方向の左右端 real, intent(in) :: y(2) ! 内挿の空間点 y 方向の左右端 real, intent(in) :: z(2,2) ! x, y での各点での値, (i,j) について, i<=x, j<=y real, intent(in) :: point(2) ! 内挿点 point(1)<=x 座標, point(2)<=y 座標 real, intent(inout) :: val ! 内挿点での値 real :: valx(2) ! y(1) での x 方向の内挿点での値 call interpolation_1d( x(1), x(2), z(1,1), z(2,1), point(1), valx(1) ) ! y(2) での x 方向の内挿点での値 call interpolation_1d( x(1), x(2), z(1,2), z(2,2), point(1), valx(2) ) ! x の内挿点からの y 方向の内挿点での値(これが求める内挿点) call interpolation_1d( y(1), y(2), valx(1), valx(2), point(2), val ) end subroutine interpolation_2d
Subroutine : | |||
x(2) : | real, intent(in)
| ||
y(2) : | real, intent(in)
| ||
z(2) : | real, intent(in)
| ||
u(2,2,2) : | real, intent(in)
| ||
point(3) : | real, intent(in)
| ||
val : | real, intent(inout)
|
3 次の重線形内挿ルーチン 本ルーチンは直線直交座標空間でのみ使用可能.
subroutine interpolation_3d( x, y, z, u, point, val ) ! 3 次の重線形内挿ルーチン ! 本ルーチンは直線直交座標空間でのみ使用可能. implicit none real, intent(in) :: x(2) ! 内挿の空間点 x 方向の左右端 real, intent(in) :: y(2) ! 内挿の空間点 y 方向の左右端 real, intent(in) :: z(2) ! 内挿の空間点 z 方向の左右端 real, intent(in) :: u(2,2,2) ! x, y, z での各点での値, (i,j,k) について, i<=x, j<=y, k<=z real, intent(in) :: point(3) ! 内挿点 point(1)<=x 座標, point(2)<=y 座標, point(3)<=z 座標 real, intent(inout) :: val ! 内挿点での値 real :: valx(2) ! z(1) での x-y 平面での重線形内挿の値 call interpolation_2d( x, y, u(:,:,1), point(1:2), valx(1) ) ! z(2) での x 方向の内挿点での値 call interpolation_2d( x, y, u(:,:,2), point(1:2), valx(2) ) ! z(1) の内挿点からの z 方向の内挿点での値(これが求める内挿点) call interpolation_1d( z(1), z(2), valx(1), valx(2), point(3), val ) end subroutine interpolation_3d
Subroutine : | |||
x(:) : | real, intent(in)
| ||
y(size(x)) : | real, intent(in)
| ||
bot : | real, intent(in)
| ||
top : | real, intent(in)
| ||
res : | real, intent(inout)
|
1 次元台形積分 不等間隔でも計算可能であるが, 精度は保証しない.
subroutine rectangle_int( x, y, bot, top, res ) ! 1 次元台形積分 ! 不等間隔でも計算可能であるが, 精度は保証しない. implicit none real, intent(in) :: bot ! 積分区間左端 real, intent(in) :: top ! 積分区間右端 real, intent(in) :: x(:) ! 積分変数 real, intent(in) :: y(size(x)) ! 非積分関数 real, intent(inout) :: res ! 台形積分の積分値 integer :: i, j, k, nx real :: dx nx=size(x) res=0.0 do i=1,nx-1 if(x(i)>=bot.and.x(i)<=top)then ! 積分開始位置を決定 res=res+0.5*(x(i+1)-x(i))*(y(i+1)+y(i)) end if end do end subroutine rectangle_int
Subroutine : | |||
nx : | integer, intent(in)
| ||
x(nx) : | real, intent(in)
| ||
anor : | real, intent(inout)
| ||
error : | real, intent(in), optional
|
1 次元データの標準偏差を計算 標準偏差$sigma $の定義は, $$sigma =sum^{nx}_{i=1}{epsilon ^2} $$ ただし, $epsilon $は平均値からのずれ$x-\bar{x}$である.
subroutine stand_vari( nx, x, anor, error ) ! 1 次元データの標準偏差を計算 ! 標準偏差$\sigma $の定義は, ! $$\sigma =\sum^{nx}_{i=1}{epsilon ^2} $$ ! ただし, $\epsilon $は平均値からのずれ$x-\bar{x}$である. implicit none integer, intent(in) :: nx ! データ数 real, intent(in) :: x(nx) ! データ real, intent(inout) :: anor ! 標準偏差 real, intent(in), optional :: error ! 欠損値 integer :: i real :: an(nx) anor=0.0 if(present(error))then call Anomaly_1d( nx, x, an, error ) do i=1,nx if(x(i)/=error)then anor=anor+an(i)**2 end if end do else call Anomaly_1d( nx, x, an ) do i=1,nx anor=anor+an(i)**2 end do end if end subroutine stand_vari