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 10, 2005 */
22 12/10/05: It's multivariate now
23 12/16/05: Scaled <eps> and sigma(eps)
31 #include "routines/tsa.h"
33 #define WID_STR "Determines the fraction of false nearest neighbors."
39 unsigned long length=ULONG_MAX,exclude=0,theiler=0;
40 unsigned int delay=1,maxdim=5,minemb=1;
41 unsigned int comp=1,maxemb=5;
42 unsigned int verbosity=0xff;
52 unsigned int *vcomp,*vemb;
53 unsigned long toolarge;
55 void show_options(char *progname)
57 what_i_do(progname,WID_STR);
58 fprintf(stderr," Usage: %s [options]\n",progname);
59 fprintf(stderr," Options:\n");
60 fprintf(stderr,"Everything not being a valid option will be interpreted"
62 " datafile.\nIf no datafile is given stdin is read. Just - also"
64 fprintf(stderr,"\t-l # of data [default: whole file]\n");
65 fprintf(stderr,"\t-x # of lines to ignore [default: 0]\n");
66 fprintf(stderr,"\t-c columns to read [default: 1]\n");
67 fprintf(stderr,"\t-m min. test embedding dimension [default: %u]\n",minemb);
68 fprintf(stderr,"\t-M # of components,max. emb. dim. [default: %u,%u]\n",
70 fprintf(stderr,"\t-d delay [default: 1]\n");
71 fprintf(stderr,"\t-f escape factor [default: %.2lf]\n",rt);
72 fprintf(stderr,"\t-t theiler window [default: 0]\n");
73 fprintf(stderr,"\t-o output file [default: 'datafile'.fnn; without -o"
75 fprintf(stderr,"\t-V verbosity level [default: 3]\n\t\t"
76 "0='only panic messages'\n\t\t"
77 "1='+ input/output messages'\n\t\t"
78 "2='+ information about the current state\n");
79 fprintf(stderr,"\t-h show these options\n");
83 void scan_options(int n,char **in)
87 if ((out=check_option(in,n,'l','u')) != NULL)
88 sscanf(out,"%lu",&length);
89 if ((out=check_option(in,n,'x','u')) != NULL)
90 sscanf(out,"%lu",&exclude);
91 if ((out=check_option(in,n,'c','s')) != NULL)
93 if ((out=check_option(in,n,'m','u')) != NULL)
94 sscanf(out,"%u",&minemb);
95 if ((out=check_option(in,n,'M','2')) != NULL) {
96 sscanf(out,"%u,%u",&comp,&maxemb);
97 maxdim=comp*(maxemb+1);
100 if ((out=check_option(in,n,'d','u')) != NULL)
101 sscanf(out,"%u",&delay);
102 if ((out=check_option(in,n,'f','f')) != NULL)
103 sscanf(out,"%lf",&rt);
104 if ((out=check_option(in,n,'t','u')) != NULL)
105 sscanf(out,"%lu",&theiler);
106 if ((out=check_option(in,n,'V','u')) != NULL)
107 sscanf(out,"%u",&verbosity);
108 if ((out=check_option(in,n,'o','o')) != NULL) {
115 void mmb(unsigned int hdim,unsigned int hemb,double eps)
124 for (i=0;i<length-(maxemb+1)*delay;i++) {
125 x=(long)(series[0][i]/eps)&ibox;
126 y=(long)(series[hdim][i+hemb]/eps)&ibox;
132 char find_nearest(long n,unsigned int dim,double eps)
134 long x,y,x1,x2,y1,i,i1,ic,ie;
135 long element,which= -1;
136 double dx,maxdx,mindx=1.1,hfactor,factor;
140 x=(long)(series[0][n]/eps)&ibox;
141 y=(long)(series[ic][n+ie]/eps)&ibox;
143 for (x1=x-1;x1<=x+1;x1++) {
145 for (y1=y-1;y1<=y+1;y1++) {
146 element=box[x2][y1&ibox];
147 while (element != -1) {
148 if (labs(element-n) > theiler) {
149 maxdx=fabs(series[0][n]-series[0][element]);
150 for (i=1;i<=dim;i++) {
153 dx=fabs(series[ic][n+i1]-series[ic][element+i1]);
157 if ((maxdx < mindx) && (maxdx > 0.0)) {
162 element=list[element];
167 if ((which != -1) && (mindx <= eps) && (mindx <= varianz/rt)) {
169 vareps += mindx*mindx;
171 for (i=1;i<=comp;i++) {
174 hfactor=fabs(series[ic][n+ie]-series[ic][which+ie])/mindx;
175 if (hfactor > factor)
185 int main(int argc,char **argv)
189 double min,inter=0.0,ind_inter,epsilon,av,ind_var;
190 char *nearest,alldone;
192 unsigned int dim,emb;
193 unsigned long donesofar;
195 if (scan_help(argc,argv))
196 show_options(argv[0]);
198 scan_options(argc,argv);
199 #ifndef OMIT_WHAT_I_DO
200 if (verbosity&VER_INPUT)
201 what_i_do(argv[0],WID_STR);
204 infile=search_datafile(argc,argv,NULL,verbosity);
208 if (outfile == NULL) {
210 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
211 strcpy(outfile,infile);
212 strcat(outfile,".fnn");
215 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
216 strcpy(outfile,"stdin.fnn");
220 test_outfile(outfile);
223 series=(double**)get_multi_series(infile,&length,exclude,&comp,"",dimset,
226 series=(double**)get_multi_series(infile,&length,exclude,&comp,column,
229 for (i=0;i<comp;i++) {
230 rescale_data(series[i],length,&min,&ind_inter);
231 variance(series[i],length,&av,&ind_var);
237 varianz=(varianz>ind_var)?ind_var:varianz;
238 inter=(inter<ind_inter)?ind_inter:inter;
242 check_alloc(list=(long*)malloc(sizeof(long)*length));
243 check_alloc(nearest=(char*)malloc(length));
244 check_alloc(box=(long**)malloc(sizeof(long*)*BOX));
246 check_alloc(box[i]=(long*)malloc(sizeof(long)*BOX));
249 file=fopen(outfile,"w");
250 if (verbosity&VER_INPUT)
251 fprintf(stderr,"Opened %s for writing\n",outfile);
254 if (verbosity&VER_INPUT)
255 fprintf(stderr,"Writing to stdout\n");
257 check_alloc(vcomp=(unsigned int*)malloc(sizeof(int)*(maxdim)));
258 check_alloc(vemb=(unsigned int*)malloc(sizeof(int)*(maxdim)));
259 for (i=0;i<maxdim;i++) {
266 vemb[i]=(i/comp)*delay;
269 for (emb=minemb;emb<=maxemb;emb++) {
277 for (i=0;i<length;i++)
279 if (verbosity&VER_USR1)
280 fprintf(stderr,"Start for dimension=%u\n",dim+1);
281 while (!alldone && (epsilon < 2.*varianz/rt)) {
283 mmb(vcomp[dim],vemb[dim],epsilon);
284 for (i=0;i<length-maxemb*delay;i++)
286 nearest[i]=find_nearest(i,dim,epsilon);
287 alldone &= nearest[i];
288 donesofar += (unsigned long)nearest[i];
290 if (verbosity&VER_USR1)
291 fprintf(stderr,"Found %lu up to epsilon=%e\n",donesofar,epsilon*inter);
296 if (donesofar == 0) {
297 fprintf(stderr,"Not enough points found!\n");
298 exit(FALSE_NEAREST_NOT_ENOUGH_POINTS);
300 aveps *= (1./(double)donesofar);
301 vareps *= (1./(double)donesofar);
303 fprintf(stdout,"%u %e %e %e\n",dim+1,(double)toolarge/(double)donesofar,
304 aveps*inter,sqrt(vareps)*inter);
308 fprintf(file,"%u %e %e %e\n",dim+1,(double)toolarge/(double)donesofar,
309 aveps*inter,sqrt(vareps)*inter);