/* nag_chi_sq_goodness_of_fit_test (g08cgc) Example Program. * * Copyright 2000 Numerical Algorithms Group. * * Mark 6, 2000. * * Mark 8 revised, 2004 * */ #include #include #include #include #include #include int main (void) { Integer exit_status=0, i, iclass, *ifreq=0, init, igen = 0; Integer iseed[] = {0, 0, 0, 0}, n, nclass, ndf, npest; NagError fail; Nag_ClassBoundary class_enum; Nag_Distributions cdist_enum; char cdist[2]; double chisq, *chisqi=0, *cint=0, *eval=0, p, *par=0, *prob=0, *x=0, xmax; double xmin; INIT_FAIL(fail); Vprintf("nag_chi_sq_goodness_of_fit_test (g08cgc) Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n]"); Vscanf("%ld %ld %s %*[^\n] ", &n, &nclass, cdist); if (*cdist == 'U') cdist_enum = Nag_Uniform; else if (*cdist == 'N') cdist_enum = Nag_Normal; else if (*cdist == 'G') cdist_enum = Nag_Gamma; else if (*cdist == 'C') cdist_enum = Nag_ChiSquare; else if (*cdist == 'E') cdist_enum = Nag_Exponential; else if (*cdist == 'A') cdist_enum = Nag_UserProb; else cdist_enum = (Nag_Distributions)-999; if (!(x = NAG_ALLOC(n, double)) || !(cint = NAG_ALLOC(nclass-1, double) ) || !(par = NAG_ALLOC(2, double)) || !(ifreq = NAG_ALLOC(nclass, Integer))) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } for (i = 1; i <= 2; ++i) Vscanf("%lf", &par[i - 1]); npest = 0; /* Generate random numbers from a uniform distribution */ init = 0; /* nag_rngs_init_repeatable (g05kbc). * Initialize seeds of a given generator for random number * generating functions (that pass seeds explicitly) to give * a repeatable sequence */ nag_rngs_init_repeatable(&igen, iseed); /* nag_rngs_uniform (g05lgc). * Generates a vector of random numbers from a uniform * distribution, seeds and generator number passed * explicitly */ nag_rngs_uniform(par[0], par[1], n, x, igen, iseed, NAGERR_DEFAULT); iclass = 0; /* Determine suitable intervals */ if (cdist_enum == Nag_Uniform) { iclass = 1; 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; } if (iclass == 1) class_enum = Nag_ClassBoundaryUser; else class_enum = Nag_ClassBoundaryComp; /* nag_frequency_table (g01aec). * Frequency table from raw data */ nag_frequency_table(n, x, nclass, class_enum, cint, ifreq, &xmin, &xmax, &fail); if (fail.code != NE_NOERROR) { Vprintf("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))) { Vprintf("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_enum, par, npest, prob, &chisq, &p, &ndf, eval, chisqi, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_chi_sq_goodness_of_fit_test (g08cgc).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n"); Vprintf("%s%10.4f\n", "Chi-squared test statistic = ", chisq); Vprintf("%s%5ld\n", "Degrees of freedom. = ", ndf); Vprintf("%s%10.4f\n", "Significance level = ", p); Vprintf("\n"); Vprintf("%s\n", "The contributions to the test statistic are :-"); for (i = 1; i <= nclass; ++i) Vprintf("%10.4f\n", chisqi[i - 1]); END: if (x) NAG_FREE(x); if (cint) NAG_FREE(cint); if (par) NAG_FREE(par); if (ifreq) NAG_FREE(ifreq); if (chisqi) NAG_FREE(chisqi); if (eval) NAG_FREE(eval); if (prob) NAG_FREE(prob); return exit_status; }