1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/* This code was graciously provided by Paul Bourke */
/* File - ar.h */
int AutoRegression(
double *inputseries,
int length,
int degree,
double *coefficients,
int method)
{
double mean;
int i, t;
double *w=NULL; /* Input series - mean */
double *h=NULL;
double *g=NULL; /* Used by mempar() */
double *per=NULL;
double *pef=NULL; /* Used by mempar() */
double **ar=NULL; /* AR coefficients, all degrees */
int success = TRUE;
/* Allocate space for working variables */
if ((w = (double *)malloc(length*sizeof(double))) == NULL) {
success = FALSE;
goto skip;
}
if ((h = (double *)malloc((degree+1)*sizeof(double))) == NULL) {
success = FALSE;
goto skip;
}
if ((g = (double *)malloc((degree+2)*sizeof(double))) == NULL) {
success = FALSE;
goto skip;
}
if ((per = (double *)malloc((length+1)*sizeof(double))) == NULL) {
success = FALSE;
goto skip;
}
if ((pef = (double *)malloc((length+1)*sizeof(double))) == NULL) {
success = FALSE;
goto skip;
}
if ((ar = (double **)malloc((degree+1)*sizeof(double*))) == NULL) {
success = FALSE;
goto skip;
}
for (i=0;i<degree+1;i++)
ar[i] = NULL;
for (i=0;i<degree+1;i++) {
if ((ar[i] = (double *)malloc((degree+1)*sizeof(double))) == NULL) {
success = FALSE;
goto skip;
}
}
/* Determine and subtract the mean from the input series */
mean = 0.0;
for (t=0;t<length;t++)
mean += inputseries[t];
mean /= (double)length;
for (t=0;t<length;t++)
w[t] = inputseries[t] - mean;
/* Perform the appropriate AR calculation */
if (method == MAXENTROPY) {
success = ARMaxEntropy(w,length,degree,ar,per,pef,h,g);
for (i=1;i<=degree;i++)
coefficients[i-1] = -ar[degree][i];
} else if (method == LEASTSQUARES) {
success = ARLeastSquare(w,length,degree,coefficients);
} else {
success = FALSE;
}
skip:
if (w != NULL)
free(w);
if (h != NULL)
free(h);
if (g != NULL)
free(g);
if (per != NULL)
free(per);
if (pef != NULL)
free(pef);
if (ar != NULL) {
for (i=0;i<degree+1;i++)
if (ar[i] != NULL)
free(ar[i]);
free(ar);
}
return(success);
}
/*
Previously called mempar()
Originally in FORTRAN, hence the array offsets of 1, Yuk.
Original code from Kay, 1988, appendix 8D.
Perform Burg's Maximum Entropy AR parameter estimation
outputting successive models en passant. Sourced from Alex Sergejew
Two small changes made by NH in November 1998:
tstarz.h no longer included, just say "typedef double REAL" instead
Declare ar by "REAL **ar" instead of "REAL ar[MAXA][MAXA]
Further "cleaning" by Paul Bourke.....for personal style only.
*/
int ARMaxEntropy(
double *inputseries,int length,int degree,double **ar,
double *per,double *pef,double *h,double *g)
{
int j,n,nn,jj;
double sn,sd;
double t1,t2;
for (j=1;j<=length;j++) {
pef[j] = 0;
per[j] = 0;
}
for (nn=2;nn<=degree+1;nn++) {
n = nn - 2;
sn = 0.0;
sd = 0.0;
jj = length - n - 1;
for (j=1;j<=jj;j++) {
t1 = inputseries[j+n] + pef[j];
t2 = inputseries[j-1] + per[j];
sn -= 2.0 * t1 * t2;
sd += (t1 * t1) + (t2 * t2);
}
g[nn] = sn / sd;
t1 = g[nn];
if (n != 0) {
for (j=2;j<nn;j++)
h[j] = g[j] + (t1 * g[n - j + 3]);
for (j=2;j<nn;j++)
g[j] = h[j];
jj--;
}
for (j=1;j<=jj;j++) {
per[j] += (t1 * pef[j]) + (t1 * inputseries[j+nn-2]);
pef[j] = pef[j+1] + (t1 * per[j+1]) + (t1 * inputseries[j]);
}
for (j=2;j<=nn;j++)
ar[nn-1][j-1] = g[j];
}
return(TRUE);
}
/*
Least squares method
Original from Rainer Hegger, Last modified: Aug 13th, 1998
Modified (for personal style and context) by Paul Bourke
*/
int ARLeastSquare(
double *inputseries,
int length,
int degree,
double *coefficients)
{
int i,j,k,hj,hi;
int success = TRUE;
double **mat;
mat = (double **)malloc(sizeof(double *)*degree);
for (i=0;i<degree;i++)
mat[i] = (double*)malloc(sizeof(double)*degree);
for (i=0;i<degree;i++) {
coefficients[i] = 0.0;
for (j=0;j<degree;j++)
mat[i][j] = 0.0;
}
for (i=degree-1;i<length-1;i++) {
hi = i + 1;
for (j=0;j<degree;j++) {
hj = i - j;
coefficients[j] += (inputseries[hi] * inputseries[hj]);
for (k=j;k<degree;k++)
mat[j][k] += (inputseries[hj] * inputseries[i-k]);
}
}
for (i=0;i<degree;i++) {
coefficients[i] /= (length - degree);
for (j=i;j<degree;j++) {
mat[i][j] /= (length - degree);
mat[j][i] = mat[i][j];
}
}
/* Solve the linear equations */
success = SolveLE(mat,coefficients,degree);
for (i=0;i<degree;i++)
if (mat[i] != NULL)
free(mat[i]);
if (mat != NULL)
free(mat);
return(success);
}
/*
Gaussian elimination solver
Author: Rainer Hegger Last modified: Aug 14th, 1998
Modified (for personal style and context) by Paul Bourke
*/
int SolveLE(double **mat,double *vec,unsigned int n)
{
int i,j,k,maxi;
double vswap,*mswap,*hvec,max,h,pivot,q;
for (i=0;i<n-1;i++) {
max = fabs(mat[i][i]);
maxi = i;
for (j=i+1;j<n;j++) {
if ((h = fabs(mat[j][i])) > max) {
max = h;
maxi = j;
}
}
if (maxi != i) {
mswap = mat[i];
mat[i] = mat[maxi];
mat[maxi] = mswap;
vswap = vec[i];
vec[i] = vec[maxi];
vec[maxi] = vswap;
}
hvec = mat[i];
pivot = hvec[i];
if (fabs(pivot) == 0.0) {
/* fprintf(stderr,"Singular matrix! Exiting!\n"); */
return(FALSE);
}
for (j=i+1;j<n;j++) {
q = - mat[j][i] / pivot;
mat[j][i] = 0.0;
for (k=i+1;k<n;k++)
mat[j][k] += q * hvec[k];
vec[j] += (q * vec[i]);
}
}
vec[n-1] /= mat[n-1][n-1];
for (i=n-2;i>=0;i--) {
hvec = mat[i];
for (j=n-1;j>i;j--)
vec[i] -= (hvec[j] * vec[j]);
vec[i] /= hvec[i];
}
return(TRUE);
}
|