/* tfmest.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: seqtsf fixtsf Uses: sdiff evfmod setevf evmax eigen Input parameters: mod_fl -> name of file of model initialization data ser_fl -> name of binary factor model time series data file [ created by gfarma ] 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 struct mcof *pfc,*par,*pma; int nfc,nar,nma,np,ndif; struct fmod *x; int nmax; void main(int na,char **av) { double *var,*cr; struct mcof *pp; double ssq,sig,ev,*pe; 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); /* enter model initialization data */ fscanf(fm,"%d %d %d %d",&nfc,&nar,&nma,&ndif); np=nfc+nar+nma; /* allocate store for madel parameters and variance */ pfc=(struct mcof *)calloc(np,sizeof(*pfc)); par=pfc+nfc; pma=par+nar; var=(double *)calloc(np*np+np,sizeof(*var)); cr=var+np*np; for(j=0,pp=pfc; jcf); } for(; jlag); pp->lag-=1; } for(; jlag); pp->lag-=1; } fclose(fm); printf(" Model Structure\n"); printf(" %d initial factor parameters\n",nfc); for(j=0,pp=pfc; jcf); printf("\n"); if(nar){ printf(" %d ar para. at lags",nar); for(j=0,pp=par; jlag+1); printf("\n"); } if(nma){ printf(" %d ma para. at lags",nma); for(j=0,pp=pma; jlag+1); printf("\n"); } printf(" difference order = %d\n",ndif); /* read factor model time series data */ strcpy(fnam,*++av); fs=fopen(fnam,"rb"); printf(" data file: %s\n",fnam); fread((void *)&nmax,sizeof(int),1,fs); x=(struct fmod *)calloc(nmax,sizeof(*x)); n=fread((char *)x,sizeof(x[0]),nmax,fs); /* difference input series if required */ if(ndif){ for(j=k=0; j=0){ x[k].val=ev; x[k++].fac=x[j].fac;} } n-=ndif; } printf(" %d points used in fit\n\n",n); /* start interactive estimation sequence */ for(j=0; ;++j){ fprintf(stderr," (s/f) q->quit "); if(*gets(cs)=='q') break; /* sequential estimation step */ if(cs[0]=='s') ssq=seqtsf(x,n,var,j); /* gauss-newton estimation step */ else ssq=fixtsf(x,n,var,cr); printf("%d ssq= %8.3f ",j,ssq); if(cs[0]=='s') printf("seq.\n"); else printf("fix\n"); printf(" p_vec: "); for(pp=pfc,k=0; kcf); /* compute maximum eigenvalue and vector of parameter variance */ ev=evmax(var,cr,np)*sqrt(ssq/(n-np)); printf("\n max ev and vector : %9.6f\n",ev); matprt(cr,1,np," %.3f"); printf("\n"); } printf(" final state:\n"); printf(" ssq = %8.3f\n",ssq); printf(" p_vec: "); for(pp=pfc,k=0; kcf); printf("\n"); sig=sqrt(ssq/(n-np)); printf(" rms e = %8.5f\n",sig); for(k=0; k