/* * This file is part of TISEAN * * Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber * * TISEAN is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * TISEAN is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with TISEAN; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ /*Author: Rainer Hegger, Last modified: Feb 6, 2006 */ /*Changes: Feb 4, 2006: First version Feb 6, 2006: Find and remove bugs (1) Feb 11, 2006: Add rand_arb_dist to iterate_***_model */ #include #include #include #include #include #include "routines/tsa.h" #define WID_STR "Fits an multivariate ARIMA model to the data and gives\ the coefficients\n\tand the residues (or an iterated model)" unsigned long length=ULONG_MAX,exclude=0; unsigned int dim=1,poles=10,ilength,ITER=50; unsigned int arpoles=0,ipoles=0,mapoles=0,offset; unsigned int verbosity=1; char *outfile=NULL,*column=NULL,stdo=1,dimset=0,run_model=0,arimaset=0; char *infile=NULL; double **series,convergence=1.0e-3; double *my_average; unsigned long ardim,armadim; unsigned int **aindex; void show_options(char *progname) { what_i_do(progname,WID_STR); fprintf(stderr," Usage: %s [options]\n",progname); fprintf(stderr," Options:\n"); fprintf(stderr,"Everything not being a valid option will be interpreted" " as a possible" " datafile.\nIf no datafile is given stdin is read. Just - also" " means stdin\n"); fprintf(stderr,"\t-l length of file [default is whole file]\n"); fprintf(stderr,"\t-x # of lines to be ignored [default is 0]\n"); fprintf(stderr,"\t-m dimension [default is 1]\n"); fprintf(stderr,"\t-c columns to read [default is 1,...,dimension]\n"); fprintf(stderr,"\t-p order of initial AR-Fit [default is %u]\n",poles); fprintf(stderr,"\t-P order of AR,I,MA-Fit [default is %u,%u,%u]\n", arpoles,ipoles,mapoles); fprintf(stderr,"\t-I # of arima iterations [default is %u]\n",ITER); fprintf(stderr,"\t-e accuracy of convergence [default is %lf]\n",convergence); fprintf(stderr,"\t-s length of iterated model [default no iteration]\n"); fprintf(stderr,"\t-o output file name [default is 'datafile'.ari]\n"); fprintf(stderr,"\t-V verbosity level [default is 1]\n\t\t" "0='only panic messages'\n\t\t" "1='+ input/output messages'\n\t\t" "2='+ print residuals though iterating a model'\n\t\t" "4='+ print original data plus residuals'\n"); fprintf(stderr,"\t-h show these options\n\n"); exit(0); } void scan_options(int argc,char **argv) { char *out; if ((out=check_option(argv,argc,'p','u')) != NULL) { sscanf(out,"%u",&poles); if (poles < 1) { fprintf(stderr,"The order should at least be one!\n"); exit(127); } } if ((out=check_option(argv,argc,'l','u')) != NULL) sscanf(out,"%lu",&length); if ((out=check_option(argv,argc,'x','u')) != NULL) sscanf(out,"%lu",&exclude); if ((out=check_option(argv,argc,'m','u')) != NULL) { sscanf(out,"%u",&dim); dimset=1; } if ((out=check_option(argv,argc,'P','3')) != NULL) { sscanf(out,"%u,%u,%u",&arpoles,&ipoles,&mapoles); if ((arpoles+ipoles+mapoles)>0) arimaset=1; } if ((out=check_option(argv,argc,'I','u')) != NULL) sscanf(out,"%u",&ITER); if ((out=check_option(argv,argc,'e','f')) != NULL) sscanf(out,"%lf",&convergence); if ((out=check_option(argv,argc,'c','u')) != NULL) column=out; if ((out=check_option(argv,argc,'V','u')) != NULL) sscanf(out,"%u",&verbosity); if ((out=check_option(argv,argc,'s','u')) != NULL) { sscanf(out,"%u",&ilength); run_model=1; } if ((out=check_option(argv,argc,'o','o')) != NULL) { stdo=0; if (strlen(out) > 0) outfile=out; } } void make_difference(void) { unsigned long i,d; for (i=length-1;i>0;i--) for (d=0;d= length) { fprintf(stderr,"It makes no sense to have more poles than data! Exiting\n"); exit(AR_MODEL_TOO_MANY_POLES); } if (arimaset) { if ((arpoles >= length) || (mapoles >= length)) { fprintf(stderr,"It makes no sense to have more poles than data! Exiting\n"); exit(AR_MODEL_TOO_MANY_POLES); } } ardim=poles*dim; aindex=make_ar_index(); check_alloc(vec=(double*)malloc(sizeof(double)*ardim)); check_alloc(mat=(double**)malloc(sizeof(double*)*ardim)); for (i=0;i mapoles)? arpoles:mapoles; offset += poles; inverse=build_matrix(mat,armadim); for (i=0;i alldiff) alldiff=xdiff[iter-1][i]; realiter=iter; if (alldiff < convergence) iter=ITER; if (iter < ITER) { for (i=0;i 0)) { if (!arimaset) iterate_model(coeff,pm,diff,NULL); else iterate_arima_model(coeff,pm,diff,NULL); } } else { file=fopen(outfile,"w"); if (verbosity&VER_INPUT) fprintf(stderr,"Opened %s for output\n",outfile); if (arimaset) { fprintf(file,"#convergence of residuals in arima fit\n"); for (i=0;i 0)) { if (!arimaset) iterate_model(coeff,pm,diff,file); else iterate_arima_model(coeff,pm,diff,file); } fclose(file); } if (outfile != NULL) free(outfile); if (infile != NULL) free(infile); for (i=0;i