Keyword: ランダム相関行列
概要
本サンプルはランダム相関行列の生成を行うC#によるサンプルプログラムです。 本サンプルは0.7、0.9、1.4の固有値をもつ 3x3 のランダム相関行列を生成し出力します。
※本サンプルはnAG Library for .NETに含まれる関数 g05py() のExampleコードです。本サンプル及び関数の詳細情報は g05py のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はg05py のマニュアルページを参照)- 1行目はタイトル行で読み飛ばされます。
- 2行目に生成される相関行列の次数(n)を指定しています。
- 3行目に固有値(d)を指定しています。
出力結果
(本関数の詳細はg05py のマニュアルページを参照)| この出力例をダウンロード |
g05py Example Program Results
1.000 -0.255 -0.100
-0.255 1.000 0.234
-0.100 0.234 1.000
- 3〜5行目に生成されたランダム相関行列が出力されています。
ソースコード
(本関数の詳細はg05py のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g05py Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
public class G05PYE
{
static bool defaultdata = true;
static string datafile = "";
static void Main(String[] args)
{
if (args.Length == 1)
{
defaultdata = false;
datafile = args[0];
}
StartExample();
}
public static void StartExample()
{
try
{
const int mseed=1;
DataReader sr;
if (defaultdata == true)
{
sr = new DataReader("exampledata/g05pye.d");
}
else
{
sr = new DataReader(datafile);
}
double eps=0.0;
int genid, i, j, n, subid;
int[] seed = new int[mseed];
int ifail;
Console.WriteLine("g05py Example Program Results");
Console.WriteLine("");
// Read data from a file
// Skip heading
sr.Reset();
// Read in initial parameters
sr.Reset();
n = int.Parse(sr.Next());
double[,] c = new double[n, n];
double[] d = new double[n];
// Check the specified array limits
if (n < 0)
{
Console.WriteLine(" ** Problem size is too small.");
goto L40;
}
// Read in the eigenvalues
sr.Reset();
for (i = 1 ; i <= n ; i++)
{
d[i - 1] = double.Parse(sr.Next());
}
// Set the size of C and tolerance
eps = 0.000010e0;
// 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;
}
// Generate the correlation matrix
G05.g05py(n, d, eps, g05State, c, out ifail);
if (ifail != 0)
{
Console.WriteLine("** g05py failed with ifail = {0,5}", ifail);
goto L40;
}
// Display the results
for (i = 1 ; i <= n ; i++)
{
for (j = 1 ; j <= n ; j++)
{
Console.Write(" {0,8:f3}", c[i - 1 , j - 1]);
}
Console.WriteLine("");
}
//
L40: ;
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine( "Exception Raised");
}
}
}
}
