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: Jun 21, 2005 */
22 Jun 17, 2005: Comments in the output file updated
23 Jun 21, 2005: free imat in make_fit
29 #include "routines/tsa.h"
32 #define WID_STR "Estimates the average forecast error for a local\n\t\
33 linear fit as a function of the neighborhood size."
36 /*number of boxes for the neighbor search algorithm*/
39 unsigned int nmax=(NMAX-1);
45 char eps0set=0,eps1set=0,causalset=0,dimset=0;
46 char *outfile=NULL,stdo=1;
48 unsigned int dim=1,embed=2,delay=1;
49 unsigned int verbosity=0xff;
51 double EPS0=1.e-3,EPS1=1.0,EPSF=1.2;
52 unsigned long LENGTH=ULONG_MAX,exclude=0,CLENGTH=ULONG_MAX,causal;
54 double **mat,*vec,*localav,*foreav,*hvec;
56 void show_options(char *progname)
58 what_i_do(progname,WID_STR);
59 fprintf(stderr," Usage: %s [options]\n",progname);
60 fprintf(stderr," Options:\n");
61 fprintf(stderr,"Everything not being a valid option will be interpreted"
63 " datafile.\nIf no datafile is given stdin is read. Just - also"
65 fprintf(stderr,"\t-l # of data to use [default: whole file]\n");
66 fprintf(stderr,"\t-x # of lines to be ignored [default: 0]\n");
67 fprintf(stderr,"\t-c columns to read [default: 1,...,# of components]\n");
68 fprintf(stderr,"\t-m # of components,embedding dimension [default: 1,2]\n");
69 fprintf(stderr,"\t-d delay [default: 1]\n");
70 fprintf(stderr,"\t-i iterations [default: length]\n");
71 fprintf(stderr,"\t-r neighborhood size to start with [default:"
72 " (interval of data)/1000)]\n");
73 fprintf(stderr,"\t-R neighborhood size to end with [default:"
74 " interval of data]\n");
75 fprintf(stderr,"\t-f factor to increase size [default: 1.2]\n");
76 fprintf(stderr,"\t-s steps to forecast [default: 1]\n");
77 fprintf(stderr,"\t-C width of causality window [default: steps]\n");
78 fprintf(stderr,"\t-o output file name [default: 'datafile.ll']\n");
79 fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
80 "0='only panic messages'\n\t\t"
81 "1='+ input/output messages'\n");
82 fprintf(stderr,"\t-h show these options\n");
86 void scan_options(int n,char **in)
90 if ((out=check_option(in,n,'l','u')) != NULL)
91 sscanf(out,"%lu",&LENGTH);
92 if ((out=check_option(in,n,'x','u')) != NULL)
93 sscanf(out,"%lu",&exclude);
94 if ((out=check_option(in,n,'c','s')) != NULL) {
98 if ((out=check_option(in,n,'m','2')) != NULL)
99 sscanf(out,"%u,%u",&dim,&embed);
100 if ((out=check_option(in,n,'d','u')) != NULL)
101 sscanf(out,"%u",&delay);
102 if ((out=check_option(in,n,'i','u')) != NULL)
103 sscanf(out,"%lu",&CLENGTH);
104 if ((out=check_option(in,n,'r','f')) != NULL) {
106 sscanf(out,"%lf",&EPS0);
108 if ((out=check_option(in,n,'R','f')) != NULL) {
110 sscanf(out,"%lf",&EPS1);
112 if ((out=check_option(in,n,'f','f')) != NULL)
113 sscanf(out,"%lf",&EPSF);
114 if ((out=check_option(in,n,'s','u')) != NULL)
115 sscanf(out,"%u",&STEP);
116 if ((out=check_option(in,n,'C','u')) != NULL) {
117 sscanf(out,"%lu",&causal);
120 if ((out=check_option(in,n,'V','u')) != NULL)
121 sscanf(out,"%u",&verbosity);
122 if ((out=check_option(in,n,'o','o')) != NULL) {
129 void multiply_matrix(double **mat,double *vec)
133 for (i=0;i<dim*embed;i++) {
135 for (j=0;j<dim*embed;j++)
136 hvec[i] += mat[i][j]*vec[j];
138 for (i=0;i<dim*embed;i++)
142 void make_fit(long act,unsigned long number)
144 double *si,*sj,lavi,lavj,fav,**imat,cast;
145 long i,i1,hi,hi1,j,j1,hj,hj1,n,which;
147 for (i=0;i<embed*dim;i++)
152 for (n=0;n<number;n++) {
154 for (j=0;j<dim;j++) {
156 foreav[j] += sj[which+STEP];
157 for (j1=0;j1<embed;j1++) {
159 localav[hj] += sj[which-j1*delay];
164 for (i=0;i<dim*embed;i++)
165 localav[i] /= number;
169 for (i=0;i<dim;i++) {
171 for (i1=0;i1<embed;i1++) {
175 for (j=0;j<dim;j++) {
177 for (j1=0;j1<embed;j1++) {
183 for (n=0;n<number;n++) {
185 mat[hi][hj] += (si[which-hi1]-lavi)*(sj[which-hj1]-lavj);
193 for (i=0;i<dim*embed;i++)
194 for (j=i;j<dim*embed;j++) {
199 imat=invert_matrix(mat,dim*embed);
201 for (i=0;i<dim;i++) {
204 for (j=0;j<dim;j++) {
206 for (j1=0;j1<embed;j1++) {
211 for (n=0;n<number;n++) {
213 vec[hj] += (si[which+STEP]-fav)*(sj[which-hj1]-lavj);
219 multiply_matrix(imat,vec);
222 for (j=0;j<dim;j++) {
224 for (j1=0;j1<embed;j1++) {
226 cast += vec[hj]*(sj[act-j1*delay]-localav[hj]);
229 error[i] += sqr(cast-series[i][act+STEP]);
231 for (i=0;i<embed*dim;i++)
236 int main(int argc,char **argv)
239 unsigned long actfound;
240 unsigned long *hfound;
242 unsigned long clength;
243 double interval,min,maxinterval;
246 double avfound,*hrms,*hav,sumerror=0.0;
249 if (scan_help(argc,argv))
250 show_options(argv[0]);
252 scan_options(argc,argv);
253 #ifndef OMIT_WHAT_I_DO
254 if (verbosity&VER_INPUT)
255 what_i_do(argv[0],WID_STR);
261 infile=search_datafile(argc,argv,NULL,verbosity);
265 if (outfile == NULL) {
267 check_alloc(outfile=(char*)calloc(strlen(infile)+4,(size_t)1));
268 sprintf(outfile,"%s.ll",infile);
271 check_alloc(outfile=(char*)calloc((size_t)9,(size_t)1));
272 sprintf(outfile,"stdin.ll");
276 test_outfile(outfile);
279 series=(double**)get_multi_series(infile,&LENGTH,exclude,&dim,"",dimset,
282 series=(double**)get_multi_series(infile,&LENGTH,exclude,&dim,column,
285 for (i=0;i<dim;i++) {
286 rescale_data(series[i],LENGTH,&min,&interval);
287 if (interval > maxinterval)
288 maxinterval=interval;
290 interval=maxinterval;
292 check_alloc(list=(long*)malloc(sizeof(long)*LENGTH));
293 check_alloc(found=(unsigned long*)malloc(sizeof(long)*LENGTH));
294 check_alloc(hfound=(unsigned long*)malloc(sizeof(long)*LENGTH));
295 check_alloc(box=(long**)malloc(sizeof(long*)*NMAX));
297 check_alloc(box[i]=(long*)malloc(sizeof(long)*NMAX));
298 check_alloc(vec=(double*)malloc(sizeof(double)*(embed*dim)));
299 check_alloc(hvec=(double*)malloc(sizeof(double)*(embed*dim)));
300 check_alloc(mat=(double**)malloc(sizeof(double*)*(embed*dim)));
301 for (i=0;i<dim*embed;i++)
302 check_alloc(mat[i]=(double*)malloc(sizeof(double)*(embed*dim)));
303 check_alloc(error=(double*)malloc(sizeof(double)*dim));
304 check_alloc(hrms=(double*)malloc(sizeof(double)*dim));
305 check_alloc(hav=(double*)malloc(sizeof(double)*dim));
306 check_alloc(hser=(double**)malloc(sizeof(double*)*dim));
307 check_alloc(foreav=(double*)malloc(sizeof(double)*dim));
308 check_alloc(localav=(double*)malloc(sizeof(double)*(embed*dim)));
315 clength=(CLENGTH <= LENGTH) ? CLENGTH-STEP : LENGTH-STEP;
318 file=fopen(outfile,"w");
319 if (verbosity&VER_INPUT)
320 fprintf(stderr,"Opened %s for writing\n",outfile);
321 fprintf(file,"#1.) neighborhood size\n");
322 fprintf(file,"#2.) average relative forecast error\n");
323 fprintf(file,"#next n.) relative forecast error of the n components\n");
324 fprintf(file,"#second last.) fraction of points with enough neighbors\n");
325 fprintf(file,"#last .) average number of neighbors used for the fit\n");
328 if (verbosity&VER_INPUT)
329 fprintf(stderr,"Writing to stdout\n");
332 for (epsilon=EPS0;epsilon<EPS1*EPSF;epsilon*=EPSF) {
335 error[i]=hrms[i]=hav[i]=0.0;
337 make_multi_box(series,box,list,LENGTH-STEP,NMAX,dim,
338 embed,delay,epsilon);
339 for (i=(embed-1)*delay;i<clength;i++) {
342 actfound=find_multi_neighbors(series,box,list,hser,LENGTH,
343 NMAX,dim,embed,delay,epsilon,hfound);
344 actfound=exclude_interval(actfound,i-causal+1,i+causal+(embed-1)*delay-1,
346 if (actfound > 2*(dim*embed+1)) {
347 make_fit(i,actfound);
349 avfound += (double)(actfound-1);
350 for (j=0;j<dim;j++) {
351 hrms[j] += series[j][i+STEP]*series[j][i+STEP];
352 hav[j] += series[j][i+STEP];
358 for (j=0;j<dim;j++) {
360 hrms[j]=sqrt(fabs(hrms[j]/(pfound-1)-hav[j]*hav[j]*pfound/(pfound-1)));
361 error[j]=sqrt(error[j]/pfound)/hrms[j];
362 sumerror += error[j];
367 fprintf(stdout,"%e %e ",epsilon*interval,sumerror/(double)dim);
369 fprintf(stdout,"%e ",error[j]);
370 fprintf(stdout,"%e %e\n",(double)pfound/(clength-(embed-1)*delay),
377 fprintf(file,"%e %e ",epsilon*interval,sumerror/(double)dim);
379 fprintf(file,"%e ",error[j]);
380 fprintf(file,"%e %e\n",(double)pfound/(clength-(embed-1)*delay),