/* nag_kernel_density_estim (g10bac) Example Program. * * Copyright 2000 Numerical Algorithms Group. * * Mark 6, 2000. * Mark 7b revised, 2004. */ #include #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; /* Integer scalar and array declarations */ Integer exit_status = 0, i, increment, j, lstate; Integer *state = 0, *isort = 0; /* NAG structures */ NagError fail; /* Double scalar and array declarations */ double high, low, window; double *s = 0, *smooth = 0, *x = 0; /* Choose the base generator */ Nag_BaseRNG genid = Nag_Basic; Integer subid = 0; /* Set the seed */ Integer seed[] = { 1762543 }; Integer lseed = 1; /* Set the distribution parameters for the simulated data */ double xmu = 0.0e0; double var = 1.0e0; /* Generate 1000 data points in the simulated data */ Integer n = 1000; /* Number of points at which to estimate density */ Integer ns = 100; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_kernel_density_estim (g10bac) 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) { fprintf(fpout, "Error from nag_rand_init_repeatable (g05kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Allocate some memory for the arrays */ if (!(x = NAG_ALLOC(n, double)) || !(s = NAG_ALLOC(ns, double)) || !(state = NAG_ALLOC(lstate, Integer)) || !(smooth = NAG_ALLOC(ns, double)) || !(isort = NAG_ALLOC(ns, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Initialise the generator to a repeatable sequence */ nag_rand_init_repeatable(genid, subid, seed, lseed, state, &lstate, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_init_repeatable (g05kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Generate the variates */ nag_rand_normal(n, xmu, var, state, x, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_normal (g05skc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Skip heading in data file */ fscanf(fpin, "%*[^\n] "); /* Read in the windowing information */ fscanf(fpin, "%lf ", &window); fscanf(fpin, "%lf, %lf", &low, &high); /* Perform kernel density estimation */ /* nag_kernel_density_estim (g10bac). * Kernel density estimate using Gaussian kernel */ nag_kernel_density_estim(n, x, window, low, high, ns, smooth, s, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_kernel_density_estim (g10bac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n Points Density Points Density Points " "Density Points Density\n"); fprintf(fpout, " Value Value " "Value Value\n\n"); increment = 25; for (i = 1; i <= ns/4; i++) { fprintf(fpout, "%9.4f %7.4f", s[i-1], smooth[i-1]); for (j = 1; j <= 3; j++) { fprintf(fpout, "%9.4f %7.4f", s[i-1+j*increment], smooth[i-1+j*increment]); } fprintf(fpout, "\n"); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (x) NAG_FREE(x); if (s) NAG_FREE(s); if (smooth) NAG_FREE(smooth); if (isort) NAG_FREE(isort); if (state) NAG_FREE(state); return exit_status; }