Keyword: コルモゴロフ・スミルノフ, Kolmogorov-Smirnov Test, 検定, 2標本
概要
本サンプルは2標本コルモゴロフ・スミルノフ検定(the two sample Kolmogorov-Smirnov Test) を行うC言語によるサンプルプログラムです。 本サンプルはプログラムの中で生成される独立した2つの標本を分析対象とし、検定統計量DとZ統計量、裾確率を算出します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_2_sample_ks_test() のExampleコードです。本サンプル及び関数の詳細情報は nag_2_sample_ks_test のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_2_sample_ks_test のマニュアルページを参照)- 1行目はタイトル行で読み飛ばされます。
- 2行目にプログラムで生成する二つの標本(一様乱数ベクトル)のサイズ(nとm (本関数の引数n1とn2))を指定しています。
- 3行目に計算する検定統計量の種類(ntype (本関数の引数dtype))を指定しています。
出力結果
(本関数の詳細はnag_2_sample_ks_test のマニュアルページを参照)1 2 3 4 5
この出力例をダウンロード |
nag_2_sample_ks_test (g08cdc) Example Program Results Test statistic D = 0.1800 Z statistic = 0.0312 Tail probability = 0.2222
- 3行目には検定統計量Dが出力されています。
- 4行目にはZ統計量が出力されています。
- 5行目には裾確率が出力されています。
ソースコード
(本関数の詳細はnag_2_sample_ks_test のマニュアルページを参照)
※本サンプルソースコードは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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
このソースコードをダウンロード |
/* nag_2_sample_ks_test (g08cdc) Example Program. * * CLL6I261D/CLL6I261DL Version. * * Copyright 2017 Numerical Algorithms Group. * * Mark 26.1, 2017. * * */ #include <stdio.h> #include <nag.h> #include <nag_stdlib.h> #include <nagg05.h> #include <nagg08.h> int main(void) { /* Integer scalar and array declarations */ Integer exit_status = 0; Integer m, n, lstate; Integer *state = 0; /* Double scalar and array declarations */ double d, p, z; double *x = 0, *y = 0; /* Character array declarations */ char nag_enum_arg[40]; /* nAG structures and data types */ Nag_TestStatistics ntype; NagError fail; /* Choose the base generator */ Nag_BaseRNG genid = Nag_Basic; Integer subid = 0; /* Set the seed */ Integer seed[] = { 423523 }; Integer lseed = 1; /* Initialize the error structure */ INIT_FAIL(fail); printf("nag_2_sample_ks_test (g08cdc) Example Program Results\n"); /* Get the length of the state array */ lstate = -1; nag_rand_init_repeatable(genid, subid, seed, lseed, state, &lstate, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_rand_init_repeatable (g05kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Skip heading in data file */ scanf("%*[^\n]"); /* Get the problem size */ scanf("%ld %ld", &n, &m); /* Allocate the arrays */ if (!(x = nAG_ALLOC(n, double)) || !(state = nAG_ALLOC(lstate, Integer)) || !(y = nAG_ALLOC(m, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } printf("\n"); /* Initialize the generator to a repeatable sequence */ nag_rand_init_repeatable(genid, subid, seed, lseed, state, &lstate, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_rand_init_repeatable (g05kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Generate vector of n uniform variates between 0 and 2 */ nag_rand_uniform(n, 0.0, 2.0, state, x, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_rand_uniform (g05sqc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Generate vector of m uniform variates between 0.25 and 2.25 */ nag_rand_uniform(m, 0.25, 2.25, state, y, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_rand_uniform (g05sqc).\n%s\n", fail.message); exit_status = 1; goto END; } scanf("%39s", nag_enum_arg); /* nag_enum_name_to_value (x04nac). * Converts nAG enum member name to value */ ntype = (Nag_TestStatistics) nag_enum_name_to_value(nag_enum_arg); /* nag_2_sample_ks_test (g08cdc). * Performs the two-sample Kolmogorov-Smirnov test */ nag_2_sample_ks_test(n, x, m, y, ntype, &d, &z, &p, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_2_sample_ks_test (g08cdc).\n%s\n", fail.message); exit_status = 1; goto END; } printf("Test statistic D = %8.4f\n", d); printf("Z statistic = %8.4f\n", z); printf("Tail probability = %8.4f\n", p); END: nAG_FREE(x); nAG_FREE(y); nAG_FREE(state); return exit_status; }