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 Dec 4, 2005 */
22 7/14/05: Changed borders of the sort routine to speed things up
23 11/25/05: Show also absolute forecast errors
24 12/04/05: Some more changes in sort
25 12/20/05: Change in increase neighborhood size loop
26 12/28/05: Found bug in memory allocation (index)
35 #include "routines/tsa.h"
37 #define WID_STR "Estimates the spectrum of Lyapunov exponents using the\n\t\
38 method of Sano and Sawada."
47 char INVERSE,*outfile=NULL;
51 unsigned long LENGTH=ULONG_MAX,ITERATIONS,exclude=0;
52 unsigned int EMBED=2,DIMENSION=1/*,DELAY=1*/,MINNEIGHBORS=30;
53 unsigned int verbosity=0xff;
56 double **series,*averr,avneig=0.0,aveps=0.0;
57 double **mat,*vec,*abstand;
59 long imax=BOX-1,count=0;
62 unsigned int alldim,**indexes;
64 void show_options(char *progname)
66 what_i_do(progname,WID_STR);
67 fprintf(stderr," Usage: %s [options]\n",progname);
68 fprintf(stderr," Options:\n");
69 fprintf(stderr,"Everything not being a valid option will be interpreted"
71 " datafile.\nIf no datafile is given stdin is read. Just - also"
73 fprintf(stderr,"\t-l # of datapoints [default is whole file]\n");
74 fprintf(stderr,"\t-x # of lines to be ignored [default is 0]\n");
75 fprintf(stderr,"\t-c column to read[default 1]\n");
76 fprintf(stderr,"\t-m # of components,embedding dimension [default %d,%d]\n",
78 // fprintf(stderr,"\t-d delay [default %d]\n",DELAY);
79 fprintf(stderr,"\t-r epsilon size to start with [default "
80 "(data interval)/1000]\n");
81 fprintf(stderr,"\t-f factor to increase epsilon [default: 1.2]\n");
82 fprintf(stderr,"\t-k # of neighbors to use [default: 30]\n");
83 fprintf(stderr,"\t-n # of iterations [default: length]\n");
84 fprintf(stderr,"\t-I invert the time series [default: no]\n");
85 fprintf(stderr,"\t-o name of output file [default 'datafile'.lyaps]\n");
86 fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
87 "0='only panic messages'\n\t\t"
88 "1='+ input/output messages'\n");
89 fprintf(stderr,"\t-h show these options\n");
94 void scan_options(int n,char **argv)
98 if ((out=check_option(argv,n,'l','u')) != NULL)
99 sscanf(out,"%lu",&LENGTH);
100 if ((out=check_option(argv,n,'x','u')) != NULL)
101 sscanf(out,"%lu",&exclude);
102 if ((out=check_option(argv,n,'c','s')) != NULL)
104 /* if ((out=check_option(argv,n,'d','u')) != NULL)
105 sscanf(out,"%u",&DELAY);*/
106 if ((out=check_option(argv,n,'m','2')) != NULL) {
107 sscanf(out,"%u,%u",&DIMENSION,&EMBED);
110 if ((out=check_option(argv,n,'n','u')) != NULL)
111 sscanf(out,"%lu",&ITERATIONS);
112 if ((out=check_option(argv,n,'r','f')) != NULL) {
114 sscanf(out,"%lf",&epsmin);
116 if ((out=check_option(argv,n,'f','f')) != NULL)
117 sscanf(out,"%lf",&EPSSTEP);
118 if ((out=check_option(argv,n,'k','u')) != NULL)
119 sscanf(out,"%u",&MINNEIGHBORS);
120 if ((out=check_option(argv,n,'V','u')) != NULL)
121 sscanf(out,"%u",&verbosity);
122 if ((out=check_option(argv,n,'I','n')) != NULL)
124 if ((out=check_option(argv,n,'o','o')) != NULL) {
131 double sort(long act,unsigned long* nfound,char *enough)
133 double maxeps=0.0,dx,dswap,maxdx;
134 long self=0,i,j,del,hf,iswap,n1;
135 unsigned long imax=*nfound;
139 for (i=0;i<imax;i++) {
142 maxdx=fabs(series[0][act]-series[0][hf]);
143 for (j=1;j<alldim;j++) {
146 dx=fabs(series[n1][act-del]-series[n1][hf-del]);
147 if (dx > maxdx) maxdx=dx;
156 if (self != (imax-1)) {
157 abstand[self]=abstand[imax-1];
158 found[self]=found[imax-1];
161 for (i=0;i<MINNEIGHBORS;i++) {
162 for (j=i+1;j<imax-1;j++) {
163 if (abstand[j]<abstand[i]) {
165 abstand[i]=abstand[j];
174 if (!epsset || (abstand[MINNEIGHBORS-1] >= epsmin)) {
175 *nfound=MINNEIGHBORS;
177 maxeps=abstand[MINNEIGHBORS-1];
182 for (i=MINNEIGHBORS;i<imax-2;i++) {
183 for (j=i+1;j<imax-1;j++) {
184 if (abstand[j]<abstand[i]) {
186 abstand[i]=abstand[j];
193 if (abstand[i] > epsmin) {
202 maxeps=abstand[imax-2];
207 void make_dynamics(double **dynamics,long act)
209 long i,hi,j,hj,k,t=act,d;
210 unsigned long nfound=0;
211 double **hser,**imat;
212 double foundeps=0.0,epsilon,hv,hv1;
216 check_alloc(hser=(double**)malloc(sizeof(double*)*DIMENSION));
217 for (i=0;i<DIMENSION;i++)
218 hser[i]=series[i]+act;
220 epsilon=epsmin/EPSSTEP;
223 if (epsilon > EPSMAX)
225 make_multi_box(series,box,list,LENGTH-DELAY,BOX,DIMENSION,EMBED,
227 nfound=find_multi_neighbors(series,box,list,hser,LENGTH-DELAY,BOX,
228 DIMENSION,EMBED,DELAY,epsilon,found);
229 if (nfound > MINNEIGHBORS) {
230 foundeps=sort(act,&nfound,&got_enough);
234 } while (epsilon < EPSMAX);
242 if (nfound < MINNEIGHBORS) {
243 fprintf(stderr,"#Not enough neighbors found. Exiting\n");
244 exit(LYAP_SPEC_NOT_ENOUGH_NEIGHBORS);
247 for (i=0;i<=alldim;i++) {
249 for (j=0;j<=alldim;j++)
253 for (i=0;i<nfound;i++) {
256 for (j=0;j<alldim;j++)
257 mat[0][j+1] += series[indexes[0][j]][act-indexes[1][j]];
258 for (j=0;j<alldim;j++) {
259 hv1=series[indexes[0][j]][act-indexes[1][j]];
261 for (k=j;k<alldim;k++)
262 mat[hj][k+1] += series[indexes[0][k]][act-indexes[1][k]]*hv1;
266 for (i=0;i<=alldim;i++)
267 for (j=i;j<=alldim;j++)
268 mat[j][i]=(mat[i][j]/=(double)nfound);
270 imat=invert_matrix(mat,alldim+1);
272 for (d=0;d<DIMENSION;d++) {
273 for (i=0;i<=alldim;i++)
275 for (i=0;i<nfound;i++) {
277 hv=series[d][act+DELAY];
279 for (j=0;j<alldim;j++)
280 vec[j+1] += hv*series[indexes[0][j]][act-indexes[1][j]];
282 for (i=0;i<=alldim;i++)
283 vec[i] /= (double)nfound;
286 for (i=0;i<=alldim;i++)
287 new_vec += imat[0][i]*vec[i];
288 for (i=1;i<=alldim;i++) {
291 for (j=0;j<=alldim;j++)
292 dynamics[d][hi] += imat[i][j]*vec[j];
294 for (i=0;i<alldim;i++)
295 new_vec += dynamics[d][i]*series[indexes[0][i]][t-indexes[1][i]];
296 averr[d] += (new_vec-series[d][t+DELAY])*(new_vec-series[d][t+DELAY]);
299 for (i=0;i<=alldim;i++)
304 void gram_schmidt(double **delta,
307 double **dnew,norm,*diff;
310 check_alloc(diff=(double*)malloc(sizeof(double)*alldim));
311 check_alloc(dnew=(double**)malloc(sizeof(double*)*alldim));
312 for (i=0;i<alldim;i++)
313 check_alloc(dnew[i]=(double*)malloc(sizeof(double)*alldim));
315 for (i=0;i<alldim;i++) {
316 for (j=0;j<alldim;j++)
320 for (k=0;k<alldim;k++)
321 norm += delta[i][k]*dnew[j][k];
322 for (k=0;k<alldim;k++)
323 diff[k] -= norm*dnew[j][k];
326 for (j=0;j<alldim;j++)
327 norm += sqr(delta[i][j]+diff[j]);
328 stretch[i]=(norm=sqrt(norm));
329 for (j=0;j<alldim;j++)
330 dnew[i][j]=(delta[i][j]+diff[j])/norm;
332 for (i=0;i<alldim;i++)
333 for (j=0;j<alldim;j++)
334 delta[i][j]=dnew[i][j];
337 for (i=0;i<alldim;i++)
342 void make_iteration(double **dynamics,
348 check_alloc(dnew=(double**)malloc(sizeof(double*)*alldim));
349 for (i=0;i<alldim;i++)
350 check_alloc(dnew[i]=(double*)malloc(sizeof(double)*alldim));
352 for (i=0;i<alldim;i++) {
353 for (j=0;j<DIMENSION;j++) {
354 dnew[i][j]=dynamics[j][0]*delta[i][0];
355 for (k=1;k<alldim;k++)
356 dnew[i][j] += dynamics[j][k]*delta[i][k];
358 for (j=DIMENSION;j<alldim;j++)
359 dnew[i][j]=delta[i][j-1];
362 for (i=0;i<alldim;i++)
363 for (j=0;j<alldim;j++)
364 delta[i][j]=dnew[i][j];
366 for (i=0;i<alldim;i++)
371 int main(int argc,char **argv)
374 double **delta,**dynamics,*lfactor;
377 double *interval,*min,*av,*var,maxinterval;
379 time_t lasttime,newtime;
382 if (scan_help(argc,argv))
383 show_options(argv[0]);
385 ITERATIONS=ULONG_MAX;
387 scan_options(argc,argv);
388 #ifndef OMIT_WHAT_I_DO
389 if (verbosity&VER_INPUT)
390 what_i_do(argv[0],WID_STR);
393 infile=search_datafile(argc,argv,NULL,verbosity);
397 if (outfile == NULL) {
399 check_alloc(outfile=(char*)calloc(strlen(infile)+7,(size_t)1));
400 strcpy(outfile,infile);
401 strcat(outfile,".lyaps");
404 check_alloc(outfile=(char*)calloc((size_t)12,(size_t)1));
405 strcpy(outfile,"stdin.lyaps");
409 test_outfile(outfile);
411 alldim=DIMENSION*EMBED;
414 series=(double**)get_multi_series(infile,&LENGTH,exclude,&DIMENSION,"",
417 series=(double**)get_multi_series(infile,&LENGTH,exclude,&DIMENSION,
418 COLUMNS,dimset,verbosity);
420 if (MINNEIGHBORS > (LENGTH-DELAY*(EMBED-1)-1)) {
421 fprintf(stderr,"Your time series is not long enough to find %d neighbors!"
422 " Exiting.\n",MINNEIGHBORS);
423 exit(LYAP_SPEC_DATA_TOO_SHORT);
426 check_alloc(min=(double*)malloc(sizeof(double)*DIMENSION));
427 check_alloc(interval=(double*)malloc(sizeof(double)*DIMENSION));
428 check_alloc(av=(double*)malloc(sizeof(double)*DIMENSION));
429 check_alloc(var=(double*)malloc(sizeof(double)*DIMENSION));
430 check_alloc(averr=(double*)malloc(sizeof(double)*DIMENSION));
432 for (i=0;i<DIMENSION;i++) {
434 rescale_data(series[i],LENGTH,&min[i],&interval[i]);
435 if (interval[i] > maxinterval)
436 maxinterval=interval[i];
437 variance(series[i],LENGTH,&av[i],&var[i]);
441 check_alloc(hseries=(double*)malloc(sizeof(double)*LENGTH));
442 for (j=0;j<DIMENSION;j++) {
443 for (i=0;i<LENGTH;i++)
444 hseries[LENGTH-1-i]=series[j][i];
445 for (i=0;i<LENGTH;i++)
446 series[j][i]=hseries[i];
454 epsmin /= maxinterval;
456 check_alloc(box=(long**)malloc(sizeof(long*)*BOX));
458 check_alloc(box[i]=(long*)malloc(sizeof(long)*BOX));
460 check_alloc(list=(long*)malloc(sizeof(long)*LENGTH));
461 check_alloc(found=(unsigned long*)malloc(sizeof(long)*LENGTH));
463 check_alloc(dynamics=(double**)malloc(sizeof(double*)*DIMENSION));
464 for (i=0;i<DIMENSION;i++)
465 check_alloc(dynamics[i]=(double*)malloc(sizeof(double)*alldim));
466 check_alloc(factor=(double*)malloc(sizeof(double)*alldim));
467 check_alloc(lfactor=(double*)malloc(sizeof(double)*alldim));
468 check_alloc(delta=(double**)malloc(sizeof(double*)*alldim));
469 for (i=0;i<alldim;i++)
470 check_alloc(delta[i]=(double*)malloc(sizeof(double)*alldim));
472 check_alloc(vec=(double*)malloc(sizeof(double)*(alldim+1)));
473 check_alloc(mat=(double**)malloc(sizeof(double*)*(alldim+1)));
474 for (i=0;i<=alldim;i++)
475 check_alloc(mat[i]=(double*)malloc(sizeof(double)*(alldim+1)));
477 indexes=(unsigned int**)make_multi_index(DIMENSION,EMBED,DELAY);
480 for (i=0;i<10000;i++)
482 for (i=0;i<alldim;i++) {
484 for (j=0;j<alldim;j++)
485 delta[i][j]=(double)rnd_long()/(double)ULONG_MAX;
487 gram_schmidt(delta,lfactor);
490 if (start>(LENGTH-DELAY))
494 file=fopen(outfile,"w");
495 if (verbosity&VER_INPUT)
496 fprintf(stderr,"Opened %s for writing\n",outfile);
499 if (verbosity&VER_INPUT)
500 fprintf(stderr,"Writing to stdout\n");
503 check_alloc(abstand=(double*)malloc(sizeof(double)*LENGTH));
506 for (i=(EMBED-1)*DELAY;i<start;i++) {
508 make_dynamics(dynamics,i);
509 make_iteration(dynamics,delta);
510 gram_schmidt(delta,lfactor);
511 for (j=0;j<alldim;j++) {
512 factor[j] += log(lfactor[j])/(double)DELAY;
514 if (((time(&newtime)-lasttime) > OUT) || (i == (start-1))) {
517 fprintf(file,"%ld ",count);
518 for (j=0;j<alldim;j++)
519 fprintf(file,"%e ",factor[j]/count);
524 fprintf(stdout,"%ld ",count);
525 for (j=0;j<alldim;j++)
526 fprintf(stdout,"%e ",factor[j]/count);
527 fprintf(stdout,"\n");
533 for (i=0;i<alldim;i++) {
539 dim=i+(dim-factor[i])/fabs(factor[i]);
543 fprintf(file,"#Average relative forecast errors:= ");
544 for (i=0;i<DIMENSION;i++)
545 fprintf(file,"%e ",sqrt(averr[i]/count)/var[i]);
547 fprintf(file,"#Average absolute forecast errors:= ");
548 for (i=0;i<DIMENSION;i++)
549 fprintf(file,"%e ",sqrt(averr[i]/count)*interval[i]);
551 fprintf(file,"#Average Neighborhood Size= %e\n",aveps*maxinterval/count);
552 fprintf(file,"#Average num. of neighbors= %e\n",avneig/count);
553 fprintf(file,"#estimated KY-Dimension= %f\n",dim);
556 fprintf(stdout,"#Average relative forecast errors:= ");
557 for (i=0;i<DIMENSION;i++)
558 fprintf(stdout,"%e ",sqrt(averr[i]/count)/var[i]);
559 fprintf(stdout,"\n");
560 fprintf(stdout,"#Average absolute forecast errors:= ");
561 for (i=0;i<DIMENSION;i++)
562 fprintf(stdout,"%e ",sqrt(averr[i]/count)*interval[i]);
563 fprintf(stdout,"\n");
564 fprintf(stdout,"#Average Neighborhood Size= %e\n",aveps*maxinterval/count);
565 fprintf(stdout,"#Average num. of neighbors= %e\n",avneig/count);
566 fprintf(stdout,"#estimated KY-Dimension= %f\n",dim);