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 29, 2000 */
26 #include "routines/tsa.h"
28 #define WID_STR "Makes a local linear fit for multivariate data\n\
29 and iterates a trajectory"
33 char onscreen=1,epsset=0,*outfile=NULL;
35 unsigned int nmax=(NMAX-1);
36 unsigned int verbosity=0xff;
37 long **box,*list,*found;
38 double **series,**cast;
39 double *interval,*min,epsilon;
41 unsigned int embed=2,dim=1,dim1,DELAY=1;
42 char *column=NULL,dimset=0,do_zeroth=0;
44 unsigned long LENGTH=ULONG_MAX,FLENGTH=1000,exclude=0;
45 double EPS0=1.e-3,EPSF=1.2;
47 double **mat,**imat,*vec,*localav,*foreav;
49 void show_options(char *progname)
51 what_i_do(progname,WID_STR);
52 fprintf(stderr," Usage: %s [Options]\n",progname);
53 fprintf(stderr," Options:\n");
54 fprintf(stderr,"Everything not being a valid option will be interpreted"
56 " datafile.\nIf no datafile is given stdin is read. Just - also"
58 fprintf(stderr,"\t-l # of data to be used [default whole file]\n");
59 fprintf(stderr,"\t-x # of lines to be ignored [default 0]\n");
60 fprintf(stderr,"\t-c column [default 1,...,# of components]\n");
61 fprintf(stderr,"\t-m #of components,embedding dimension [default 1,2]\n");
62 fprintf(stderr,"\t-d delay for the embedding [default 1]\n");
63 fprintf(stderr,"\t-L # of iterations [default 1000]\n");
64 fprintf(stderr,"\t-k # of neighbors [default 30]\n");
65 fprintf(stderr,"\t-r size of initial neighborhood ["
66 " default (data interval)/1000]\n");
67 fprintf(stderr,"\t-f factor to increase size [default 1.2]\n");
68 fprintf(stderr,"\t-0 perfom a zeroth order fit [default not set]\n");
69 fprintf(stderr,"\t-o output file [default 'datafile'.cast;"
70 " no -o means write to stdout]\n");
71 fprintf(stderr,"\t-V verbosity level [default 1]\n\t\t"
72 "0='only panic messages'\n\t\t"
73 "1='+ input/output messages'\n");
74 fprintf(stderr,"\t-h show these options\n");
78 void scan_options(int n,char **in)
82 if ((out=check_option(in,n,'l','u')) != NULL)
83 sscanf(out,"%lu",&LENGTH);
84 if ((out=check_option(in,n,'x','u')) != NULL)
85 sscanf(out,"%lu",&exclude);
86 if ((out=check_option(in,n,'c','s')) != NULL) {
90 if ((out=check_option(in,n,'m','2')) != NULL)
91 sscanf(out,"%u,%u",&dim,&embed);
92 if ((out=check_option(in,n,'d','u')) != NULL)
93 sscanf(out,"%u",&DELAY);
94 if ((out=check_option(in,n,'L','u')) != NULL)
95 sscanf(out,"%lu",&FLENGTH);
96 if ((out=check_option(in,n,'k','u')) != NULL)
97 sscanf(out,"%u",&MINN);
98 if ((out=check_option(in,n,'0','n')) != NULL)
100 if ((out=check_option(in,n,'V','u')) != NULL)
101 sscanf(out,"%u",&verbosity);
102 if ((out=check_option(in,n,'r','f')) != NULL) {
104 sscanf(out,"%lf",&EPS0);
106 if ((out=check_option(in,n,'f','f')) != NULL)
107 sscanf(out,"%lf",&EPSF);
108 if ((out=check_option(in,n,'o','o')) != NULL) {
115 void put_in_boxes(void)
121 hdim=(embed-1)*DELAY;
127 for (n=hdim;n<LENGTH-1;n++) {
128 i=(int)(series[0][n]*epsinv)&nmax;
129 j=(int)(series[dim1][n-hdim]*epsinv)&nmax;
135 unsigned int hfind_neighbors(void)
138 int i,j,i1,i2,j1,k,l,element;
141 double max,dx,epsinv;
143 hdim=(embed-1)*DELAY;
145 i=(int)(cast[hdim][0]*epsinv)&nmax;
146 j=(int)(cast[0][dim1]*epsinv)&nmax;
148 for (i1=i-1;i1<=i+1;i1++) {
150 for (j1=j-1;j1<=j+1;j1++) {
151 element=box[i2][j1&nmax];
152 while (element != -1) {
155 for (l=0;l<dim;l++) {
156 for (k=0;k<=hdim;k += DELAY) {
157 dx=fabs(series[l][element-k]-cast[hdim-k][l]);
158 max=(dx>max) ? dx : max;
168 found[nfound++]=element;
169 element=list[element];
176 void multiply_matrix(double **mat,double *vec)
181 check_alloc(hvec=(double*)malloc(sizeof(double)*dim*embed));
182 for (i=0;i<dim*embed;i++) {
184 for (j=0;j<dim*embed;j++)
185 hvec[i] += mat[i][j]*vec[j];
187 for (i=0;i<dim*embed;i++)
192 void make_fit(int number,double *newcast)
194 double *sj,*si,lavi,lavj,fav;
195 long i,i1,j,j1,hi,hj,hi1,hj1,n,which;
198 hdim=(embed-1)*DELAY;
200 for (i=0;i<dim*embed;i++)
205 for (n=0;n<number;n++) {
207 for (j=0;j<dim;j++) {
209 foreav[j] += sj[which+1];
210 for (j1=0;j1<embed;j1++) {
212 localav[hj] += sj[which-j1*DELAY];
217 for (i=0;i<dim*embed;i++)
218 localav[i] /= number;
222 for (i=0;i<dim;i++) {
224 for (i1=0;i1<embed;i1++) {
228 for (j=0;j<dim;j++) {
230 for (j1=0;j1<embed;j1++) {
236 for (n=0;n<number;n++) {
238 mat[hi][hj] += (si[which-hi1]-lavi)*(sj[which-hj1]-lavj);
246 for (i=0;i<dim*embed;i++)
247 for (j=i;j<dim*embed;j++) {
252 imat=invert_matrix(mat,dim*embed);
254 for (i=0;i<dim;i++) {
257 for (j=0;j<dim;j++) {
259 for (j1=0;j1<embed;j1++) {
264 for (n=0;n<number;n++) {
266 vec[hj] += (si[which+1]-fav)*(sj[which-hj1]-lavj);
272 multiply_matrix(imat,vec);
274 newcast[i]=foreav[i];
275 for (j=0;j<dim;j++) {
276 for (j1=0;j1<embed;j1++) {
278 newcast[i] += vec[hj]*(cast[hdim-j1*DELAY][j]-localav[hj]);
283 for (i=0;i<dim*embed;i++)
288 void make_zeroth(int number,double *newcast)
293 for (d=0;d<dim;d++) {
296 for (i=0;i<number;i++)
297 newcast[d] += sj[found[i]];
298 newcast[d] /= number;
302 int main(int argc,char **argv)
305 long i,j,hdim,actfound;
306 double maxinterval,*swap,*newcast;
309 if (scan_help(argc,argv))
310 show_options(argv[0]);
312 scan_options(argc,argv);
313 #ifndef OMIT_WHAT_I_DO
314 if (verbosity&VER_INPUT)
315 what_i_do(argv[0],WID_STR);
318 infile=search_datafile(argc,argv,NULL,verbosity);
322 if (outfile == NULL) {
324 check_alloc(outfile=(char*)calloc(strlen(infile)+6,(size_t)1));
325 strcpy(outfile,infile);
326 strcat(outfile,".cast");
329 check_alloc(outfile=(char*)calloc((size_t)11,(size_t)1));
330 strcpy(outfile,"stdin.cast");
334 test_outfile(outfile);
336 hdim=(embed-1)*DELAY+1;
338 series=(double**)get_multi_series(infile,&LENGTH,exclude,&dim,"",dimset,
341 series=(double**)get_multi_series(infile,&LENGTH,exclude,&dim,column,
343 check_alloc(min=(double*)malloc(sizeof(double)*dim));
344 check_alloc(interval=(double*)malloc(sizeof(double)*dim));
347 for (i=0;i<dim;i++) {
348 rescale_data(series[i],LENGTH,&min[i],&interval[i]);
349 if (interval[i] > maxinterval)
350 maxinterval=interval[i];
353 check_alloc(cast=(double**)malloc(sizeof(double*)*hdim));
355 check_alloc(cast[i]=(double*)malloc(sizeof(double)*dim));
356 check_alloc(newcast=(double*)malloc(sizeof(double)*dim));
358 check_alloc(list=(long*)malloc(sizeof(long)*LENGTH));
359 check_alloc(found=(long*)malloc(sizeof(long)*LENGTH));
360 check_alloc(box=(long**)malloc(sizeof(long*)*NMAX));
362 check_alloc(box[i]=(long*)malloc(sizeof(long)*NMAX));
364 check_alloc(localav=(double*)malloc(sizeof(double)*dim*embed));
365 check_alloc(foreav=(double*)malloc(sizeof(double)*dim));
366 check_alloc(vec=(double*)malloc(sizeof(double)*dim*embed));
367 check_alloc(mat=(double**)malloc(sizeof(double*)*dim*embed));
368 for (i=0;i<dim*embed;i++)
369 check_alloc(mat[i]=(double*)malloc(sizeof(double)*dim*embed));
376 cast[i][j]=series[j][LENGTH-hdim+i];
379 file=fopen(outfile,"w");
380 if (verbosity&VER_INPUT)
381 fprintf(stderr,"Opened %s for writing\n",outfile);
384 if (verbosity&VER_INPUT)
385 fprintf(stderr,"Writing to stdout\n");
388 for (i=0;i<FLENGTH;i++) {
394 actfound=hfind_neighbors();
395 if (actfound >= MINN) {
397 make_fit(actfound,newcast);
399 make_zeroth(actfound,newcast);
401 for (j=0;j<dim-1;j++)
402 printf("%e ",newcast[j]*interval[j]+min[j]);
403 printf("%e\n",newcast[dim-1]*interval[dim-1]+min[dim-1]);
407 for (j=0;j<dim-1;j++)
408 fprintf(file,"%e ",newcast[j]*interval[j]+min[j]);
409 fprintf(file,"%e\n",newcast[dim-1]*interval[dim-1]+min[dim-1]);
413 for (j=0;j<dim;j++) {
414 if ((newcast[j] > 2.0) || (newcast[j] < -1.0)) {
415 fprintf(stderr,"Forecast failed. Escaping data region!\n");
416 exit(NSTEP__ESCAPE_REGION);
421 for (j=0;j<hdim-1;j++)
425 cast[hdim-1][j]=newcast[j];
434 for (i=0;i<embed*dim;i++)