Professional Documents
Culture Documents
Listing Prog Matlab
Listing Prog Matlab
%
% function p = nl_test (data, NumSurr, EmbDim, NumNeig);
%
% this function performs a testfor non liniearity in a time series by means
of surrogate data.
% surrogate calculation is carried out by phase randomization according to
the method
% of amplitude adjusted fourier transform
%
% input parameters:
%
% data : time series to be tested
% Numsurr : number of surrogetes to be calculated
% EmbDim : embedding dimension
% NumNeig : number of nearest neighbors to be included into calculation of
prredicted values
%
% Return parameter
% p-value
%
% this value should be less than 0.01 to claim that the time series under
study is
% not linier random origin
%
% Note that for HRV signals, after extensive study of optimal value for the
embedding
% dimension and naerest neighbors were found to be 3 and 6 respectively
% nl_test is the main matlab procedure that calls two pascal programs
% histgram.exe (which perform the histogram-transform) and
% forecast.exe (which calculate the prediction error)
%
% data exchange matlab and the external programs is handled through several
% files, see comments in the source code bellow.
% nl_test itself generates an ensemble of surrogate time series from the
rescaled original
% time series, calculates the distribution of our test statistics-the MAE-for
the
% surrogates, and compares it to the mae of the original data set.
%Store the Median Absolute Error of the prediction error time series and
%store the absolute values of the prediction error time series in the next
%item in the array e
e(h) = median(abs(pred_err));
end
% call the forecasting alogarithm to pridict the oroginal time series under
% study called HRV.dat and store the absolute values of the prediction
% errors in pred_err.dat
callForecaStr = sprintf('!Forecast %d %d HRV.dat pred_err.dat', EmbDim,
NumNeigh);
eval (CallForecastStr);
load pred_err.dat;
% calculate the median absolute error (AME)
E = median (abs(pred_err));
% Compute the p-value
p = erfc(abs(E-mean(e))/std(e))/sqr(2));
{
this program implements the Histogram and inverse histogram
transformation
the way this transformation is implemented is by storing the two time
series
in an array LIST where each element in the array consists of four values
if we simply swap the inputs passed to the program, we are simply doing
the inverse
histogram transformation.
}
program hstgram;
uses crt;
type
element=node;
node=record
indx:real;
valx:real;
indg:real;
valg:real;
arraytype=array[1..1024] of element;
indextype=0..1024;
var list:arraytype;
number,i:integer;
fp,fp2:text;
temp:element;
filename:string[20];
procedure inter_swap(n:integer);
for i:=1 to number do
begin
temp'.valx:=list[i]'.valx;
temp'.indx:=list[i]'.indg;
if n=1 then
begin
list[i]'.valx:=list[i]'.valg;
list[i]'.indx:=list[i]'.indg;
end
else
begin
list[i]'.valx:=list[i]'.valg;
list[i]'.indx:=list[i]'.indg;
end;
list[i]'.valx:=temp'.valx;
list[i]'.indx:=temp'.indx;
end;
end
procedure swap (var x,y:element;n:integer);
begin
temp'.indx:=x'.indx;temp'.valx:=x'.valx;
if n=2 then begin temp'.indg:=x'.indg;temp'.valg:=x'.valg;end;
x'.indx:=y'.indx;x'.valx:=y'.valx;
if n=2 then begin x'.indg:=y'.indg;x'.valg:=y'.valg;end;
y'.indx:=temp'.indx;y'.valx:=temp'.valx;
if n=2 then begin y'.indg:=temp'.indg'y'.valg:=temp'.valg;end;
end;
(this procedure is called resurcevely by the quicksort procedure qsort)
Close(fp);
Close(fp2);
(sort the first time series in descending order-the data points and keep track of the indices)
Qsort(list,1,number,1);
Inter_swap(1);
(sort the second time series in descending order-the data points and keep track of the indices)
Qsort(list,1,number,1);
(swap the indices)
Inter_swap(2);
Qsort(list,1,number,2);
Assign(fp,paramStr(3));
Rewrite(fp);
Writeln(fp.list(i)’.valg);
Close(fp);
End.
Program forecast;
Uses crt;
Const
Dmax=50;
Tiny=1e-20;
Type
X_arr=array[1..511] of double;
Var
Filename:string[20];
temp:double;
indx,ndex:int;
a:two;
fp1,fp:text;
X:array[0..1024] of double;
Deff.x_pred:x_arr;
(the source code of of the following procedure is omitted due to copyright reasons it can be found on
the book
w.h.press,S.A.teukolsky,w.t.vetterling,B.P.flannery
1st Edition
ndex[k]:=mindex;
deff[mindex]:=300000;
end;
end;
begin
writeln('');
writeln('ERROR-wrong number in parameters');
writeln('forecast Embdim NumNeig PredFile PredErrFile');
writeln('Parameters');
writeln('EmbDim embedding demention');
writeln(NumNieg number of neighbors to include in linear fit');
writeln('predfile input ASCII-File containing values of time series to be
predicted');
writeln('PredErrFile output ASCII-File containing prediction error');
exit;
end;
clrscr;
val(paramstr(1), dim, converr);
if converr <> 0 then
begin
writeln ('cannot evaluate expression for command line argument EmbDim');
exit
end;
val(paramStr (2), kv, converr);
if convErr <> 0 then
begin
writeln ('cannot evaluate expression for command line argument
NumbNeig');
exit
end;
(fp point to the file containing the time series to be predicted)
assign(fp,paramstr(3));
reset(fp);
(fp1 points to the prediction-error file)
assign(fp1,paramstr(4));
rewrite(fp1);
n:=1;
number := 0;
while not eof (fp) do
begin
for i:=1 to dim-1 do deff[i]:= 30000;
for i:=dim to ( number div 2-2) do
begin
temp:=0;
for ii:=0 to dim-1 do temp:=temp+sqr(x[j-ii]-x[i-ii]);
deff[i]:=sqrt(temp);
end;
selectsort(deff, (number div 2-2),ndex);
Begin
Begin
A[I,k]:=0
For ii:=1 to kv do a[i,k]:= a[I,k]+x[ndex[ii]-k+1]*x[ndex[ii]-i+1];
end;
a[i,dim+1]:=0;
End;
Begin
a[dim+1,k]:=0;
a[dim+1, dim+1]:=kv;
Begin
b[i]:=0
b[dim+1]:=0;
Ludcmp(a,dim+1,indx,d);
Lubksb(a, dim+1,indx,b);
x_pred[n]:=0;
x_pred[n]:=x_pred(n)+x[j-ii+1]*b(ii);
x_pred[n]:=x_pred(n)+b[dim+1];
Writeln(fpl,abs(x_pred[n]-x[j+1]));
Inc(n);
End;
End.