
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%  Title: StatisCLAS: A Package of Classification Methods   %%%%
%%%%  Author: David Casado de Lucas                            %%%%
%%%%  Version: 29th May 2013                                   %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% PURPOSE: This function generates time series from an autoregressive
%                 process of order 2 with one time-varying coefficient.

% INPUT VARIABLES:
%    paramVector: Vector with the parameters of the model.
%    sampleSize: Number of series to be generated.
%    T: Length of the series to be generated.

% OUTPUT VARIABLES:
%    series: Matrix of size 'sampleSize by T' with the generated series.

% COMMENTS:
%   (1) The expression of the time-varying coefficient is given in the helping file.
%   (2)  The user must check that, for the parameter values entered, the roots of 
%         the characteristic polynomial of the autoregressive process are inside the
%         unit circle  (the stochastic AR process would not be stationary otherwise).

function series = funcAR2tv(paramVector, sampleSize, T)

p = 2;  % Order of the AR process
T2 = T+10*p;  % To avoid a possible initial effect
matrixNoise = normrnd(0, 1, sampleSize, T2);
a = paramVector(1)*(1-paramVector(2)*cos((1:T)*pi/T)); % Time-varying coefficients
a = [a(1)*ones(1,10*p) a];
matrixAR2slex = [normrnd(0, 1, sampleSize, p), zeros(sampleSize, T2)];
for i1 = (p+1):(p+T2)
   matrixAR2slex(:,i1) =...
       matrixAR2slex(:,(i1-p):(i1-1))*[-paramVector(3); a(i1-p)] + matrixNoise(:,i1-p);
end
series = matrixAR2slex(:,(end-T+1):(end));  % Taking only the last T columns

end
