Mac binaries
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / xzero.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 <limits.h>
25 #include "routines/tsa.h"
26
27 #define WID_STR "Estimates the average cross forecast error for a zeroth\n\t\
28 order fit between two series given as two columns of one file."
29
30 #ifndef _MATH_H
31 #include <math.h>
32 #endif
33
34 /*number of boxes for the neighbor search algorithm*/
35 #define NMAX 128
36
37 unsigned int nmax=(NMAX-1);
38 long **box,*list;
39 unsigned long *found;
40 double *series1,*series2;
41 double interval,min,epsilon;
42
43 char epsset=0;
44 char *infile=NULL;
45 char *outfile=NULL,stdo=1;
46 char *COLUMNS=NULL;
47 unsigned int DIM=3,DELAY=1;
48 unsigned int verbosity=0xff;
49 int MINN=30,STEP=1;
50 double EPS0=1.e-3,EPSF=1.2;
51 unsigned long LENGTH=ULONG_MAX,exclude=0,CLENGTH=ULONG_MAX;
52
53 void show_options(char *progname)
54 {
55   what_i_do(progname,WID_STR);
56   fprintf(stderr," Usage: %s [options]\n",progname);
57   fprintf(stderr," Options:\n");
58   fprintf(stderr,"Everything not being a valid option will be interpreted"
59           " as a possible"
60           " datafile.\nIf no datafile is given stdin is read. Just - also"
61           " means stdin\n");
62   fprintf(stderr,"\t-l # of data to use [default: whole file]\n");
63   fprintf(stderr,"\t-x # of lines to be ignored [default: 0]\n");
64   fprintf(stderr,"\t-c columns to read [default: 1,2]\n");
65   fprintf(stderr,"\t-m embedding dimension [default: 3]\n");
66   fprintf(stderr,"\t-d delay [default: 1]\n");
67   fprintf(stderr,"\t-n # of reference points [default: length]\n");
68   fprintf(stderr,"\t-k minimal number of neighbors for the fit "
69           "[default: 30]\n");
70   fprintf(stderr,"\t-r neighborhoud size to start with "
71           "[default: (data interval)/1000]\n");
72   fprintf(stderr,"\t-f factor to increase size [default: 1.2]\n");
73   fprintf(stderr,"\t-s steps to forecast [default: 1]\n");
74   fprintf(stderr,"\t-o output file [default: 'datafile.cze',"
75           " without -o: stdout]\n");
76   fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
77           "0='only panic messages'\n\t\t"
78           "1='+ input/output messages'\n");
79   fprintf(stderr,"\t-h show these options\n");
80   exit(0);
81 }
82
83 void scan_options(int n,char **in)
84 {
85   char *out;
86
87   if ((out=check_option(in,n,'l','u')) != NULL)
88     sscanf(out,"%lu",&LENGTH);
89   if ((out=check_option(in,n,'x','u')) != NULL)
90     sscanf(out,"%lu",&exclude);
91   if ((out=check_option(in,n,'c','s')) != NULL)
92     COLUMNS=out;
93   if ((out=check_option(in,n,'m','u')) != NULL)
94     sscanf(out,"%u",&DIM);
95   if ((out=check_option(in,n,'d','u')) != NULL)
96     sscanf(out,"%u",&DELAY);
97   if ((out=check_option(in,n,'n','u')) != NULL)
98     sscanf(out,"%lu",&CLENGTH);
99   if ((out=check_option(in,n,'k','u')) != NULL)
100     sscanf(out,"%u",&MINN);
101   if ((out=check_option(in,n,'r','f')) != NULL) {
102     epsset=1;
103     sscanf(out,"%lf",&EPS0);
104   }
105   if ((out=check_option(in,n,'f','f')) != NULL)
106     sscanf(out,"%lf",&EPSF);
107   if ((out=check_option(in,n,'s','u')) != NULL)
108     sscanf(out,"%u",&STEP);
109   if ((out=check_option(in,n,'V','u')) != NULL)
110     sscanf(out,"%u",&verbosity);
111   if ((out=check_option(in,n,'o','o')) != NULL) {
112     stdo=0;
113     if (strlen(out) > 0)
114       outfile=out;
115   }
116 }
117
118 double make_fit(unsigned long act,unsigned long number,unsigned long istep)
119 {
120   double casted=0.0;
121   int i;
122   
123   for (i=0;i<number;i++)
124     casted += series1[found[i]+istep];
125   casted /= number;
126
127   return (casted-series2[act+istep])*(casted-series2[act+istep]);
128 }
129
130 int main(int argc,char **argv)
131 {
132   char stdi=0;
133   char alldone,*done;
134   unsigned long i,j,actfound;
135   unsigned long clength;
136   unsigned int dummy=2;
137   double rms2,av2,*error;
138   double **both,hinter;
139   FILE *file;
140
141   if (scan_help(argc,argv))
142     show_options(argv[0]);
143   
144   scan_options(argc,argv);
145 #ifndef OMIT_WHAT_I_DO
146   if (verbosity&VER_INPUT)
147     what_i_do(argv[0],WID_STR);
148 #endif
149
150   infile=search_datafile(argc,argv,0L,verbosity);
151   if (infile == NULL)
152     stdi=1;
153
154   if (outfile == NULL) {
155     if (!stdi) {
156       check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
157       sprintf(outfile,"%s.cze",infile);
158     }
159     else {
160       check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
161       sprintf(outfile,"stdin.cze");
162     }
163   }
164   if (!stdo)
165     test_outfile(outfile);
166
167   if (COLUMNS == NULL)
168     both=(double**)get_multi_series(infile,&LENGTH,exclude,&dummy,"",
169                                     (char)1,verbosity);
170   else
171     both=(double**)get_multi_series(infile,&LENGTH,exclude,&dummy,COLUMNS,
172                                     (char)0,verbosity);
173   series1=both[0];
174   series2=both[1];
175   rescale_data(series1,LENGTH,&min,&hinter);
176   interval=hinter;
177   rescale_data(series2,LENGTH,&min,&hinter);
178   interval=(interval+hinter)/2.0;
179
180   variance(series2,LENGTH,&av2,&rms2);
181   
182   check_alloc(list=(long*)malloc(sizeof(long)*LENGTH));
183   check_alloc(found=(unsigned long*)malloc(sizeof(long)*LENGTH));
184   check_alloc(done=(char*)malloc(sizeof(char)*LENGTH));
185   check_alloc(box=(long**)malloc(sizeof(long*)*NMAX));
186   check_alloc(error=(double*)malloc(sizeof(double)*STEP));
187   for (i=0;i<STEP;i++)
188     error[i]=0.0;
189
190   for (i=0;i<NMAX;i++)
191     check_alloc(box[i]=(long*)malloc(sizeof(long)*NMAX));
192     
193   for (i=0;i<LENGTH;i++)
194     done[i]=0;
195
196   alldone=0;
197   if (epsset)
198     EPS0 /= interval;
199
200   epsilon=EPS0/EPSF;
201   clength=(CLENGTH <= LENGTH) ? CLENGTH-STEP : LENGTH-STEP;
202
203   while (!alldone) {
204     alldone=1;
205     epsilon*=EPSF;
206     make_box(series1,box,list,LENGTH-STEP,NMAX,DIM,DELAY,epsilon);
207     for (i=(DIM-1)*DELAY;i<clength;i++)
208       if (!done[i]) {
209         actfound=find_neighbors(series1,box,list,series2+i,LENGTH,NMAX,
210                                 DIM,DELAY,epsilon,found);
211         if (actfound >= MINN) {
212           for (j=1;j<=STEP;j++)
213             error[j-1] += make_fit(i,actfound,j);
214           done[i]=1;
215         }
216         alldone &= done[i];
217       }
218   }
219   if (stdo) {
220     if (verbosity&VER_INPUT)
221       fprintf(stderr,"Writing to stdout\n");
222     for (i=0;i<STEP;i++)
223       fprintf(stdout,"%lu %e\n",i+1,
224               sqrt(error[i]/(clength-(DIM-1)*DELAY))/rms2);
225   }
226   else {
227     file=fopen(outfile,"w");
228     if (verbosity&VER_INPUT)
229       fprintf(stderr,"Opened %s for writing\n",outfile);
230     for (i=0;i<STEP;i++)
231       fprintf(file,"%lu %e\n",i+1,
232               sqrt(error[i]/(clength-(DIM-1)*DELAY))/rms2);
233     fclose(file);
234   }
235   
236   return 0;
237 }