
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%  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
%                 moving average stochastic process of orders p and q, an ARMA(p,q).

% INPUT VARIABLES:
%    vectorFi: Vector with the parameters of the autoregressive part, AR(p).
%    vectorTheta: Vector with the parameters of the moving average part, MA(q).
%    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) This function can be used outside the framework of this paper.

function series = funcARMApq(vectorFi, vectorTheta, sampleSize, T)

p = length(vectorFi);  % Order of the AR structure
q = length(vectorTheta);  % Order of the MA structure
T2 = T+10*(p+q);  % To avoid a possible initial effect
matrixNoise = normrnd(0,1,sampleSize,q+T2);
matrixARMA = [normrnd(0,1,sampleSize,p), zeros(sampleSize, T2)];
for i1 = (p+1):(p+T2)
   matrixARMA(:,i1) = matrixARMA(:,(i1-p):(i1-1))*vectorFi(p:-1:1)' +...       
       matrixNoise(:,i1-p+q) -...
       matrixNoise(:,(i1-p):(i1-p+q-1))*vectorTheta(q:-1:1)';
end
series = matrixARMA(:,(end-T+1):(end));  % Taking the last T columns

end
