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: Mar 11, 2002 */
25 #include "routines/tsa.h"
28 #define WID_STR "Fits a RBF-model to the data"
30 char *outfile=NULL,stdo=1,MAKECAST=0;
33 int DIM=2,DELAY=1,CENTER=10,STEP=1;
34 unsigned int COLUMN=1;
35 unsigned int verbosity=0xff;
37 unsigned long LENGTH=ULONG_MAX,INSAMPLE=ULONG_MAX,exclude=0;
39 double *series,*coefs;
40 double varianz,interval,min;
43 void show_options(char *progname)
45 what_i_do(progname,WID_STR);
46 fprintf(stderr," Usage: %s [options]\n",progname);
47 fprintf(stderr," Options:\n");
48 fprintf(stderr,"Everything not being a valid option will be interpreted"
50 " datafile.\nIf no datafile is given stdin is read. Just - also"
52 fprintf(stderr,"\t-l # of data to use [default: all from file]\n");
53 fprintf(stderr,"\t-x # of lines to be ignored [default: 0]\n");
54 fprintf(stderr,"\t-c column to read [default: %u]\n",COLUMN);
55 fprintf(stderr,"\t-m embedding dimension [default: %d]\n",DIM);
56 fprintf(stderr,"\t-d delay [default: %d]\n",DELAY);
57 fprintf(stderr,"\t-p number of centers [default: %d]\n",CENTER);
58 fprintf(stderr,"\t-X deactivate drift [default: activated]\n");
59 fprintf(stderr,"\t-s steps to forecast [default: %d]\n",STEP);
60 fprintf(stderr,"\t-n # of points for insample [default: # of data]\n");
61 fprintf(stderr,"\t-L steps to cast [default: none]\n");
62 fprintf(stderr,"\t-o output file name [default: 'datafile'.rbf]\n");
63 fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
64 "0='only panic messages'\n\t\t"
65 "1='+ input/output messages'\n");
66 fprintf(stderr,"\t-h show these options\n");
70 void scan_options(int n,char **in)
74 if ((out=check_option(in,n,'l','u')) != NULL)
75 sscanf(out,"%lu",&LENGTH);
76 if ((out=check_option(in,n,'x','u')) != NULL)
77 sscanf(out,"%lu",&exclude);
78 if ((out=check_option(in,n,'c','u')) != NULL)
79 sscanf(out,"%u",&COLUMN);
80 if ((out=check_option(in,n,'m','u')) != NULL)
81 sscanf(out,"%u",&DIM);
82 if ((out=check_option(in,n,'d','u')) != NULL)
83 sscanf(out,"%u",&DELAY);
84 if ((out=check_option(in,n,'p','u')) != NULL)
85 sscanf(out,"%u",&CENTER);
86 if ((out=check_option(in,n,'X','n')) != NULL)
88 if ((out=check_option(in,n,'s','u')) != NULL)
89 sscanf(out,"%u",&STEP);
90 if ((out=check_option(in,n,'V','u')) != NULL)
91 sscanf(out,"%u",&verbosity);
92 if ((out=check_option(in,n,'n','u')) != NULL)
93 sscanf(out,"%lu",&INSAMPLE);
94 if ((out=check_option(in,n,'L','u')) != NULL) {
96 sscanf(out,"%lu",&CLENGTH);
98 if ((out=check_option(in,n,'o','o')) != NULL) {
105 double avdistance(void)
110 for (i=0;i<CENTER;i++)
111 for (j=0;j<CENTER;j++)
114 dist += sqr(center[i][k]-center[j][k]);
116 return sqrt(dist/(CENTER-1)/CENTER/DIM);
119 double rbf(double *act,double *cen)
125 denum=2.0*varianz*varianz;
128 r += sqr(*(act-i*DELAY)-cen[i]);
130 return exp(-r/denum);
135 double *force,h,h1,step=1e-2,step1;
138 check_alloc(force=(double*)malloc(sizeof(double)*d2));
140 for (i=0;i<CENTER;i++) {
143 for (k=0;k<CENTER;k++) {
145 h=center[i][j]-center[k][j];
146 force[j] += h/sqr(h)/fabs(h);
156 if (((center[i][j]+h1) > -0.1) && ((center[i][j]+h1) < 1.1))
170 check_alloc(mat=(double**)malloc(sizeof(double*)*(CENTER+1)));
171 for (i=0;i<=CENTER;i++)
172 check_alloc(mat[i]=(double*)malloc(sizeof(double)*(CENTER+1)));
173 check_alloc(hcen=(double*)malloc(sizeof(double)*CENTER));
175 for (i=0;i<=CENTER;i++) {
177 for (j=0;j<=CENTER;j++)
181 for (n=(DIM-1)*DELAY;n<INSAMPLE-STEP;n++) {
183 for (i=0;i<CENTER;i++)
184 hcen[i]=rbf(&series[n],center[i]);
185 coefs[0] += series[nst];
187 for (i=1;i<=CENTER;i++)
188 mat[i][0] += hcen[i-1];
189 for (i=1;i<=CENTER;i++) {
190 coefs[i] += series[nst]*(h=hcen[i-1]);
192 mat[i][j] += h*hcen[j-1];
196 h=(double)(INSAMPLE-STEP-(DIM-1)*DELAY);
197 for (i=0;i<=CENTER;i++) {
205 solvele(mat,coefs,(unsigned int)(CENTER+1));
207 for (i=0;i<=CENTER;i++)
213 double forecast_error(unsigned long i0,unsigned long i1)
218 for (n=i0+(DIM-1)*DELAY;n<i1-STEP;n++) {
220 for (i=1;i<=CENTER;i++)
221 h += coefs[i]*rbf(&series[n],center[i-1]);
222 error += (series[n+STEP]-h)*(series[n+STEP]-h);
225 return sqrt(error/(i1-i0-STEP-(DIM-1)*DELAY));
228 void make_cast(FILE *out)
234 check_alloc(cast=(double*)malloc(sizeof(double)*(dim+1)));
236 cast[i]=series[LENGTH-1-dim+i];
238 for (n=0;n<CLENGTH;n++) {
240 for (i=1;i<=CENTER;i++)
241 new_el += coefs[i]*rbf(&cast[dim],center[i-1]);
242 fprintf(out,"%e\n",new_el*interval+min);
249 int main(int argc,char **argv)
256 if (scan_help(argc,argv))
257 show_options(argv[0]);
259 scan_options(argc,argv);
260 #ifndef OMIT_WHAT_I_DO
261 if (verbosity&VER_INPUT)
262 what_i_do(argv[0],WID_STR);
265 infile=search_datafile(argc,argv,&COLUMN,verbosity);
269 if (outfile == NULL) {
271 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
272 strcpy(outfile,infile);
273 strcat(outfile,".rbf");
276 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
277 strcpy(outfile,"stdin.rbf");
281 test_outfile(outfile);
283 series=(double*)get_series(infile,&LENGTH,exclude,COLUMN,verbosity);
284 rescale_data(series,LENGTH,&min,&interval);
285 variance(series,LENGTH,&av,&varianz);
287 if (INSAMPLE > LENGTH)
296 check_alloc(coefs=(double*)malloc(sizeof(double)*(CENTER+1)));
297 check_alloc(center=(double**)malloc(sizeof(double*)*CENTER));
298 for (i=0;i<CENTER;i++)
299 check_alloc(center[i]=(double*)malloc(sizeof(double)*DIM));
301 cstep=LENGTH-1-(DIM-1)*DELAY;
302 for (i=0;i<CENTER;i++)
304 center[i][j]=series[(DIM-1)*DELAY-j*DELAY+(i*cstep)/(CENTER-1)];
308 varianz=avdistance();
312 file=fopen(outfile,"w");
313 if (verbosity&VER_INPUT)
314 fprintf(stderr,"Opened %s for writing\n",outfile);
315 fprintf(file,"#Center points used:\n");
316 for (i=0;i<CENTER;i++) {
319 fprintf(file," %e",center[i][j]*interval+min);
322 fprintf(file,"#variance= %e\n",varianz*interval);
323 fprintf(file,"#Coefficients:\n");
324 fprintf(file,"#%e\n",coefs[0]*interval+min);
325 for (i=1;i<=CENTER;i++)
326 fprintf(file,"#%e\n",coefs[i]*interval);
329 if (verbosity&VER_INPUT)
330 fprintf(stderr,"Writing to stdout\n");
331 fprintf(stdout,"#Center points used:\n");
332 for (i=0;i<CENTER;i++) {
335 fprintf(stdout," %e",center[i][j]*interval+min);
336 fprintf(stdout,"\n");
338 fprintf(stdout,"#variance= %e\n",varianz*interval);
339 fprintf(stdout,"#Coefficients:\n");
340 fprintf(stdout,"#%e\n",coefs[0]*interval+min);
341 for (i=1;i<=CENTER;i++)
342 fprintf(stdout,"#%e\n",coefs[i]*interval);
345 for (i=0;i<INSAMPLE;i++) {
347 sigma += series[i]*series[i];
350 sigma=sqrt(fabs(sigma/INSAMPLE-av*av));
352 fprintf(file,"#insample error= %e\n",forecast_error(0LU,INSAMPLE)/sigma);
354 fprintf(stdout,"#insample error= %e\n",forecast_error(0LU,INSAMPLE)/sigma);
356 if (INSAMPLE < LENGTH) {
358 for (i=INSAMPLE;i<LENGTH;i++) {
360 sigma += series[i]*series[i];
362 av /= (LENGTH-INSAMPLE);
363 sigma=sqrt(fabs(sigma/(LENGTH-INSAMPLE)-av*av));
365 fprintf(file,"#out of sample error= %e\n",
366 forecast_error(INSAMPLE,LENGTH)/sigma);
368 fprintf(stdout,"#out of sample error= %e\n",
369 forecast_error(INSAMPLE,LENGTH)/sigma);