一変量時系列の標本自己相関関数

C言語によるサンプルソースコード : 使用関数名:nag_tsa_auto_corr (g13abc)

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > 一変量時系列の標本自己相関関数 (C言語/C++)

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

概要

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

自己相関のデータ 

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

入力データ

(本関数の詳細はnag_tsa_auto_corr のマニュアルページを参照)
1
2
3
4
5
6
7
8
9
10
11
12

このデータをダウンロード
nag_tsa_auto_corr (g13abc) 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)を指定しています。

出力結果

(本関数の詳細はnag_tsa_auto_corr のマニュアルページを参照)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

この出力例をダウンロード
nag_tsa_auto_corr (g13abc) 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
     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行目に入力された時系列データの標本分散が出力されています。
  • 9~18行目にはラグと標本自己相関係数が出力されています。
  • 20行目には時系列の真の自己相関関数がゼロであるという仮説を検証するために使用される統計量の値が出力されています。

ソースコード

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

※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

このソースコードをダウンロード
/* nag_tsa_auto_corr (g13abc) Example Program.
 *
 * CLL6I261D/CLL6I261DL Version.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 *
 */

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <nagg13.h>

int main(void)
{

  Integer exit_status = 0, i, nk, nx;
  NagError fail;
  double mean, *r = 0, stat, *x = 0, xv;

  INIT_FAIL(fail);

  printf("nag_tsa_auto_corr (g13abc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld %ld", &nx, &nk);

  if (nk > 0 && nx > 1 && nk < nx) {
    if (!(r = nAG_ALLOC(nk, double)) || !(x = nAG_ALLOC(nx, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  }
  else {
    printf("Invalid nx or nk.\n");
    exit_status = 1;
    return exit_status;
  }
  for (i = 0; i < nx; ++i)
    scanf("%lf", &x[i]);
  printf("\nThe first %2ld coefficients are required\n", nk);

  /* nag_tsa_auto_corr (g13abc).
   * Sample autocorrelation function
   */
  nag_tsa_auto_corr(x, nx, nk, &mean, &xv, r, &stat, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_tsa_auto_corr (g13abc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("The input array has sample mean %12.4f\n", mean);
  printf("The input array has sample variance %12.4f\n", xv);
  printf("The sample autocorrelation coefficients are\n\n");
  printf("   Lag    Coeff\n");
  for (i = 0; i < 10; ++i)
    printf("%6ld%10.4f\n", i + 1, r[i]);
  printf("\nThe value of stat is %12.4f\n", stat);
END:
  nAG_FREE(r);
  nAG_FREE(x);
  return exit_status;
}


関連情報
© 日本ニューメリカルアルゴリズムズグループ株式会社 2025
Privacy Policy  /  Trademarks