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

% PURPOSE: This function generates the stochastic functional models of simulation exercises.

% INPUT VARIABLES:
%    numberExercise: Number of the simulation exercise.
%    class: Number of the population.
%    sampleSize: Number of elements in the sample.
%    T: Length of the functions to be generated.
%    paramVector: Vector with the parameters of the model.

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

% COMMENTS:
%   (1) The smoothing method 1, based on the function 'csaps()', needs the Curve Fitting Toolbox of MATLAB.
%   (2) The user can easily add new models of stochastic functions.

function functions = dataExercisesFD(numberExercise, class, sampleSize, T, paramVector)

% Grid of points of the independent variables. It plays and important role
% in calculating the derivative with a formula for equispaced points.
t = 0:1/(T-1):1;

switch numberExercise
  case 1
    switch class
      case 1
        a = 0;  b= 1;
      case 2
        a = 1/2;  b= 3/2;
    end
    functions = paramVector(1)*nncopy(t, sampleSize, 1) + paramVector(2)*nncopy(random('unif',a,b,sampleSize,1),1,length(t));
  case 2
    switch class
      case 1
        functions = (paramVector(1)*nncopy(t,sampleSize,1) + paramVector(2)*nncopy(random('unif',0,1,sampleSize,1),1,length(t))).^2;
      case 2
        functions = paramVector(1)*(nncopy(t,sampleSize,1).^2) + paramVector(2)*nncopy(random('unif',0,1,sampleSize,1),1,length(t));
    end
  case 3
    switch class
      case 1
        a = 0;  b= 1;
      case 2
        a = 1/2;  b= 3/2;
    end
    functions = (paramVector(1)*nncopy(t,sampleSize,1) + paramVector(2)*nncopy(random('unif',a,b,sampleSize,1),1,length(t))).^2;
    if isequal(class,1)
      functions = functions + paramVector(3);
    end
  case 4
    % The covariance structure
    S = zeros(length(t),length(t));
    for i = 1:length(t)
      for j = i:length(t)
        S(i,j) = 0.25*exp(-(i-j)^2);
        S(j,i) = S(i,j);
      end
    end 
    functions = paramVector(1)*nncopy(t,sampleSize,1) + paramVector(2)*mvnrnd(zeros(1,length(t)),S,sampleSize);
    functions = smoothingFD(1, functions,t);
  case 5
    h1 = max(6/20-abs(t-10/20),0);
    h2 = max(6/20-abs(t-14/20),0); %  h2 = max(6/20-abs(t-4/20-10/20),0);
    h3 = max(6/20-abs(t-6/20),0); %  h3 = max(6/20-abs(t+4/20-10/20),0);
    switch class
      case 1
        functions = nncopy(rand(sampleSize,1),1,length(t)).*nncopy((h1-h2),sampleSize,1) + nncopy(h2,sampleSize,1);
      case 2
        functions = nncopy(rand(sampleSize,1),1,length(t)).*nncopy((h1-h3),sampleSize,1) + nncopy(h3,sampleSize,1);
    end
    functions = functions + paramVector(1)*randn(sampleSize,length(t));
    functions = smoothingFD(1, functions,t);
  case 6
    % The covariance structure
    S = zeros(length(t),length(t));
    for i = 1:length(t)
      for j = i:length(t)
        S(i,j) = 0.25*exp(-(i-j)^2);
        S(j,i) = S(i,j);
      end
    end 
    functions = paramVector(1)*nncopy(t,sampleSize,1) + paramVector(2)*mvnrnd(zeros(1,length(t)),S,sampleSize);
    if isequal(class,1)
      % Adding a peak
      functions = functions + paramVector(3)*nncopy(generatePeak(t,'Gauss',0.001,normpdf(0,0,0.001)),sampleSize,1);  % With T = 300; t = 0:1/(T-1):1;
    end
    functions = smoothingFD(1, functions,t);
  case 7
    switch class
      case 1
        functions = paramVector(1)*nncopy(generatePeak(t,'Dirichlet',paramVector(3),2*paramVector(3)+1),sampleSize,1);
      case 2
        functions = paramVector(1)*nncopy(generatePeak(t,'Fejer',paramVector(3),paramVector(3)^2),sampleSize,1);
    end
    functions = functions + paramVector(2)*randn(sampleSize,length(t));
    functions = smoothingFD(1, functions,t);
  case 60
    % This function generates the same models as 'functionsExercise6' but by
    % multiplying each (whole) peak for a random variable with uniform law in
    % the interval [0.8, 1]
    % The covariance structure
    S = zeros(length(t),length(t));
    for i = 1:length(t)
      for j = i:length(t)
        S(i,j) = 0.25*exp(-(i-j)^2);
        S(j,i) = S(i,j);
      end
    end 
    functions = paramVector(1)*nncopy(t,sampleSize,1) + paramVector(2)*mvnrnd(zeros(1,length(t)),S,sampleSize);
    if isequal(class,1)
      % Adding a peak
      functions = functions +...
          paramVector(3)*nncopy((1-0.2*rand(sampleSize,1)),1,length(t)).*nncopy(generatePeak(t,'Gauss',0.001,normpdf(0,0,0.001)),sampleSize,1);  % With T = 300; t = 0:1/(T-1):1;
    end
    functions = smoothingFD(1, functions,t);
  case 70
    % This function generates the same models as 'functionsExercise7' but by
    % multiplying each (whole) peak for a random variable with uniform law in
    % the interval [0.8, 1]
    switch class
      case 1
        functions = paramVector(1)*nncopy((1-0.2*rand(sampleSize,1)),1,length(t)).*nncopy(generatePeak(t,'Dirichlet',paramVector(3),2*paramVector(3)+1),sampleSize,1);  % With T = 300; t = 0:1/(T-1):1;
      case 2
        functions = paramVector(1)*nncopy((1-0.2*rand(sampleSize,1)),1,length(t)).*nncopy(generatePeak(t,'Fejer',paramVector(3),1),sampleSize,paramVector(3)^2);  % With T = 300; t = 0:1/(T-1):1;
    end
    functions = functions + paramVector(2)*randn(sampleSize,length(t));
    functions = smoothingFD(1, functions,t);
end

end
