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 4, 1999 */
27 #include "routines/tsa.h"
29 #define WID_STR "Does a backward elimination for a polynomial"
31 char *outfile=NULL,stdo=1;
32 char *parin=NULL,*infile=NULL;
33 unsigned long length=ULONG_MAX,insample=ULONG_MAX,exclude=0;
34 unsigned int plength=UINT_MAX;
35 unsigned int column=1,dim=2,delay=1,down_to=1,step=1;
37 unsigned int verbosity=0xff;
38 double *series,*param;
40 void show_options(char *progname)
42 what_i_do(progname,WID_STR);
43 fprintf(stderr,"Usage: %s [Options]\n",progname);
44 fprintf(stderr,"Options:\n");
45 fprintf(stderr,"Everything not being a valid option will be interpreted"
47 " datafile.\nIf no datafile is given stdin is read. Just - also"
49 fprintf(stderr,"\t-l # of data to use [default: whole file]\n");
50 fprintf(stderr,"\t-x # of lines to ignore [default: %lu]\n",exclude);
51 fprintf(stderr,"\t-c column to read [default: %u]\n",column);
52 fprintf(stderr,"\t-m embedding dimension [default: %u]\n",dim);
53 fprintf(stderr,"\t-d delay [default: %u]\n",delay);
54 fprintf(stderr,"\t-n insample data [default: all]\n");
55 fprintf(stderr,"\t-s steps to forecast [default: %u]\n",step);
56 fprintf(stderr,"\t-# reduce down to # terms [default: %u]\n",down_to);
57 fprintf(stderr,"\t-p name of parameter file [default: parameter.pol]\n");
58 fprintf(stderr,"\t-o output file name [default: 'datafile'.pbe]\n");
59 fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
60 "0='only panic messages'\n\t\t"
61 "1='+ input/output messages'\n");
62 fprintf(stderr,"\t-h show these options\n");
66 void scan_options(int n,char **in)
70 if ((out=check_option(in,n,'l','u')) != NULL)
71 sscanf(out,"%lu",&length);
72 if ((out=check_option(in,n,'x','u')) != NULL)
73 sscanf(out,"%lu",&exclude);
74 if ((out=check_option(in,n,'c','u')) != NULL)
75 sscanf(out,"%u",&column);
76 if ((out=check_option(in,n,'m','u')) != NULL)
77 sscanf(out,"%u",&dim);
78 if ((out=check_option(in,n,'d','u')) != NULL)
79 sscanf(out,"%u",&delay);
80 if ((out=check_option(in,n,'n','u')) != NULL)
81 sscanf(out,"%lu",&insample);
82 if ((out=check_option(in,n,'#','u')) != NULL)
83 sscanf(out,"%u",&down_to);
84 if ((out=check_option(in,n,'s','u')) != NULL)
85 sscanf(out,"%u",&step);
86 if ((out=check_option(in,n,'V','u')) != NULL)
87 sscanf(out,"%u",&verbosity);
88 if ((out=check_option(in,n,'p','s')) != NULL)
90 if ((out=check_option(in,n,'o','o')) != NULL) {
97 double polynom(unsigned long act,unsigned int which)
102 for (i=0;i<dim;i++) {
103 h=series[act-i*delay];
104 for (j=0;j<order[which][i];j++)
118 check_alloc(vec=(double*)malloc(sizeof(double)*plength));
119 check_alloc(mat=(double**)malloc(sizeof(double*)*plength));
120 for (i=0;i<plength;i++)
121 check_alloc(mat[i]=(double*)malloc(sizeof(double)*plength));
123 for (i=0;i<plength;i++) {
125 for (j=0;j<plength;j++)
129 for (n=(dim-1)*delay;n<insample-step;n++) {
130 for (i=0;i<plength;i++) {
131 vec[i] += series[n+step]*(h=polynom(n,i));
132 for (j=i;j<plength;j++)
133 mat[i][j] += polynom(n,j)*h;
136 for (i=0;i<plength;i++) {
137 vec[i] /= (insample-step-(dim-1)*delay);
138 for (j=i;j<plength;j++)
139 mat[j][i]=(mat[i][j]/=(insample-step-(dim-1)*delay));
142 solvele(mat,vec,plength);
144 for (i=0;i<plength;i++)
148 for (i=0;i<plength;i++)
153 double forecast_error(unsigned long i0,unsigned long i1)
159 for (n=i0+(dim-1)*delay;n<i1-step;n++) {
161 for (i=0;i<plength;i++)
162 h += param[i]*polynom(n,i);
163 error += (series[n+step]-h)*(series[n+step]-h);
166 return sqrt(error/(i1-i0-step-(dim-1)*delay));
169 int main(int argc,char **argv)
171 int i,j,k,l,hl,ibest,counter;
172 char stdi=0,out_set=1,*parout;
173 double **dummy,besti,besto,withalli,withallo,errori=0.,erroro=0.;
175 unsigned long hlength=ULONG_MAX;
176 unsigned int **ini_params,*isout,offset;
179 if (scan_help(argc,argv))
180 show_options(argv[0]);
182 scan_options(argc,argv);
183 #ifndef OMIT_WHAT_I_DO
184 if (verbosity&VER_INPUT)
185 what_i_do(argv[0],WID_STR);
188 infile=search_datafile(argc,argv,&column,verbosity);
192 if (outfile == NULL) {
194 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
195 sprintf(outfile,"%s.pbe",infile);
198 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
199 sprintf(outfile,"stdin.pbe");
203 test_outfile(outfile);
206 check_alloc(parin=(char*)calloc((size_t)14,(size_t)1));
207 sprintf(parin,"parameter.pol");
209 file=fopen(parin,"r");
211 fprintf(stderr,"File %s does not exist. Exiting!\n",parin);
212 exit(POLYBACK__WRONG_PARAMETER_FILE);
216 if (verbosity&VER_INPUT)
217 fprintf(stderr,"Using %s as the parameter file\n",parin);
218 dummy=(double**)get_multi_series(parin,&hlength,0LU,&dim,"",(char)1,
221 offset=(unsigned int)(log((double)hlength)/log(10.0)+1.0);
222 check_alloc(parout=(char*)calloc(strlen(parin)+offset+2,(size_t)1));
224 check_alloc(ini_params=(unsigned int**)malloc(sizeof(int*)*hlength));
225 for (i=0;i<hlength;i++) {
226 check_alloc(ini_params[i]=(unsigned int*)malloc(sizeof(int)*dim));
228 ini_params[i][j]=(unsigned int)dummy[j][i];
230 check_alloc(isout=(unsigned int*)malloc(sizeof(int)*hlength));
232 series=(double*)get_series(infile,&length,exclude,column,verbosity);
233 variance(series,length,&av,&varianz);
235 if (insample >= length) {
240 check_alloc(order=(unsigned int**)malloc(sizeof(int*)*hlength));
241 check_alloc(param=(double*)malloc(sizeof(double)*hlength));
242 for (i=0;i<hlength;i++) {
244 check_alloc(order[i]=(unsigned int*)malloc(sizeof(int)*dim));
246 order[i][j]=ini_params[i][j];
251 withalli=forecast_error(0LU,insample);
254 withallo=forecast_error(insample+1,length);
257 fprintf(stdout,"%lu %e %e\n",hlength,withalli/varianz,withallo/varianz);
261 file=fopen(outfile,"w");
262 fprintf(file,"%lu %e %e\n",hlength,withalli/varianz,withallo/varianz);
266 for (i=0;i<plength;i++)
270 if ((down_to < 1) || (down_to > hlength))
273 for (i=1;i<=hlength-down_to;i++) {
277 check_alloc(order=(unsigned int**)malloc(sizeof(int*)*plength));
278 check_alloc(param=(double*)malloc(sizeof(double)*plength));
279 for (j=0;j<plength;j++) {
280 check_alloc(order[j]=(unsigned int*)malloc(sizeof(int)*dim));
283 for (j=0;j<hlength;j++)
287 for (k=0;k<hlength;k++) {
290 order[hl][l]=ini_params[k][l];
295 errori=forecast_error(0LU,insample);
297 erroro=forecast_error(insample+1,length);
306 if (erroro < besto) {
313 if (errori < besti) {
324 for (j=0;j<plength;j++)
328 fprintf(stdout,"%u %e %e ",plength,besti/varianz,besto/varianz);
330 fprintf(stdout,"%u ",ini_params[ibest][j]);
331 fprintf(stdout,"\n");
335 fprintf(file,"%u %e %e ",plength,besti/varianz,besto/varianz);
337 fprintf(file,"%u ",ini_params[ibest][j]);
341 sprintf(parout,"%s.%u",parin,plength);
342 fpars=fopen(parout,"w");
343 for (j=0;j<hlength;j++)
346 fprintf(fpars,"%u ",ini_params[j][k]);