/* 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 int main(void) { Integer exit_status=0, i, increment, init, *isort=0, j, n, ns; NagError fail; double enda, endb, high, low, *s=0, *smooth=0, window, *x=0; INIT_FAIL(fail); Vprintf("nag_kernel_density_estim (g10bac) Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n] "); Vscanf("%lf ", &window); Vscanf("%lf, %lf", &low, &high); /* Generate Normal (0,1) Distribution */ n = 1000; ns = 100; if (!(x = NAG_ALLOC(n, double)) || !(s = NAG_ALLOC(ns, double)) || !(smooth = NAG_ALLOC(ns, double)) || !(isort = NAG_ALLOC(ns, Integer))) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } init = 0; /* nag_random_init_repeatable (g05cbc). * Initialize random number generating functions to give * repeatable sequence */ nag_random_init_repeatable(init); enda = 0.0; endb = 1.0; for (i = 0; i < n; i++) /* nag_random_normal (g05ddc). * Pseudo-random real numbers, Normal distribution */ x[i] = nag_random_normal(enda, endb); /* 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) { Vprintf("Error from nag_kernel_density_estim (g10bac).\n%s\n", fail.message); exit_status = 1; goto END; } printf(" Points Density Points Density Points " "Density Points Density\n"); printf(" Value Value " "Value Value\n\n"); increment = 25; for (i=1; i<= ns/4; i++) { printf("%10.4f %10.4f", s[i-1], smooth[i-1]); for (j=1; j <= 3; j++) { printf("%10.4f %10.4f", s[i-1+j*increment], smooth[i-1+j*increment]); } printf("\n"); } END: if (x) NAG_FREE(x); if (s) NAG_FREE(s); if (smooth) NAG_FREE(smooth); if (isort) NAG_FREE(isort); return exit_status; }