Keyword: Dirichlet分布, 疑似乱数ベクトル
概要
本サンプルはDirichlet分布から疑似乱数ベクトルの生成を行うC#によるサンプルプログラムです。 本サンプルは以下に示される確率密度関数で m(次元)=4 かつ α={2.0,2.0,2.0,2.0} の場合のDirichlet分布から5個の疑似乱数を生成し出力します。
※本サンプルはnAG Library for .NETに含まれる関数 g05se() のExampleコードです。本サンプル及び関数の詳細情報は g05se のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はg05se のマニュアルページを参照)| この出力例をダウンロード |
g05se Example Program Results 0.3600 0.3138 0.0837 0.2426 0.2874 0.5121 0.1497 0.0509 0.2286 0.2190 0.3959 0.1566 0.1744 0.3961 0.2764 0.1530 0.1522 0.2845 0.2074 0.3559
- 4〜8行目に生成された疑似乱数が出力されています。
ソースコード
(本関数の詳細はg05se のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g05se Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
public class G05SEE
{
static void Main(String[] args)
{
StartExample();
}
public static void StartExample()
{
try
{
const int mseed=1;
const int n=5;
const int m=4;
int genid, i, j, subid;
double[] a = new double[m];
double[,] x = new double[n, m];
int[] seed = new int[mseed];
int ifail;
Console.WriteLine("g05se Example Program Results");
Console.WriteLine("");Console.WriteLine("");
// Set the distribution parameters A
for (i = 1 ; i <= m ; i++)
{
a[i - 1] = 2.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 L60;
}
// Generate the variates
G05.g05se(n, m, a, g05State, x, out ifail);
if (ifail != 0)
{
Console.WriteLine("** g05se failed with ifail = {0,5}", ifail);
goto L60;
}
// 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("");
}
//
L60: ;
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine( "Exception Raised");
}
}
}
}
