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 のマニュアルページを参照)| この出力例をダウンロード |
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等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
/* 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;
}
