Keyword: 多変量正規分布, 疑似乱数ベクトル
概要
本サンプルは多変量正規分布から疑似乱数ベクトルの生成を行うFortranによるサンプルプログラムです。 本サンプルは以下に示される確率密度関数をもつ多変量正規分布から10個の疑似乱数を生成し出力します。
※本サンプルはnAG Fortranライブラリに含まれるルーチン g05rzf() のExampleコードです。本サンプル及びルーチンの詳細情報は g05rzf のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本ルーチンの詳細はg05rzf のマニュアルページを参照)| このデータをダウンロード |
G05RZF Example Program Data
1 1 1762543 :: GENID,SUBID,SEED(1)
10 4 :: N,M
1.0 2.0 -3.0 0.0 :: XMU
1.69 0.39 -1.86 0.07
98.01 -7.07 -0.71
11.56 0.03
0.01 :: End of C (upper triangular part)
- 1行目はタイトル行で読み飛ばされます。
- 2行目に使用する生成器(genid=1:nAG基本生成器)、生成器に関する情報(subid=1:GENIDが1の場合この値は参照されません)、生成器の初期値(seed=1762543)を指定しています。
- 3行目に生成される乱数の数(n=10)と次元の数(m=4)を指定しています。
- 4行目に分布の平均ベクトル(xmu)を指定しています。
- 5〜8行目に分布の共分散行列の上三角部分(c)を指定しています。
出力結果
(本ルーチンの詳細はg05rzf のマニュアルページを参照)| この出力例をダウンロード |
G05RZF Example Program Results
Variates
1 2 3 4
1 1.4534 -14.1206 -3.7410 0.1184
2 -0.6191 -4.8000 -0.1473 -0.0304
3 1.8607 5.3206 -5.0753 0.0106
4 2.0861 -13.6996 -1.3451 0.1428
5 -0.6326 3.9729 0.5721 -0.0770
6 0.9754 -3.8162 -4.2978 0.0040
7 0.6174 -5.1573 2.5037 0.0772
8 2.0352 26.9359 2.2939 -0.0826
9 0.9941 14.7700 -1.0421 -0.0549
10 1.5780 2.8916 -2.1725 -0.0129
- 5〜14行目に生成された疑似乱数が出力されています。
ソースコード
(本ルーチンの詳細はg05rzf のマニュアルページを参照)
※本サンプルソースコードは科学技術・統計計算ライブラリである「nAG Fortranライブラリ」のルーチンを呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
PROGRAM g05rzfe
! G05RZF Example Program Text
! Mark 23 Release. nAG Copyright 2011.
! .. Use Statements ..
USE nag_library, ONLY : g05kff, g05rzf, nag_wp, x04caf
! .. Implicit None Statement ..
IMPLICIT NONE
! .. Parameters ..
INTEGER, PARAMETER :: lseed = 1, nin = 5, nout = 6
! .. Local Scalars ..
INTEGER :: genid, i, ifail, ldc, ldx, lr, &
lstate, m, mode, n, subid
! .. Local Arrays ..
REAL (KIND=nag_wp), ALLOCATABLE :: c(:,:), r(:), x(:,:), xmu(:)
INTEGER :: seed(lseed)
INTEGER, ALLOCATABLE :: state(:)
! .. Executable Statements ..
WRITE (nout,*) 'G05RZF Example Program Results'
WRITE (nout,*)
FLUSH (nout)
! Skip heading in data file
READ (nin,*)
! Read in the base generator information and seed
READ (nin,*) genid, subid, seed(1)
! Initial call to initialiser to get size of STATE array
lstate = 0
ALLOCATE (state(lstate))
ifail = 0
CALL g05kff(genid,subid,seed,lseed,state,lstate,ifail)
! Reallocate STATE
DEALLOCATE (state)
ALLOCATE (state(lstate))
! Initialize the generator to a repeatable sequence
ifail = 0
CALL g05kff(genid,subid,seed,lseed,state,lstate,ifail)
! Read in sample size and number of dimensions
READ (nin,*) n, m
ldc = m
ldx = n
lr = m*(m+1) + 1
ALLOCATE (x(ldx,m),c(ldc,m),r(lr),xmu(m))
! Read in vector of means
READ (nin,*) xmu(1:m)
! Read in upper triangle portion of the covariance matrix
DO i = 1, m
READ (nin,*) c(i,i:m)
END DO
! Using a single call to G05RZF, so set up reference vector
! and generate values in one go
mode = 2
! Generate variates
ifail = 0
CALL g05rzf(mode,n,m,xmu,c,ldc,r,lr,state,x,ldx,ifail)
! Display the variates
ifail = 0
CALL x04caf('General',' ',n,m,x,ldx,'Variates',ifail)
END PROGRAM g05rzfe
