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: Feb 19, 2007 */
22 2/19/2007: Changed name and default for noise
23 10/26/2006: Add seed option
31 #include "routines/tsa.h"
33 #define WID_STR "Makes a local zeroth order forecast for multivariate data\n\
34 and iterates a trajectory"
38 char onscreen=1,epsset=0,*outfile=NULL,setsort=1,setnoise=0;
40 unsigned int nmax=(NMAX-1);
41 unsigned int verbosity=0xff;
42 long **box,*list,*found;
43 double **series,**cast,*abstand,*var;
46 unsigned int embed=2,dim=1,dim1,DELAY=1;
47 char *column=NULL,dimset=0;
49 unsigned int **indexes;
50 unsigned long LENGTH=ULONG_MAX,FLENGTH=1000,exclude=0;
51 unsigned long seed=0x9074325L;
52 double EPS0=1.e-3,EPSF=1.2,Q=10.0;
54 double **mat,*vec,*hsum,*newav;
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 be used [default whole file]\n");
66 fprintf(stderr,"\t-x # of lines to be ignored [default 0]\n");
67 fprintf(stderr,"\t-c column [default 1,...,# of components]\n");
68 fprintf(stderr,"\t-m #of components,embedding dimension [default 1,2]\n");
69 fprintf(stderr,"\t-d delay for the embedding [default 1]\n");
70 fprintf(stderr,"\t-L # of iterations [default 1000]\n");
71 fprintf(stderr,"\t-k # of neighbors [default %u]\n",MINN);
72 fprintf(stderr,"\t-K fix # of neighbors [default no]\n");
73 fprintf(stderr,"\t-%% # variance of noise [default %3.1lf]\n",Q);
74 fprintf(stderr,"\t-I seed for the rnd-generator (If seed=0, the time\n"
75 "\t\tcommand is used to set the seed) [Default: fixed]\n");
76 fprintf(stderr,"\t-r size of initial neighborhood ["
77 " default (data interval)/1000]\n");
78 fprintf(stderr,"\t-f factor to increase size [default 1.2]\n");
79 fprintf(stderr,"\t-o output file [default 'datafile'.lzr;"
80 " no -o means write to stdout]\n");
81 fprintf(stderr,"\t-V verbosity level [default 1]\n\t\t"
82 "0='only panic messages'\n\t\t"
83 "1='+ input/output messages'\n");
84 fprintf(stderr,"\t-h show these options\n");
88 void scan_options(int n,char **in)
92 if ((out=check_option(in,n,'l','u')) != NULL)
93 sscanf(out,"%lu",&LENGTH);
94 if ((out=check_option(in,n,'x','u')) != NULL)
95 sscanf(out,"%lu",&exclude);
96 if ((out=check_option(in,n,'c','s')) != NULL) {
100 if ((out=check_option(in,n,'m','2')) != NULL)
101 sscanf(out,"%u,%u",&dim,&embed);
102 if ((out=check_option(in,n,'d','u')) != NULL)
103 sscanf(out,"%u",&DELAY);
104 if ((out=check_option(in,n,'L','u')) != NULL)
105 sscanf(out,"%lu",&FLENGTH);
106 if ((out=check_option(in,n,'k','u')) != NULL)
107 sscanf(out,"%u",&MINN);
108 if ((out=check_option(in,n,'K','n')) != NULL)
110 if ((out=check_option(in,n,'I','u')) != NULL) {
111 sscanf(out,"%lu",&seed);
113 seed=(unsigned long)time((time_t*)&seed);
115 if ((out=check_option(in,n,'V','u')) != NULL)
116 sscanf(out,"%u",&verbosity);
117 if ((out=check_option(in,n,'r','f')) != NULL) {
119 sscanf(out,"%lf",&EPS0);
121 if ((out=check_option(in,n,'f','f')) != NULL)
122 sscanf(out,"%lf",&EPSF);
123 if ((out=check_option(in,n,'%','f')) != NULL) {
124 sscanf(out,"%lf",&Q);
128 if ((out=check_option(in,n,'o','o')) != NULL) {
135 void sort(unsigned long nfound)
138 int i,j,k,hf,iswap,hdim;
140 hdim=(embed-1)*DELAY;
142 for (i=0;i<nfound;i++) {
145 for (j=0;j<dim;j++) {
146 for (k=0;k<=hdim;k += DELAY) {
147 dx=fabs(series[j][hf-k]-cast[hdim-k][j]);
148 if (dx > abstand[i]) abstand[i]=dx;
154 for (j=i+1;j<nfound;j++)
155 if (abstand[j]<abstand[i]) {
157 abstand[i]=abstand[j];
165 void put_in_boxes(void)
171 hdim=(embed-1)*DELAY;
177 for (n=hdim;n<LENGTH-1;n++) {
178 i=(int)(series[0][n]*epsinv)&nmax;
179 j=(int)(series[dim1][n-hdim]*epsinv)&nmax;
185 unsigned int hfind_neighbors(void)
188 int i,j,i1,i2,j1,l,hc,hd,element;
191 double max,dx,epsinv;
193 hdim=(embed-1)*DELAY;
195 i=(int)(cast[hdim][0]*epsinv)&nmax;
196 j=(int)(cast[0][dim1]*epsinv)&nmax;
198 for (i1=i-1;i1<=i+1;i1++) {
200 for (j1=j-1;j1<=j+1;j1++) {
201 element=box[i2][j1&nmax];
202 while (element != -1) {
205 for (l=0;l<dim*embed;l++) {
208 dx=fabs(series[hc][element-hd]-cast[hdim-hd][hc]);
209 max=(dx>max) ? dx : max;
216 found[nfound++]=element;
217 element=list[element];
224 void make_zeroth(int number,double *newcast)
229 for (d=0;d<dim;d++) {
232 for (i=0;i<number;i++)
233 newcast[d] += sd[found[i]];
234 newcast[d] /= (double)number;
239 newcast[d] += gaussian(var[d]*Q);
243 int main(int argc,char **argv)
246 long i,j,hdim,actfound;
247 unsigned long count=1;
248 double *swap,*newcast,maxinterval,*min,*interval,dummy,epsilon0;
251 if (scan_help(argc,argv))
252 show_options(argv[0]);
254 scan_options(argc,argv);
255 #ifndef OMIT_WHAT_I_DO
256 if (verbosity&VER_INPUT)
257 what_i_do(argv[0],WID_STR);
260 infile=search_datafile(argc,argv,NULL,verbosity);
264 if (outfile == NULL) {
266 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
267 strcpy(outfile,infile);
268 strcat(outfile,".lzr");
271 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
272 strcpy(outfile,"stdin.lzr");
276 test_outfile(outfile);
278 hdim=(embed-1)*DELAY+1;
280 series=(double**)get_multi_series(infile,&LENGTH,exclude,&dim,"",dimset,
283 series=(double**)get_multi_series(infile,&LENGTH,exclude,&dim,column,
288 check_alloc(min=(double*)malloc(sizeof(double)*dim));
289 check_alloc(interval=(double*)malloc(sizeof(double)*dim));
290 check_alloc(var=(double*)malloc(sizeof(double)*dim));
294 for (i=0;i<dim;i++) {
295 rescale_data(series[i],LENGTH,&min[i],&interval[i]);
296 variance(series[i],LENGTH,&dummy,&var[i]);
297 if (interval[i] > maxinterval)
298 maxinterval=interval[i];
304 check_alloc(cast=(double**)malloc(sizeof(double*)*hdim));
306 check_alloc(cast[i]=(double*)malloc(sizeof(double)*dim));
307 check_alloc(newcast=(double*)malloc(sizeof(double)*dim));
308 check_alloc(newav=(double*)malloc(sizeof(double)*dim));
310 check_alloc(list=(long*)malloc(sizeof(long)*LENGTH));
311 check_alloc(found=(long*)malloc(sizeof(long)*LENGTH));
312 check_alloc(abstand=(double*)malloc(sizeof(double)*LENGTH));
313 check_alloc(box=(long**)malloc(sizeof(long*)*NMAX));
315 check_alloc(box[i]=(long*)malloc(sizeof(long)*NMAX));
317 check_alloc(vec=(double*)malloc(sizeof(double)*dim));
318 check_alloc(hsum=(double*)malloc(sizeof(double)*dim));
319 check_alloc(mat=(double**)malloc(sizeof(double*)*dim));
320 for (i=0;i<dim;i++) {
321 check_alloc(mat[i]=(double*)malloc(sizeof(double)*dim));
326 cast[i][j]=series[j][LENGTH-hdim+i];
328 indexes=make_multi_index(dim,embed,DELAY);
331 file=fopen(outfile,"w");
332 if (verbosity&VER_INPUT)
333 fprintf(stderr,"Opened %s for writing\n",outfile);
336 if (verbosity&VER_INPUT)
337 fprintf(stderr,"Writing to stdout\n");
347 for (i=0;i<FLENGTH;i++) {
350 epsilon= epsilon0/((double)count*EPSF);
356 actfound=hfind_neighbors();
357 if (actfound >= MINN) {
364 make_zeroth(actfound,newcast);
366 for (j=0;j<dim-1;j++)
367 printf("%e ",newcast[j]*interval[j]+min[j]);
368 printf("%e\n",newcast[dim-1]*interval[dim-1]+min[dim-1]);
372 for (j=0;j<dim-1;j++)
373 fprintf(file,"%e ",newcast[j]*interval[j]+min[j]);
374 fprintf(file,"%e\n",newcast[dim-1]*interval[dim-1]+min[dim-1]);
379 for (j=0;j<hdim-1;j++)
383 cast[hdim-1][j]=newcast[j];