/* nag_estim_gen_pareto (g07bfc) Example Program. * * Copyright 2009, Numerical Algorithms Group. * * Mark 9, 2009. */ /* Pre-processor includes */ #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin = 0, *fpout = 0; char *outfile = 0; /* Integer scalar and array declarations */ Integer exit_status = 0; Integer i, n; /* Double scalar and array declarations */ double asvc[4], beta, ll, obsvc[4], xi, *y = 0; /* Character scalar and array declarations */ char soptopt[12]; /* NAG types */ NagError fail; Nag_OptimOpt optopt; /* Initialise the error structure */ 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); (void) nag_example_file_io(argc, argv, "-nag_write", &outfile); fprintf(fpout, "nag_estim_gen_pareto (g07bfc) Example Program Results\n\n"); /* Skip header in data file */ fscanf(fpin, "%*[^\n] "); /* Read parameter values */ fscanf(fpin, "%ld%" "s%*[^\n]", &n, soptopt); optopt = (Nag_OptimOpt) nag_enum_name_to_value(soptopt); /* Allocate data array */ if (!(y = NAG_ALLOC(n, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read data values */ for (i = 1; i <= n; i++) fscanf(fpin, "%lf", &y[i - 1]); fscanf(fpin, "%*[^\n]"); /* Calculate the GPD parameter estimates */ nag_estim_gen_pareto(n, y, optopt, &xi, &beta, asvc, obsvc, &ll, &fail); /* Print parameter estimates */ switch (fail.code) { case NE_NOERROR: case NW_PARAM_DIST: case NW_PARAM_DIST_ASYM: case NW_PARAM_DIST_OBS: fprintf(fpout, " Parameter estimates\n"); fprintf(fpout, " %-12s%12.6e\n %-12s%12.6e\n", "xi", xi, "beta", beta); break; default: fprintf(fpout, "Error from nag_estim_gen_pareto (g07bfc).\n%s\n", fail.message); exit_status = -1; goto END; } /* Print parameter distribution */ if (optopt == Nag_MOMMLE || optopt == Nag_PWMMLE) { switch (fail.code) { case NW_PARAM_DIST: case NW_PARAM_DIST_OBS: fprintf(fpout, " %s\n", fail.message); exit_status = -1; break; default: fprintf(fpout, "\n Observed distribution\n"); fprintf(fpout, " %-20s%12.6e\n %-20s%12.6e\n %-20s%12.6e\n", "Var(xi)", obsvc[0], "Var(beta)", obsvc[3], "Covar(xi,beta)", obsvc[1]); fprintf(fpout, "\n Final log-likelihood: %12.6e\n", ll); } } else { switch (fail.code) { case NW_PARAM_DIST: case NW_PARAM_DIST_ASYM: fprintf(fpout, " %s\n", fail.message); exit_status = -1; default: fprintf(fpout, "\n Asymptotic distribution\n"); fprintf(fpout, " %-20s%12.6e\n %-20s%12.6e\n %-20s%12.6e\n", "Var(xi)", asvc[0], "Var(beta)", asvc[3], "Covar(xi,beta)", asvc[1]); } } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (y) NAG_FREE(y); return exit_status; }