Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / polyback.c
1 /*
2  *   This file is part of TISEAN
3  *
4  *   Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber
5  *
6  *   TISEAN is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   TISEAN is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with TISEAN; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 /*Author: Rainer Hegger. Last modified Sep 4, 1999 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25 #include <limits.h>
26 #include <time.h>
27 #include "routines/tsa.h"
28
29 #define WID_STR "Does a backward elimination for a polynomial"
30
31 char *outfile=NULL,stdo=1;
32 char *parin=NULL,*infile=NULL;
33 unsigned long length=ULONG_MAX,insample=ULONG_MAX,exclude=0;
34 unsigned int plength=UINT_MAX;
35 unsigned int column=1,dim=2,delay=1,down_to=1,step=1;
36 unsigned int **order;
37 unsigned int verbosity=0xff;
38 double *series,*param;
39
40 void show_options(char *progname)
41 {
42   what_i_do(progname,WID_STR);
43   fprintf(stderr,"Usage: %s [Options]\n",progname);
44   fprintf(stderr,"Options:\n");
45   fprintf(stderr,"Everything not being a valid option will be interpreted"
46           " as a possible"
47           " datafile.\nIf no datafile is given stdin is read. Just - also"
48           " means stdin\n");
49   fprintf(stderr,"\t-l # of data to use [default: whole file]\n");
50   fprintf(stderr,"\t-x # of lines to ignore [default: %lu]\n",exclude);
51   fprintf(stderr,"\t-c column to read [default: %u]\n",column);
52   fprintf(stderr,"\t-m embedding dimension [default: %u]\n",dim);
53   fprintf(stderr,"\t-d delay [default: %u]\n",delay);
54   fprintf(stderr,"\t-n insample data [default: all]\n");
55   fprintf(stderr,"\t-s steps to forecast [default: %u]\n",step);
56   fprintf(stderr,"\t-# reduce down to # terms [default: %u]\n",down_to);
57   fprintf(stderr,"\t-p name of parameter file [default: parameter.pol]\n");
58   fprintf(stderr,"\t-o output file name [default: 'datafile'.pbe]\n");
59   fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
60           "0='only panic messages'\n\t\t"
61           "1='+ input/output messages'\n");
62   fprintf(stderr,"\t-h show these options\n");
63   exit(0);
64 }
65
66 void scan_options(int n,char **in)
67 {
68   char *out;
69   
70   if ((out=check_option(in,n,'l','u')) != NULL)
71     sscanf(out,"%lu",&length);
72   if ((out=check_option(in,n,'x','u')) != NULL)
73     sscanf(out,"%lu",&exclude);
74   if ((out=check_option(in,n,'c','u')) != NULL)
75     sscanf(out,"%u",&column);
76   if ((out=check_option(in,n,'m','u')) != NULL)
77     sscanf(out,"%u",&dim);
78   if ((out=check_option(in,n,'d','u')) != NULL)
79     sscanf(out,"%u",&delay);
80   if ((out=check_option(in,n,'n','u')) != NULL)
81     sscanf(out,"%lu",&insample);
82   if ((out=check_option(in,n,'#','u')) != NULL)
83     sscanf(out,"%u",&down_to);
84   if ((out=check_option(in,n,'s','u')) != NULL)
85     sscanf(out,"%u",&step);
86   if ((out=check_option(in,n,'V','u')) != NULL)
87     sscanf(out,"%u",&verbosity);
88   if ((out=check_option(in,n,'p','s')) != NULL)
89     parin=out;
90   if ((out=check_option(in,n,'o','o')) != NULL) {
91     stdo=0;
92     if (strlen(out) > 0)
93       outfile=out;
94   }
95 }
96
97 double polynom(unsigned long act,unsigned int which)
98 {
99   unsigned int i,j;
100   double ret=1.0,h;
101   
102   for (i=0;i<dim;i++) {
103     h=series[act-i*delay];
104     for (j=0;j<order[which][i];j++)
105       ret *= h;
106   }
107   
108   return ret;
109 }
110
111 void make_fit(void)
112 {
113   double **mat,*vec;
114   double h;
115   unsigned long n;
116   unsigned int i,j;
117
118   check_alloc(vec=(double*)malloc(sizeof(double)*plength));
119   check_alloc(mat=(double**)malloc(sizeof(double*)*plength));
120   for (i=0;i<plength;i++)
121     check_alloc(mat[i]=(double*)malloc(sizeof(double)*plength));
122
123   for (i=0;i<plength;i++) {
124     vec[i]=0.0;
125     for (j=0;j<plength;j++)
126       mat[i][j]=0.0;
127   }
128   
129   for (n=(dim-1)*delay;n<insample-step;n++) {
130     for (i=0;i<plength;i++) {
131       vec[i] += series[n+step]*(h=polynom(n,i));
132       for (j=i;j<plength;j++)
133         mat[i][j] += polynom(n,j)*h;
134     }
135   }
136   for (i=0;i<plength;i++) {
137     vec[i] /= (insample-step-(dim-1)*delay);
138     for (j=i;j<plength;j++)
139       mat[j][i]=(mat[i][j]/=(insample-step-(dim-1)*delay));
140   }
141   
142   solvele(mat,vec,plength);
143
144   for (i=0;i<plength;i++)
145     param[i]=vec[i];
146
147   free(vec);
148   for (i=0;i<plength;i++)
149     free(mat[i]);
150   free(mat);
151 }
152
153 double forecast_error(unsigned long i0,unsigned long i1)
154 {
155   unsigned int i;
156   unsigned long n;
157   double h,error=0.0;
158
159   for (n=i0+(dim-1)*delay;n<i1-step;n++) {
160     h=0.0;
161     for (i=0;i<plength;i++)
162       h += param[i]*polynom(n,i);
163     error += (series[n+step]-h)*(series[n+step]-h);
164   }
165   
166   return sqrt(error/(i1-i0-step-(dim-1)*delay));
167 }
168
169 int main(int argc,char **argv)
170 {
171   int i,j,k,l,hl,ibest,counter;
172   char stdi=0,out_set=1,*parout;
173   double **dummy,besti,besto,withalli,withallo,errori=0.,erroro=0.;
174   double av,varianz;
175   unsigned long hlength=ULONG_MAX;
176   unsigned int **ini_params,*isout,offset;
177   FILE *file,*fpars;
178
179   if (scan_help(argc,argv))
180     show_options(argv[0]);
181
182   scan_options(argc,argv);
183 #ifndef OMIT_WHAT_I_DO
184   if (verbosity&VER_INPUT)
185     what_i_do(argv[0],WID_STR);
186 #endif
187
188   infile=search_datafile(argc,argv,&column,verbosity);
189   if (infile == NULL)
190     stdi=1;
191   
192   if (outfile == NULL) {
193     if (!stdi) {
194       check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
195       sprintf(outfile,"%s.pbe",infile);
196     }
197     else {
198       check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
199       sprintf(outfile,"stdin.pbe");
200     }
201   }
202   if (!stdo)
203     test_outfile(outfile);
204
205   if (parin == NULL) {
206     check_alloc(parin=(char*)calloc((size_t)14,(size_t)1));
207     sprintf(parin,"parameter.pol");
208   }
209   file=fopen(parin,"r");
210   if (file == NULL) {
211     fprintf(stderr,"File %s does not exist. Exiting!\n",parin);
212     exit(POLYBACK__WRONG_PARAMETER_FILE);
213   }
214   fclose(file);
215
216   if (verbosity&VER_INPUT)
217     fprintf(stderr,"Using %s as the parameter file\n",parin);
218   dummy=(double**)get_multi_series(parin,&hlength,0LU,&dim,"",(char)1,
219                                    verbosity);
220
221   offset=(unsigned int)(log((double)hlength)/log(10.0)+1.0);
222   check_alloc(parout=(char*)calloc(strlen(parin)+offset+2,(size_t)1));
223   
224   check_alloc(ini_params=(unsigned int**)malloc(sizeof(int*)*hlength));
225   for (i=0;i<hlength;i++) {
226     check_alloc(ini_params[i]=(unsigned int*)malloc(sizeof(int)*dim));
227     for (j=0;j<dim;j++)
228       ini_params[i][j]=(unsigned int)dummy[j][i];
229   }
230   check_alloc(isout=(unsigned int*)malloc(sizeof(int)*hlength));
231
232   series=(double*)get_series(infile,&length,exclude,column,verbosity);
233   variance(series,length,&av,&varianz);
234
235   if (insample >= length) {
236     insample=length;
237     out_set=0;
238   }
239
240   check_alloc(order=(unsigned int**)malloc(sizeof(int*)*hlength));
241   check_alloc(param=(double*)malloc(sizeof(double)*hlength));
242   for (i=0;i<hlength;i++) {
243     isout[i]=0;
244     check_alloc(order[i]=(unsigned int*)malloc(sizeof(int)*dim));
245     for (j=0;j<dim;j++)
246       order[i][j]=ini_params[i][j];
247   }
248   plength=hlength;
249
250   make_fit();
251   withalli=forecast_error(0LU,insample);
252   withallo=0.0;
253   if (out_set)
254     withallo=forecast_error(insample+1,length);
255
256   if (stdo) {
257     fprintf(stdout,"%lu %e %e\n",hlength,withalli/varianz,withallo/varianz);
258     fflush(stdout);
259   }
260   else {
261     file=fopen(outfile,"w");
262     fprintf(file,"%lu %e %e\n",hlength,withalli/varianz,withallo/varianz);
263     fflush(file);
264   }
265   free(param);
266   for (i=0;i<plength;i++)
267     free(order[i]);
268   free(order);
269   
270   if ((down_to < 1) || (down_to > hlength))
271     down_to=1;
272
273   for (i=1;i<=hlength-down_to;i++) {
274     plength=hlength-i;
275     besti=besto=0.0;
276     ibest= -1;
277     check_alloc(order=(unsigned int**)malloc(sizeof(int*)*plength));
278     check_alloc(param=(double*)malloc(sizeof(double)*plength));
279     for (j=0;j<plength;j++) {
280       check_alloc(order[j]=(unsigned int*)malloc(sizeof(int)*dim));
281     }
282     counter=plength;
283     for (j=0;j<hlength;j++)
284       if (!isout[j]) {
285         isout[j]++;
286         hl=0;
287         for (k=0;k<hlength;k++) {
288           if (!isout[k]) {
289             for (l=0;l<dim;l++)
290               order[hl][l]=ini_params[k][l];
291             hl++;
292           }
293         }
294         make_fit();
295         errori=forecast_error(0LU,insample);
296         if (out_set)
297           erroro=forecast_error(insample+1,length);
298         if (ibest == -1) {
299           besti=errori;
300           if (out_set)
301             besto=erroro;
302           ibest=j;
303         }
304         else {
305           if (out_set) {
306             if (erroro < besto) {
307               besto=erroro;
308               besti=errori;
309               ibest=j;
310             }
311           }
312           else {
313             if (errori < besti) {
314               besti=errori;
315               besto=erroro;
316               ibest=j;
317             }
318           }
319         }
320         isout[j]--;
321       }
322     isout[ibest]++;
323     free(param);
324     for (j=0;j<plength;j++)
325       free(order[j]);
326     free(order);
327     if (stdo) {
328       fprintf(stdout,"%u %e %e ",plength,besti/varianz,besto/varianz);
329       for (j=0;j<dim;j++)
330         fprintf(stdout,"%u ",ini_params[ibest][j]);
331       fprintf(stdout,"\n");
332       fflush(stdout);
333     }
334     else {
335       fprintf(file,"%u %e %e ",plength,besti/varianz,besto/varianz);
336       for (j=0;j<dim;j++)
337         fprintf(file,"%u ",ini_params[ibest][j]);
338       fprintf(file,"\n");
339       fflush(file);
340     }
341     sprintf(parout,"%s.%u",parin,plength);
342     fpars=fopen(parout,"w");
343     for (j=0;j<hlength;j++)
344       if (!isout[j]) {
345         for (k=0;k<dim;k++)
346           fprintf(fpars,"%u ",ini_params[j][k]);
347         fprintf(fpars,"\n");
348       }
349     fclose(fpars);
350   }
351  
352   if (!stdo)
353     fclose(file);
354   return 0;
355 }