f03904f66eee69c3181f60e75c1bf0f369677f91
[jabaws.git] / arima-model.c
1 /*
2  *   This file is part of TISEAN
3  *
4  *   Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber
5  *
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.
10  *
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.
15  *
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
19  */
20 /*Author: Rainer Hegger, Last modified: Feb 6, 2006 */
21 /*Changes:
22   Feb 4, 2006: First version
23   Feb 6, 2006: Find and remove bugs (1)
24   Feb 11, 2006: Add rand_arb_dist to iterate_***_model
25  */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <math.h>
31 #include "routines/tsa.h"
32
33 #define WID_STR "Fits an multivariate ARIMA model to the data and gives\
34  the coefficients\n\tand the residues (or an iterated model)"
35
36 unsigned long length=ULONG_MAX,exclude=0;
37 unsigned int dim=1,poles=10,ilength,ITER=50;
38 unsigned int arpoles=0,ipoles=0,mapoles=0,offset;
39 unsigned int verbosity=1;
40 char *outfile=NULL,*column=NULL,stdo=1,dimset=0,run_model=0,arimaset=0;
41 char *infile=NULL;
42 double **series,convergence=1.0e-3;
43
44 double *my_average;
45 unsigned long ardim,armadim;
46 unsigned int **aindex;
47
48 void show_options(char *progname)
49 {
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"
54           " as a possible"
55           " datafile.\nIf no datafile is given stdin is read. Just - also"
56           " means stdin\n");
57   fprintf(stderr,"\t-l length of file [default is whole file]\n");
58   fprintf(stderr,"\t-x # of lines to be ignored [default is 0]\n");
59   fprintf(stderr,"\t-m dimension [default is 1]\n");
60   fprintf(stderr,"\t-c columns to read [default is 1,...,dimension]\n");
61   fprintf(stderr,"\t-p order of initial AR-Fit [default is %u]\n",poles);
62   fprintf(stderr,"\t-P order of AR,I,MA-Fit [default is %u,%u,%u]\n",
63           arpoles,ipoles,mapoles);
64   fprintf(stderr,"\t-I # of arima iterations [default is %u]\n",ITER);
65   fprintf(stderr,"\t-e accuracy of convergence [default is %lf]\n",convergence);
66   fprintf(stderr,"\t-s length of iterated model [default no iteration]\n");
67   fprintf(stderr,"\t-o output file name [default is 'datafile'.ari]\n");
68   fprintf(stderr,"\t-V verbosity level [default is 1]\n\t\t"
69           "0='only panic messages'\n\t\t"
70           "1='+ input/output messages'\n\t\t"
71           "2='+ print residuals though iterating a model'\n\t\t"
72           "4='+ print original data plus residuals'\n");
73   fprintf(stderr,"\t-h show these options\n\n");
74   exit(0);
75 }
76
77 void scan_options(int argc,char **argv)
78 {
79   char *out;
80
81   if ((out=check_option(argv,argc,'p','u')) != NULL) {
82     sscanf(out,"%u",&poles);
83     if (poles < 1) {
84       fprintf(stderr,"The order should at least be one!\n");
85       exit(127);
86     }
87   }
88   if ((out=check_option(argv,argc,'l','u')) != NULL)
89     sscanf(out,"%lu",&length);
90   if ((out=check_option(argv,argc,'x','u')) != NULL)
91     sscanf(out,"%lu",&exclude);
92   if ((out=check_option(argv,argc,'m','u')) != NULL) {
93     sscanf(out,"%u",&dim);
94     dimset=1;
95   }
96   if ((out=check_option(argv,argc,'P','3')) != NULL) {
97     sscanf(out,"%u,%u,%u",&arpoles,&ipoles,&mapoles);
98     if ((arpoles+ipoles+mapoles)>0)
99       arimaset=1;
100   }
101   if ((out=check_option(argv,argc,'I','u')) != NULL)
102     sscanf(out,"%u",&ITER);
103   if ((out=check_option(argv,argc,'e','f')) != NULL)
104     sscanf(out,"%lf",&convergence);
105   if ((out=check_option(argv,argc,'c','u')) != NULL)
106     column=out;
107   if ((out=check_option(argv,argc,'V','u')) != NULL)
108     sscanf(out,"%u",&verbosity);
109   if ((out=check_option(argv,argc,'s','u')) != NULL) {
110     sscanf(out,"%u",&ilength);
111     run_model=1;
112   }
113   if ((out=check_option(argv,argc,'o','o')) != NULL) {
114     stdo=0;
115     if (strlen(out) > 0)
116       outfile=out;
117   }
118 }
119
120 void make_difference(void)
121 {
122   unsigned long i,d;
123
124   for (i=length-1;i>0;i--)
125     for (d=0;d<dim;d++)
126       series[d][i]=series[d][i]-series[d][i-1];
127 }
128
129 unsigned int** make_ar_index(void)
130 {
131   unsigned int** ar_index;
132   unsigned long i;
133
134   check_alloc(ar_index=(unsigned int**)malloc(sizeof(unsigned int*)*2));
135   for (i=0;i<2;i++)
136     check_alloc(ar_index[i]=(unsigned int*)
137                 malloc(sizeof(unsigned int)*ardim));
138   for (i=0;i<ardim;i++) {
139     ar_index[0][i]=i/poles;
140     ar_index[1][i]=i%poles;
141   }
142   return ar_index;
143 }
144
145 unsigned int** make_arima_index(unsigned int ars,unsigned int mas)
146 {
147   unsigned int** arima_index;
148   unsigned int armad;
149   unsigned long i,i0;
150
151   armad=(ars+mas)*dim;
152   check_alloc(arima_index=(unsigned int**)malloc(sizeof(unsigned int*)*2));
153   for (i=0;i<2;i++)
154     check_alloc(arima_index[i]=(unsigned int*)
155                 malloc(sizeof(unsigned int)*armad));
156   for (i=0;i<ars*dim;i++) {
157     arima_index[0][i]=i/ars;
158     arima_index[1][i]=i%ars;
159   }
160   i0=ars*dim;
161   for (i=0;i<mas*dim;i++) {
162     arima_index[0][i+i0]=dim+i/mas;
163     arima_index[1][i+i0]=i%mas;
164   }
165
166   return arima_index;
167 }
168
169 void set_averages_to_zero(void)
170 {
171   double var;
172   long i,j;
173   
174   for (i=0;i<dim;i++) {
175     variance(series[i],length,&my_average[i],&var);
176     for (j=0;j<length;j++)
177       series[i][j] -= my_average[i];
178   }
179 }
180
181 double** build_matrix(double **mat,unsigned int size)
182 {
183   long n,i,j,is,id,js,jd;
184   double norm;
185   
186   norm=1./((double)length-1.0-(double)poles-(double)offset);
187
188   for (i=0;i<size;i++) {
189     id=aindex[0][i];
190     is=aindex[1][i];
191     for (j=i;j<size;j++) {
192       jd=aindex[0][j];
193       js=aindex[1][j];
194       mat[i][j]=0.0;
195       for (n=offset+poles-1;n<length-1;n++)
196         mat[i][j] += series[id][n-is]*series[jd][n-js];
197       mat[i][j] *= norm;
198       mat[j][i]=mat[i][j];
199     }
200   }
201
202   return invert_matrix(mat,size);
203 }
204
205 void build_vector(double *vec,unsigned int size,long comp)
206 {
207   long i,is,id,n;
208   double norm;
209
210   norm=1./((double)length-1.0-(double)poles-(double)offset);
211
212   for (i=0;i<size;i++) {
213     id=aindex[0][i];
214     is=aindex[1][i];
215     vec[i]=0.0;
216     for (n=offset+poles-1;n<length-1;n++)
217       vec[i] += series[comp][n+1]*series[id][n-is];
218     vec[i] *= norm;
219   }
220 }
221
222 double* multiply_matrix_vector(double **mat,double *vec,unsigned int size)
223 {
224   long i,j;
225   double *new_vec;
226
227   check_alloc(new_vec=(double*)malloc(sizeof(double)*size));
228
229   for (i=0;i<size;i++) {
230     new_vec[i]=0.0;
231     for (j=0;j<size;j++)
232       new_vec[i] += mat[i][j]*vec[j];
233   }
234
235   return new_vec;
236 }
237
238 double* make_residuals(double **diff,double **coeff,unsigned int size)
239 {
240   long n,n1,d,i,is,id;
241   double *resi;
242   
243   check_alloc(resi=(double*)malloc(sizeof(double)*dim));
244   for (i=0;i<dim;i++)
245     resi[i]=0.0;
246
247   for (n=poles-1;n<length-1;n++) {
248     n1=n+1;
249     for (d=0;d<dim;d++) {
250       diff[d][n1]=series[d][n1];
251       for (i=0;i<size;i++) {
252         id=aindex[0][i];
253         is=aindex[1][i];
254         diff[d][n1] -= coeff[d][i]*series[id][n-is];
255       }
256       resi[d] += sqr(diff[d][n1]);
257     }
258   }
259
260   for (i=0;i<dim;i++)
261     resi[i]=sqrt(resi[i]/((double)length-(double)poles));
262
263   return resi;
264 }
265
266 void iterate_model(double **coeff,double *sigma,double **diff,FILE *file)
267 {
268   long i,j,i1,i2,n,d;
269   double **iterate,*swap,**myrand;
270   
271   check_alloc(iterate=(double**)malloc(sizeof(double*)*(poles+1)));
272   for (i=0;i<=poles;i++)
273     check_alloc(iterate[i]=(double*)malloc(sizeof(double)*dim));
274
275   check_alloc(myrand=(double**)malloc(sizeof(double*)*dim));
276   for (i=0;i<dim;i++)
277     myrand[i]=rand_arb_dist(diff[i],length,ilength+poles,100,0x44325);
278
279   rnd_init(0x44325);
280   for (i=0;i<1000;i++)
281     rnd_long();
282   for (i=0;i<dim;i++)
283     for (j=0;j<poles;j++)
284       iterate[j][i]=myrand[i][j];
285   
286   for (n=0;n<ilength;n++) {
287     for (d=0;d<dim;d++) {
288       iterate[poles][d]=myrand[d][n+poles];
289       for (i1=0;i1<dim;i1++)
290         for (i2=0;i2<poles;i2++)
291           iterate[poles][d] += coeff[d][i1*poles+i2]*iterate[poles-1-i2][i1];
292     }
293     if (file != NULL) {
294       for (d=0;d<dim;d++)
295         fprintf(file,"%e ",iterate[poles][d]);
296       fprintf(file,"\n");
297     }
298     else {
299       for (d=0;d<dim;d++)
300         printf("%e ",iterate[poles][d]);
301       printf("\n");
302     }
303
304     swap=iterate[0];
305     for (i=0;i<poles;i++)
306       iterate[i]=iterate[i+1];
307     iterate[poles]=swap;
308   }
309
310   for (i=0;i<=poles;i++)
311     free(iterate[i]);
312   free(iterate);
313
314   for (i=0;i<dim;i++)
315     free(myrand[i]);
316   free(myrand);
317 }
318
319 void iterate_arima_model(double **coeff,double *sigma,double **diff,FILE *file)
320 {
321   double **iterate,*swap,**myrand;
322   unsigned long i,j,n,is,id;
323
324   check_alloc(iterate=(double**)malloc(sizeof(double*)*(poles+1)));
325   for (i=0;i<=poles;i++)
326     check_alloc(iterate[i]=(double*)malloc(sizeof(double)*2*dim));
327
328   check_alloc(myrand=(double**)malloc(sizeof(double*)*dim));
329   for (i=0;i<dim;i++)
330     myrand[i]=rand_arb_dist(diff[i],length,ilength+poles,100,0x44325);
331
332   rnd_init(0x44325);
333   for (i=0;i<1000;i++)
334     rnd_long();
335   for (i=0;i<dim;i++)
336     for (j=0;j<poles;j++)
337       iterate[j][i]=iterate[j][dim+i]=myrand[i][j];
338
339   for (n=0;n<ilength;n++) {
340     for (i=0;i<dim;i++)
341       iterate[poles][i]=iterate[poles][i+dim]=myrand[i][n+poles];
342
343     for (j=0;j<dim;j++) {
344       for (i=0;i<armadim;i++) {
345         id=aindex[0][i];
346         is=aindex[1][i];
347         iterate[poles][j] += coeff[j][i]*iterate[poles-1-is][id];
348       }
349     }
350
351     if (file != NULL) {
352       for (i=0;i<dim;i++)
353         fprintf(file,"%e ",iterate[poles][i]);
354       fprintf(file,"\n");
355     }
356     else {
357       for (i=0;i<dim;i++)
358         printf("%e ",iterate[poles][i]);
359       printf("\n");
360     }
361
362     swap=iterate[0];
363     for (i=0;i<poles;i++)
364       iterate[i]=iterate[i+1];
365     iterate[poles]=swap;
366   }
367
368   for (i=0;i<=poles;i++)
369     free(iterate[i]);
370   free(iterate);
371   for (i=0;i<dim;i++)
372     free(myrand[i]);
373   free(myrand);
374 }
375
376 int main(int argc,char **argv)
377 {
378   char stdi=0;
379   double *pm;
380   long i,j,iter,hj,realiter=0;
381   unsigned int size,is,id;
382   FILE *file;
383   double **mat,**inverse,*vec,**coeff,**diff,**hseries;
384   double **oldcoeff,*diffcoeff=NULL;
385   double hdiff,**xdiff=NULL,avpm;
386   double loglikelihood,aic,alldiff;
387   
388   if (scan_help(argc,argv))
389     show_options(argv[0]);
390   
391   scan_options(argc,argv);
392 #ifndef OMIT_WHAT_I_DO
393   if (verbosity&VER_INPUT)
394     what_i_do(argv[0],WID_STR);
395 #endif
396
397   infile=search_datafile(argc,argv,NULL,verbosity);
398   if (infile == NULL)
399     stdi=1;
400
401   if (outfile == NULL) {
402     if (!stdi) {
403       check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
404       strcpy(outfile,infile);
405       strcat(outfile,".ari");
406     }
407     else {
408       check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
409       strcpy(outfile,"stdin.ari");
410     }
411   }
412   if (!stdo)
413     test_outfile(outfile);
414
415   if (column == NULL)
416     series=(double**)get_multi_series(infile,&length,exclude,&dim,"",dimset,
417                                       verbosity);
418   else
419     series=(double**)get_multi_series(infile,&length,exclude,&dim,column,
420                                       dimset,verbosity);
421
422   check_alloc(my_average=(double*)malloc(sizeof(double)*dim));
423
424   for (i=0;i<ipoles;i++)
425     make_difference();
426
427   for (i=0;i<dim;i++)
428     series[i] += ipoles;
429   length -= ipoles;
430
431   set_averages_to_zero();
432
433   if (poles >= length) {
434     fprintf(stderr,"It makes no sense to have more poles than data! Exiting\n");
435     exit(AR_MODEL_TOO_MANY_POLES);
436   }
437   if (arimaset) {
438     if ((arpoles >= length) || (mapoles >= length)) {
439       fprintf(stderr,"It makes no sense to have more poles than data! Exiting\n");
440       exit(AR_MODEL_TOO_MANY_POLES);
441     }
442   }
443  
444   ardim=poles*dim;
445   aindex=make_ar_index();
446
447   check_alloc(vec=(double*)malloc(sizeof(double)*ardim));
448   check_alloc(mat=(double**)malloc(sizeof(double*)*ardim));
449   for (i=0;i<ardim;i++)
450     check_alloc(mat[i]=(double*)malloc(sizeof(double)*ardim));
451
452   check_alloc(coeff=(double**)malloc(sizeof(double*)*dim));
453   inverse=build_matrix(mat,ardim);
454   for (i=0;i<dim;i++) {
455     build_vector(vec,ardim,i);
456     coeff[i]=multiply_matrix_vector(inverse,vec,ardim);
457   }
458
459   check_alloc(diff=(double**)malloc(sizeof(double*)*dim));
460   for (i=0;i<dim;i++)
461     check_alloc(diff[i]=(double*)malloc(sizeof(double)*length));
462
463   pm=make_residuals(diff,coeff,ardim);
464
465   free(vec);
466   for (i=0;i<ardim;i++) {
467     free(mat[i]);
468     free(inverse[i]);
469   }
470   free(mat);
471   free(inverse);
472   size=ardim;
473   
474   if (arimaset) {
475     offset=poles;
476     for (i=0;i<2;i++)
477       free(aindex[i]);
478     free(aindex);
479
480     for (i=0;i<dim;i++)
481       free(coeff[i]);
482     free(coeff);
483     check_alloc(xdiff=(double**)malloc(sizeof(double*)*ITER));
484     for (i=0;i<ITER;i++)
485       check_alloc(xdiff[i]=(double*)malloc(sizeof(double)*dim));
486
487     armadim=(arpoles+mapoles)*dim;
488     aindex=make_arima_index(arpoles,mapoles);
489     size=armadim;
490
491     check_alloc(hseries=(double**)malloc(sizeof(double*)*2*dim));
492     for (i=0;i<dim;i++) {
493       check_alloc(hseries[i]=(double*)malloc(sizeof(double)*length));
494       check_alloc(hseries[i+dim]=(double*)malloc(sizeof(double)*length));
495       for (j=0;j<length;j++) {
496         hseries[i][j]=series[i][j];
497         hseries[i+dim][j]=diff[i][j];
498       }
499     }
500
501     for (i=0;i<dim;i++)
502       free(series[i]-ipoles);
503     free(series);
504
505     series=hseries;
506
507     check_alloc(oldcoeff=(double**)malloc(sizeof(double*)*dim));
508     for (i=0;i<dim;i++) {
509       check_alloc(oldcoeff[i]=(double*)malloc(sizeof(double)*armadim));
510       for (j=0;j<armadim;j++)
511         oldcoeff[i][j]=0.0;
512     }
513     check_alloc(diffcoeff=(double*)malloc(sizeof(double)*ITER));
514
515     for (iter=1;iter<=ITER;iter++) {
516       check_alloc(vec=(double*)malloc(sizeof(double)*armadim));
517       check_alloc(mat=(double**)malloc(sizeof(double*)*armadim));
518       for (i=0;i<armadim;i++)
519         check_alloc(mat[i]=(double*)malloc(sizeof(double)*armadim));
520
521       check_alloc(coeff=(double**)malloc(sizeof(double*)*dim));
522
523       poles=(arpoles > mapoles)? arpoles:mapoles;
524
525       offset += poles;
526       inverse=build_matrix(mat,armadim);
527
528       for (i=0;i<dim;i++) {
529         build_vector(vec,armadim,i);
530         coeff[i]=multiply_matrix_vector(inverse,vec,armadim);
531       }
532
533       pm=make_residuals(diff,coeff,armadim);
534
535       for (j=0;j<dim;j++) {
536         hdiff=0.0;
537         hj=j+dim;
538         for (i=offset;i<length;i++)
539           hdiff += sqr(series[hj][i]-diff[j][i]);
540         for (i=0;i<length;i++) {
541           series[hj][i]=diff[j][i];
542         }
543         xdiff[iter-1][j]=sqrt(hdiff/(double)(length-offset));
544       }
545
546       free(vec);
547       for (i=0;i<armadim;i++) {
548         free(mat[i]);
549         free(inverse[i]);
550       }
551       free(mat);
552       free(inverse);
553
554       diffcoeff[iter-1]=0.0;
555       for (i=0;i<dim;i++)
556         for (j=0;j<dim;j++) {
557           diffcoeff[iter-1] += sqr(coeff[i][j]-oldcoeff[i][j]);
558           oldcoeff[i][j]=coeff[i][j];
559         }
560       diffcoeff[iter-1]=sqrt(diffcoeff[iter-1]/(double)armadim);
561       alldiff=xdiff[iter-1][0];
562       for (i=1;i<dim;i++)
563         if (xdiff[iter-1][i] > alldiff)
564           alldiff=xdiff[iter-1][i];
565       realiter=iter;
566       if (alldiff < convergence)
567         iter=ITER;
568   
569       if (iter < ITER) {
570         for (i=0;i<dim;i++)
571           free(coeff[i]);
572         free(coeff);
573       }
574     }
575   }
576
577   if (stdo) {
578     if (arimaset) {
579       printf("#convergence of residuals in arima fit\n");
580       for (i=0;i<realiter;i++) {
581         printf("#iteration %ld ",i+1);
582         for (j=0;j<dim;j++)
583           printf("%e ",xdiff[i][j]);
584         printf("%e",diffcoeff[i]);
585         printf("\n");
586       }
587     }
588     avpm=pm[0]*pm[0];
589     loglikelihood= -log(pm[0]);
590     for (i=1;i<dim;i++) {
591       avpm += pm[i]*pm[i];
592       loglikelihood -= log(pm[i]);
593     }
594     loglikelihood *= ((double)length);
595     loglikelihood += -((double)length)*
596       ((1.0+log(2.*M_PI))*dim)/2.0;
597     avpm=sqrt(avpm/dim);
598     printf("#average forcast error= %e\n",avpm);
599     printf("#individual forecast errors: ");
600      for (i=0;i<dim;i++)
601       printf("%e ",pm[i]);
602     printf("\n");
603     if (arimaset)
604       aic=2.0*(arpoles+mapoles)-2.0*loglikelihood;
605     else
606       aic=2.0*poles-2.0*loglikelihood;
607     printf("#Log-Likelihood= %e\t AIC= %e\n",loglikelihood,aic);
608     for (i=0;i<size;i++) {
609       id=aindex[0][i];
610       is=aindex[1][i];
611       if (id < dim)
612         printf("#x_%u(n-%u) ",id+1,is);
613       else
614         printf("#e_%u(n-%u) ",id+1-dim,is);
615       for (j=0;j<dim;j++)
616         printf("%e ",coeff[j][i]);
617       printf("\n");
618     }
619     if (!run_model || (verbosity&VER_USR1)) {
620       for (i=poles;i<length;i++) {
621         if (run_model)
622           printf("#");
623         for (j=0;j<dim;j++)
624           if (verbosity&VER_USR2)
625             printf("%e %e ",series[j][i]+my_average[j],diff[j][i]);
626           else
627             printf("%e ",diff[j][i]);
628         printf("\n");
629       }
630     }
631     if (run_model && (ilength > 0)) {
632       if (!arimaset)
633         iterate_model(coeff,pm,diff,NULL);
634       else 
635         iterate_arima_model(coeff,pm,diff,NULL);
636     }
637   }
638   else {
639     file=fopen(outfile,"w");
640     if (verbosity&VER_INPUT)
641       fprintf(stderr,"Opened %s for output\n",outfile);
642     if (arimaset) {
643       fprintf(file,"#convergence of residuals in arima fit\n");
644       for (i=0;i<realiter;i++) {
645         fprintf(file,"#iteration %ld ",i+1);
646         for (j=0;j<dim;j++)
647           fprintf(file,"%e ",xdiff[i][j]);
648         fprintf(file,"%e",diffcoeff[i]);
649         fprintf(file,"\n");
650       }
651     }
652     avpm=pm[0]*pm[0];
653     loglikelihood= -log(pm[0]);
654     for (i=1;i<dim;i++) {
655       avpm += pm[i]*pm[i];
656       loglikelihood -= log(pm[i]);
657     }
658     loglikelihood *= ((double)length);
659     loglikelihood += -((double)length)*
660       ((1.0+log(2.*M_PI))*dim)/2.0;
661     avpm=sqrt(avpm/dim);
662     fprintf(file,"#average forcast error= %e\n",avpm);
663     fprintf(file,"#individual forecast errors: ");
664     for (i=0;i<dim;i++)
665       fprintf(file,"%e ",pm[i]);
666     fprintf(file,"\n");
667     if (arimaset)
668       aic=2.0*(arpoles+mapoles)-2.0*loglikelihood;
669     else
670       aic=2.0*poles-2.0*loglikelihood;
671     fprintf(file,"#Log-Likelihood= %e\t AIC= %e\n",loglikelihood,aic);
672     for (i=0;i<size;i++) {
673       id=aindex[0][i];
674       is=aindex[1][i];
675       if (id < dim)
676         fprintf(file,"#x_%u(n-%u) ",id+1,is);
677       else
678         fprintf(file,"#e_%u(n-%u) ",id+1-dim,is);
679       for (j=0;j<dim;j++)
680         fprintf(file,"%e ",coeff[j][i]);
681       fprintf(file,"\n");
682     }
683     if (!run_model || (verbosity&VER_USR1)) {
684       for (i=poles;i<length;i++) {
685         if (run_model)
686           fprintf(file,"#");
687         for (j=0;j<dim;j++)
688           if (verbosity&VER_USR2)
689             fprintf(file,"%e %e ",series[j][i]+my_average[j],diff[j][i]);
690           else
691             fprintf(file,"%e ",diff[j][i]);
692         fprintf(file,"\n");
693       }
694     }
695     if (run_model && (ilength > 0)) {
696       if (!arimaset)
697         iterate_model(coeff,pm,diff,file);
698       else
699         iterate_arima_model(coeff,pm,diff,file);
700     }
701     fclose(file);
702   }
703   if (outfile != NULL)
704     free(outfile);
705   if (infile != NULL)
706     free(infile);
707   for (i=0;i<dim;i++) {
708     free(coeff[i]);
709     free(diff[i]);
710     free(series[i]);
711     if (arimaset)
712       free(series[i+dim]);
713   }
714   free(coeff);
715   free(diff);
716   free(series);
717
718   free(pm);
719
720   return 0;
721 }