/* tmest.c CCMATH mathematics library source code. * * Copyright (C) 2000 Daniel A. Atkinson All rights reserved. * This code may be redistributed under the terms of the GNU library * public license (LGPL). ( See the lgpl.license file for details.) * ------------------------------------------------------------------------ */ /* Test: fixts seqts (Estimate an ARMA model of a time series.) Uses: eigen evmax evmod Input parameters: mod_fl -> name of model initialization file ser_fl -> name of binary ARMA time series data file [ created by garma ] Prompted input: at prompt ' (s,f) q->quit ' enter s for a sequential parameter update f for a gauss-newton parameter update q to terminate estimation at prompt ' save residuals? (y/n) ' enter y to save residuals to a file n to exit without saving residuals */ #include "ccmath.h" #include double *x,*var,*cr; struct mcof *par,*pma; int nar,nma,np; double *x; int nmax; void main(int na,char **av) { struct mcof *pp; double ssq,sig,ev; int n,j,k; FILE *fm,*fs; char fnam[32],cs[4]; if(na!=3){ printf("para: mod_fl ser_fl\n"); exit(-1);} fm=fopen(*++av,"r"); printf(" model file: %s\n",*av); /* model initialization */ fscanf(fm," %d %d",&nar,&nma); np=nar+nma; /* size model parameter and variance arrays */ par=(struct mcof *)calloc(np,sizeof(*par)); pma=par+nar; /* store for variance eigenvalues and vectors */ var=(double *)calloc(np*np+np,sizeof(double)); cr=var+np*np; printf(" model structure:\n"); for(j=0,pp=par; jlag)); printf(" ar at lag %d\n",pp->lag); pp->lag-=1; ++pp; } for(j=0; jlag)); printf(" ma at lag %d\n",pp->lag); pp->lag-=1; ++pp; } fclose(fm); /* read series file */ strcpy(fnam,*++av); fs=fopen(fnam,"rb"); printf(" data file: %s\n",fnam); fread((void *)&nmax,sizeof(int),1,fs); x=(double *)calloc(nmax,sizeof(double)); n=fread((char *)x,sizeof(x[0]),nmax,fs); printf(" %d points input\n\n",n); /* start interactive estimation of model parameters */ for(j=0; ;++j){ fprintf(stderr," (s,f) q->quit "); if(*gets(cs)=='q') break; /* sequential estimation step */ if(cs[0]=='s') ssq=seqts(x,n,var,j); /* gauss-newton estimation step */ else ssq=fixts(x,n,var,cr); printf(" step %d ssq= %8.3f %s\n",j,ssq,cs); printf(" p_vec: "); for(pp=par,k=0; kcf); printf("\n"); /* compute maximum eigenvalue and vector of parameter variance */ ev=evmax(var,cr,np)*sqrt(ssq/(n-np)); printf(" ev_max= %.9.6f and vector:\n",ev); matprt(cr,1,np," %.3f"); printf("\n"); } printf(" final state:\n"); printf(" ssq= %8.3f\n",ssq); printf(" final p_vec: "); for(pp=par,k=0; kcf); printf("\n"); sig=sqrt(ssq/(n-np)); printf(" rms e= %8.5f\n",sig); for(k=0; k