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 5, 2004 */
26 #include "routines/tsa.h"
28 #define WID_STR "Tests for nonstationarity by means of the average\n\t\
29 forecast error for a zeroth order fit"
36 /*number of boxes for the neighbor search algorithm*/
39 unsigned int nmax=(NMAX-1);
42 double *series,*series1,*series2;
43 double interval,min,epsilon;
45 char epsset=0,causalset=0;
47 char *outfile=NULL,stdo=1,centerset=0;
48 char *firstwindow,*secondwindow,**window;
49 unsigned int COLUMN=1,pieces;
50 unsigned int verbosity=0xff;
51 int DIM=3,DELAY=1,MINN=30,STEP=1;
52 int firstoffset= -1,secondoffset= -1;
53 double EPS0=1.e-3,EPSF=1.2;
54 unsigned long LENGTH=ULONG_MAX,exclude=0,center,causal;
56 void show_options(char *progname)
58 what_i_do(progname,WID_STR);
59 fprintf(stderr," Usage: %s -# [other 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 column to read [default: 1]\n");
68 fprintf(stderr,"\t-m embedding dimension [default: 3]\n");
69 fprintf(stderr,"\t-d delay [default: 1]\n");
70 fprintf(stderr,"\t-# # of pieces [no default]\n");
71 fprintf(stderr,"\t-1 which pieces for the first window "
72 "[default: 1-pieces]\n");
73 fprintf(stderr,"\t-2 which pieces for the second window "
74 "[default: 1-pieces]\n");
75 fprintf(stderr,"\t-n # of reference points in the window [default: all]\n");
76 fprintf(stderr,"\t-k minimal number of neighbors for the fit "
78 fprintf(stderr,"\t-r neighborhoud size to start with "
79 "[default: (data interval)/1000]\n");
80 fprintf(stderr,"\t-f factor to increase size [default: 1.2]\n");
81 fprintf(stderr,"\t-s steps to forecast [default: 1]\n");
82 fprintf(stderr,"\t-C width of causality window [default: steps]\n");
83 fprintf(stderr,"\t-o output file [default: 'datafile.nsz',"
84 " without -o: stdout]\n");
85 fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
86 "0='only panic messages'\n\t\t"
87 "1='+ input/output messages'\n");
88 fprintf(stderr,"\t-h show these options\n");
89 fprintf(stderr,"\n\t The -# option has to be set\n");
93 void parse_minus(char *str,char *array,char *wopt)
95 int cm=0,i,strl,n1,n2;
102 fprintf(stderr,"Invalid string for the %s option! "
103 "Please consult the help-page\n",wopt);
104 exit(NSTAT_Z__INVALID_STRING_FOR_OPTION);
107 sscanf(str,"%d",&n1);
110 fprintf(stderr,"Numbers in %s option must be larger than 0!\n",wopt);
111 exit(NSTAT_Z__NOT_UNSIGNED_FOR_OPTION);
114 fprintf(stderr,"Numbers in %s option must be smaller than %u!\n",wopt,
116 exit(NSTAT_Z__TOO_LARGE_FOR_OPTION);
121 sscanf(str,"%d-%d",&n1,&n2);
124 if ((n1 < 0) || (n2 < 0)) {
125 fprintf(stderr,"Numbers in %s option must be larger than 0!\n",wopt);
126 exit(NSTAT_Z__NOT_UNSIGNED_FOR_OPTION);
128 if ((n1 >= pieces) || (n2 >= pieces)) {
129 fprintf(stderr,"Numbers in %s option must be smaller than %u!\n",wopt,
131 exit(NSTAT_Z__TOO_LARGE_FOR_OPTION);
143 void parse_comma(char *str,char *array,char *wopt)
145 unsigned int strl,i,cp=1,which,iwhich;
154 parse_minus(str,array,wopt);
158 check_alloc(hstr=(char**)malloc(sizeof(char*)*cp));
160 check_alloc(hstr[i]=(char*)calloc(strl,1));
163 for (i=0;i<strl;i++) {
165 hstr[which][iwhich++]=str[i];
172 if (hstr[i][0] == '\0') {
173 fprintf(stderr,"Invalid string for the %s option! "
174 "Please consult the help-page\n",wopt);
175 exit(NSTAT_Z__INVALID_STRING_FOR_OPTION);
177 if (!isdigit(hstr[i][strlen(hstr[i])-1])) {
178 fprintf(stderr,"Invalid string for the %s option! "
179 "Please consult the help-page\n",wopt);
180 exit(NSTAT_Z__INVALID_STRING_FOR_OPTION);
182 parse_minus(hstr[i],array,wopt);
189 void parse_out(char *str,char *array,char *which)
194 for (i=0;i<pieces;i++)
197 for (i=0;i<strlen(str);i++) {
198 test= (str[i] == '-') || (str[i] == ',') || isdigit(str[i]);
200 fprintf(stderr,"Invalid string for the %s option! "
201 "Please consult the help-page\n",which);
202 exit(NSTAT_Z__INVALID_STRING_FOR_OPTION);
205 if (!isdigit(str[strlen(str)-1])) {
206 fprintf(stderr,"Invalid string for the %s option! "
207 "Please consult the help-page\n",which);
208 exit(NSTAT_Z__INVALID_STRING_FOR_OPTION);
210 parse_comma(str,array,which);
213 void parse_offset(char *str,int *iwhich,char *array,char *which)
221 if (!isdigit(str[i])) {
222 fprintf(stderr,"Invalid string for the %s option! "
223 "Please consult the help-page\n",which);
224 exit(NSTAT_Z__INVALID_STRING_FOR_OPTION);
226 sscanf(str,"+%d",iwhich);
227 for (i=0;i<pieces;i++)
231 void scan_options(int n,char **in)
234 char *out,piecesset=0;
236 if ((out=check_option(in,n,'l','u')) != NULL)
237 sscanf(out,"%lu",&LENGTH);
238 if ((out=check_option(in,n,'x','u')) != NULL)
239 sscanf(out,"%lu",&exclude);
240 if ((out=check_option(in,n,'c','u')) != NULL)
241 sscanf(out,"%u",&COLUMN);
242 if ((out=check_option(in,n,'m','u')) != NULL)
243 sscanf(out,"%u",&DIM);
244 if ((out=check_option(in,n,'d','u')) != NULL)
245 sscanf(out,"%u",&DELAY);
246 if ((out=check_option(in,n,'V','u')) != NULL)
247 sscanf(out,"%u",&verbosity);
248 if ((out=check_option(in,n,'#','u')) != NULL) {
249 sscanf(out,"%u",&pieces);
253 check_alloc(firstwindow=(char*)malloc(pieces));
254 check_alloc(secondwindow=(char*)malloc(pieces));
255 for (i=0;i<pieces;i++)
256 firstwindow[i]=secondwindow[i]=1;
257 check_alloc(window=(char**)malloc(sizeof(char*)*pieces));
258 for (i=0;i<pieces;i++)
259 check_alloc(window[i]=(char*)malloc(pieces));
262 fprintf(stderr,"\tThe -# option wasn't set. Please add it!\n");
263 exit(NSTAT_Z__OPTION_NOT_SET);
265 if ((out=check_option(in,n,'1','s')) != NULL) {
266 parse_offset(out,&firstoffset,firstwindow,"-1");
267 if (firstoffset == -1)
268 parse_out(out,firstwindow,"-1");
270 if ((out=check_option(in,n,'2','s')) != NULL) {
271 parse_offset(out,&secondoffset,secondwindow,"-2");
272 if (secondoffset == -1)
273 parse_out(out,secondwindow,"-2");
275 if ((out=check_option(in,n,'n','u')) != NULL) {
276 sscanf(out,"%lu",¢er);
279 if ((out=check_option(in,n,'k','u')) != NULL)
280 sscanf(out,"%u",&MINN);
281 if ((out=check_option(in,n,'r','f')) != NULL) {
283 sscanf(out,"%lf",&EPS0);
285 if ((out=check_option(in,n,'f','f')) != NULL)
286 sscanf(out,"%lf",&EPSF);
287 if ((out=check_option(in,n,'s','u')) != NULL)
288 sscanf(out,"%u",&STEP);
289 if ((out=check_option(in,n,'C','u')) != NULL) {
290 sscanf(out,"%lu",&causal);
293 if ((out=check_option(in,n,'o','o')) != NULL) {
300 double make_fit(long act,unsigned long number)
302 double casted=0.0,*help;
306 for (i=0;i<number;i++) {
307 casted += help[found[i]];
311 return sqr(casted-series2[act+STEP]);
314 int main(int argc,char **argv)
317 char alldone,*done,sdone;
318 long i,first,second,pstart;
319 unsigned long *hfound;
320 unsigned long actfound;
321 unsigned long clength;
322 double *rms,av,error;
325 if (scan_help(argc,argv))
326 show_options(argv[0]);
328 scan_options(argc,argv);
333 #ifndef OMIT_WHAT_I_DO
334 if (verbosity&VER_INPUT)
335 what_i_do(argv[0],WID_STR);
338 infile=search_datafile(argc,argv,&COLUMN,verbosity);
342 if (outfile == NULL) {
344 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
345 sprintf(outfile,"%s.nsz",infile);
348 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
349 sprintf(outfile,"stdin.nsz");
353 test_outfile(outfile);
355 series=(double*)get_series(infile,&LENGTH,exclude,COLUMN,verbosity);
357 rescale_data(series,LENGTH,&min,&interval);
359 check_alloc(list=(long*)malloc(sizeof(long)*LENGTH));
360 check_alloc(found=(unsigned long*)malloc(sizeof(long)*LENGTH));
361 check_alloc(hfound=(unsigned long*)malloc(sizeof(long)*LENGTH));
362 check_alloc(done=(char*)malloc(sizeof(char)*LENGTH));
363 check_alloc(box=(long**)malloc(sizeof(long*)*NMAX));
366 check_alloc(box[i]=(long*)malloc(sizeof(long)*NMAX));
371 clength=(LENGTH-(DIM-1)*DELAY)/pieces;
372 if ((clength-(DIM-1)*DELAY-STEP) < MINN) {
373 fprintf(stderr,"You chose too many pieces and will never find enough"
375 exit(NSTAT_Z__TOO_MANY_PIECES);
377 check_alloc(rms=(double*)malloc(sizeof(double)*pieces));
378 for (i=0;i<pieces;i++) {
379 series1=series+i*clength;
380 variance(series1,clength,&av,&rms[i]);
383 pstart=(DIM-1)*DELAY;
387 center=(center < (clength-STEP-pstart)) ? center : clength-STEP-pstart;
390 if (verbosity&VER_INPUT)
391 fprintf(stderr,"Writing to stdout\n");
394 file=fopen(outfile,"w");
395 if (verbosity&VER_INPUT)
396 fprintf(stderr,"Opened %s for writing\n",outfile);
398 for (first=0;first<pieces;first++)
399 for (second=0;second<pieces;second++)
400 window[first][second]=firstwindow[first]&&secondwindow[second];
401 if (firstoffset != -1) {
402 for (second=0;second<pieces;second++)
403 for (first=second-firstoffset;first<=second+firstoffset;first++)
404 if ((first >= 0) && (first < pieces))
405 window[first][second]=secondwindow[second];
407 if (secondoffset != -1) {
408 for (first=0;first<pieces;first++)
409 for (second=first-secondoffset;second<=first+secondoffset;second++)
410 if ((second >= 0) && (second < pieces))
411 window[first][second]=firstwindow[first];
417 for (first=0;first<pieces;first++) {
419 for (second=0;second<pieces;second++) {
420 if (window[first][second]) {
422 series1=series+first*clength;
423 series2=series+second*clength;
424 for (i=0;i<LENGTH;i++)
432 make_box(series1,box,list,clength-STEP,NMAX,(unsigned int)DIM,
433 (unsigned int)DELAY,epsilon);
434 for (i=pstart;i<pstart+center;i++)
436 actfound=find_neighbors(series1,box,list,series2+i,clength,NMAX,
437 (unsigned int)DIM,(unsigned int)DELAY,
439 actfound=exclude_interval(actfound,i-causal+1,
440 i+causal+pstart-1,hfound,found);
441 if (actfound >= MINN) {
442 error += make_fit(i,actfound);
449 fprintf(stdout,"%ld %ld %e\n",first+1,second+1,
450 sqrt(error/center)/rms[second]);
452 fprintf(file,"%ld %ld %e\n",first+1,second+1,
453 sqrt(error/center)/rms[second]);
460 fprintf(stdout,"\n");
478 for (i=0;i<pieces;i++)