Keyword: 多変量スチューデントt-分布, 疑似乱数ベクトル
概要
本サンプルは多変量スチューデントt-分布から疑似乱数ベクトルの生成を行うC#によるサンプルプログラムです。 本サンプルは以下に示される確率密度関数をもつ多変量スチューデントt-分布から10個の疑似乱数を生成し出力します。
※本サンプルはnAG Library for .NETに含まれる関数 g05ry() のExampleコードです。本サンプル及び関数の詳細情報は g05ry のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はg05ry のマニュアルページを参照)| この出力例をダウンロード |
g05ry Example Program Results
1.4957 -15.6226 -3.8101 0.1294
-1.0827 -6.7473 0.6696 -0.0391
2.1369 6.3861 -5.7413 0.0140
2.2481 -16.0417 -1.0982 0.1641
-0.2550 3.5166 -0.2541 -0.0592
0.9731 -4.3553 -4.4181 0.0043
0.7098 -3.4281 1.1741 0.0586
1.8827 23.2619 1.5140 -0.0704
0.9904 22.7479 0.1811 -0.0893
1.5026 2.7753 -2.2805 -0.0112
- 4〜13行目に生成された疑似乱数が出力されています。
ソースコード
(本関数の詳細はg05ry のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g05ry Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
public class G05RYE
{
static void Main(String[] args)
{
StartExample();
}
public static void StartExample()
{
try
{
const int mseed=1;
int df, genid, i, j, m, mode, n, subid; m = 4;
n = 10;
double[,] c = new double[m, m];
double[] r = new double[m*(m+1)+2];
double[,] x = new double[n, m];
double[] xmu = new double[m];
int[] seed = new int[mseed];
int ifail;
Console.WriteLine("g05ry Example Program Results");
Console.WriteLine("");Console.WriteLine("");
// Set the number of variables and variates
// 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;
// Set the degrees of freedom
df = 10;
// 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;
// int order = 2;
G05.g05ry(mode, n, df, m, xmu, c,r, g05State, x, out ifail);
if (ifail != 0)
{
Console.WriteLine("** g05ry 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, 10:f4}", x[i - 1 , j - 1]);
}
Console.WriteLine("");
}
//
L40: ;
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine( "Exception Raised");
}
}
}
}
