Keyword: 一様準乱数列
概要
本サンプルは一様準乱数列の生成を行うC#によるサンプルプログラムです。 本サンプルは一様準乱数列を生成し以下に示される積分を推定し出力します。
※本サンプルはnAG Library for .NETに含まれる関数 g05ym() のExampleコードです。本サンプル及び関数の詳細情報は g05ym のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はg05ym のマニュアルページを参照)| この出力例をダウンロード |
g05ym Example Program Results Value of integral = 1.0410 First 10 variates 1 0.7197 0.5967 0.0186 0.1768 0.7803 0.4072 0.5459 0.3994 2 0.9697 0.3467 0.7686 0.9268 0.5303 0.1572 0.2959 0.1494 3 0.4697 0.8467 0.2686 0.4268 0.0303 0.6572 0.7959 0.6494 4 0.3447 0.4717 0.1436 0.3018 0.1553 0.7822 0.4209 0.0244 5 0.8447 0.9717 0.6436 0.8018 0.6553 0.2822 0.9209 0.5244 6 0.5947 0.2217 0.3936 0.0518 0.9053 0.0322 0.1709 0.7744 7 0.0947 0.7217 0.8936 0.5518 0.4053 0.5322 0.6709 0.2744 8 0.0635 0.1904 0.0498 0.4580 0.6240 0.2510 0.9521 0.8057 9 0.5635 0.6904 0.5498 0.9580 0.1240 0.7510 0.4521 0.3057 10 0.8135 0.4404 0.2998 0.2080 0.3740 0.5010 0.7021 0.0557
- 3行目に推定された積分の値が出力されています。
- 7〜16行目に生成された一様準乱数列の最初の10行が出力されています。
ソースコード
(本関数の詳細はg05ym のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g05ym Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
public class G05YME
{
static void Main(String[] args)
{
StartExample();
}
public static void StartExample()
{
try
{
const int n=200;
const int midim=8;
const int ordrc=1;
const int liref=32 * midim + 7;
double sum, tmp, vsbl; int d, genid, i, idim, iskip, j;
int[] iref = new int[liref];
int ifail;
Console.WriteLine("g05ym Example Program Results");
idim = midim;
double[,] quas = new double[idim, n];
// Skip the first few variates in the sequence
iskip = 1000;
// Initialise the Sobol generator
genid = 1;
G05.g05yl(genid, idim, iref, iskip, out ifail);
if (ifail != 0)
{
Console.WriteLine("** g05yl failed with ifail = {0,5}", ifail);
goto L80;
}
// Generate N quasi-random variates
G05.g05ym(n, ordrc, quas, iref, out ifail);
if (ifail != 0)
{
Console.WriteLine("** g05ym failed with ifail = {0,5}", ifail);
goto L80;
}
// Evaluate the function, and sum
sum = 0.00e0;
for (i = 1 ; i <= n ; i++)
{
tmp = 1.00e0;
for (d = 1 ; d <= idim ; d++)
{
tmp = tmp * Math.Abs(4.00e0 * quas[d - 1 , i - 1] - 2.00e0);
}
sum = sum + tmp;
}
// Convert sum to mean value
vsbl = sum / (double)n;
Console.WriteLine(" ");
Console.WriteLine(" {0}{1,8:f4}","Value of integral = ",vsbl);
// Dump the first 10 variates
Console.WriteLine(" ");
Console.WriteLine(" {0}","First 10 variates");
Console.WriteLine("");
for (i = 1 ; i <= 10 ; i++)
{
Console.Write(" {0,3} ", i);
for (j = 1 ; j <= idim ; j++)
{
Console.Write(" {0,8:f4}", quas[j - 1 , i - 1]);
}
Console.WriteLine("");
}
//
L80: ;
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine( "Exception Raised");
}
}
}
}
