1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
|
/* gretl - The Gnu Regression, Econometrics and Time-series Library
* Copyright (C) 1999-2002 Allin Cottrell
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* nistcheck -- program to check libgretl estimation functions against
the NIST reference datasets for linear regression. */
#include "libgretl.h"
#include "version.h"
#include <string.h>
#include <float.h>
#ifdef LONGLEY_ONLY
# define MAX_DIGITS 12
#else
# define MAX_DIGITS 12
#endif
#define MIN_DIGITS 4
#define MP_CHECK_DIGITS 12
#define MP_PRINT_DIGITS 15
#define LIBGRETLSTR "Standard libgretl:"
static int verbose;
static int noint;
static char datadir[FILENAME_MAX];
typedef struct mp_results_ mp_results;
struct mp_results_ {
int ncoeff;
double *coeff;
double *sderr;
double sigma;
double ess;
double rsq;
double fstt;
};
void free_mp_results (mp_results *mpvals)
{
if (mpvals != NULL) {
free(mpvals->coeff);
free(mpvals->sderr);
free(mpvals);
}
}
mp_results *mp_results_new (int nc)
{
mp_results *mpvals;
int i;
mpvals = malloc(sizeof *mpvals);
if (mpvals == NULL) {
return NULL;
}
mpvals->coeff = NULL;
mpvals->sderr = NULL;
mpvals->ncoeff = nc;
mpvals->coeff = malloc(nc * sizeof *mpvals->coeff);
mpvals->sderr = malloc(nc * sizeof *mpvals->sderr);
if (mpvals->coeff == NULL ||
mpvals->sderr == NULL) {
free_mp_results(mpvals);
return NULL;
}
for (i=0; i<nc; i++) {
mpvals->coeff[i] = NADBL;
mpvals->sderr[i] = NADBL;
}
mpvals->sigma = mpvals->ess = NADBL;
mpvals->rsq = mpvals->fstt = NADBL;
return mpvals;
}
/* special stuff:
Noint1, NoInt2: no intercept, use alternative R^2 calculation
Filip: create powers of x, 2 through 10
Wampler1 - Wampler5: create powers of x, 2 to 5
*/
static int get_data_digits (const char *s)
{
int digits = 0;
while (*s == '-' || *s == '.' || *s == ',' ||
isdigit((unsigned char) *s)) {
if (isdigit((unsigned char) *s)) digits++;
s++;
}
return digits;
}
static int grab_nist_data (FILE *fp, DATASET *dset,
int *zdigits, int polyterms,
PRN *prn)
{
double xx;
int i, t, dit;
int realvars = dset->v - polyterms;
char numstr[64];
if (verbose > 1) {
pputs(prn, "\nGetting data...\n\n");
}
for (i=1; i<realvars; i++) {
if (i == 1) {
strcpy(dset->varname[i], "y");
} else if (realvars > 3) {
sprintf(dset->varname[i], "x%d", i - 1);
} else {
strcpy(dset->varname[i], "x");
}
if (verbose > 1) {
pprintf(prn, "reading variable %d as '%s'\n",
i, dset->varname[i]);
}
}
for (t=0; t<dset->n; t++) {
for (i=1; i<realvars; i++) {
if (fscanf(fp, "%s", numstr) != 1) {
pputs(prn, "Data ended prematurely\n");
return 1;
} else {
if (zdigits != NULL) {
dit = get_data_digits(numstr);
if (dit > zdigits[i]) {
zdigits[i] = dit;
}
}
xx = atof(numstr);
}
dset->Z[i][t] = xx;
}
}
return 0;
}
static int grab_mp_results (FILE *fp, mp_results *certvals,
int nlines, PRN *prn)
{
int i = 0, lcount = 0, check;
char line[MAXLEN];
if (verbose > 1) {
pputs(prn, "\nGetting certified values...\n\n");
}
for (lcount=0; lcount<nlines; lcount++) {
if (fgets(line, MAXLEN-1, fp) == NULL) {
pputs(prn, "Results ended prematurely\n");
return 1;
}
if (sscanf(line, " B%d %lf %lf", &check, &certvals->coeff[i],
&certvals->sderr[i]) == 3) {
if (verbose > 1) {
pprintf(prn, " B%d: coeff = %.10g, std. error = %.10g\n",
check, certvals->coeff[i], certvals->sderr[i]);
}
i++;
}
if (na(certvals->sigma) &&
sscanf(line, " Standard Deviation %lf", &certvals->sigma) == 1) {
if (verbose > 1) {
pprintf(prn, " sigma = %.10g\n", certvals->sigma);
}
}
if (na(certvals->rsq) &&
sscanf(line, " R-Squared %lf", &certvals->rsq) == 1) {
if (verbose > 1) {
pprintf(prn, " R^2 = %.10g\n", certvals->rsq);
}
}
if (na(certvals->fstt) &&
sscanf(line, "Regression %*d %*f %*f %lf", &certvals->fstt) == 1) {
if (verbose > 1) {
pprintf(prn, " F = %.10g\n", certvals->fstt);
}
}
if (na(certvals->ess) &&
sscanf(line, "Residual %*d %lf %*f", &certvals->ess) == 1) {
if (verbose > 1) {
pprintf(prn, " ESS = %.10g\n", certvals->ess);
}
}
}
return 0;
}
static
void get_difficulty_level (const char *line, char *s)
{
size_t i, len;
while (isspace((unsigned char) *line)) line++;
strncat(s, line, 47);
len = strlen(s);
for (i=len-1; i>0; i--) {
if (isspace((unsigned char) s[i])) s[i] = 0;
else break;
}
}
#define mylog10(x) (log(x) / 2.3025850929940459)
static double log_error (double q, double c, PRN *prn)
{
double le = 0.0;
int lae = 0;
if (q == c) {
le = 15.0;
} else if (isinf(c)) {
/* certval is inf (e.g. F-stat in some cases):
can't really handle this? */
if (na(q) || isinf(q)) {
le = 15.0;
} else {
le = -log(0);
}
} else if (c == 0.0) {
le = -mylog10(fabs(q));
} else {
le = -mylog10(fabs(q - c) / fabs(c));
}
if (isnan(le)) {
pprintf(prn, "q = %g, c = %g\n", q, c);
} else {
pprintf(prn, "%10.3f %s\n", le, (lae)? "(log abs error)" : "");
}
return le;
}
static int allocate_data_digits (const DATASET *dinfo, int **zdigits)
{
int *zd;
int i;
zd = malloc(dinfo->v * sizeof *zd);
if (zd == NULL) return 1;
for (i=0; i<dinfo->v; i++) {
zd[i] = 0;
}
*zdigits = zd;
return 0;
}
static int read_nist_file (const char *fname,
DATASET **pdset,
mp_results **pcertvals,
int *polyterms,
int **zdigits,
PRN *prn)
{
FILE *fp;
char *p, line[MAXLEN], difficulty[48];
gchar *fullname;
int cstart = 0, cstop = 0;
int dstart = 0, dstop = 0;
int lcount = 0, nvar = 0, nobs = 0;
DATASET *dset = NULL;
mp_results *certvals = NULL;
int i, t, npoly = 0;
#ifdef WIN32
fullname = g_strdup_printf("%s\\%s", datadir, fname);
fp = gretl_fopen(fullname, "r");
#else
fullname = g_strdup_printf("%s/%s", datadir, fname);
fp = gretl_fopen(fullname, "r");
#endif
g_free(fullname);
if (fp == NULL) {
pprintf(prn, "Couldn't open %s\n", fname);
return 1;
}
pprintf(prn, "\n *** %s ***\n", fname);
if (verbose) pputc(prn, '\n');
/* allow for generated data: powers of x */
if (strstr(fname, "Pontius")) {
npoly = 1;
}
if (strstr(fname, "Filip")) {
npoly = 9;
}
if (strstr(fname, "Wampler")) {
npoly = 4;
}
if (strstr(fname, "NoInt")) {
noint = 1;
} else {
noint = 0;
}
*difficulty = 0;
while (fgets(line, MAXLEN-1, fp)) {
lcount++;
/* level of difficulty? */
if (*difficulty == 0 && strstr(line, "Level of Difficulty")) {
get_difficulty_level(line, difficulty);
if (*difficulty) {
pprintf(prn, "(\"%s\")\n", difficulty);
}
}
/* where are the certified results? */
if (cstart == 0 && (p = strstr(line, "Certified")) != NULL) {
if (sscanf(p, "Certified Values (lines %d to %d)",
&cstart, &cstop) == 2) {
;
}
}
/* where are the data? */
if (dstart == 0 && (p = strstr(line, "Data")) != NULL) {
if (sscanf(p, "Data (lines %d to %d)",
&dstart, &dstop) == 2) {
;
}
}
/* how many variables are there? */
if (nvar == 0 && (p = strstr(line, "Predictor")) != NULL) {
if (sscanf(line, "%d Predictor Variable", &nvar) == 1) {
nvar++;
if (verbose) pprintf(prn, " Number of variables: %d\n", nvar);
}
}
/* how many observations are there? */
if (nobs == 0 && (p = strstr(line, "Observations")) != NULL) {
if (sscanf(line, "%d Observations", &nobs) == 1) {
if (verbose) pprintf(prn, " Number of observations: %d\n", nobs);
}
}
/* allocate results struct once we know its size */
if (nvar > 0 && nobs > 0 && certvals == NULL) {
int nc = nvar - 1 + npoly;
if (!noint) nc++;
certvals = mp_results_new(nc);
if (certvals == NULL) {
fclose(fp);
return 1;
}
}
/* allocate data matrix once we know its size */
if (nvar > 0 && nobs > 0 && dset == NULL) {
dset = create_auxiliary_dataset(nvar + 1 + npoly,
nobs, 0);
if (dset == NULL) {
free_mp_results(certvals);
fclose(fp);
return 1;
}
if (allocate_data_digits(dset, zdigits)) {
free_mp_results(certvals);
free_datainfo(dset);
fclose(fp);
return 1;
}
}
/* read the certified results */
if (cstart > 0 && lcount == cstart - 1) {
if (certvals == NULL) {
pputs(prn, "Results coming but storage is not "
"allocated: file is problematic\n");
fclose(fp);
return 1;
} else {
int nlines = cstop - cstart + 1;
if (grab_mp_results(fp, certvals, nlines, prn)) {
fclose(fp);
return 1;
}
lcount += nlines;
}
}
/* read the data */
if (dstart > 0 && lcount == dstart - 1) {
if (dset == NULL || dset->Z == NULL) {
pputs(prn, "Data coming but data matrix is not "
"allocated: file is problematic\n");
fclose(fp);
return 1;
} else if (grab_nist_data(fp, dset, *zdigits, npoly, prn)) {
fclose(fp);
return 1;
}
} /* end if ready to grab data */
} /* end main fgets loop */
if (verbose > 1) {
if (dset != NULL && dset->Z != NULL) {
int i, t;
for (t=0; t<nobs; t++) {
for (i=1; i<=nvar; i++) {
pprintf(prn, "%#.20g", dset->Z[i][t]);
pputc(prn, ((i == nvar)? '\n' : ' '));
}
}
}
}
if (npoly && verbose) {
pputc(prn, '\n');
}
for (i=2; i<=npoly+1; i++) {
if (verbose) {
pprintf(prn, "Generating var %d, 'x^%d' = x ** %d\n", i+1, i, i);
}
sprintf(dset->varname[i+1], "x^%d", i);
for (t=0; t<dset->n; t++) {
dset->Z[i+1][t] = pow(dset->Z[2][t], i);
}
}
fclose(fp);
*pdset = dset;
*pcertvals = certvals;
*polyterms = npoly;
return 0;
}
static
double get_accuracy (MODEL *pmod, mp_results *certvals, PRN *prn)
{
PRN *vprn;
char label[32];
double le, lemin = 32;
int i;
vprn = (verbose)? prn : NULL;
pprintf(vprn, "\nstatistic log relative error\n\n");
for (i=0; i<pmod->ncoeff; i++) {
sprintf(label, "B[%d]", i);
pprintf(vprn, "%-12s", label);
le = log_error(pmod->coeff[i], certvals->coeff[i], vprn);
if (le < lemin) {
lemin = le;
}
sprintf(label, "Std.Err.");
pprintf(vprn, "%-12s", label);
le = log_error(pmod->sderr[i], certvals->sderr[i], vprn);
if (le < lemin) {
lemin = le;
}
}
pprintf(vprn, "%-12s", "sigma");
le = log_error(pmod->sigma, certvals->sigma, vprn);
if (le < lemin) {
lemin = le;
}
pprintf(vprn, "%-12s", "ESS");
le = log_error(pmod->ess, certvals->ess, vprn);
if (le < lemin) {
lemin = le;
}
pprintf(vprn, "%-12s", "R-squared");
le = log_error(pmod->rsq, certvals->rsq, vprn);
if (le < lemin) {
lemin = le;
}
pprintf(vprn, "%-12s", "F-stat");
le = log_error(pmod->fstt, certvals->fstt, vprn);
if (le < lemin) {
lemin = le;
}
return lemin;
}
static
void print_nist_summary (int ntests, int missing, int modelerrs,
int poorvals, int mpfails,
const char *prog, PRN *prn)
{
pprintf(prn, "\nSummary of NIST linear regression test results:\n"
" * number of tests carried out: %d\n"
" * reference data files missing or corrupted: %d\n"
" * unexpected errors in estimation of models: %d\n"
" * poor or unacceptable results with libgretl: %d\n"
" * cases where results from the gretl GMP plugin disagreed with the NIST\n"
" certified values, at %d significant figures: %d\n",
ntests - missing, missing, modelerrs, poorvals,
MP_CHECK_DIGITS, mpfails);
#ifdef STANDALONE
pprintf(prn, "\nYou may run '%s -v' or '%s -vv' for details\n\n",
prog, prog);
#endif
}
# ifdef STANDALONE
static void *get_mplsq (void);
# endif
static
int run_gretl_mp_comparison (DATASET *dset,
mp_results *certvals, int npoly,
const int *zdigits, int *mpfails,
PRN *prn)
{
int (*mplsq)(const int *, const int *, const int *,
const DATASET *, MODEL *, gretlopt);
int *list = NULL, *polylist = NULL;
int i, err = 0;
int realv = dset->v - npoly;
MODEL model;
double acc;
gretl_model_init(&model, dset);
/* create regression list */
list = gretl_list_new(realv);
if (list == NULL) return 1;
if (noint) {
list[0] = realv - 1;
for (i=1; i<=list[0]; i++) {
list[i] = i;
}
} else {
list[0] = realv;
list[1] = 1;
list[2] = 0;
for (i=3; i<=list[0]; i++) {
list[i] = i - 1;
}
}
/* set up list of polynomial terms, if needed */
if (npoly) {
polylist = gretl_list_new(npoly);
if (polylist == NULL) {
free(list);
return 1;
}
for (i=1; i<=npoly; i++) {
polylist[i] = i + 1;
}
}
#ifdef STANDALONE
mplsq = get_mplsq();
#else
mplsq = get_plugin_function("mplsq");
#endif
if (mplsq == NULL) {
pputs(prn, "Couldn't load mplsq function\n");
err = 1;
}
if (!err) {
err = (*mplsq)(list, polylist, zdigits, dset,
&model, OPT_NONE);
}
free(list);
free(polylist);
if (verbose) {
pprintf(prn, "\nChecking gretl multiple-precision results (%d coefficients):\n\n"
"%44s%24s\n\n", certvals->ncoeff, "certified", "libgretl");
for (i=0; i<certvals->ncoeff; i++) {
char label[24];
if (!na(certvals->coeff[i])) {
sprintf(label, "B[%d] estimate", i);
pprintf(prn, " %-20s %#24.*g %#24.*g\n",
label,
MP_PRINT_DIGITS, certvals->coeff[i],
MP_PRINT_DIGITS, model.coeff[i]);
}
if (!na(certvals->sderr[i])) {
pprintf(prn, " %-20s %#24.*g %#24.*g\n",
"(std. error)",
MP_PRINT_DIGITS, certvals->sderr[i],
MP_PRINT_DIGITS, model.sderr[i]);
}
}
pputc(prn, '\n');
pprintf(prn, " %-20s %#24.*g %#24.*g\n"
" %-20s %#24.*g %#24.*g\n"
" %-20s %#24.*g %#24.*g\n"
" %-20s %#24.*g %#24.*g\n",
"standard error",
MP_PRINT_DIGITS, certvals->sigma,
MP_PRINT_DIGITS, model.sigma,
"error sum of squares",
MP_PRINT_DIGITS, certvals->ess,
MP_PRINT_DIGITS, model.ess,
"R-squared",
MP_PRINT_DIGITS, certvals->rsq,
MP_PRINT_DIGITS, model.rsq,
"F",
MP_PRINT_DIGITS, certvals->fstt,
MP_PRINT_DIGITS, model.fstt);
}
acc = get_accuracy(&model, certvals, prn);
if (verbose) {
pputc(prn, '\n');
}
if (acc < 12.0) {
*mpfails += 1;
pprintf(prn, "* Using gretl GMP plugin: errors found when using"
" %d significant figures\n (worst-case log relative error = %.3f)\n",
MP_CHECK_DIGITS, acc);
} else {
pprintf(prn, "* Using gretl GMP plugin: results correct to"
" at least %d digits\n", (int) acc);
pprintf(prn, " (worst-case log relative error = %.3f)\n", acc);
}
clear_model(&model);
return err;
}
static
int run_gretl_comparison (const char *datname,
DATASET *dset,
mp_results *certvals,
int *errs, int *poor,
PRN *prn)
{
int *list = NULL;
MODEL *model = NULL;
double acc;
int wc = 0;
int i;
static int modelnum;
list = gretl_list_new(dset->v);
if (list == NULL) return 1;
if (noint) {
list[0] = dset->v - 1;
for (i=1; i<=list[0]; i++) {
list[i] = i;
}
} else {
list[0] = dset->v;
list[1] = 1;
list[2] = 0;
for (i=3; i<=list[0]; i++) {
list[i] = i - 1;
}
}
model = gretl_model_new();
*model = lsq(list, dset, OLS, OPT_Z);
if (model->errcode) {
if (verbose) {
pputc(prn, '\n');
}
pprintf(prn, "gretl error code: %d\n", model->errcode);
errmsg(model->errcode, prn);
if (strcmp(datname, "Filip.dat") == 0 &&
model->errcode == E_SINGULAR) {
pputs(prn, "(This error was expected with standard libgretl)\n");
} else {
*errs += 1;
}
goto free_stuff;
}
if (verbose) {
int i;
model->ID = ++modelnum;
printmodel(model, dset, OPT_NONE, prn);
for (i=0; i<model->ncoeff; i++) {
pprintf(prn, " gretl coefficient[%d] = %#.10g\n", i,
model->coeff[i]);
}
}
/* special treatment when there's no intercept */
if (noint) {
double xx = 0.0;
int t;
for (t=0; t<dset->n; t++) {
xx += dset->Z[1][t] * dset->Z[1][t];
}
model->rsq = 1.0 - model->ess / xx;
}
acc = get_accuracy(model, certvals, prn);
if (verbose) {
pputs(prn, "\n ***");
}
if (acc >= 6.0) {
pprintf(prn, "* %s results correct to at least %d digits\n",
LIBGRETLSTR, (int) acc);
} else if (acc >= MIN_DIGITS) {
if (strcmp(datname, "Filip.dat") && strcmp(datname, "Wampler5.dat")) {
pprintf(prn, "* %s results correct to only %d digits: "
"POOR\n", LIBGRETLSTR, (int) acc);
*poor += 1;
} else {
pprintf(prn, "* %s results correct to at least %.2f digits\n"
" (OK on Filip.dat and Wampler5.dat)\n", LIBGRETLSTR, acc);
wc = 1;
}
} else {
pprintf(prn, "* %s results correct to less than "
"%d digits: UNACCEPTABLE\n", LIBGRETLSTR, MIN_DIGITS);
*poor += 1;
}
if (!wc) {
pprintf(prn, " (worst-case log relative error = %.3f)\n", acc);
}
if (verbose) {
pputc(prn, '\n');
}
free_stuff:
free(list);
gretl_model_free(model);
return 0;
}
#ifdef STANDALONE
int main (int argc, char *argv[])
{
int j;
PRN *prn;
DATASET *dataset;
mp_results *certvals = NULL;
int ntests, missing = 0, modelerrs = 0, poorvals = 0;
int polyterms = 0, mpfails = 0;
int *zdigits = NULL;
const char *prog;
# ifdef LONGLEY_ONLY
const char *nist_files[] = {
"Longley.dat"
};
# else
const char *nist_files[] = {
"Norris.dat",
"Pontius.dat",
"NoInt1.dat",
"NoInt2.dat",
"Filip.dat",
"Longley.dat",
"Wampler1.dat",
"Wampler2.dat",
"Wampler3.dat",
"Wampler4.dat",
"Wampler5.dat"
};
# endif /* LONGLEY_ONLY */
ntests = sizeof nist_files / sizeof *nist_files;
prog = argv[0];
if (argc >= 2 && strcmp(argv[1], "-v") == 0) verbose = 1;
if (argc >= 2 && strcmp(argv[1], "-vv") == 0) verbose = 2;
strcpy(datadir, ".");
if (argc == 2 && argv[1][0] != '-') {
strcpy(datadir, argv[1]);
} else if (argc == 3) {
strcpy(datadir, argv[2]);
}
libgretl_init();
prn = gretl_print_new(GRETL_PRINT_STDOUT, NULL);
for (j=0; j<ntests; j++) {
if (read_nist_file(nist_files[j], &dataset, &certvals,
&polyterms, &zdigits, prn)) {
pprintf(prn, "Error processing %s\n", nist_files[j]);
missing++;
} else {
run_gretl_comparison (nist_files[j], dataset, certvals,
&modelerrs, &poorvals, prn);
run_gretl_mp_comparison (dataset, certvals, polyterms,
zdigits, &mpfails, prn);
free_mp_results(certvals);
certvals = NULL;
destroy_dataset(dataset);
dataset = NULL;
free(zdigits);
zdigits = NULL;
}
}
print_nist_summary(ntests, missing, modelerrs, poorvals, mpfails,
prog, prn);
gretl_print_destroy(prn);
libgretl_cleanup();
return (missing || modelerrs || poorvals);
}
#else /* !STANDALONE */
static void nist_intro (PRN *prn)
{
pputs(prn, "What you should see below: A series of 11 tests, using the "
"reference data sets for linear regression from the U.S. National "
"Institute of Standards and Technology (NIST). If you scroll to "
"the bottom you will see a summary of the results: if all is well "
"there should be 0 values for \"data files missing\", \"unexpected "
"errors\" and \"poor results\".\n\n");
pputs(prn, "The \"log relative error\" is defined as the negative of the "
"base-10 logarithm of (|q - c| / |c|), where q denotes the result "
"produced by gretl and c denotes the certified value. It represents "
"the number of correct digits in the gretl output. For more details, "
"see B. D. McCullough, \"Assessing the Reliability of "
"Statistical Software: Part I\", The American Statistician, 52 "
"(1998), pp. 358-366.\n\n");
pputs(prn, "Each test case is run twice, once using the standard "
"linear regression calculation in the gretl library and once "
"in multiple precision arithmetic using the GMP library.\n\n");
pputs(prn, "For more information, please see\n"
"http://www.itl.nist.gov/div898/strd/general/main.html");
pputs(prn, "\n\n");
}
int run_nist_tests (const char *datapath, const char *outfile, int verbosity)
{
int j;
PRN *prn;
DATASET *dataset;
mp_results *certvals = NULL;
int *zdigits = NULL;
int ntests, missing = 0, modelerrs = 0, poorvals = 0;
int polyterms = 0, mpfails = 0;
const char *nist_files[] = {
"Norris.dat",
"Pontius.dat",
"NoInt1.dat",
"NoInt2.dat",
"Filip.dat",
"Longley.dat",
"Wampler1.dat",
"Wampler2.dat",
"Wampler3.dat",
"Wampler4.dat",
"Wampler5.dat"
};
int err = 0;
gretl_push_c_numeric_locale();
ntests = sizeof nist_files / sizeof *nist_files;
verbose = verbosity;
sprintf(datadir, "%snist", datapath);
prn = gretl_print_new_with_filename(outfile, &err);
nist_intro(prn);
for (j=0; j<ntests; j++) {
if (read_nist_file(nist_files[j], &dataset, &certvals,
&polyterms, &zdigits, prn)) {
pprintf(prn, "Error processing %s\n", nist_files[j]);
missing++;
} else {
run_gretl_comparison (nist_files[j], dataset, certvals,
&modelerrs, &poorvals, prn);
run_gretl_mp_comparison (dataset, certvals, polyterms,
zdigits, &mpfails, prn);
free_mp_results(certvals);
certvals = NULL;
destroy_dataset(dataset);
dataset = NULL;
free(zdigits);
zdigits = NULL;
}
}
print_nist_summary(ntests, missing, modelerrs, poorvals, mpfails,
NULL, prn);
gretl_pop_c_numeric_locale();
gretl_print_destroy(prn);
return (missing || modelerrs || poorvals);
}
#endif /* STANDALONE */
#ifdef STANDALONE
# ifdef _WIN32
# include <windows.h>
# else
# include <dlfcn.h>
# endif
static void *get_mplsq (void)
{
void *handle;
void *funp;
# ifdef _WIN32
handle = LoadLibrary("plugins\\mp_ols.dll");
if (handle == NULL) return NULL;
# else
handle = dlopen("../plugin/.libs/mp_ols.so", RTLD_LAZY);
if (handle == NULL) {
fputs(dlerror(), stderr);
return NULL;
}
# endif /* _WIN32 */
# ifdef _WIN32
funp = GetProcAddress(handle, "mplsq");
# else
funp = dlsym(handle, "mplsq");
if (funp == NULL) {
/* try munged version */
funp = dlsym(handle, "_mplsq");
if (funp == NULL) {
fputs(dlerror(), stderr);
}
}
# endif /* _WIN32 */
if (funp == NULL) {
close_plugin(handle);
}
return funp;
}
#endif /* STANDALONE */
|