Keyword: 多変量正規分布, 疑似乱数ベクトル
概要
本サンプルは多変量正規分布から疑似乱数ベクトルの生成を行うC#によるサンプルプログラムです。 本サンプルは以下に示される確率密度関数をもつ多変量正規分布から10個の疑似乱数を生成し出力します。
※本サンプルはnAG Library for .NETに含まれる関数 g05rz() のExampleコードです。本サンプル及び関数の詳細情報は g05rz のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はg05rz のマニュアルページを参照)| この出力例をダウンロード |
g05rz Example Program Results 1.4534 -14.1206 -3.7410 0.1184 -0.6191 -4.8000 -0.1473 -0.0304 1.8607 5.3206 -5.0753 0.0106 2.0861 -13.6996 -1.3451 0.1428 -0.6326 3.9729 0.5721 -0.0770 0.9754 -3.8162 -4.2978 0.0040 0.6174 -5.1573 2.5037 0.0772 2.0352 26.9359 2.2939 -0.0826 0.9941 14.7700 -1.0421 -0.0549 1.5780 2.8916 -2.1725 -0.0129
- 3〜12行目に生成された疑似乱数が出力されています。
ソースコード
(本関数の詳細はg05rz のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g05rz Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
public class G05RZE
{
static void Main(String[] args)
{
StartExample();
}
public static void StartExample()
{
try
{
const int mseed=1;
int genid, i, j, m, mode, n, subid;
int[] seed = new int[mseed];
int ifail;
Console.WriteLine("g05rz Example Program Results");
Console.WriteLine("");
// Set the number of variables and variates
m = 4;
n = 10;
double[] r = new double[m*(m+1)+1];
double[,] x = new double[n, m];
double[] xmu = new double[m];
double[,] c = new double[m, m];
// Input the upper triangle portion of the covariance matrix
c[0 , 0] = 1.690e0;
c[0 , 1] = 0.390e0;
c[0 , 2] = -1.860e0;
c[0 , 3] = 0.070e0;
c[1 , 0] = 0.390e0;
c[1 , 1] = 98.010e0;
c[1 , 2] = -7.070e0;
c[1 , 3] = -0.710e0;
c[2 , 0] = -1.860e0;
c[2 , 1] = -7.070e0;
c[2 , 2] = 11.560e0;
c[2 , 3] = 0.030e0;
c[3 , 0] = 0.070e0;
c[3, 1] = -0.710e0;
c[3 , 2] = 0.030e0;
c[3 , 3] = 0.010e0;
// Input the means
xmu[0] = 1.00e0;
xmu[1] = 2.00e0;
xmu[2] = -3.00e0;
xmu[3] = 0.00e0;
// Initialise the seed
seed[0] = 1762543;
// genid and subid identify the base generator
genid = 1;
subid = 1;
// Initialise the generator to a repeatable sequence
G05.G05State g05State = new G05.G05State(genid, subid, seed, out ifail);
if (ifail != 0)
{
Console.WriteLine("** Generator initialisation failed with ifail = {0,5}", ifail);
goto L40;
}
// Set up reference vector and generate N numbers
// Choose mode = 2
mode = 2;
G05.g05rz(mode, n, m, xmu, c,r, g05State, x, out ifail);
if (ifail != 0)
{
Console.WriteLine("** g05rz failed with ifail = {0,5}", ifail);
goto L40;
}
// Display the variates
for (i = 1 ; i <= n ; i++)
{
for (j = 1 ; j <= m ; j++)
{
Console.Write(" {0,8:f4} ", x[i - 1 , j - 1]);
}
Console.WriteLine("");
}
//
L40: ;
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine( "Exception Raised");
}
}
}
}
