Mac binaries
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / polynom.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*/
21 /* Changes:
22    6/30/2006: Norm of the errors was wrong
23 */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #include <limits.h>
28 #include <string.h>
29 #include "routines/tsa.h"
30
31 #define WID_STR "Fits a polynomial to the data"
32
33 char CAST=0,sinsample=0,*outfile=NULL;
34 char *infile=NULL;
35 unsigned long LENGTH=ULONG_MAX,exclude=0;
36 long CLENGTH=1000;
37 unsigned long INSAMPLE=ULONG_MAX;
38 int DIM=2,DELAY=1,N=2;
39 unsigned int COLUMN=1;
40 unsigned int pars=1,hpar;
41 unsigned int verbosity=0xff;
42
43 long *coding;
44 long maxencode;
45 double *series,*results;
46 double std_dev;
47
48 void show_options(char *progname)
49 {
50   what_i_do(progname,WID_STR);
51   fprintf(stderr," Usage: %s [options]\n",progname);
52   fprintf(stderr," Options:\n");
53   fprintf(stderr,"Everything not being a valid option will be interpreted"
54           " as a possible"
55           " datafile.\nIf no datafile is given stdin is read. Just - also"
56           " means stdin\n");
57   fprintf(stderr,"\t-l # of data to use [default: whole file]\n");
58   fprintf(stderr,"\t-x # of lines to be ignored [default: 0]\n");
59   fprintf(stderr,"\t-c column to read [default: 1]\n");
60   fprintf(stderr,"\t-m embedding dimension [default: 2]\n");
61   fprintf(stderr,"\t-d delay [default: 1]\n");
62   fprintf(stderr,"\t-p order of the polynomial [default: 2]\n");
63   fprintf(stderr,"\t-n # of points for insample [default: # of data]\n");
64   fprintf(stderr,"\t-L steps to cast [default: none]\n");
65   fprintf(stderr,"\t-o output file name [default: 'datafile'.pol]\n");
66   fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
67           "0='only panic messages'\n\t\t"
68           "1='+ input/output messages'\n");
69   fprintf(stderr,"\t-h show these options\n");
70   exit(0);
71 }
72
73 void scan_options(int n,char **in)
74 {
75   char *out;
76
77   if ((out=check_option(in,n,'l','u')) != NULL)
78     sscanf(out,"%lu",&LENGTH);
79   if ((out=check_option(in,n,'x','u')) != NULL)
80     sscanf(out,"%lu",&exclude);
81   if ((out=check_option(in,n,'c','u')) != NULL)
82     sscanf(out,"%u",&COLUMN);
83   if ((out=check_option(in,n,'m','u')) != NULL)
84     sscanf(out,"%u",&DIM);
85   if ((out=check_option(in,n,'d','u')) != NULL)
86     sscanf(out,"%u",&DELAY);
87   if ((out=check_option(in,n,'p','u')) != NULL)
88     sscanf(out,"%u",&N);
89   if ((out=check_option(in,n,'V','u')) != NULL)
90     sscanf(out,"%u",&verbosity);
91   if ((out=check_option(in,n,'n','u')) != NULL) {
92     sscanf(out,"%lu",&INSAMPLE);
93     sinsample=1;
94   }
95   if ((out=check_option(in,n,'L','u')) != NULL) {
96     CAST=1;
97     sscanf(out,"%lu",&CLENGTH);
98   }
99   if ((out=check_option(in,n,'o','o')) != NULL)
100     if (strlen(out) > 0)
101       outfile=out;
102 }
103
104 double polynom(int act,int dim,long cur,long fac)
105 {
106   int j,n,hi;
107   double ret=1.0;
108
109   n=cur/fac;
110   hi=act-(dim-1)*DELAY;
111   for (j=1;j<=n;j++)
112     ret *= series[hi];
113   if (dim > 1) 
114     ret *= polynom(act,dim-1,cur-n*fac,fac/(N+1));
115
116   return ret;
117 }
118
119 int number_pars(int ord,int start)
120 {
121   int i,ret=0;
122
123   if (ord == 1)
124     for (i=start;i<=DIM;i++)
125       ret += 1;
126   else
127     for (i=start;i<=DIM;i++)
128       ret += number_pars(ord-1,i);
129
130   return ret;
131 }
132
133 void make_coding(int ord,int d,int fac,int cur)
134 {
135   int j;
136
137   if ( d == -1)
138     coding[hpar++]=cur;
139   else
140     for (j=0;j<=ord;j++)
141       make_coding(ord-j,d-1,fac*(N+1),cur+j*fac);
142 }
143
144 void make_fit(void)
145 {
146   int i,j,k;
147   double **mat,*b;
148   
149   check_alloc(b=(double*)malloc(sizeof(double)*pars));
150   check_alloc(mat=(double**)malloc(sizeof(double*)*pars));
151   for (i=0;i<pars;i++)
152     check_alloc(mat[i]=(double*)malloc(sizeof(double)*pars));
153
154   for (i=0;i<pars;i++) {
155     b[i]=0.0;
156     for (j=0;j<pars;j++)
157       mat[i][j]=0.0;
158   }
159
160   for (i=0;i<pars;i++)
161     for (j=i;j<pars;j++)
162       for (k=(DIM-1)*DELAY;k<INSAMPLE-1;k++)
163         mat[i][j] += polynom(k,DIM,coding[i],maxencode)*
164           polynom(k,DIM,coding[j],maxencode);
165   for (i=0;i<pars;i++)
166     for (j=i;j<pars;j++)
167       mat[j][i]=(mat[i][j] /= (INSAMPLE-1-(DIM-1)*DELAY));
168
169   for (i=0;i<pars;i++) {
170     for (j=(DIM-1)*DELAY;j<INSAMPLE-1;j++)
171       b[i] += series[j+1]*polynom(j,DIM,coding[i],maxencode);
172     b[i] /= (INSAMPLE-1-(DIM-1)*DELAY);
173   }
174   solvele(mat,b,pars);
175
176   for (i=0;i<pars;i++)
177     results[i]=b[i];
178   
179   free(b);
180   for (i=0;i<pars;i++)
181     free(mat[i]);
182   free(mat);
183 }
184
185 void decode(int *out,int dim,long cur,long fac)
186 {
187   int n;
188   
189   n=cur/fac;
190   out[dim]=n;
191   if (dim > 0) 
192     decode(out,dim-1,cur-(long)n*fac,fac/(N+1));
193 }
194
195 double make_error(unsigned long i0,unsigned long i1)
196 {
197   int j,k;
198   double h,err;
199   
200   err=0.0;
201   for (j=i0+(DIM-1)*DELAY;j<(long)i1-1;j++) {
202     h=0.0;
203     for (k=0;k<pars;k++) 
204       h += results[k]*polynom(j,DIM,coding[k],maxencode);
205     err += (series[j+1]-h)*(series[j+1]-h);
206   }
207   return err /= ((long)i1-(long)i0-(DIM-1)*DELAY);
208 }
209
210 void make_cast(FILE *fcast)
211 {
212   int i,j,k,hi;
213   double casted;
214   
215   for (i=0;i<=(DIM-1)*DELAY;i++)
216     series[i]=series[LENGTH-(DIM-1)*DELAY-1+i];
217
218   hi=(DIM-1)*DELAY;
219   for (i=1;i<=CLENGTH;i++) {
220     casted=0.0;
221     for (k=0;k<pars;k++)
222       casted += results[k]*polynom((DIM-1)*DELAY,DIM,coding[k],maxencode);
223     fprintf(fcast,"%e\n",casted*std_dev);
224     fflush(fcast);
225     for (j=0;j<(DIM-1)*DELAY;j++)
226       series[j]=series[j+1];
227     series[hi]=casted;
228   }
229   fclose(fcast);
230 }
231
232 int main(int argc,char **argv)
233 {
234   char stdi=0;
235   int i,j,k;
236   int *opar,sumpar;
237   double in_error,out_error,av;
238   FILE *file;
239
240   if (scan_help(argc,argv))
241     show_options(argv[0]);
242
243   scan_options(argc,argv);
244 #ifndef OMIT_WHAT_I_DO
245   if (verbosity&VER_INPUT)
246     what_i_do(argv[0],WID_STR);
247 #endif
248
249   infile=search_datafile(argc,argv,&COLUMN,verbosity);
250   if (infile == NULL)
251     stdi=1;
252
253   if (outfile == NULL) {
254     if (!stdi) {
255       check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
256       strcpy(outfile,infile);
257       strcat(outfile,".pol");
258     }
259     else {
260       check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
261       strcpy(outfile,"stdin.pol");
262     }
263   }
264   test_outfile(outfile);
265
266   series=(double*)get_series(infile,&LENGTH,exclude,COLUMN,verbosity);
267   variance(series,LENGTH,&av,&std_dev);
268   for (i=0;i<LENGTH;i++)
269     series[i] /= std_dev;
270
271   if (!sinsample || (INSAMPLE > LENGTH))
272     INSAMPLE=LENGTH;
273
274   maxencode=1;
275   for (i=1;i<DIM;i++)
276     maxencode *= (N+1);
277   
278   for (i=1;i<=N;i++) {
279     pars += number_pars(i,1);
280   }
281   file=fopen(outfile,"w");
282   if (verbosity&VER_INPUT)
283     fprintf(stderr,"Opened %s for writing\n",outfile);
284   fprintf(file,"#number of free parameters= %d\n\n",pars);
285   fflush(file);
286   check_alloc(coding=(long*)malloc(sizeof(long)*pars));
287   hpar=0;
288   make_coding(N,DIM-1,1,0);
289
290   check_alloc(results=(double*)malloc(sizeof(double)*pars));
291   make_fit();
292
293   check_alloc(opar=(int*)malloc(sizeof(int)*DIM));
294   fprintf(file,"#used norm for the fit= %e\n",std_dev);
295
296   for (j=0;j<pars;j++) {
297     decode(opar,DIM-1,coding[j],maxencode);
298     fprintf(file,"#");
299     sumpar=0;
300     for (k=0;k<DIM;k++) {
301       sumpar += opar[k];
302       fprintf(file,"%d ",opar[k]);
303     }
304     fprintf(file,"%e\n",results[j]/pow(std_dev,(double)(sumpar-1)));
305   }
306   fprintf(file,"\n");
307
308   in_error=make_error((unsigned long)0,INSAMPLE);
309
310   fprintf(file,"#average insample error= %e\n",sqrt(in_error));
311
312   if (INSAMPLE < LENGTH) {
313     out_error=make_error(INSAMPLE,LENGTH);
314     fprintf(file,"#average out of sample error= %e\n",sqrt(out_error));
315   }
316
317   if (CAST)
318     make_cast(file);
319   fclose(file);
320   
321   return 0;
322 }