C#による 一変量時系列の標本自己相関関数

C#によるサンプルソースコード : 使用関数名:g13ab

Keyword: 一変量時系列, 標本自己相関関数, ACF

概要

本サンプルは一変量時系列の標本自己相関関数の計算を行うC#によるサンプルプログラムです。 本サンプルは以下に示される時系列データを分析し、標本自己相関係数、標本平均や標本分散を出力します。

自己相関のデータ 

※本サンプルはnAG Library for .NETに含まれる関数 g13ab() のExampleコードです。本サンプル及び関数の詳細情報は g13ab のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで

入力データ

(本関数の詳細はg13ab のマニュアルページを参照)

このデータをダウンロード
g13ab Example Program Data
 50 10
   5.0  11.0  16.0  23.0  36.0
  58.0  29.0  20.0  10.0   8.0
   3.0   0.0   0.0   2.0  11.0
  27.0  47.0  63.0  60.0  39.0
  28.0  26.0  22.0  11.0  21.0
  40.0  78.0 122.0 103.0  73.0
  47.0  35.0  11.0   5.0  16.0
  34.0  70.0  81.0 111.0 101.0
  73.0  40.0  20.0  16.0   5.0
  11.0  22.0  40.0  60.0  80.9 

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目に時系列データの数(nx)、自己相関が必要なラグの数(nk)を指定しています。
  • 3〜12行目に時系列データ(x)を指定しています。

出力結果

(本関数の詳細はg13ab のマニュアルページを参照)

この出力例をダウンロード
g13ab Example Program Results

  The first 10 coefficients are required
  The input array has sample mean      37.4180
  The input array has sample variance    1002.0301
 The sample autocorrelation coefficients are

      Lag  Coeff           Lag  Coeff
     
  1  0.8004            2  0.4355          
       3  0.0328            4 -0.2835          
       5 -0.4505            6 -0.4242          
       7 -0.2419            8  0.0550          
       9  0.3783           10  0.5857          
     

  The value of STAT is      92.1231

  • 3行目にラグの数(この場合は10個)の自己相関係数が必要であることが示されています。
  • 4行目に入力された時系列データの標本平均が出力されています。
  • 5行目に入力された時系列データの標本分散が出力されています。
  • 8〜14行目にはラグと標本自己相関係数が出力されています。
  • 17行目には時系列の真の自己相関関数がゼロであるという仮説を検証するために使用される統計量の値が出力されています。

ソースコード

(本関数の詳細はg13ab のマニュアルページを参照)

※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法


このソースコードをダウンロード
//      g13ab Example Program Text
//      C# version, nAG Copyright 2008
using System;
using NagLibrary;
namespace NagDotNetExamples
{
  public class G13ABE
  {
    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
      {
        DataReader sr = null;
        if (defaultdata)
        {
          sr = new DataReader("exampledata/g13abe.d");
        }
        else
        {
          sr = new DataReader(datafile);
        }
        double stat,   xm,   xv; int i,  nk,  nx;
        int ifail;
        Console.WriteLine("g13ab Example Program Results");
        //      Skip heading in data file
        sr.Reset();
        sr.Reset();
        nx = int.Parse(sr.Next());
        nk = int.Parse(sr.Next());
        double[] r = new double[nk];
        double[] x = new double[nx];
        Console.WriteLine("");
        if (nk > 0 && nx > 0)
        {
          sr.Reset();
          for (i = 1; i <= nx; i++)
          {
            x[i - 1] = double.Parse(sr.Next());
          }
          Console.WriteLine("  {0}{1,2}{2}", "The first ", nk, " coefficients are required");
          //
          G13.g13ab(x, nx, nk, out xm, out xv, r, out stat, out ifail);
          //
          Console.WriteLine("  {0}{1,12:f4}", "The input array has sample mean ", xm);
          Console.WriteLine("  {0}{1,12:f4}", "The input array has sample variance ", xv);
          Console.WriteLine(" {0}", "The sample autocorrelation coefficients are");
          Console.WriteLine("");
          Console.WriteLine(" {0}", "     Lag  Coeff           Lag  Coeff");
          Console.WriteLine("     ");
          for (i = 1; i <= nk; i++)
          {
            Console.Write("{0,3}{1,8:f4}          {2}", i, r[i - 1], i % 2 == 0 || i == 10 ? "\n     " : "");
          }
          Console.WriteLine(""); ;
          Console.WriteLine("");
          Console.WriteLine("  {0}{1,12:f4}", "The value of STAT is ", stat);
        }
        //
      }
      catch (Exception e)
      {
        Console.WriteLine(e.Message);
        Console.WriteLine("Exception Raised");
      }
    }
  }
}


関連情報
Privacy Policy  /  Trademarks