2 * This file is part of TISEAN
4 * Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber
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.
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.
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
20 /*Author: Rainer Hegger. Last modified: Sep 4, 1999 */
25 #include "routines/tsa.h"
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."
34 /*number of boxes for the neighbor search algorithm*/
37 unsigned int nmax=(NMAX-1);
40 double *series1,*series2;
41 double interval,min,epsilon;
45 char *outfile=NULL,stdo=1;
47 unsigned int DIM=3,DELAY=1;
48 unsigned int verbosity=0xff;
50 double EPS0=1.e-3,EPSF=1.2;
51 unsigned long LENGTH=ULONG_MAX,exclude=0,CLENGTH=ULONG_MAX;
53 void show_options(char *progname)
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"
60 " datafile.\nIf no datafile is given stdin is read. Just - also"
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 "
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");
83 void scan_options(int n,char **in)
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)
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) {
103 sscanf(out,"%lf",&EPS0);
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) {
118 double make_fit(unsigned long act,unsigned long number,unsigned long istep)
123 for (i=0;i<number;i++)
124 casted += series1[found[i]+istep];
127 return (casted-series2[act+istep])*(casted-series2[act+istep]);
130 int main(int argc,char **argv)
134 unsigned long i,j,actfound;
135 unsigned long clength;
136 unsigned int dummy=2;
137 double rms2,av2,*error;
138 double **both,hinter;
141 if (scan_help(argc,argv))
142 show_options(argv[0]);
144 scan_options(argc,argv);
145 #ifndef OMIT_WHAT_I_DO
146 if (verbosity&VER_INPUT)
147 what_i_do(argv[0],WID_STR);
150 infile=search_datafile(argc,argv,0L,verbosity);
154 if (outfile == NULL) {
156 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
157 sprintf(outfile,"%s.cze",infile);
160 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
161 sprintf(outfile,"stdin.cze");
165 test_outfile(outfile);
168 both=(double**)get_multi_series(infile,&LENGTH,exclude,&dummy,"",
171 both=(double**)get_multi_series(infile,&LENGTH,exclude,&dummy,COLUMNS,
175 rescale_data(series1,LENGTH,&min,&hinter);
177 rescale_data(series2,LENGTH,&min,&hinter);
178 interval=(interval+hinter)/2.0;
180 variance(series2,LENGTH,&av2,&rms2);
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));
191 check_alloc(box[i]=(long*)malloc(sizeof(long)*NMAX));
193 for (i=0;i<LENGTH;i++)
201 clength=(CLENGTH <= LENGTH) ? CLENGTH-STEP : LENGTH-STEP;
206 make_box(series1,box,list,LENGTH-STEP,NMAX,DIM,DELAY,epsilon);
207 for (i=(DIM-1)*DELAY;i<clength;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);
220 if (verbosity&VER_INPUT)
221 fprintf(stderr,"Writing to stdout\n");
223 fprintf(stdout,"%lu %e\n",i+1,
224 sqrt(error[i]/(clength-(DIM-1)*DELAY))/rms2);
227 file=fopen(outfile,"w");
228 if (verbosity&VER_INPUT)
229 fprintf(stderr,"Opened %s for writing\n",outfile);
231 fprintf(file,"%lu %e\n",i+1,
232 sqrt(error[i]/(clength-(DIM-1)*DELAY))/rms2);