testregress.c

Go to the documentation of this file.
00001 /* ********************************************************* */
00002 /* testregress Test multifit linear regression GSL function. */
00003 /* testregress.c                                             */
00004 /* ********************************************************* */
00005 /* Author: Christian Page, CERFACS, Toulouse, France.        */
00006 /* ********************************************************* */
00011 /* LICENSE BEGIN
00012 
00013 Copyright Cerfacs (Christian Page) (2015)
00014 
00015 christian.page@cerfacs.fr
00016 
00017 This software is a computer program whose purpose is to downscale climate
00018 scenarios using a statistical methodology based on weather regimes.
00019 
00020 This software is governed by the CeCILL license under French law and
00021 abiding by the rules of distribution of free software. You can use, 
00022 modify and/ or redistribute the software under the terms of the CeCILL
00023 license as circulated by CEA, CNRS and INRIA at the following URL
00024 "http://www.cecill.info". 
00025 
00026 As a counterpart to the access to the source code and rights to copy,
00027 modify and redistribute granted by the license, users are provided only
00028 with a limited warranty and the software's author, the holder of the
00029 economic rights, and the successive licensors have only limited
00030 liability. 
00031 
00032 In this respect, the user's attention is drawn to the risks associated
00033 with loading, using, modifying and/or developing or reproducing the
00034 software by the user in light of its specific status of free software,
00035 that may mean that it is complicated to manipulate, and that also
00036 therefore means that it is reserved for developers and experienced
00037 professionals having in-depth computer knowledge. Users are therefore
00038 encouraged to load and test the software's suitability as regards their
00039 requirements in conditions enabling the security of their systems and/or 
00040 data to be ensured and, more generally, to use and operate it in the 
00041 same conditions as regards security. 
00042 
00043 The fact that you are presently reading this means that you have had
00044 knowledge of the CeCILL license and that you accept its terms.
00045 
00046 LICENSE END */
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 #ifdef HAVE_CONFIG_H
00055 #include <config.h>
00056 #endif
00057 
00059 #define _GNU_SOURCE
00060 
00061 /* C standard includes */
00062 #ifdef HAVE_SYS_TYPES_H
00063 #include <sys/types.h>
00064 #endif
00065 #ifdef HAVE_SYS_STAT_H
00066 #include <sys/stat.h>
00067 #endif
00068 #ifdef HAVE_FCNTL_H
00069 #include <fcntl.h>
00070 #endif
00071 #ifdef HAVE_UNSTD_H
00072 #include <unistd.h>
00073 #endif
00074 #ifdef HAVE_STDIO_H
00075 #include <stdio.h>
00076 #endif
00077 #ifdef HAVE_STRING_H
00078 #include <string.h>
00079 #endif
00080 #ifdef HAVE_STDLIB_H
00081 #include <stdlib.h>
00082 #endif
00083 #ifdef HAVE_MATH_H
00084 #include <math.h>
00085 #endif
00086 #ifdef HAVE_TIME_H
00087 #include <time.h>
00088 #endif
00089 #ifdef HAVE_LIBGEN_H
00090 #include <libgen.h>
00091 #endif
00092 
00093 #include <utils.h>
00094 #include <regress.h>
00095 
00097 void show_usage(char *pgm);
00098 
00100 int main(int argc, char **argv)
00101 {
00109   int npts;
00110   int nterm;
00111   int pts;
00112 #if DEBUG >= 6
00113   int term;
00114 #endif
00115 
00116   int i;
00117   int istat;
00118 
00119   double *x = NULL;
00120   double *y = NULL;
00121   double *yreg = NULL;
00122   double *coef = NULL;
00123   double *yerr = NULL;
00124   double *vif = NULL;
00125   double cte;
00126   double chisq;
00127   double rsq;
00128   double autocor;
00129 
00130   /* Print BEGIN banner */
00131   (void) banner(basename(argv[0]), "1.0", "BEGIN");
00132 
00133   /* Get command-line arguments and set appropriate variables */
00134   for (i=1; i<argc; i++) {
00135     if ( !strcmp(argv[i], "-h") ) {
00136       (void) show_usage(basename(argv[0]));
00137       (void) banner(basename(argv[0]), "OK", "END");
00138       return 0;
00139     }
00140     else {
00141       (void) fprintf(stderr, "%s:: Wrong arg %s.\n\n", basename(argv[0]), argv[i]);
00142       (void) show_usage(basename(argv[0]));
00143       (void) banner(basename(argv[0]), "ABORT", "END");
00144       (void) abort();
00145     }
00146   }
00147 
00148   npts = 6;
00149   nterm = 2;
00150 
00151   x = (double *) calloc(npts*nterm, sizeof(double));
00152   if (x == NULL) alloc_error(__FILE__, __LINE__);
00153   
00154   x[0+0*npts] = 1.0;
00155   x[1+0*npts] = 2.0;
00156   x[2+0*npts] = 4.0;
00157   x[3+0*npts] = 8.0;
00158   x[4+0*npts] = 16.0;
00159   x[5+0*npts] = 32.0;
00160 
00161   x[0+1*npts] = 0.0;
00162   x[1+1*npts] = 1.0;
00163   x[2+1*npts] = 2.0;
00164   x[3+1*npts] = 3.0;
00165   x[4+1*npts] = 4.0;
00166   x[5+1*npts] = 5.0;
00167 
00168   y = (double *) calloc(npts, sizeof(double));
00169   if (y == NULL) alloc_error(__FILE__, __LINE__);
00170   yreg = (double *) calloc(npts, sizeof(double));
00171   if (yreg == NULL) alloc_error(__FILE__, __LINE__);
00172   yerr = (double *) calloc(npts, sizeof(double));
00173   if (yerr == NULL) alloc_error(__FILE__, __LINE__);
00174   coef = (double *) calloc(nterm, sizeof(double));
00175   if (coef == NULL) alloc_error(__FILE__, __LINE__);
00176   vif = (double *) calloc(nterm, sizeof(double));
00177   if (vif == NULL) alloc_error(__FILE__, __LINE__);
00178 
00179   for (pts=0; pts<npts; pts++)
00180     y[pts] = 5.0 + 3.0 * x[pts+0*npts] - 4.0 * x[pts+1*npts];
00181 
00182   istat = regress(coef, x, y, &cte, yreg, yerr, &chisq, &rsq, vif, &autocor, nterm, npts);
00183 
00184 #if DEBUG >= 6
00185   for (term=0; term<nterm; term++)
00186     for (pts=0; pts<npts; pts++)
00187       (void) fprintf(stdout, "%s: term=%d pts=%d x=%lf\n", __FILE__, term, pts, x[pts+term*npts]);
00188 
00189   for (term=0; term<nterm; term++)
00190     (void) fprintf(stdout, "%s: term=%d coef=%lf\n", __FILE__, term, coef[term]);
00191 
00192   (void) fprintf(stdout, "%s: cte=%lf chisq=%lf\n", __FILE__, cte, chisq);
00193 
00194   for (pts=0; pts<npts; pts++)
00195     (void) fprintf(stdout, "%s: pts=%d y=%lf yreg=%lf yerr=%lf\n", __FILE__, pts, y[pts], yreg[pts], yerr[pts]);
00196 #endif
00197 
00198   (void) free(coef);
00199   (void) free(yreg);
00200   (void) free(yerr);
00201   (void) free(vif);
00202   (void) free(y);
00203   (void) free(x);
00204 
00205   /* Print END banner */
00206   (void) banner(basename(argv[0]), "OK", "END");
00207 
00208   return 0;
00209 }
00210 
00211 
00215 void show_usage(char *pgm) {
00220   (void) fprintf(stderr, "%s: usage:\n", pgm);
00221   (void) fprintf(stderr, "-h: help\n");
00222 
00223 }

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1