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*/
22 6/30/2006: Norm of the errors was wrong
29 #include "routines/tsa.h"
31 #define WID_STR "Fits a polynomial to the data"
33 char CAST=0,sinsample=0,*outfile=NULL;
35 unsigned long LENGTH=ULONG_MAX,exclude=0;
37 unsigned long INSAMPLE=ULONG_MAX;
38 int DIM=2,DELAY=1,N=2;
39 unsigned int COLUMN=1;
40 unsigned int pars=1,hpar;
41 unsigned int verbosity=0xff;
45 double *series,*results;
48 void show_options(char *progname)
50 what_i_do(progname,WID_STR);
51 fprintf(stderr," Usage: %s [options]\n",progname);
52 fprintf(stderr," Options:\n");
53 fprintf(stderr,"Everything not being a valid option will be interpreted"
55 " datafile.\nIf no datafile is given stdin is read. Just - also"
57 fprintf(stderr,"\t-l # of data to use [default: whole file]\n");
58 fprintf(stderr,"\t-x # of lines to be ignored [default: 0]\n");
59 fprintf(stderr,"\t-c column to read [default: 1]\n");
60 fprintf(stderr,"\t-m embedding dimension [default: 2]\n");
61 fprintf(stderr,"\t-d delay [default: 1]\n");
62 fprintf(stderr,"\t-p order of the polynomial [default: 2]\n");
63 fprintf(stderr,"\t-n # of points for insample [default: # of data]\n");
64 fprintf(stderr,"\t-L steps to cast [default: none]\n");
65 fprintf(stderr,"\t-o output file name [default: 'datafile'.pol]\n");
66 fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
67 "0='only panic messages'\n\t\t"
68 "1='+ input/output messages'\n");
69 fprintf(stderr,"\t-h show these options\n");
73 void scan_options(int n,char **in)
77 if ((out=check_option(in,n,'l','u')) != NULL)
78 sscanf(out,"%lu",&LENGTH);
79 if ((out=check_option(in,n,'x','u')) != NULL)
80 sscanf(out,"%lu",&exclude);
81 if ((out=check_option(in,n,'c','u')) != NULL)
82 sscanf(out,"%u",&COLUMN);
83 if ((out=check_option(in,n,'m','u')) != NULL)
84 sscanf(out,"%u",&DIM);
85 if ((out=check_option(in,n,'d','u')) != NULL)
86 sscanf(out,"%u",&DELAY);
87 if ((out=check_option(in,n,'p','u')) != NULL)
89 if ((out=check_option(in,n,'V','u')) != NULL)
90 sscanf(out,"%u",&verbosity);
91 if ((out=check_option(in,n,'n','u')) != NULL) {
92 sscanf(out,"%lu",&INSAMPLE);
95 if ((out=check_option(in,n,'L','u')) != NULL) {
97 sscanf(out,"%lu",&CLENGTH);
99 if ((out=check_option(in,n,'o','o')) != NULL)
104 double polynom(int act,int dim,long cur,long fac)
110 hi=act-(dim-1)*DELAY;
114 ret *= polynom(act,dim-1,cur-n*fac,fac/(N+1));
119 int number_pars(int ord,int start)
124 for (i=start;i<=DIM;i++)
127 for (i=start;i<=DIM;i++)
128 ret += number_pars(ord-1,i);
133 void make_coding(int ord,int d,int fac,int cur)
141 make_coding(ord-j,d-1,fac*(N+1),cur+j*fac);
149 check_alloc(b=(double*)malloc(sizeof(double)*pars));
150 check_alloc(mat=(double**)malloc(sizeof(double*)*pars));
152 check_alloc(mat[i]=(double*)malloc(sizeof(double)*pars));
154 for (i=0;i<pars;i++) {
162 for (k=(DIM-1)*DELAY;k<INSAMPLE-1;k++)
163 mat[i][j] += polynom(k,DIM,coding[i],maxencode)*
164 polynom(k,DIM,coding[j],maxencode);
167 mat[j][i]=(mat[i][j] /= (INSAMPLE-1-(DIM-1)*DELAY));
169 for (i=0;i<pars;i++) {
170 for (j=(DIM-1)*DELAY;j<INSAMPLE-1;j++)
171 b[i] += series[j+1]*polynom(j,DIM,coding[i],maxencode);
172 b[i] /= (INSAMPLE-1-(DIM-1)*DELAY);
185 void decode(int *out,int dim,long cur,long fac)
192 decode(out,dim-1,cur-(long)n*fac,fac/(N+1));
195 double make_error(unsigned long i0,unsigned long i1)
201 for (j=i0+(DIM-1)*DELAY;j<(long)i1-1;j++) {
204 h += results[k]*polynom(j,DIM,coding[k],maxencode);
205 err += (series[j+1]-h)*(series[j+1]-h);
207 return err /= ((long)i1-(long)i0-(DIM-1)*DELAY);
210 void make_cast(FILE *fcast)
215 for (i=0;i<=(DIM-1)*DELAY;i++)
216 series[i]=series[LENGTH-(DIM-1)*DELAY-1+i];
219 for (i=1;i<=CLENGTH;i++) {
222 casted += results[k]*polynom((DIM-1)*DELAY,DIM,coding[k],maxencode);
223 fprintf(fcast,"%e\n",casted*std_dev);
225 for (j=0;j<(DIM-1)*DELAY;j++)
226 series[j]=series[j+1];
232 int main(int argc,char **argv)
237 double in_error,out_error,av;
240 if (scan_help(argc,argv))
241 show_options(argv[0]);
243 scan_options(argc,argv);
244 #ifndef OMIT_WHAT_I_DO
245 if (verbosity&VER_INPUT)
246 what_i_do(argv[0],WID_STR);
249 infile=search_datafile(argc,argv,&COLUMN,verbosity);
253 if (outfile == NULL) {
255 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
256 strcpy(outfile,infile);
257 strcat(outfile,".pol");
260 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
261 strcpy(outfile,"stdin.pol");
264 test_outfile(outfile);
266 series=(double*)get_series(infile,&LENGTH,exclude,COLUMN,verbosity);
267 variance(series,LENGTH,&av,&std_dev);
268 for (i=0;i<LENGTH;i++)
269 series[i] /= std_dev;
271 if (!sinsample || (INSAMPLE > LENGTH))
279 pars += number_pars(i,1);
281 file=fopen(outfile,"w");
282 if (verbosity&VER_INPUT)
283 fprintf(stderr,"Opened %s for writing\n",outfile);
284 fprintf(file,"#number of free parameters= %d\n\n",pars);
286 check_alloc(coding=(long*)malloc(sizeof(long)*pars));
288 make_coding(N,DIM-1,1,0);
290 check_alloc(results=(double*)malloc(sizeof(double)*pars));
293 check_alloc(opar=(int*)malloc(sizeof(int)*DIM));
294 fprintf(file,"#used norm for the fit= %e\n",std_dev);
296 for (j=0;j<pars;j++) {
297 decode(opar,DIM-1,coding[j],maxencode);
300 for (k=0;k<DIM;k++) {
302 fprintf(file,"%d ",opar[k]);
304 fprintf(file,"%e\n",results[j]/pow(std_dev,(double)(sumpar-1)));
308 in_error=make_error((unsigned long)0,INSAMPLE);
310 fprintf(file,"#average insample error= %e\n",sqrt(in_error));
312 if (INSAMPLE < LENGTH) {
313 out_error=make_error(INSAMPLE,LENGTH);
314 fprintf(file,"#average out of sample error= %e\n",sqrt(out_error));