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 5, 2004 */
27 #include "routines/tsa.h"
29 #define WID_STR "Fits a polynomial to the data."
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 long plength=UINT_MAX;
35 unsigned long step=1000;
36 unsigned int column=1,dim=2,delay=1,down_to=1;
38 unsigned int verbosity=0xff;
39 double *series,*param;
41 void show_options(char *progname)
43 what_i_do(progname,WID_STR);
44 fprintf(stderr,"Usage: %s [Options]\n",progname);
45 fprintf(stderr,"Options:\n");
46 fprintf(stderr,"Everything not being a valid option will be interpreted"
48 " datafile.\nIf no datafile is given stdin is read. Just - also"
50 fprintf(stderr,"\t-l # of data to use [default: whole file]\n");
51 fprintf(stderr,"\t-x # of lines to ignore [default: %lu]\n",exclude);
52 fprintf(stderr,"\t-c column to read [default: %u]\n",column);
53 fprintf(stderr,"\t-m embedding dimension [default: %u]\n",dim);
54 fprintf(stderr,"\t-d delay [default: %u]\n",delay);
55 fprintf(stderr,"\t-n insample data [default: all]\n");
56 fprintf(stderr,"\t-L length of forecasted series [default: %lu]\n",step);
57 fprintf(stderr,"\t-p name of parameter file [default: parameter.pol]\n");
58 fprintf(stderr,"\t-o output file name [default: 'datafile'.pbf]\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,'V','u')) != NULL)
81 sscanf(out,"%u",&verbosity);
82 if ((out=check_option(in,n,'n','u')) != NULL)
83 sscanf(out,"%lu",&insample);
84 if ((out=check_option(in,n,'L','u')) != NULL)
85 sscanf(out,"%lu",&step);
86 if ((out=check_option(in,n,'p','s')) != NULL)
88 if ((out=check_option(in,n,'o','o')) != NULL) {
95 double polynom(unsigned long act,unsigned int which)
100 for (i=0;i<dim;i++) {
101 h=series[act-i*delay];
102 for (j=0;j<order[which][i];j++)
116 check_alloc(vec=(double*)malloc(sizeof(double)*plength));
117 check_alloc(mat=(double**)malloc(sizeof(double*)*plength));
118 for (i=0;i<plength;i++)
119 check_alloc(mat[i]=(double*)malloc(sizeof(double)*plength));
121 for (i=0;i<plength;i++) {
123 for (j=0;j<plength;j++)
127 for (n=(dim-1)*delay;n<insample-1;n++) {
129 for (i=0;i<plength;i++) {
130 vec[i] += series[hn]*(h=polynom(n,i));
131 for (j=i;j<plength;j++)
132 mat[i][j] += polynom(n,j)*h;
135 for (i=0;i<plength;i++) {
136 vec[i] /= (insample-(dim-1)*delay-1);
137 for (j=i;j<plength;j++)
138 mat[j][i]=(mat[i][j]/=(insample-(dim-1)*delay)-1);
141 solvele(mat,vec,plength);
143 for (i=0;i<plength;i++)
147 for (i=0;i<plength;i++)
152 double forecast_error(unsigned long i0,unsigned long i1)
158 for (n=i0+(dim-1)*delay;n<i1-1;n++) {
160 for (i=0;i<plength;i++)
161 h += param[i]*polynom(n,i);
162 error += (series[n+1]-h)*(series[n+1]-h);
165 return sqrt(error/(i1-i0-(dim-1)*delay-1));
168 void make_cast(FILE *fcast)
174 for (i=0;i<=(dim-1)*delay;i++)
175 series[i]=series[length-(dim-1)*delay+i-1];
178 for (i=1;i<=step;i++) {
180 for (k=0;k<plength;k++)
181 casted += param[k]*polynom((unsigned long)((dim-1)*delay),k);
183 fprintf(fcast,"%e\n",casted);
187 fprintf(stdout,"%e\n",casted);
190 for (j=0;j<(dim-1)*delay;j++)
191 series[j]=series[j+1];
196 int main(int argc,char **argv)
200 double **dummy,withalli,withallo;
204 if (scan_help(argc,argv))
205 show_options(argv[0]);
207 scan_options(argc,argv);
208 #ifndef OMIT_WHAT_I_DO
209 if (verbosity&VER_INPUT)
210 what_i_do(argv[0],WID_STR);
213 infile=search_datafile(argc,argv,&column,verbosity);
217 if (outfile == NULL) {
219 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
220 sprintf(outfile,"%s.pbf",infile);
223 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
224 sprintf(outfile,"stdin.pbf");
228 test_outfile(outfile);
231 check_alloc(parin=(char*)calloc((size_t)14,(size_t)1));
232 sprintf(parin,"parameter.pol");
234 file=fopen(parin,"r");
236 fprintf(stderr,"File %s does not exist. Exiting!\n",parin);
237 exit(POLYNOMP__WRONG_PARAMETER_FILE);
241 dummy=(double**)get_multi_series(parin,&plength,0LU,
242 &dim,"",(char)"1",verbosity);
244 check_alloc(order=(unsigned int**)malloc(sizeof(int*)*plength));
245 for (i=0;i<plength;i++) {
246 check_alloc(order[i]=(unsigned int*)malloc(sizeof(int)*dim));
248 order[i][j]=(unsigned int)dummy[j][i];
251 series=(double*)get_series(infile,&length,exclude,column,verbosity);
252 variance(series,length,&av,&varianz);
254 if (insample >= length) {
259 check_alloc(param=(double*)malloc(sizeof(double)*plength));
262 withalli=forecast_error(0LU,insample);
265 withallo=forecast_error(insample+1,length);
268 if (verbosity&VER_INPUT)
269 fprintf(stderr,"Writing to stdout\n");
270 fprintf(stdout,"#FCE: %e %e\n",withalli/varianz,withallo/varianz);
271 for (i=0;i<plength;i++) {
272 fprintf(stdout,"# ");
274 fprintf(stdout,"%u ",order[i][j]);
275 fprintf(stdout,"%e\n",param[i]);
280 file=fopen(outfile,"w");
281 if (verbosity&VER_INPUT)
282 fprintf(stderr,"Opened %s for writing\n",outfile);
283 fprintf(file,"#FCE: %e %e\n",withalli/varianz,withallo/varianz);
284 for (i=0;i<plength;i++) {
287 fprintf(file,"%u ",order[i][j]);
288 fprintf(file,"%e\n",param[i]);