
clc
close all
clear fig

% Calculation of several values
Np.a = 25;
Np.c = 5;
% Np.tot = Np.a:Np.c:opt.Np;
% n.PLS  = (opt.Np-Np.a)/Np.c+1;
itg.t = 410;
Np.tot = Np.a:Np.c:itg.t;
n.PLS  = (itg.t-Np.a)/Np.c+1;
%%
% Calculation of the norms
if ~exist('norm_comb_mean_1','var') || isempty(norm_comb_mean_1{2,1})
    
% Predefinition of the matrices
if true
    xload      = cell(n.PLS,n.red);
    yload      = xload;
    xloads     = xload;
    load_used  = xload;
    xscores    = xload;
    yscores    = xload;
    stats      = xload;
    beta       = xload;
    pctvar     = xload;
    sumpct     = xload;
    diff_loads = xload;
    diff_mat   = cell(n.in,n.red);
    
    % 1-norm
    norm_comb_1           = xload;
    norm_comb_mean_1      = xloads;
    norm_comb_mean_1(1,:) = num2cell(ones(1,n.red));
    
    % 2-norm
    norm_comb_2           = xload;
    norm_comb_mean_2      = xloads;
    norm_comb_mean_2(1,:) = num2cell(ones(1,n.red));
    
    % inf-norm
    norm_comb_i           = xload;
    norm_comb_mean_i      = xloads;
    norm_comb_mean_i(1,:) = num2cell(ones(1,n.red));
    
    % frob-norm
    norm_comb_f           = xload;
    norm_comb_mean_f      = xloads;
    norm_comb_mean_f(1,:) = num2cell(ones(1,n.red));
    
    norm_loads = cell(n.PLS,n.red,n.in);
    norm_loads_mean = norm_loads;
    optpls = statset('UseParallel',true);
end
    
for k = 1:n.PLS
    
    for j = 1:n.red     
        
        % Scaling of the independent variables
        [U.tra.scaled,Var.U.mu,Var.U.std] = zscore(U.tra.sel(1:Np.tot(k),:));

        % Scaling of the dependent variables
        [x.tra.scaled,Var.x.mu,Var.x.std]  = zscore(x.tra.red(1:Np.tot(k),j));
        
        % Calculation of the Partial least square regression
        [xload{k,j},yload{k,j},xscores{k,j},yscores{k,j},...
                 beta{k,j},pctvar{k,j},~,stats{k,j}] = plsregress(U.tra.scaled,x.tra.scaled,n.in,'options',optpls);
         
        load_used{k,j} = stats{k,j}.W;xload{k,j};%/(xload{k,j}'*xload{k,j});  
        % Rearrangement and normalization of the loads
%         xloads{k,j} = load_used{k,j}./(ones(n.in,1)*max(abs(load_used{k,j})));
        xloads{k,j} = load_used{k,j}./(ones(n.in,1)*sqrt(diag(stats{k,j}.W'*stats{k,j}.W)'));
        sumpct{k,j} = sum(pctvar{k,j}(2,:) > opt.comptresh);
        if k > 1
            diff_loads{k,j} = (xloads{k,j}-xloads{k-1,j});
            
            % 1-norm
            norm_comb_1{k,j}   = norm(diff_loads{k,j}(:,1:min(sumpct{k,j},sumpct{k-1,j})),1);
            norm_comb_mean_1{k,j} = mean(cell2mat(norm_comb_1(max(k-n.norm,1)+1:k,j)));
            
            % 2-norm
            norm_comb_2{k,j}   = norm(diff_loads{k,j}(:,1:min(sumpct{k,j},sumpct{k-1,j})),2);
            norm_comb_mean_2{k,j} = mean(cell2mat(norm_comb_2(max(k-n.norm,1)+1:k,j)));
            
            % inf-norm
            norm_comb_i{k,j}   = norm(diff_loads{k,j}(:,1:min(sumpct{k,j},sumpct{k-1,j})),inf);
            norm_comb_mean_i{k,j} = mean(cell2mat(norm_comb_i(max(k-n.norm,1)+1:k,j)));
            
            % inf-norm
            norm_comb_f{k,j}   = norm(diff_loads{k,j}(:,1:min(sumpct{k,j},sumpct{k-1,j})),'fro');
            norm_comb_mean_f{k,j} = mean(cell2mat(norm_comb_f(max(k-n.norm,1)+1:k,j)));
            for l = 1:n.in
                norm_loads{k,j,l} = norm(diff_loads{k,j}(:,l),'fro');
                norm_loads_mean{k,j,l} = mean(cell2mat(norm_loads(max(k-n.norm,1)+1:k,j,l)));
                diff_mat{l,j}(:,k) = diff_loads{k,j}(:,l);
            end
        end
        test2{j}(:,k) = xloads{k,j}(:,1);
        test3{j}(:,k) = mean(test2{j}(:,max(k-n.norm,1)+1:k),2);
    end
    
    if mod(Np.tot(k),100) == 0
        fprintf('%i points are sampled.\n',Np.tot(k))
    end
    
end
end

% Definition of integer values
itg.fig = 1;
itg.lab = 1;
itg.fda = 1;
itg.axe = 1;
itg.ann = 1;

% Definition of figure and values
fig.dimw12 = 12.3;  % 12.5 for ESCAPE
fig.dimh12 = 5.5;
fig.dimw21 = 8.4;
fig.dimh21 = 12;
fig.dimw = 8.4;
fig.dimh = 6;
fig.Font = [10 8];
fig.label = [];
fig.ann   = [];
fig.dat   = [];

% Definition of a colour string
rk = {'k','k','k','b','b'};%[0.93 0.69 0.13]}
ls = {'-','--','-.','-','--'};

for j = 1:n.red
    fig.in(itg.fig) = figure(itg.fig);
    set(fig.in(itg.fig),'units','centimeters','position', [5+itg.fig,5+itg.fig, fig.dimw, fig.dimh]);
    fig.axes(itg.axe) = gca;
    hold on
    for l = 1:sumpct{end,j}
        fig.dat(itg.fda) = plot(Np.tot(2:n.PLS),cell2mat(norm_loads_mean(1:n.PLS,j,l)),...
                                'Color',rk{l},'LineStyle',ls{l}); itg.fda = itg.fda+1;
    end
%     fig.dat(itg.fda) = plot(Np.tot(2:n.PLS),cell2mat(norm_comb_mean_1(2:n.PLS,j)),'r--'); itg.fda = itg.fda+1;
%     fig.dat(itg.fda) = plot(Np.tot(2:n.PLS),cell2mat(norm_comb_mean_2(2:n.PLS,j)),'k--'); itg.fda = itg.fda+1;
%     fig.dat(itg.fda) = plot(Np.tot(2:n.PLS),cell2mat(norm_comb_mean_i(2:n.PLS,j)),'r'); itg.fda = itg.fda+1;
    fig.dat(itg.fda) = plot(Np.tot(2:n.PLS),cell2mat(norm_comb_mean_f(2:n.PLS,j)),'Color','r'); itg.fda = itg.fda+1;
    legend('$\Delta\mathbf{w}_{1,k}$','$\Delta\mathbf{w}_{2,k}$','$\Delta\mathbf{w}_{3,k}$','$\Delta\mathbf{W}_{1:3,k}$') %,'$\mathbf{w}_4$'
    set(fig.axes(itg.axe).Legend,'Location','Best','FontSize',fig.Font(1),'Interpreter','latex','Box','off')
    fig.label(itg.lab) = xlabel('Number of sampling points $n_p$'); itg.lab = itg.lab+1;
    fig.label(itg.lab) = ylabel('Norm $\left|\cdot\right|_{2/F}$'); itg.lab = itg.lab+1;
    fig.axes(itg.axe).YScale = 'log';
    ylim([10^-4 10]);
    fig.axes(itg.axe).YTick = 10.^(-4:1:1);
%     fig.axes(itg.axe).XScale = 'log';
    itg.axe = itg.axe+1;
    itg.fig = itg.fig+1;
    
%     fig.in(itg.fig) = figure(itg.fig);
%     for l = 1:n.in
%         subplot(2,ceil(n.in/2),l)
%         title(['Component ',num2str(l)])
%         plot(Np.tot,diff_mat{l,j}')
%         ylim(opt.normtresh*[-1 1])
%     end
%     subplot(2,ceil(n.in/2),l+1)
%     plot(Np.tot,cell2mat(sumpct))
%     itg.fig = itg.fig+1;
end


% set(fig.ann(1:2), 'Interpreter', 'Latex');
% set(fig.ann(1:2), 'Fontsize', 10);
% set(fig.ann(1:2), 'LineStyle', 'none');
set(fig.label, 'Fontsize', fig.Font(1));
set(fig.label, 'Interpreter', 'Latex');
set(fig.axes,'FontSize', fig.Font(2));
set(fig.axes,'Box','on');
set(fig.axes,'TickLabelInterpreter', 'Latex');
set(fig.axes,'LineWidth',.75);
set(fig.axes,'XMinorTick', 'on');
set(fig.axes,'YMinorTick', 'on');
set(fig.dat,'LineWidth',.75)
% fig.axes(2).FontSize = 6;

