Keyword: カイ二乗適合度, chi-square, goodness of fit test, 検定
概要
本サンプルはカイ二乗適合度検定(the chi-square goodness of fit test) を行うC言語によるサンプルプログラムです。 本サンプルは一様分布から生成される疑似乱数ベクトルを分析対象とし、カイ二乗検定統計量、自由度、有意水準と検定統計量の寄与率を算出します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_chi_sq_goodness_of_fit_test() のExampleコードです。本サンプル及び関数の詳細情報は nag_chi_sq_goodness_of_fit_test のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_chi_sq_goodness_of_fit_test のマニュアルページを参照)| このデータをダウンロード |
nag_chi_sq_goodness_of_fit_test (g08cgc) Example Program Data 100 5 Nag_Uniform :n nclass cdist 0.0 1.0 :par[0] par[2]
- 1行目はタイトル行で読み飛ばされます。
- 2行目に一様分布から生成する乱数の数(n)、階級数(nclass)、検定する分布の種類(cdist)(一様分布)を指定しています。
- 3行目に一様分布の下限端点(par(0))と上限端点(par(1))を指定しています。
出力結果
(本関数の詳細はnag_chi_sq_goodness_of_fit_test のマニュアルページを参照)| この出力例をダウンロード |
nag_chi_sq_goodness_of_fit_test (g08cgc) Example Program Results
Chi-squared test statistic = 4.0000
Degrees of freedom. = 4
Significance level = 0.4060
The contributions to the test statistic are :-
1.8000
1.2500
0.4500
0.0500
0.4500
- 3行目にはカイ二乗検定統計量が出力されています。
- 4行目には自由度が出力されています。
- 5行目には有意水準が出力されています。
- 8行目〜12行目には各階級の検定統計量への寄与率が出力されています。
ソースコード
(本関数の詳細はnag_chi_sq_goodness_of_fit_test のマニュアルページを参照)
※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
/* nag_chi_sq_goodness_of_fit_test (g08cgc) 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 <nagg01.h>
#include <nagg05.h>
#include <nagg08.h>
int main(void)
{
/* Integer scalar and array declarations */
Integer exit_status = 0, i, n, nclass, ndf, npest, lstate;
Integer *ifreq = 0, *state = 0;
/* nAG structures */
Nag_ClassBoundary classb;
Nag_Distributions cdist;
NagError fail;
/* Double scalar and array declarations */
double chisq, *chisqi = 0, *cint = 0, *eval = 0, p, *par = 0;
double *prob = 0, *x = 0, xmax, xmin;
/* Character array declarations */
char nag_enum_arg[40];
/* Choose the base generator */
Nag_BaseRNG genid = Nag_Basic;
Integer subid = 0;
/* Set the seed */
Integer seed[] = { 1762543 };
Integer lseed = 1;
INIT_FAIL(fail);
printf("nag_chi_sq_goodness_of_fit_test (g08cgc) 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]");
scanf("%ld %ld %39s %*[^\n] ", &n, &nclass,
nag_enum_arg);
/* nag_enum_name_to_value (x04nac).
* Converts nAG enum member name to value
*/
cdist = (Nag_Distributions) nag_enum_name_to_value(nag_enum_arg);
if (!(x = nAG_ALLOC(n, double))
|| !(state = nAG_ALLOC(lstate, Integer))
|| !(cint = nAG_ALLOC(nclass - 1, double))
|| !(par = nAG_ALLOC(2, double))
|| !(ifreq = nAG_ALLOC(nclass, Integer)))
{
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
for (i = 1; i <= 2; ++i)
scanf("%lf", &par[i - 1]);
npest = 0;
/* 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 random numbers from a uniform distribution */
nag_rand_uniform(n, par[0], par[1], state, x, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_rand_uniform (g05sqc).\n%s\n", fail.message);
return 1;
}
classb = Nag_ClassBoundaryComp;
/* Determine suitable intervals */
if (cdist == Nag_Uniform) {
classb = Nag_ClassBoundaryUser;
cint[0] = par[0] + (par[1] - par[0]) / nclass;
for (i = 2; i <= nclass - 1; ++i)
cint[i - 1] = cint[i - 2] + (par[1] - par[0]) / nclass;
}
/* nag_frequency_table (g01aec).
* Frequency table from raw data
*/
nag_frequency_table(n, x, nclass, classb, cint, ifreq, &xmin, &xmax, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_frequency_table (g01aec).\n%s\n", fail.message);
return 1;
}
if (!(chisqi = nAG_ALLOC(nclass, double))
|| !(eval = nAG_ALLOC(nclass, double))
|| !(prob = nAG_ALLOC(nclass, double)))
{
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* nag_chi_sq_goodness_of_fit_test (g08cgc).
* Performs the chi^2 goodness of fit test, for standard
* continuous distributions
*/
nag_chi_sq_goodness_of_fit_test(nclass, ifreq, cint, cdist, par, npest,
prob, &chisq, &p, &ndf, eval, chisqi,
&fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_chi_sq_goodness_of_fit_test (g08cgc).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
printf("\n");
printf("%s%10.4f\n", "Chi-squared test statistic = ", chisq);
printf("%s%5ld\n", "Degrees of freedom. = ", ndf);
printf("%s%10.4f\n", "Significance level = ", p);
printf("\n");
printf("%s\n", "The contributions to the test statistic are :-");
for (i = 1; i <= nclass; ++i)
printf("%10.4f\n", chisqi[i - 1]);
END:
nAG_FREE(x);
nAG_FREE(cint);
nAG_FREE(par);
nAG_FREE(ifreq);
nAG_FREE(chisqi);
nAG_FREE(eval);
nAG_FREE(prob);
nAG_FREE(state);
return exit_status;
}
