function p_eff = gp_peff(gp, x, y, varargin);
%GP_PEFF  The effective number of parameters in GP model with focus
%         on latent variables
%
%  Description
%    P_EFF = EP_PEFF(GP, X, Y) Takes the Gaussian process structure
%    GP, training inputs X and training outputs and returns the
%    effective number of parameters as defined by Spiegelhalter et
%    al. (2002).
%
%    NOTE! The effective number of parameters is evaluated with
%    focus on latent variable f. This means that the parameters th
%    (parameters of covariance function and likelihood) are
%    considered fixed. (See Spiegelhalter et al (2002) for
%    discussion on the parameters in focus in Bayesian model).
%    Thus, the returned p_eff tells the effective number of latent
%    variables. This statistics is important for example when
%    assessing the goodness of Laplace or EP approximation in case
%    of non-Gaussian likelihood (See Vanhatalo et al for
%    discussion).
%
%    If you want to evaluate the effective number of parameters
%    with focus on parameters, see GP_DIC.
%
%    The effective number of parameters is approximated as follows:
%        p_eff = n - trace( K\C ),
%
%    where K is the prior covariance matrix and C the posterior
%    covariance matrix. This approximation is introduced by
%    Spiegelhalter et al. (2002) in equation (16). If the
%    likelihood is non-Gaussian and gp.latent_method is either
%    Laplace or EP, then C is the Laplace or EP approximation for
%    the posterior covariance.
%
%  References:
%    Spiegelhalter, Best, Carlin and van der Linde (2002).
%    Bayesian measures of model complexity and fit. J. R.
%    Statist. Soc. B, 64(4):583-639.
%
%    Vanhatalo, J., Pietil�inen V. and Vehtari, A. (2010).
%    Approximate inference for disease mapping with sparse
%    Gaussian processes. Statistics in Medicine, 29(15):1580-1607.
%
%  See also
%    GP_DIC, DEMO_MODELASSESMENT1
%
%
% Copyright (c) 2009-2010 Jarno Vanhatalo

% This software is distributed under the GNU General Public
% License (version 3 or later); please refer to the file
% License.txt, included with the software, for details.

  ip=inputParser;
  ip.FunctionName = 'GP_PEFF';
  ip.addRequired('gp',@isstruct);
  ip.addRequired('x', @(x) ~isempty(x) && isreal(x) && all(isfinite(x(:))))
  ip.addRequired('y', @(x) ~isempty(x) && isreal(x) && all(isfinite(x(:))))
  ip.addParamValue('z', [], @(x) isreal(x) && all(isfinite(x(:))))
  ip.parse(gp, x, y, varargin{:});
  z=ip.Results.z;

  tn = size(x,1);

    if isfield(gp.lik.fh,'trcov')
      % a Gaussian likelihood

        switch gp.type
          case 'FULL'

            [K, C] = gp_trcov(gp, x);
            L = chol(C);
            p_eff = trace( L\(L'\K) );

          case 'FIC'
            u = gp.X_u;
            m = size(u,1);
            % Turn the inducing vector on right direction
            if size(u,2) ~= size(x,2)
                u=u';
            end
            % Calculate some help matrices
            [Kv_ff, Cv_ff] = gp_trvar(gp, x);  % 1 x f  vector
            K_fu = gp_cov(gp, x, u);   % f x u
            K_uu = gp_trcov(gp, u);     % u x u, noiseles covariance K_uu
            Luu = chol(K_uu)';

            % Evaluate Lambda (La) for specific model
            % Q_ff = K_fu*inv(K_uu)*K_fu'
            % Here we need only the diag(Q_ff), which is evaluated below
            B=Luu\(K_fu');
            Qv_ff=sum(B.^2)';
            Lav = Kv_ff-Qv_ff;
            Lav2 = Cv_ff-Qv_ff;

            iLaKfu = zeros(size(K_fu));  % f x u,
            n = size(x,1);
            for i=1:n
                iLaKfu(i,:) = K_fu(i,:)./Lav2(i);  % f x u
            end
            A = K_uu+K_fu'*iLaKfu;
            A = (A+A')./2;

            L = iLaKfu/chol(A);

            p_eff = sum(Lav./Lav2) + sum(sum( repmat(Lav2,1,m).\B'.*B' - L.*(L'*B'*B)' - L.*(L'.*repmat(Lav',m,1))', 2));

% $$$             % Check the result using full matrices
% $$$             C = B'*B + diag(Lav2);
% $$$             K = B'*B + diag(Lav);
% $$$             L = chol(C);
% $$$             p_eff = trace( L\(L'\K) );

          case {'PIC' 'PIC_BLOCK'}
            u = gp.X_u;
            ind = gp.tr_index;
            if size(u,2) ~= size(x,2)
                u=u';
            end

            % Calculate some help matrices
            [Kv_ff, Cv_ff] = gp_trvar(gp, x);  % 1 x f  vector
            K_fu = gp_cov(gp, x, u);         % f x u
            K_uu = gp_trcov(gp, u);    % u x u, noiseles covariance K_uu
            Luu = chol(K_uu)';

            % Evaluate the Lambda (La) for specific model
            % Q_ff = K_fu*inv(K_uu)*K_fu'
            % Here we need only the diag(Q_ff), which is evaluated below
            B=Luu\K_fu';
            iLaKfu = zeros(size(K_fu));  % f x u
            for i=1:length(ind)
                Qbl_ff = B(:,ind{i})'*B(:,ind{i});
                [Kbl_ff, Cbl_ff] = gp_trcov(gp, x(ind{i},:));
                La{i} = Kbl_ff - Qbl_ff;
                La2{i} = Cbl_ff - Qbl_ff;
                iLaKfu(ind{i},:) = La2{i}\K_fu(ind{i},:);
            end
            A = K_uu+K_fu'*iLaKfu;
            A = (A+A')./2;            % Ensure symmetry
            L = iLaKfu/chol(A);

            p_eff = sum(sum(- L.*(L'*B'*B)',2));
            for i=1:length(ind)
                LLa2 = chol(La2{i});
                p_eff = p_eff + trace(LLa2\(LLa2'\La{i})) + trace( LLa2\(LLa2'\B(:,ind{i})'*B(:,ind{i})) - L(ind{i},:)*L(ind{i},:)'*La{i} );
            end

          case 'CS+FIC'
            u = gp.X_u;
            m = size(u,1);
            % Turn the inducing vector on right direction
            if size(u,2) ~= size(x,2)
                u=u';
            end

            % Indexes to all non-compact support and compact support covariances.
            cf1 = [];
            cf2 = [];

            ncf = length(gp.cf);
            % Loop through all covariance functions
            for i = 1:ncf
                % Non-CS covariances
                if ~isfield(gp.cf{i},'cs')
                    cf1 = [cf1 i];
                    % CS-covariances
                else
                    cf2 = [cf2 i];
                end
            end

            [Kv_ff, Cv_ff] = gp_trvar(gp, x, cf1);  % f x 1  vector
            K_fu = gp_cov(gp,x,u,cf1);         % f x u
            K_uu = gp_trcov(gp,u,cf1);    % u x u, noiseles covariance K_uu
            K_uu = (K_uu+K_uu')./2;     % ensure the symmetry of K_uu

            Kcs = gp_trcov(gp, x, cf2);
            Luu = chol(K_uu)';
            B=Luu\(K_fu');       % u x f
            Qv_ff=sum(B.^2)';
            Lav = Cv_ff-Qv_ff;   % f x 1, Vector of diagonal elements
            Lav2 = Kv_ff-Qv_ff;   % f x 1, Vector of diagonal elements

            La = sparse(1:tn,1:tn,Lav,tn,tn) + Kcs;
            La2 = sparse(1:tn,1:tn,Lav2,tn,tn) + Kcs;

            iLaKfu = La\K_fu;
            A = K_uu+K_fu'*iLaKfu;
            A = (A+A')./2;     % Ensure symmetry
            L = iLaKfu/chol(A);

            LLa2 = L'*La2;
            LaB = La\B';
            LBB = L'*B'*B;

            VD = ldlchol(La);
            spiLa = spinv(VD,1);

            p_eff = sum( sum(LaB.*B',2) + sum(spiLa.*La2,2) - sum(L.*LBB',2) - sum(L.*LLa2',2) );

% $$$
% $$$             C = B'*B + Kcs + diag(Lav);
% $$$             K = B'*B + Kcs + diag(Lav2);
% $$$
% $$$             L = chol(C);
% $$$             p_eff = trace( L\(L'\K) );
        end

    else
      % ============================
      % A non Gaussian likelihood
      % ============================

        switch gp.type
          case 'FULL'
            switch gp.latent_method
              case 'EP'
                %[e, edata, eprior, tautilde, nutilde, L] = gpep_e(gp_pak(gp), gp, x, y, 'z', z);
                [e, edata, eprior, p] = gpep_e(gp_pak(gp), gp, x, y, 'z', z);
                [tautilde, L] = deal(p.tautilde, p.L);

                % The prior variance
                K=gp_trcov(gp,x);

                if all(tautilde > 0) && ~isequal(gp.latent_opt.optim_method, 'robust-EP')
                    sqrttautilde = sqrt(tautilde);
                    Stildesqroot = sparse(1:tn, 1:tn, sqrttautilde, tn, tn);

                    if issparse(L)
                        p_eff = trace(Stildesqroot*ldlsolve(L, Stildesqroot*K));
                    else
                        p_eff = trace(Stildesqroot*(L'\(L\Stildesqroot)*K));
                    end
                else
                    C = L*L';
                    L = chol(K);
                    Varf = tn - trace(L\(L'\C));
                end

              case 'Laplace'
                %[e, edata, eprior, f, L] = gpla_e(gp_pak(gp), gp, x, y, 'z', z);
                [e, edata, eprior, p] = gpla_e(gp_pak(gp), gp, x, y, 'z', z);
                [f, L] = deal(p.f, p.L);

                W = -gp.lik.fh.llg2(gp.lik, y, f, 'latent', z);

                % Evaluate the prior variance
                K = gp_trcov(gp,x);

                if W >= 0
                    if issparse(K) && issparse(L)

                        sqrtW = sparse(1:tn, 1:tn, sqrt(W), tn, tn);
                        sqrtWK = sqrtW*K;
                        p_eff = trace(sqrtW*ldlsolve(L,sqrtWK));
                    else
                        W = diag(W);
                        p_eff = trace(sqrt(W)*(L'\(L\(sqrt(W)*K))));
                    end
                else
                    C = L*L';
                    L = chol(K);
                    p_eff = tn - trace(L\(L'\C));
                end
            end

          case 'FIC'
            u = gp.X_u;
            m = size(u,1);
            % Calculate some help matrices
            [Kv_ff, Cv_ff] = gp_trvar(gp, x);  % 1 x f  vector
            K_fu = gp_cov(gp, x, u);   % f x u
            K_uu = gp_trcov(gp, u);     % u x u, noiseles covariance K_uu
            Luu = chol(K_uu)';
            B=Luu\(K_fu');
            Qv_ff=sum(B.^2)';
            Lav = Kv_ff-Qv_ff;

            switch gp.latent_method
              case 'EP'
                %[e, edata, eprior, tautilde, nutilde, L, La, b] = gpep_e(gp_pak(gp), gp, x, y, 'z', z);
                %[e, edata, eprior, p] = gpep_e(gp_pak(gp), gp, x, y, 'z', z);
                [L, La] = deal(p.L, p.La2);

                k = gp_trvar(gp,x);

                p_eff = sum( sum( (repmat(1./La,1,m).*B').*B',2) ) - sum(sum(L.*((L'*B')*B)',2));
                Lav = k - sum(B.^2)';
                p_eff = p_eff + sum(Lav./La) - sum(sum(L.*L,2).*Lav);

% $$$                 C = diag(1./La) - L*L';
% $$$                 K = B'*B + diag(k - sum(B.^2)');
% $$$
% $$$                 p_eff = trace(C*K);

              case 'Laplace'
                %[e, edata, eprior, f, L, a, La2] = gpla_e(gp_pak(gp), gp, x, y, 'z', z);
                [e, edata, eprior, p] = gpla_e(gp_pak(gp), gp, x, y, 'z', z);
                [f, La2] = deal(p.f, p.La2);

                W = -gp.lik.fh.llg2(gp.lik, y, f, 'latent', z);
                La = W.*Lav;
                Lahat = 1 + La;
                sqrtW = sqrt(W);
                B = (repmat(sqrtW,1,m).*K_fu);

                % Components for (I + W^(1/2)*(Qff + La2)*W^(1/2))^(-1) = Lahat^(-1) - L2*L2'
                B2 = repmat(Lahat,1,m).\B;
                A2 = K_uu + B'*B2; A2=(A2+A2)/2;
                L2 = B2/chol(A2);

                BB=Luu\(K_fu');
                BB2=B/Luu';

                p_eff = sum(W./Lahat.*Lav);
                p_eff = p_eff + sum(sqrtW .* (sum((repmat(Lahat,1,m).\BB2).*BB',2)...
                                             - sum(L2.*(L2'.*repmat(sqrtW'.*La2',m,1))',2)...
                                             - sum(L2.*(L2'*BB2*BB)',2)) );

% $$$                 K = BB'*BB + diag(Lav);
% $$$                 sW = diag(sqrt(W));
% $$$                 B = eye(size(K)) + sW*K*sW;
% $$$                 L = chol(B)';
% $$$
% $$$                 W = diag(W);
% $$$                 p_eff = trace(sqrt(W)*(L'\(L\(sqrt(W)*K))));
            end

          case {'PIC' 'PIC_BLOCK'}
            u = gp.X_u;
            K_fu = gp_cov(gp, x, u);
            K_uu = gp_trcov(gp, u);
            K_uu = (K_uu+K_uu')./2;

            m = size(u,1);
            ind = gp.tr_index;
            Luu = chol(K_uu)';
            B=Luu\(K_fu');

            switch gp.latent_method
              case 'EP'

                %[e, edata, eprior, tautilde, nutilde, L, La, b] = gpep_e(gp_pak(gp), gp, x, y, 'z', z);
                [e, edata, eprior, p] = gpep_e(gp_pak(gp), gp, x, y, 'z', z);
                [L, La] = deal(p.L, p.La2);

                p_eff = - sum(sum(L.*((L'*B')*B)',2));

                for i=1:length(ind)
                    La1 = gp_trcov(gp, x(ind{i},:)) - B(:,ind{i})'*B(:,ind{i});
                    p_eff = p_eff + trace(La{i}\B(:,ind{i})'*B(:,ind{i}));
                    p_eff = p_eff + trace(La{i}\La1);
                    p_eff = p_eff - trace(L(ind{i},:)*L(ind{i},:)'*La1);
                end

              case 'Laplace'
                %[e, edata, eprior, f, L, a, La2] = gpla_e(gp_pak(gp), gp, x, y, 'z', z);
                [e, edata, eprior, p] = gpla_e(gp_pak(gp), gp, x, y, 'z', z);
                [f, La2] = deal(p.f, p.La2);

                iKuuKuf = K_uu\K_fu';

                % Evaluate the variance

                W = -gp.lik.fh.llg2(gp.lik, y, f, 'latent', z);
                sqrtW = sqrt(W);

                % Components for (I + W^(1/2)*(Qff + La2)*W^(1/2))^(-1) = Lahat^(-1) - L2*L2'
                for i=1:length(ind)
                    La{i} = diag(sqrtW(ind{i}))*La2{i}*diag(sqrtW(ind{i}));
                    Lahat{i} = eye(size(La{i})) + La{i};
                    LLahat{i} = chol(Lahat{i});
                end
                sKfu = (repmat(sqrt(W),1,m).*K_fu);
                for i=1:length(ind)
                    iLasKfu(ind{i},:) = Lahat{i}\sKfu(ind{i},:);
                end
                A2 = K_uu + sKfu'*iLasKfu; A2=(A2+A2)/2;
                L2 = iLasKfu/chol(A2);

                p_eff = -sum(sqrtW.*sum(L2.*(L2'*(repmat(sqrtW,1,m).*B')*B)',2));
                for i=1:length(ind)
                    dsqrtW = diag(sqrtW(ind{i}));
                   p_eff = p_eff + trace( dsqrtW*(LLahat{i}\(LLahat{i}'\(dsqrtW*La2{i}))));
                   p_eff = p_eff + trace( dsqrtW*(LLahat{i}\(LLahat{i}'\(dsqrtW*B(:,ind{i})'*B(:,ind{i})))));
                   p_eff = p_eff - trace(dsqrtW*L2(ind{i},:)*L2(ind{i},:)'*dsqrtW*La2{i});
                end

% $$$                 K = B'*B;
% $$$                 C = -L2*L2';
% $$$                 for i=1:length(ind)
% $$$                    K(ind{i},ind{i}) =  K(ind{i},ind{i}) + La2{i};
% $$$                    C(ind{i},ind{i}) =  C(ind{i},ind{i}) + inv(Lahat{i});
% $$$                 end
% $$$
% $$$                 p_eff = trace (diag(sqrtW)*C*diag(sqrtW)*K) ;

            end

          case 'CS+FIC'
            u = gp.X_u;
            [n,nin]=size(x);
            % Indexes to all non-compact support and compact support covariances.
            cf1 = [];
            cf2 = [];

            ncf = length(gp.cf);
            % Loop through all covariance functions
            for i = 1:ncf
                % Non-CS covariances
                if ~isfield(gp.cf{i},'cs')
                    cf1 = [cf1 i];
                    % CS-covariances
                else
                    cf2 = [cf2 i];
                end
            end

            K_fu = gp_cov(gp,x,u,cf1);         % f x u
            K_uu = gp_trcov(gp,u,cf1);    % u x u, noiseles covariance K_uu
            K_uu = (K_uu+K_uu')./2;     % ensure the symmetry of K_uu

            Kcs = gp_trcov(gp, x, cf2);
            Luu = chol(K_uu)';
            B=Luu\(K_fu');

            switch gp.latent_method
              case 'EP'

                %[e, edata, eprior, tautilde, nutilde, L, La, b] = gpep_e(gp_pak(gp), gp, x, y, 'z', z);
                [e, edata, eprior, p] = gpep_e(gp_pak(gp), gp, x, y, 'z', z);
                [L, La] = deal(p.L, p.La2);

                k = gp_trvar(gp,x,cf1);
                Lav = k - sum(B.^2)';
                La1 = Kcs + sparse(1:n, 1:n, Lav, n, n);

                issparse(La)
                VD = ldlchol(La);
                siLa = spinv(VD,1);
                p_eff = sum(sum(ldlsolve(VD,B').*B')) + sum(sum(siLa.*La1)) - sum(sum(L.*((L'*B')*B)',2));
                p_eff = p_eff - sum(sum(L.*(L'*La1)',2));

% $$$                 C = inv(La) - L*L';
% $$$                 K = B'*B + diag(k - sum(B.^2)') + Kcs;
% $$$
% $$$                 p_eff = trace(C*K);

              case 'Laplace'
                %[e, edata, eprior, f, L, a, La2] = gpla_e(gp_pak(gp), gp, x, y, 'z', z);
                [e, edata, eprior, p] = gpla_e(gp_pak(gp), gp, x, y, 'z', z);
                [f, La2] = deal(p.f, p.La2);

                W = -gp.lik.fh.llg2(gp.lik, y, f, 'latent', z);
                sqrtW = sparse(1:tn,1:tn,sqrt(W),tn,tn);
                Lahat = sparse(1:tn,1:tn,1,tn,tn) + sqrtW*La2*sqrtW;
                B = sqrtW*K_fu;

                % Components for (I + W^(1/2)*(Qff + La2)*W^(1/2))^(-1) = Lahat^(-1) - L2*L2'
                B2 = Lahat\B;
                A2 = K_uu + B'*B2; A2=(A2+A2)/2;
                L2 = B2/chol(A2);

                BB = Luu\(K_fu');
                BB2 = B/Luu';

                VD = ldlchol(Lahat);
                spiLahat = spinv(VD,1);

                p_eff = sum(sum(sqrtW*spiLahat*sqrtW.*La2,2)) + sum(sqrtW * (sum(ldlsolve(VD, BB2).*BB',2) - sum(L2.*(L2'*sqrtW*La2)',2) - sum(L2.*(L2'*BB2*BB)',2)) );

            end

        end
    end

end


classdef RWMOP50 < PROBLEM
% <multi> <real> <constrained>
% Power distribution system planning

%------------------------------- Reference --------------------------------
% A. Kumar, G. Wu, M. Ali, Q. Luo, R. Mallipeddi, P. Suganthan, and S. Das,
% A benchmark-suite of real-world constrained multi-objective optimization
% problems and some baseline results, Swarm and Evolutionary Computation,
% 2021, 67: 100961.
%------------------------------- Copyright --------------------------------
% Copyright (c) 2023 BIMK Group. You are free to use the PlatEMO for
% research purposes. All publications which use this platform or any code
% in the platform should acknowledge the use of "PlatEMO" and reference "Ye
% Tian, Ran Cheng, Xingyi Zhang, and Yaochu Jin, PlatEMO: A MATLAB platform
% for evolutionary multi-objective optimization [educational forum], IEEE
% Computational Intelligence Magazine, 2017, 12(4): 73-87".
%--------------------------------------------------------------------------

    methods
        %% Initialization
        function Setting(obj)
            obj.M        = 2;
            obj.D        = 6;
            obj.lower    = [10 10 35 35 125 130];
            obj.upper    = [125 150 210 225 315 325];
            obj.encoding = ones(1,obj.D);
        end
        %% Evaluate multiple solutions
        function Population = Evaluation(obj,varargin)
            x = varargin{1};
            PD = 1200;
            B = [140 17 15 19 26 22;
                  17 60 13 16 15 20;
                  15 13 65 17 24 19;
                  19 16 17 71 30 25;
                  26 15 24 30 69 32;
                  22 20 19 25 32 85] * 10^-6;
            a     = [756.7988 451.3251 1243.5311 1049.9977 1356.6592 1658.5696];
            b     = [38.5390  46.1591 38.3055  40.3965 38.2704 36.3278];
            c     = [0.15247  0.10587  0.03546  0.02803  0.01799 0.02111];
            alpha = [13.8593 13.8593  40.2669  40.2669 42.8955 42.8955];
            beta  = [0.32767  0.32767 -0.54551 -0.54551 -0.51116 -0.51116];
            gamma = [0.00419 0.00419  0.00683  0.00683 0.00461 0.00461];
            PL = zeros(size(x,1),1);
            for i = 1 : size(x,2)
                for j = 1 : size(x,2)
                    PL = PL + x(:,i) .* B(i,j) .* x(j);
                end
            end
            % Objectives
            f = zeros(size(x,1),2);
            for i = 1 : size(x,2)
                f(:,1) = f(:,1) + a(i) + b(i) .* x(:,i) + c(i) .* x(:,i).^2 ;
            end
            for i = 1 : size(x,2)
                f(:,2) = f(:,2) + alpha(i) + beta(i) .* x(:,i) + gamma(i) .* x(:,i).^2;
            end
            % Constraints
            h = sum(x,2) - PD - PL;
            Population = SOLUTION(varargin{1},f,abs(h)-1e-4,varargin{2:end});
            obj.FE     = obj.FE + length(Population);
        end
        %% Generate a point for hypervolume calculation
        function R = GetOptimum(obj,~)
            R = [6.4895619e+04   1.2538936e+03];
        end
    end
end


function [fA1Value, fA2Value] = calc_MaxLikelihoodA1A2(mCatalog, mControl, fBValue, bReBin)
% function [fAValue] = calc_MaxLikelihoodA(mCatalog, mControl, fBValue, bReBin)
% -----------------------------------------------------------------------------
% Computes the maximum likelihood a-values for a set of given catalogs
%   and a fixed b-value
%
% Input parameters:
%   mCatalog        Catalog containing different periods with varying
%                   magnitude of completeness
%   mControl        Controlmatrix containing informations about the single catalogs
%                   mControl(n,:) contains information about caCatalogs{n}
%                   Column 1: Starting time of catalog
%                   Column 2: Magnitude of completeness
%                   Column 3: Starting magnitude bin
%                   Column 4: Magnitude bin stepsize (must be 0.1)
%   fBValue         Fixed b-value
%   bReBin          Rebin the catalogs to the given binning in mControl
%                   (must be 0)
%
% Output parameters:
%   fAValue         Maximum likelihooda-value
%
% Danijel Schorlemmer
% July 5, 2002

% Get the number of different periods in the catalog
[nRow_, nColumn_] = size(mControl);
% Init vector with ending times for each period
vMaxTime_ = zeros(nRow_, 1);
% Loop over the control matrix
for nCnt_ = 1:nRow_
  % Determine starting time of period
  fMinTime_ = mControl(nCnt_, 1);
  % Determine ending time of period
  if nCnt_ < nRow_
    vMaxTime_(nCnt_) = mControl(nCnt_+1, 1);
  else
    vMaxTime_(nCnt_) = max(mCatalog(:,3));
  end
  % Create subcatalog for period
  vSel_ = (mCatalog(:,3) >= fMinTime_) & (mCatalog(:,3) < vMaxTime_(nCnt_));
  mTmpCatalog_ = mCatalog(vSel_,:);
  % Rebin the catalog (should not be used)
  if bReBin
    fMaxMag_ = max(mTmpCatalog_(:,6));
    for nBin_ = mControl(nCnt_,3):mControl(nCnt_,4):(fMaxMag_ + mControl(nCnt_,4))
      vSel_ = (mTmpCatalog_(:,6) >= (nBin_ - (mControl(nCnt_,4)/2))) & (mTmpCatalog_(:,6) < (nBin_ + (mControl(nCnt_,4)/2)));
      mTmpCatalog_(vSel_,6) = nBin_;
    end
  end
  % Cut the subcatalog at magnitude of completeness
  vSel_ = mTmpCatalog_(:,6) >= mControl(nCnt_,2);
  mTmpCatalog_ = mTmpCatalog_(vSel_,:);
  % Store the subcatalog
  caCatalogs_{nCnt_} = mTmpCatalog_;
end
% Add the ending times to the control matrix
mControl = [mControl vMaxTime_];
% Set the callback starting values
vStartValue = [1; 1];
% Find the maximum likelihood solution
[fAValues, vDummy, bExitFlag_] = fminsearch('callback_LogLikelihoodA1A2Value', vStartValue, [], caCatalogs_, mControl, fBValue);
fA1Value = fAValues(1);
fA2Value = fAValues(2);


function linplus_test571 ( )

%*****************************************************************************80
%
%% TEST571 tests R8SP_INDICATOR.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    14 March 2009
%
%  Author:
%
%    John Burkardt
%
  m = 7;
  n = 5;
  nz_num = 10;
  col = [ 2, 5, 1, 5, 1, 2, 3, 4, 4, 1 ];
  row = [ 1, 1, 2, 2, 4, 4, 4, 5, 6, 7 ];

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST571\n' );
  fprintf ( 1, '  R8SP_INDICATOR sets up a R8SP indicator matrix;\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Matrix rows M =    %d\n', m );
  fprintf ( 1, '  Matrix columns N = %d\n', n );
  fprintf ( 1, '  Matrix nonzeros =  %d\n', nz_num );

  a = r8sp_indicator ( m, n, nz_num, row, col );

  r8sp_print ( m, n, nz_num, row, col, a, '  The R8SP indicator matrix:' );

  return
end


% zica() - Z-transform of ICA activations; useful for studying component SNR
%
% Usage: >> [zact,basesd,maz,mazc,mazf] = zica(activations,frames,baseframes)
%
% Inputs:
%   activations - activations matrix produced by runica()
%   frames      - frames per epoch {0|default ->  length(activations)}
%   baseframes  - vector of frames in z-defining baseline period {default frames}
%
% Outputs:
%   zact        - activations z-scaled and reorded in reverse order of max abs
%   basesd      - standard deviations in each activation row (reverse ordered)
%   maz         - maximum absolute z-value for each activation row (rev ordered)
%   mazc        - component indices of the reverse-sorted max abs z-values
%                 (this is the act -> zact reordering)
%   mazf        - frame indices of the max abs z-values (reverse ordered)
%
% Author: Scott Makeig, SCCN/INC/UCSD, La Jolla, 2-25-98
%
% See also: runica()

function [zact,basesd,maxabsz,maxc,maxabszf] = zica(activations,frames,baseframes)

% Copyright (C) 2-25-98 Scott Makeig, SCCN/INC/UCSD, scott@sccn.ucsd.edu
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

% 3-2-98 added frames variable -sm
% 1-25-01 put revsort subfunction in the core of the zica program -ad
% 01-25-02 reformated help & license, added link -ad

if nargin<1
   help zica
   return
end

[chans,framestot] = size(activations);

if nargin < 3
   baseframes = 0;
end
if nargin < 2
   frames = 0;
end
if frames == 0
   frames = framestot;
end
if baseframes == 0
   baseframes = 1:frames
end
epochs = floor(framestot/frames);
if frames*epochs ~= framestot
   fprintf('zica(): indicated frames does not divide data length.\n');
   return
end

if length(baseframes) < 3
  fprintf('\n  zica() - too few baseframes (%d).\n',length(baseframes));
  help zica
  return
end

if min(baseframes) < 1 | max(baseframes) > frames
  fprintf('\n  zica() - baseframes out of range.\n');
  help zica
  return
end

baselength = length(baseframes);
baseact = zeros(epochs*baselength,chans);
for e=1:epochs
 baseact((e-1)*baselength+1:e*baselength,:) = ...
                        matsel(activations,frames,baseframes,0,e)';
end
basesd = sqrt(covary(baseact));
zact = activations./(basesd'*ones(1,framestot));
[maxabsz,maxabszf] = sort(abs(zact'));
maxabsz  = maxabsz(frames,:);
maxabszf = maxabszf(frames,:);

%
% reorder outputs in reverse order of max abs z
[maxabsz,maxc] = revsort(maxabsz);
zact = zact(maxc,:);
basesd = basesd(maxc);
maxabszf = maxabszf(maxc);

% revsort  - reverse sort columns (biggest 1st, ...)
function [out,i] = revsort(in)

if size(in,1) == 1
   in = in'; % make column vector
end

[out,i] = sort(in);
out = out(size(in,1):-1:1,:);
  i = i(size(in,1):-1:1,:);
return;


function h = polarScatter(az, decl, point_size, color, flag, plot_bg)
% Equivalent of polar scatter but with support for older Matlab versions
% SYNTAX:
%    polarScatter(az, decl, point_size, color, <flag>)
%
% INPUT
%   az      azimuth      [rad]
%   decl    declination  [rad]
%   size    size of the scattered point
%   color   data field for scatter
%   flag    at the moment can only be 'filled'
%
% OUTPUT
%   h       handle to the scattered points

%--------------------------------------------------------------------------
%               ___ ___ ___
%     __ _ ___ / __| _ | __|
%    / _` / _ \ (_ |  _|__ \
%    \__, \___/\___|_| |___/
%    |___/                    v 1.0RC1
%
%--------------------------------------------------------------------------
%  Copyright (C) 2021 Geomatics Research & Development srl (GReD)
%  Written by:       Mirko Reguzzoni
%  Contributors:     Giulio Tagliaferro, Andrea Gatti ...
%  A list of all the historical goGPS contributors is in CREDITS.nfo
%--------------------------------------------------------------------------
%
%    This program is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with this program.  If not, see <http://www.gnu.org/licenses/>.
%----------------------------------------------------------------------------------------------

%%% INTERNAL PARAMETER
    scale = 1;
    %%%

    decl_n = decl/(pi/2)*scale;
    x = sin(az) .* decl_n;
    y = cos(az) .* decl_n;
    if nargin > 4 && strcmp(flag,'filled')
        h = scatter(x,y,point_size,color,'filled');
    else
        h = scatter(x,y,point_size,color);
    end
    is_hold = ishold();
    if nargin < 6
        plot_bg = ~is_hold;
    end
    if plot_bg
        hold on
        %plot parallel
        az_l = [0:pi/200:2*pi];
        d_step = 15/180*pi;
        decl_s = ([0:d_step:pi/2]/(pi/2))*scale;
        for d = decl_s
            x = cos(az_l).*d;
            y = sin(az_l).*d;
            plot(x,y,'color',[0.6 0.6 0.6]);
            text(cos(80/180*pi)*d,sin(80/180*pi)*d,sprintf('%d',round(d*90)),'HorizontalAlignment','center', 'FontWeight', 'bold', 'FontSize', 13);
        end
        %plot meridian
        az_step = 30/180 *pi;
        az_s = [0:az_step:2*pi];
        decl_l = ([0 1])*scale;
        for a = az_s
            x = cos(a).*decl_l;
            y = sin(a).*decl_l;
            plot(x,y,'color',[0.6 0.6 0.6]);
            if abs(a-2*pi) > 0.0001
                text(cos(a)*1.1,sin(a)*1.1,sprintf('%d', mod(round((2*pi - a + pi/2) / pi * 180), 360)), 'HorizontalAlignment','center', 'FontWeight', 'bold', 'FontSize', 13);
            end
        end
        axis equal
        % xlim([-2 2])
        % ylim([-2 2])
        axis off
        set(gcf,'color','w');
        if ~is_hold
            hold off
        end
        xlim([-1.15 1.15]); ylim([-1.15 1.15]);
    end
end


function classifier = lapsvmp(options,data)
% {lapsvmp} trains a Laplacian SVM classifier in the primal.
%
%      classifier = lapsvmp(options,data)
%
%      options: a structure with the following fields
%               options.gamma_A: regularization parameter (ambient norm)
%               options.gamma_I: regularization parameter (intrinsic norm)
%
%               [optional fields]
%               options.Cg: {0,1} i.e. train with Newton's method or PCG
%                           (default=0)
%               options.MaxIter: maximum number of iterations (default=200)
%               options.Hinge: {0,1} i.e. train a LapSVM (1) or LapRLSC (0)
%                              (default=1)
%               options.UseBias: {0,1} i.e. use or not a bias (default=0)
%               options.InitAlpha: if it's 0, the initial weights are null;
%                                  if it's <0, they are randomly taken;
%                                  otherwise it is the initial vector
%                                  (default=0)
%               options.InitBias: the initial bias (default=0)
%               options.NewtonLineSearch: {0,1} i.e. use or not exact line
%                                         search with Newton's method
%                                         (default=0).
%               options.NewtonCholesky: {0,1} i.e. use or not Cholesky
%                                       factorization and rank 1 updates of
%                                       the Heassian (default=0 for LapSVM.
%                                       If set to 1, the  Hessian must be
%                                       positive definite).
%               options.CgStopType: {0,1,2,3,4,5,6,7,-1}
%                                   the data-based stopping criterion of cg
%                                   only (default=0), where:
%                                   0: do not stop
%                                   1: stability stop
%                                   2: validation stop
%                                   3: stability & validation stop
%                                   4: gradient norm (normalized)
%                                   5: prec. gradient norm (normalized)
%                                   6: mixed gradient norm (normalized)
%                                   7: relative objective function change
%                                  -1: debug (it saves many stats)
%               options.CgStopIter: number of cg iters after which check
%                                   the stopping condition.
%               options.CgStopParam: the parameter for the selected
%                                    CgStopType. If CgStopType is:
%                                    0: ignored
%                                    1: percentage [0,1] of tolerated
%                                       different decisions between two
%                                       consecutive checks
%                                    2: percentage [0,1] of error rate
%                                       decrement required  between two
%                                       consecutive checks
%                                    3: the two params above (i.e. it is an
%                                       array of two elements)
%                                    4: the minimum gradient norm
%                                    5: the minimum prec. gradient norm
%                                    6: the minimum mixed gradient norm
%                                    7: relative objective function change
%                                       between two consecutive checks
%                                    -1: ignored
%                                    see the code for default values.
%               options.Verbose: {0,1} (default=1)
%
%      data: a structure with the following fields
%            data.X: a N-by-D matrix of N D-dimensional training examples
%            data.K: a N-by-N kernel Gram matrix of N training examples
%            data.Y: a N-by-1 label vector in {-1,0,+1}, where 0=unlabeled
%                    (it is in {0,+1} in the case of One-Class SVM/LapSVM.
%            data.L: a N-by-N matrix of the Laplacian
%
%            [other fields]
%            data.Kv: a V-by-N kernel Gram matrix of V validation examples,
%                     required if CgStopType is 2 or 3 (validation check)
%            data.Yv: a V-by-1 vector of {-1,+1} labels for the validation
%                     examples, required if CgStopType is 2 or 3
%                     (validation check)
%
%      classifier: structure of the trained classifier (see the
%                  'saveclassfier' function).
%
% Author: Stefano Melacci (2012)
%         mela@dii.unisi.it
%         * the One-Class extension is joint work with Salvatore Frandina,
%           salvatore.frandina@gmail.com
%         * the original code structure was based on the primal SVM code of
%           Olivier Chapelle, olivier.chapelle@tuebingen.mpg.de

n=length(data.Y);
nn=nnz(data.Y==-1);

% initializing option structure with default values
if ~isfield(options,'Verbose'),           options.Verbose=1; end
if ~isfield(options,'Hinge'),             options.Hinge=1; end
if ~isfield(options,'Cg'),                options.Cg=0; end
if ~isfield(options,'MaxIter'),           options.MaxIter=200; end
if ~isfield(options,'UseBias'),           options.UseBias=0; end
if ~isfield(options,'InitAlpha'),         options.InitAlpha=false; end
if ~isfield(options,'InitBias'),          options.InitBias=0; end
if ~isfield(options,'NewtonLineSearch'),  options.NewtonLineSearch=0; end
if ~isfield(options,'NewtonCholesky'),    options.NewtonCholesky=0; end
if ~isfield(options,'CgStopType'),        options.CgStopType=0; end
switch options.CgStopType
    case 0 % none
        if ~isfield(options,'CgStopParam') || ...
            isempty(options.CgStopParam), options.CgStopParam=0; end
        if ~isfield(options,'CgStopIter'),
            options.CgStopIter=options.MaxIter+1; end
    case 1 % stability
        if ~isfield(options,'CgStopParam') || ...
            isempty(options.CgStopParam), options.CgStopParam=0.015; end
        if ~isfield(options,'CgStopIter') || isempty(options.CgStopIter),
            options.CgStopIter=round(sqrt(n)/2); end
    case 2 % validation
        v=length(data.Yv);
        if ~isfield(options,'CgStopParam') || ...
            isempty(options.CgStopParam), options.CgStopParam=1/v; end
        if ~isfield(options,'CgStopIter') || isempty(options.CgStopIter),
            options.CgStopIter=round(sqrt(n)/2); end
    case 3 % stability & validation
        v=length(data.Yv);
        if ~isfield(options,'CgStopParam') || ...
            isempty(options.CgStopParam), options.CgStopParam=...
                                                           [1/v,0.015];
        end
        if ~isfield(options,'CgStopIter'),
            options.CgStopIter=round(sqrt(n)/2); end
    case 4 % gradient norm
        if ~isfield(options,'CgStopParam') || ...
            isempty(options.CgStopParam), options.CgStopParam=1e-8; end
        if ~isfield(options,'CgStopIter') || isempty(options.CgStopIter),
            options.CgStopIter=1; end
    case 5 % preconditioned gradient norm
        if ~isfield(options,'CgStopParam') || ...
            isempty(options.CgStopParam), options.CgStopParam=1e-8; end
        if ~isfield(options,'CgStopIter') || isempty(options.CgStopIter),
            options.CgStopIter=1; end
    case 6 % mixed gradient norm
        if ~isfield(options,'CgStopParam') || ...
            isempty(options.CgStopParam), options.CgStopParam=1e-8; end
        if ~isfield(options,'CgStopIter') || isempty(options.CgStopIter),
            options.CgStopIter=1; end
    case 7 % relative objective function decrease
        if ~isfield(options,'CgStopParam') || ...
            isempty(options.CgStopParam), options.CgStopParam=1e-6; end
        if ~isfield(options,'CgStopIter') || isempty(options.CgStopIter),
            options.CgStopIter=1; end
    case -1 % debug
        if ~isfield(options,'CgStopParam') || ...
            isempty(options.CgStopParam), options.CgStopParam=0; end
        if ~isfield(options,'CgStopIter') || isempty(options.CgStopIter),
            options.CgStopIter=options.MaxIter+1; end
    otherwise
        error('Invalid CgStopType.');
end
if nn==0, oc=1; else oc=0; end

% initial alpha vector
if length(options.InitAlpha)>1
    alpha=options.InitAlpha;
else
    if options.InitAlpha==0
        alpha=[];
    else
        alpha=randn(n,1);
        alpha=alpha./norm(alpha);
    end
end

% checking common error conditions
if oc==1 && ~options.UseBias
    error('One-Class SVM requires the UseBias option to be turned on.');
end
if options.UseBias && options.LaplacianNormalize
    error(['The current implementation does not support a normalized ' ...
           'Laplacian when the UseBias options is turned on.']);
end

% initial bias
b=options.InitBias;

switch options.Cg
    case 0
        % newton
        [alpha,b,t,sec,lsiters]=newton(options,data,alpha,b,oc);
        stats=[];
    case 1
        % pcg
        [alpha,b,t,sec,lsiters,stats]=pcg(options,data,alpha,b,oc);
    otherwise
        error('Invalid solver specified in the field .Cg');
end

svs=find(alpha~=0);
classifier=saveclassifier('lapsvmp',svs,alpha(svs), ...
                          data.X(svs,:),b,options,sec,t,lsiters,stats);

function [alpha,b,t,sec,lsiters] = newton(options,data,alpha,b,oc)
% {newton} trains the classifier using the Newton's method.

tic
n=length(data.Y);
labeled=data.Y~=0;
l=nnz(labeled);
gamma_A=options.gamma_A;
gamma_I=options.gamma_I;

% initial seeding
if isempty(alpha)
    alpha=zeros(n,1);
    Kalpha=zeros(n,1);
else
    Kalpha=data.K*alpha;
end

t=0;
lr=0;
sv=false(n,1);
if nargout>4, lsiters=zeros(options.MaxIter,1); end

if gamma_I~=0, LK=data.L*data.K; end

while 1

    if options.Hinge
        sv_prev=sv;
        hloss=sparse([],[],[],n,1,l);
        hloss(labeled)=1-data.Y(labeled).*(Kalpha(labeled,:)+b);
        sv=hloss>0;
        nsv=nnz(sv);
    else
        sv_prev=sv;
        sv=labeled;
        nsv=l;
    end

    if options.Verbose
        if ~options.Hinge
            hloss=sparse([],[],[],n,1,l);
            hloss(labeled)=1-data.Y(labeled).*(Kalpha(labeled,:)+b);
        end
        if gamma_I~=0
            obj=(gamma_A*alpha'*Kalpha+sum(hloss(sv).^2)+...
                gamma_I*Kalpha'*data.L*Kalpha+oc*b)/2;
        else
            obj=(gamma_A*alpha'*Kalpha+sum(hloss(sv).^2)+oc*b)/2;
        end

        fprintf('[t=%d] obj=%f nev=%d lr=%.4f\n', [t full(obj) nsv lr]);
    end

    % goal conditions
    if t>=options.MaxIter, break, end
    if isequal(sv_prev,sv), break, end

    t=t+1;

    IsvK=sparse([],[],[],n,n,nsv*n);
    IsvK(sv,:)=data.K(sv,:);

    % computing new alphas
    onev=ones(1,n);

    if gamma_I==0 % SVM (sparse solution)
        if options.UseBias
            alpha_new=zeros(n,1);
            alpha_b_new=[0,onev(1:nsv);onev(1:nsv)',...
                         gamma_A*speye(nsv)+IsvK(sv,sv)]\ ...
                         [oc/(2*gamma_A);data.Y(sv)];
            alpha_new(sv)=alpha_b_new(2:end);
            b_new=alpha_b_new(1);
        else
            alpha_new=zeros(n,1);
            alpha_new(sv)=(gamma_A*speye(nsv)+IsvK(sv,sv))\data.Y(sv);
            b_new=0;
        end

    else % LapSVM

        % inversion by factorization
        if options.NewtonCholesky
            if t==1
                % compute the Cholesky factorization of the Hessian
                if options.UseBias
                    sumKsv=sum(data.K(sv,:));
                    hess=chol([nsv,sumKsv;sumKsv',...
                               data.K*(gamma_A*speye(n)+IsvK+gamma_I*LK)]);
                    alpha_b_new=hess\(hess'\([sum(data.Y(sv))-oc/2; ...
                                              data.K(:,sv)*data.Y(sv)]));
                    alpha_new=alpha_b_new(2:end);
                    b_new=alpha_b_new(1);
                else
                    hess=chol(data.K*(gamma_A*speye(n)+IsvK+gamma_I*LK));
                    alpha_new=hess\(hess'\(data.K(:,sv)*data.Y(sv)));
                    b_new=0;
                end
                LK=[];
            else
                % update the Cholesky factorization of the Hessian
                sv_diff=~(sv&sv_prev);
                sv_add=find(sv&sv_diff)';
                sv_rem=find(sv_prev&sv_diff)';
                if options.UseBias
                    if ~isempty(sv_add)
                        for i=sv_add
                            hess=cholupdate(hess,[1;data.K(:,i)],'+');
                        end
                    end
                    if ~isempty(sv_rem)
                        for i=sv_rem
                            hess=cholupdate(hess,[1;data.K(:,i)],'-');
                        end
                    end
                    alpha_b_new=hess\(hess'\([sum(data.Y(sv))-oc/2; ...
                                              data.K(:,sv)*data.Y(sv)]));
                    alpha_new=alpha_b_new(2:end);
                    b_new=alpha_b_new(1);
                else
                    if ~isempty(sv_add)
                        for i=sv_add
                            hess=cholupdate(hess,data.K(:,i),'+');
                        end
                    end
                    if ~isempty(sv_rem)
                        for i=sv_rem
                            hess=cholupdate(hess,data.K(:,i),'-');
                        end
                    end
                    alpha_new=hess\(hess'\(data.K(:,sv)*data.Y(sv)));
                    b_new=0;
                end
            end
        else
            % inversion without factorization
            IsvY=sparse([],[],[],n,1,nsv);
            IsvY(sv)=data.Y(sv);
            if options.UseBias
                alpha_b_new=([0,onev;sv,...
                             gamma_A*speye(n)+IsvK+gamma_I*LK])\ ...
                             [oc/(2*gamma_A);IsvY];
                alpha_new=alpha_b_new(2:end);
                b_new=alpha_b_new(1);
            else
                alpha_new=(gamma_A*speye(n)+IsvK+gamma_I*LK)\IsvY;
                b_new=0;
            end

        end
    end

    % step
    if options.NewtonLineSearch && (options.Hinge || nnz(alpha)>0)
        step=alpha_new-alpha;
        step_b=b_new-b;

        [lr,Kalpha,lsi]=linesearch(data,labeled,step,step_b,Kalpha,b,...
                                   gamma_A,gamma_I,[],[],options.Hinge,...
                                   oc);
        alpha=alpha+lr*step;
        b=b+lr*step_b;
        lsiters(t)=lsi;
    else
        alpha=alpha_new;
        b=b_new;
        lr=1;
        lsiters(t)=0;
        Kalpha=data.K*alpha;
    end
end

if nargout>4, lsiters=lsiters(1:t); end
sec=toc;

function [alpha,b,t,sec,lsiters,stats] = pcg(options,data,alpha,b,oc)
% {pcg} trains the classifier using preconditioned conjugate gradient.

tic
n=length(data.Y);
labeled=data.Y~=0;
unlabeled=data.Y==0;
l=nnz(labeled);
u=n-l;

if isfield(data,'Yv')
    v=length(data.Yv);
end
gamma_A=options.gamma_A;
gamma_I=options.gamma_I;

if isempty(alpha)
    alpha=zeros(n,1);
    Kalpha=zeros(n,1);
    if gamma_I~=0, LKalpha=zeros(n,1); else LKalpha=[]; end
    go=data.Y-b*labeled;
    obj0=(sum((1-data.Y(labeled)*b*options.UseBias).^2)+oc*b)/2;
    if options.UseBias, go_b=sum(data.Y(labeled)-b)-oc/2;
    else go_b=0; b=0; end
else
    Kalpha=data.K*alpha;
    if gamma_I~=0, LKalpha=data.L*Kalpha; else LKalpha=[]; end
    out=sparse([],[],[],n,1,l);
    out(labeled)=Kalpha(labeled)+b;
    if options.Hinge
        sv=false(n,1);
        sv(labeled)=(data.Y(labeled).*out(labeled)<1);
    else
        sv=labeled;
    end
    go=-gamma_A*alpha;
    if gamma_I~=0, go=go-gamma_I*LKalpha; end
    obj0=(Kalpha'*(-g0)+sum((out(sv)-data.Y(sv)).^2)+oc*b)/2;
    go(sv)=go(sv)-(out(sv)-data.Y(sv));

    if options.UseBias, go_b=sum(data.Y(sv)-Kalpha(sv)-b)-oc/2;
    else go_b=0; b=0; end

end
d=go; % initial search direction
d_b=go_b;
Kgo=data.K*go;
Kstep=Kgo;

t=0;
stats=[];

switch options.CgStopType
    case 4, ng0=sqrt(sum(Kgo.^2)+go_b^2);
    case 5, ngp0=sqrt(sum(go.^2)+go_b^2);
    case 6, ngm0=sqrt(sum(Kgo'*go)+go_b^2);
    case -1
        stats=zeros(options.MaxIter+1,5+n+1);
        ng0=sqrt(sum(Kgo.^2)+go_b^2);
        ngp0=sqrt(sum(go.^2)+go_b^2);
        ngm0=sqrt(sum(Kgo'*go)+go_b^2);
        stats(t+1,:)=[t,obj0/obj0,ng0/ng0,ngp0/ngp0,ngm0/ngm0,alpha',b];
end

if nargout>3, lsiters=zeros(options.MaxIter,1); end
valerr_prev=1;
obj_prev=obj0;
yfx_unlabeled_prev=false(u,1);

while 1
    t=t+1;

    % goal condition: maximum number of iterations
    if t>options.MaxIter, t=t-1; break, end

    % do an exact line search
    [lr,Kalpha,lsi,LKalpha]=linesearch(data,labeled,d,d_b,Kalpha,b,...
                                       gamma_A,gamma_I,Kstep,LKalpha,...
                                       options.Hinge,oc);

    % goal condition: converged to optimal solution
    if lr==0, t=t-1; break, end

    alpha=alpha+lr*d;
    b=b+lr*d_b;
    lsiters(t)=lsi;

    % compute new precgradient and objective
    out=sparse([],[],[],n,1,l);
    out(labeled)=Kalpha(labeled)+b;

    if options.Hinge
        sv=false(n,1);
        sv(labeled)=(data.Y(labeled).*out(labeled)<1);
    else
        sv=labeled;
    end

    g=gamma_A*alpha;
    if gamma_I~=0, g=g+gamma_I*LKalpha; end

    if options.Verbose || options.CgStopType==7 || options.CgStopType==-1
        obj=(Kalpha'*(g)+sum((out(sv)-data.Y(sv)).^2)+oc*b)/2;
        if options.Verbose
            fprintf('[t=%d] obj=%f nev=%d lr=%.4f\n', ...
                     [t full(obj) nnz(sv) lr]);
        end
    end

    g(sv)=g(sv)+(out(sv)-data.Y(sv));

    if options.UseBias, g_b=sum(out(sv)-data.Y(sv))+oc/2; else g_b=0; end

    gn=-g;
    gn_b=-g_b;

    % goal condition: data based
    if mod(t-1,options.CgStopIter)==(options.CgStopIter-1)
        switch options.CgStopType
            case 1 % stability
                yfx_unlabeled=sign(Kalpha(unlabeled)+b);
                diff_unlabeled=1-nnz(yfx_unlabeled==yfx_unlabeled_prev)/u;
                if diff_unlabeled<options.CgStopParam, break, end
                yfx_unlabeled_prev=yfx_unlabeled;
            case 2 % validation
                valerr=1-nnz(sign(data.Kv*alpha+b)==data.Yv)/v;
                if (valerr>(valerr_prev-options.CgStopParam)), break, end
                valerr_prev=valerr;
            case 3 % stability & validation
                valerr=1-nnz(sign(data.Kv*alpha+b)==data.Yv)/v;
                yfx_unlabeled=sign(Kalpha(unlabeled)+b);
                diff_unlabeled=1-nnz(yfx_unlabeled==yfx_unlabeled_prev)/u;
                if (valerr>(valerr_prev-options.CgStopParam(1))) && ...
                    diff_unlabeled<options.CgStopParam(2), break, end
                valerr_prev=valerr;
                yfx_unlabeled_prev=yfx_unlabeled;
            case 4 % gradient norm
                ng=sqrt(sum(gn.^2)+gn_b^2);
                if ng/ng0<options.CgStopParam, break, end
            case 5 % preconditioned gradient norm
                ngp=sqrt(sum(go.^2)+go_b^2);
                if ngp/ngp0<options.CgStopParam, break, end
            case 6 % mixed gradient norm
                ngm=sqrt(Kgo'*go+go_b^2);
                if ngm/ngm0<options.CgStopParam, break, end
            case 7 % relative objective function decrease
                if (obj_prev-obj)<options.CgStopParam, break, end
                obj_prev=obj;
        end
    end

    Kgn=data.K*gn; % multiply by the preconditioner

    % debug
    if options.CgStopType==-1
        ng=sqrt(sum(Kgn.^2)+gn_b^2);
        ngp=sqrt(sum(gn.^2)+gn_b^2);
        ngm=sqrt(Kgn'*gn+gn_b^2);
        stats(t+1,:)=[t,obj/obj0,ng/ng0,ngp/ngp0,ngm/ngm0,alpha',b];
    end

    % Polack-Ribiere update with automatic restart
    be=max(0,(Kgn'*(gn-go)+gn_b*(gn_b-go_b))/(Kgo'*go+go_b^2));

    d=be*d+gn;
    d_b=be*d_b+gn_b;

    Kstep=be*Kstep+Kgn;

    go=gn;
    go_b=gn_b;
    Kgo=Kgn;
end
sec=toc;
if nargout>4, lsiters=lsiters(1:t);
    if nargout>5 && ~isempty(stats), stats=stats(1:t,:); end
end

function [lr,Kalpha,lsi,LKalpha] = linesearch(data,labeled,step,step_b, ...
                                              Kalpha,b,gamma_A,gamma_I, ...
                                              Kstep,LKalpha,hinge,oc)
% {linesearch} does a line search in direction step.

act=step~=0; % the set of points for which alpha change (active)
if isempty(Kstep)
    Kstep=data.K(:,act)*step(act);
end

% precomputations
stepKstep=step(act)'*Kstep(act);
stepKalpha=step(act)'*Kalpha(act);
if gamma_I~=0
    KstepL=Kstep'*data.L;
    KstepLKalpha=KstepL*Kalpha;
    KstepLKstep=KstepL*Kstep;
end

out=Kalpha(labeled)+b;
outstep=Kstep(labeled)+step_b;
out_minus_Y=out-data.Y(labeled);

% breakpoints
if hinge
    sv=(1-out.*data.Y(labeled))>0;
    deltas=-out_minus_Y./outstep;
    deltas(deltas<0)=0;
    [deltas,deltas_map]=sort(deltas);
    lab=length(deltas);
    i=find(deltas>0,1);
    lsi=1;
else
    sv=true(length(out),1);
    lsi=1;
end

% intercepts
if gamma_I~=0
    left=outstep(sv)'*out_minus_Y(sv)+gamma_A*stepKalpha+...
         gamma_I*KstepLKalpha + (oc/2)*step_b;
    right=outstep(sv)'*outstep(sv)+gamma_A*stepKstep+...
          gamma_I*KstepLKstep;
else
    left=outstep(sv)'*out_minus_Y(sv)+gamma_A*stepKalpha + (oc/2)*step_b;
    right=outstep(sv)'*outstep(sv)+gamma_A*stepKstep;
end

% first minimum
zcross=-left/right;
if right<=0, zcross=0; end

if hinge && ~isempty(i)
    if sv(deltas_map(i))==true
        if 0<=zcross && zcross<deltas(i), not_got_it=0;
        else not_got_it=1; end
    else
        if 0<=zcross && zcross<=deltas(i), not_got_it=0;
        else not_got_it=1; end
    end

    while not_got_it
        % updating support vectors
        j=-2*sv(deltas_map(i))+1;
        sv(deltas_map(i))=~sv(deltas_map(i));

        % updating intercepts
        left=left+j*outstep(deltas_map(i))*out_minus_Y(deltas_map(i));
        right=right+j*outstep(deltas_map(i))*outstep(deltas_map(i));

        % computing minimum
        zcross=-left/right;

        % goal conditions
        if i==lab, break, end
        if sv(deltas_map(i))==true
            if sv(deltas_map(i+1))==true
                if deltas(i)<zcross && zcross<deltas(i+1),
                    not_got_it=0; end
            else
                if deltas(i)<zcross && zcross<=deltas(i+1),
                    not_got_it=0; end
            end
        else
            if sv(deltas_map(i+1))==true
                if deltas(i)<=zcross && zcross<deltas(i+1),
                    not_got_it=0; end
            else
                if deltas(i)<=zcross && zcross<=deltas(i+1),
                    not_got_it=0; end
            end
        end

        i=i+1;
        lsi=lsi+1;
    end
end
lr=zcross;

if lr<0, lr=0; return, end % converged
Kalpha=Kalpha+lr*Kstep;
if nargout>3 && gamma_I~=0, LKalpha=LKalpha+lr*KstepL'; end


function H = calc_Hazard(H0,betaV,dkV)
%Hazard calculated using Cox model
%AI 04/20/22

H = H0*exp(sum(betaV.*dkV));

end


% predicting numerical values using CCA
% large values and small values are corrected

clear all;
format long
disp('===== Linear CCA for Regression ====');
disp('Reading featur vector');

output_mean_mse_array = zeros(7,9);
colinoutput = 0;
for helel_data = [100,200,400,800,1200,1500, 2000,5000,10000]
    colinoutput = colinoutput+1;
    Indices = crossvalind('Kfold',helel_data , 10);%crossvalind('Kfold', 26548, 10);
    figure;

    subplot(7,2,1);

    for feat = 1:7
        MSEarray =[];
        elapsedarray =[];
        for crossvalidateIter = 1:10
            (fprintf('%d, %.5f',crossvalidateIter, mean(MSEarray)));
            possiblefeaturizations =  {'tfidfbucket','all','logmultinomial', 'logmultinomial2', 'logmultinomial3','bernouli', 'tfidf','multinomial'};
            %featurization = 'bernouli'%'tfidf'%'tfidf'%'multinomial'%'tfidf' %'multinomial'; % 'bernouli', 'tfidf'
            featurization  = possiblefeaturizations{feat};
            featurs = csvread('data\forWeka_featuresonly.csv');
            featurs = featurs(:,2:size(featurs,2));

            num_data = helel_data%size(featurs,1); %5000;

            %disp(sprintf('Number of datapoints %d',num_data))
            if strcmp(featurization,'multinomial')
                %just pass
            elseif strcmp(featurization,'bernouli')
                featurs = bernoulli(featurs);
            elseif strcmp(featurization,'tfidf')
                featurs = tfidf(featurs);
            elseif strcmp(featurization,'logmultinomial')
                featurs = log(featurs+1)./log(2);
            elseif strcmp(featurization,'logmultinomial2')
                featurs = round(log(featurs+1)./log(2));
            elseif strcmp(featurization,'logmultinomial3')
                featurs = (log(featurs+1));
            elseif strcmp(featurization,'all')
                featurs = [bernoulli(featurs), tfidf(featurs), (log(featurs+1))] ;
            elseif strcmp(featurization,'tfidfbucket')
                featurs = [log(1000*tfidf(featurs)+1)] ;

            end

            size_training = floor(.9*num_data);

            trainingset = featurs(Indices~=crossvalidateIter,:);
            testset = featurs(Indices==crossvalidateIter,:);

            %disp('Splitting up data into training/test sets');
            [num,txt,raw] = xlsread('data\final104.xls');

            % reading the description of each shoe
            descriptions = raw(2:size(raw,1),2);
            style_ratings = num(1:size(num,1),1);
            comfort_ratings = num(1:size(num,1),4);
            overal_ratings = num(1:size(num,1),5);

            % only take m data points
            m=num_data;
            descriptions = descriptions(1:m);
            style_ratings = style_ratings(1:m);
            comfort_ratings = comfort_ratings(1:m);
            overal_ratings = overal_ratings(1:m);

            responsevals = [style_ratings, comfort_ratings, overal_ratings];

            responsevals_training = responsevals(Indices~=crossvalidateIter,:);
            responsevals_test = responsevals(Indices==crossvalidateIter,:);

            %disp('Adjusted Linear CCA ');
            % http://www.mathworks.com/help/toolbox/stats/classregtree.html

            tic;

            %predictions = [];
            %actual = [];

            [Wx, Wy, r, U, V]  = canoncorr(trainingset,responsevals_training);
            % in [A,B,r,U,V],  U and V are cannonical scores
            % U = (X-repmat(mean(X),N,1))*A
            % V = (Y-repmat(mean(Y),N,1))*B

            %recheck this part
            N =  size(testset,1);
            predictions = ((testset-repmat(mean(testset),N,1))*Wx*pinv(Wy))+repmat(mean(responsevals_test),N,1);
            actual = responsevals_test;
            predictions( predictions>5)=5;
            predictions( predictions<1)=1;
            MSE = mean(sum(((predictions-actual).^2)'));
            elapsed = toc;
            MSEarray = [MSEarray MSE];
            elapsedarray = [elapsedarray elapsed];

        end
        featurization  = possiblefeaturizations{feat}
        fprintf('avergae MSE for adjusted CCA is %0.10f\n', mean(MSEarray));
        subplot(7,2,feat*2-1)
        plot(MSEarray,'-o');
        title(strcat('MSE adjusted CCA (',featurization,') ', sprintf(' avergae MSE = %0.10f\n', mean(MSEarray))));
        xlabel('10 fold cross-validation (iteration no)')
        ylabel('MSE')
        subplot(7,2,feat*2)
        plot(elapsedarray,'-o');
        title(strcat('Elapsed time for adjusted CCA (',featurization, ') ' , sprintf(' avergae elapsed time = %0.10f\n', mean(elapsedarray))));
        xlabel('10 fold cross-validation (iteration no)')
        ylabel('Elapsed Time')
        mtit(sprintf('%d datapoints',helel_data))
        drawnow;
        output_mean_mse_array(feat,colinoutput) = mean(MSEarray);
    end

end


function epsilon = QPSK_Alamouti_eps(Mr, k, L, nc, fp, SNR_dB, spa)
% QPSK_ALAMOUTI_EPS
%
% Inputs
%   Mr:     no of rx antennas
%   k:      number of information bits
%   L:      no of coherence blocks
%   nc:     length of coherence block
%   fp:     fraction of block for training
%   SNR_dB: SNR in dB
%   spa:    flag for saddlepoint approximation
%

%% Parameters and initialisation

% Simulation parameters
RELSPREAD_MAX = 0.40;           % max relative spread of outcomes

% Parallelisation
ncores = feature('numcores');   % number of cores on current machine
nworkers = ncores;              % use all cores for the algorithm
poolobj = gcp('nocreate');      % if no pool do not create new one
if isempty(poolobj)
    parpool(nworkers); % initialise the pool
end

% System parameters
n = L * nc;             % blocklength
R = k/n;                % rate (bits)
np = floor(fp*nc);      % number of pilots

nd = nc - np;
if mod(nd,2) == 1
    np = np + 1;
end

%% Compute error probability

rho_dB = SNR_dB;
rho = 10^(rho_dB/10);

eps_trial = eps_RCUs_Alamouti_stable(R, Mr, L, np, nc, rho, spa, RELSPREAD_MAX);
epsilon = median( eps_trial );


%% Image Pyramids
%
% In this tutorial:
%
% * We will learn about Image Pyramids
% * We will use Image pyramids to create a new fruit, "Orapple"
% * We will see these functions: |cv.pyrUp|, |cv.pyrDown|, |cv.buildPyramid|
%
% Sources:
%
% * <https://docs.opencv.org/3.1.0/dc/dff/tutorial_py_pyramids.html>
%

%% Theory
% Normally, we used to work with an image of constant size. But on some
% occasions, we need to work with (the same) images in different resolutions.
% For example, while searching for something in an image, like face, we are
% not sure at what size the object will be present in said image. In that
% case, we will need to create a set of the same image with different
% resolutions and search for object in all of them. These set of images with
% different resolutions are called *Image Pyramids* (because when they are
% kept in a stack with the highest resolution image at the bottom and the
% lowest resolution image at top, it looks like a pyramid).
%
% There are two kinds of Image Pyramids:
%
% # Gaussian Pyramid
% # Laplacian Pyramids
%
% Higher level (Low resolution) in a Gaussian Pyramid is formed by removing
% consecutive rows and columns in Lower level (higher resolution) image.
% Then each pixel in higher level is formed by the contribution from 5 pixels
% in underlying level with gaussian weights. By doing so, a |MxN| image
% becomes |M/2xN/2| image. So area reduces to one-fourth of original area. It
% is called an Octave. The same pattern continues as we go upper in pyramid
% (ie, resolution decreases). Similarly while expanding, area becomes 4 times
% in each level. We can find Gaussian pyramids using |cv.pyrDown| and
% |cv.pyrUp| functions.
%
% Below is the 4 levels in an image pyramid:
%

% compute Gaussian pyramid
img = cv.imread(fullfile(mexopencv.root(),'test','fruits.jpg'), 'ReduceScale',2);
p = cv.buildPyramid(img, 'MaxLevel',4);

% combine all levels in one image
[rows,cols,~] = cellfun(@size, p);
dst = zeros([rows(1) cols(1)+cols(2) size(img,3)], class(img));
r_start = [0 0 cumsum(rows(2:end-1))] + 1;
r_end = [rows(1) cumsum(rows(2:end))];
c_start = [0 repmat(cols(1),1,numel(p)-1)] + 1;
c_end = [cols(1) cols(1)+cols(2:end)];
for i=1:numel(p)
    dst(r_start(i):r_end(i), c_start(i):c_end(i), :) = p{i};
end
figure, imshow(dst)

%%
% Now you can go down the image pyramid with |cv.pyrUp| function.
%
% Remember, |higher_reso2| is not equal to |higher_reso|, because once you
% decrease the resolution, you loose the information.
%

higher_reso = img;
lower_reso = cv.pyrDown(higher_reso);
higher_reso2 = cv.pyrUp(lower_reso);
figure, imshow(cat(2, higher_reso, higher_reso2))

%%
% Laplacian Pyramids are formed from the Gaussian Pyramids. There is no
% exclusive function for that. Laplacian pyramid images are like edge images
% only. Most of its elements are zeros. They are used in image compression.
% A level in Laplacian Pyramid is formed by the difference between that level
% in Gaussian Pyramid and expanded version of its upper level in Gaussian
% Pyramid. The three levels of a Laplacian level will look like below
% (contrast is adjusted to enhance the contents):
%

% compute Gaussian pyramid
img = imread(fullfile(mexopencv.root(),'test','butterfly.jpg'));
p = cv.Blender.createLaplacePyr(img, 4);

% combine all levels in one image
[rows,cols,~] = cellfun(@size, p);
dst = zeros([rows(1)+rows(2) cols(1) size(img,3)], class(img));
r_start = [0 repmat(rows(1),1,numel(p)-1)] + 1;
r_end = [rows(1) rows(1)+rows(2:end)];
c_start = [0 0 cumsum(cols(2:end-1))] + 1;
c_end = [cols(1) cumsum(cols(2:end))];
for i=1:numel(p)-1
    dst(r_start(i):r_end(i),  c_start(i):c_end(i), :) = uint8(p{i});
end
figure, imshow(dst)
%imshow(cv.CLAHE(rgb2gray(dst)))

%% Image Blending using Pyramids
% One application of Pyramids is Image Blending. For example, in image
% stitching, you will need to stack two images together, but it may not look
% good due to discontinuities between images. In that case, image blending
% with Pyramids gives you seamless blending without leaving much data in the
% images. One classical example of this is the blending of two fruits, Orange
% and Apple.
%
% Please check first reference in additional resources, it has full
% diagramatic details on image blending, Laplacian Pyramids etc. Simply it is
% done as follows:
%
% * Load the two images of apple and orange
% * Find the Gaussian Pyramids for apple and orange (in this particular
%   example, number of levels is 6)
% * From Gaussian Pyramids, find their Laplacian Pyramids
% * Now join the left half of apple and right half of orange in each levels
%   of Laplacian Pyramids
% * Finally from this joint image pyramids, reconstruct the original image.
%
% Below is the full code. (For sake of simplicity, each step is done
% separately which may take more memory. You can optimize it if you want so).
%

% a pair of images of same size
A = imread(fullfile(mexopencv.root(),'test','apple.jpg'));
B = imread(fullfile(mexopencv.root(),'test','orange.jpg'));

% generate Laplacian Pyramids
pA = cv.Blender.createLaplacePyr(A, 5);
pB = cv.Blender.createLaplacePyr(B, 5);

% add left and right halves of images in each level
p = pB;
for i=1:numel(p)
    idx = round(size(pA{i},2)/2);
    p{i}(:,1:idx,:) = pA{i}(:,1:idx,:);
end
%{
% add top and bottom halves of images in each level
p = pB;
for i=1:numel(p)
    idx = round(size(pA{i},1)/2);
    p{i}(1:idx,:,:) = pA{i}(1:idx,:,:);
end
%}

% reconstruct
C = cv.Blender.restoreImageFromLaplacePyr(p);
C = uint8(C);

% image with direct connecting each half
idx = round(size(A,2)/2);
D = B;
D(:,1:idx,:) = A(:,1:idx,:);

% show images
figure
subplot(221), imshow(A), title('Apple')
subplot(222), imshow(B), title('Orange')
subplot(223), imshow(C), title('Pyramid Blending')
subplot(224), imshow(D), title('Direct Connection')

%% Additional Resources
%
% <http://pages.cs.wisc.edu/~csverma/CS766_09/ImageMosaic/imagemosaic.html Image Blending>
%


% Test file for chebtech/cumsum.m

function pass = test_cumsum(pref)

% Get preferences.
if ( nargin < 1 )
    pref = chebtech.techPref();
end

% Generate a few random points to use as test values.
seedRNG(6178);
x = 2 * rand(100, 1) - 1;

for n = 1:2
    if ( n == 1 )
        testclass = chebtech1();
    else
        testclass = chebtech2();
    end

  %%
  % Spot-check antiderivatives for a couple of functions.  We verify that the
  % chebtech antiderivatives match the true ones up to a constant by checking
  % that the standard deviation of the difference between the two on a large
  % random grid is small. We also check that feval(cumsum(f), -1) == 0 each
  % time.

  f = testclass.make(@(x) exp(x) - 1, [],  pref);
  F = cumsum(f);
  F_ex = @(x) exp(x) - x;
  err = std(feval(F, x) - F_ex(x));
  tol = 20*vscale(F)*eps;
  pass(n, 1) = (err < tol) && (abs(feval(F, -1)) < tol);

  f = testclass.make(@(x) 1./(1 + x.^2), [], pref);
  F = cumsum(f);
  F_ex = @(x) atan(x);
  err = feval(F, x) - F_ex(x);
  tol = 10*vscale(F)*eps;
  pass(n, 2) = (std(err) < tol) && (abs(feval(F, -1)) < tol);

  f = testclass.make(@(x) cos(1e4*x), [], pref);
  F = cumsum(f);
  F_ex = @(x) sin(1e4*x)/1e4;
  err = feval(F, x) - F_ex(x);
  tol = 5e4*vscale(F)*eps;
  pass(n, 3) = (std(err) < tol) && (abs(feval(F, -1)) < tol);

  z = exp(2*pi*1i/6);
  f = testclass.make(@(t) sinh(t*z), [], pref);
  F = cumsum(f);
  F_ex = @(t) cosh(t*z)/z;
  err = feval(F, x) - F_ex(x);
  tol = 10*vscale(F)*eps;
  pass(n, 4) = (std(err) < tol) && (abs(feval(F, -1)) < tol);

  %%
  % Check that applying cumsum() and direct construction of the antiderivative
  % give the same results (up to a constant).

  f = testclass.make(@(x) sin(4*x).^2, [], pref);
  F = testclass.make(@(x) 0.5*x - 0.0625*sin(8*x), [], pref);
  G = cumsum(f);
  err = G - F;
  tol = 10*vscale(G)*eps;
  values = err.coeffs2vals(err.coeffs);
  pass(n, 5) = (std(values) < tol) && (abs(feval(G, -1)) < tol);

  %%
  % Check that diff(cumsum(f)) == f and that cumsum(diff(f)) == f up to a
  % constant.

  f = testclass.make(@(x) x.*(x - 1).*sin(x) + 1, [], pref);
  g = diff(cumsum(f));
  err = feval(f, x) - feval(g, x);
  tol = 10*vscale(g)*eps;
  pass(n, 6) = (norm(err, inf) < 100*tol);
  h = cumsum(diff(f));
  err = feval(f, x) - feval(h, x);
  tol = 10*vscale(h)*eps;
  pass(n, 7) = (std(err) < tol)  && (abs(feval(h, -1)) < tol);

  %%
  % Check operation for array-valued chebtech objects.

  f = testclass.make(@(x) [sin(x) x.^2 exp(1i*x)], [], pref);
  F_exact = testclass.make(@(x) [(-cos(x)) (x.^3/3) (exp(1i*x)/1i)], [], pref);
  F = cumsum(f);
  err = std(feval(F, x) - feval(F_exact, x));
  tol = 10*max(vscale(F)*eps);
  pass(n, 8) = (norm(err, inf) < tol)  && all(abs(feval(F, -1)) < tol);

end

end


% Test for bezier curves

N = 1024;
cont = true;
hold on;
axis([0 1 0 1]);
C = [];
while cont
    [x,y,b] = ginput(1);
    cont = b==1;
    if cont
        plot(x,y, '.');
        axis([0 1 0 1]);
        C(:,end+1) = [x;y];
    end
end
compute_bezier_curve(C,N);
hold off;


function dist = codedist_loss(C1,C2,Ylat,loss)
% compute the distance using a loss function metric between 'C1' and  'C2'
%
%    dist = codedist_loss(C1,C2,Ylatent)
%    dist = codedist_loss(C1,C2,Ylatent, loss_fct)
%
% 'C1' contains the result code,
% 'C2' contains the codebooks prototype. An infine small number 'eps'  represents the don't care
% 'loss_fct' is the loss function used in order to compute the loss
%            between 2 codewords. By default the sum of squares is used.
%            One can do 'winner-takes-all' decoding by using the
%            loss function 'max'.
%
% An example:
%  >> Ye = [1 1; 1 1; -1 -1; 1 -1];
%  >> codebook = [1 2 3];
%  >> old_codebook = [1 1 -1; 1 -1 -1];
%  >> code(Ye, codebook, [],old_codebook,'codedist_loss',{'Ylatent','mse'})
%
% To use this distance measure in LS-SVMlab, the following
% procedure is to be followed, assume input data 'X' and multiclass
% output 'Y'
%
% % encode for training
% >> model = initlssvm(X,Y,'classification',gam,sig2,'preprocess','RBF_kernel');
% >> model = changelssvm(model,'codetype','code_OneVsOne');
% >> model = trainlssvm(model);
%
% % decode for simulating
% >> [Yhamming, Ylatent] = simlssvm(model,Xt);
% >> model = changelssvm(model,'codedist_fct','codedist_loss');
% >> model = changelssvm(model,'codedist_args',{Ylatent,'sse'});
% >>   Yt  = simlssvm(model,Xt);
%
% see also:
%   bay_modoutClass, codedist_hamming, code_ECOC

% (c) SCD-KULeuven, rights & help @ http://www.esat.kuleuven.ac.be/sista/lssvmlab

if nargin<3,
  warning('The latent variables should be used, proceeding with the binary classifiers...');
  Ylat = C1;
end
eval('loss;','loss=''sse'';');

[nb,nbin] = size(Ylat);
[~,dim] = size(C2);
dist = zeros(nb,dim);

for d = 1:dim,
  for n= 1:nb,
    nondontcare = find(Ylat(n,:)~=eps & C2(:,d)'~=eps);
    dist(n,d) = feval(loss, Ylat(n,nondontcare),C2(nondontcare,d)');
  end
end
dist

function l = sse(X,Y)
l = sum(sum((X-Y).^2));

function l = winnertakesall(X,Y)
p = find(Y>0);
l = max(X(P));


RGB = imread('saturn.png');
I = rgb2gray(RGB);
J = imnoise(I,'gaussian', 0, 0.005);
K = wiener2(J, [10, 10]);
H = fspecial('disk', 10);
blurred = imfilter(J, H, 'replicate');
figure, imshow(RGB);
figure, imshow(I);
figure, imshow(J);
figure, imshow(H);
figure, imshow(blurred);
figure, imshow(K);


function Qbar = computeQbar(C, Q, S, ny)
    Qbar_tmp = cell(ny, ny);

    for i=1:ny
       for j=1:ny
          if i==j
              if i == ny
                  Qbar_tmp{ny,ny} = C'*S*C;
              else
                  Qbar_tmp{i,j} = C'*Q*C;
              end
          else
              Qbar_tmp{i,j} = zeros(size(C'*Q*C));
          end
       end
    end

    Qbar = cell2mat(Qbar_tmp);
end


function [x,aspc,spec] = invmelfcc(cep, sr, varargin)
% [x,aspc,spec] = invmelfcc(cep, sr[, opts ...])
%    Attempt to invert plp cepstra back to a full spectrum
%    and even a waveform.  Takes all the same options as melfcc.
%    x is (noise-excited) time domain waveform; aspc is the
%    auditory spectrogram, spec is the |STFT| spectrogram.
% 2005-05-15 dpwe@ee.columbia.edu

% Parse out the optional arguments
[wintime, hoptime, numcep, lifterexp, sumpower, preemph, dither, ...
 minfreq, maxfreq, nbands, bwidth, dcttype, fbtype, usecmp, modelorder, broaden, excitation] = ...
    process_options(varargin, 'wintime', 0.025, 'hoptime', 0.010, ...
          'numcep', 13, 'lifterexp', 0.6, 'sumpower', 1, 'preemph', 0.97, ...
	  'dither', 0, 'minfreq', 0, 'maxfreq', 4000, ...
	  'nbands', 40, 'bwidth', 1.0, 'dcttype', 2, ...
	  'fbtype', 'mel', 'usecmp', 0, 'modelorder', 0, 'broaden', ...
                    0, 'excitation', []);

winpts = round(wintime*sr);
nfft = 2^(ceil(log(winpts)/log(2)));

cep = lifter(cep, lifterexp, 1);   % 3rd arg nonzero means undo liftering

% Need to reconstruct the two extra flanking bands for invpostaud to delete
% (if we're doing usecmp)
pspc = cep2spec(cep, nbands+2*broaden, dcttype);

if (usecmp)
  aspc = invpostaud(pspc, maxfreq, fbtype, broaden);
else
  aspc = pspc;
end

% Undo the auditory spectrum
spec = invaudspec(aspc, sr, nfft, fbtype, minfreq, maxfreq, sumpower, bwidth);

% Back to waveform (modulate white noise, or specified excitation)
x = invpowspec(spec, sr, wintime, hoptime, excitation);

if preemph ~= 0
  % Undo the original preemphasis
  x = filter(1, [1 -preemph], x);
end


classdef BatchNorm < dagnn.ElementWise
    properties
        numChannels
        epsilon = 1e-3
        opts = {'NoCuDNN'} % ours seems slightly faster
    end

    properties (Transient)
        moments
    end

    methods
        function outputs = forward(obj, inputs, params)
                outputs{1} = vl_nnbnorm(inputs{1}, params{1}, params{2}, ...
                    'moments', params{3}, ...
                    'epsilon', obj.epsilon, ...
                    obj.opts{:}) ;
        end

        function [derInputs, derParams] = backward(obj, inputs, params, derOutputs)
            [derInputs{1}, derParams{1}, derParams{2}, derParams{3}] = ...
                vl_nnbnorm(inputs{1}, params{1}, params{2}, derOutputs{1}, ...
                'epsilon', obj.epsilon, ...
                'moments', params{3}, ...
                obj.opts{:}) ;
            obj.moments = [] ;
            % multiply the moments update by the number of images in the batch
            % this is required to make the update additive for subbatches
            % and will eventually be normalized away
            derParams{3} = derParams{3} * size(inputs{1},4) ;
        end

        % ---------------------------------------------------------------------
        function obj = BatchNorm(varargin)
            obj.load(varargin{:}) ;
        end

        function params = initParams(obj)
            params{1} = ones(obj.numChannels,1,'single') ;
            params{2} = zeros(obj.numChannels,1,'single') ;
            params{3} = zeros(obj.numChannels,2,'single') ;
        end

        function attach(obj, net, index)
            attach@dagnn.ElementWise(obj, net, index) ;
            p = net.getParamIndex(net.layers(index).params{3}) ;
            net.params(p).trainMethod = 'average' ;
            net.params(p).learningRate = 0.1 ;
        end
    end
end


%==============================================================================
% This code is part of the Matlab-based toolbox
% FAIR - Flexible Algorithms for Image Registration.
% For details see
% - https://github.com/C4IR and
% - http://www.siam.org/books/fa06/
%==============================================================================
%
% Tutorial for FAIR: SSD versus translations, splineInter, HNSP level=8
%
%==============================================================================

clear, close all, help(mfilename)

setup2DHNSPData;
level = 8; m = ML{level}.m;
imgModel('set','imgModel','splineInter');
[T,R] = imgModel('coefficients',ML{level}.T,ML{level}.R,omega);
xc    = getCellCenteredGrid(omega,m);
Rc    = imgModel(R,omega,xc);

trafo('set','trafo','translation2D');
figure(1); clf;
[w1,w2] = ndgrid(0.2*linspace(-1,1,21),0.2*linspace(-1,1,21));
dc  = zeros(size(w1));
for j=1:numel(dc),
  yc = trafo([w1(j);w2(j)],xc);
  Tc = imgModel(T,omega,yc);
  dc(j) = SSD(Tc,Rc,omega,m);
  viewImage(Tc,omega,m); FAIRpause(1/100)
end;
figure(1); clf; surf(w1,w2,dc); hold on; grid off; contour(w1,w2,dc)
title(sprintf('translation, m=[%d,%d]',m)); view(-135,33);
%==============================================================================


clear all; close all;
I=imread('coins.png');
I=im2double(I);
V=zeros(size(I));
for i=1:size(V, 1)
    V(i,:)=0.02*i/size(V,1);
end
J=imnoise(I, 'localvar', V);
figure;
subplot(121);  imshow(I);
subplot(122);  imshow(J);


function xs = searchOptim(x0,gpstruct,LB,UB,Scale,optimState,options)
%SEARCHOPTIM Search step by local maximization of expected improvement.

if nargin < 1
    xs = 'ei';
    return;
end

optoptions = optimset('Display','off','GradObj','on','DerivativeCheck','off',...
    'TolX',optimState.TolMesh,'TolFun',options.TolFun);

[ymui,ys2i,fmui,fs2i,~,post] = gp(gpstruct.hyp(1),gpstruct.inf,gpstruct.mean,gpstruct.cov, ...
    gpstruct.lik,gpstruct.x,gpstruct.y,x0);
gpstruct.post = post;

idx = 1;
funcCount = 0;
while funcCount < options.Nsearch && idx <= 10
    x0 = x0 + 0.1*Scale*randn(size(x0));
    try
        optoptions.MaxFunEval = options.Nsearch - funcCount;
        % [xs(idx,:),~,~,output] = fmincon(@(x_) LowestUpperBound(x_,gpstruct,Scale),x0,[],[],[],[],LB,UB,[],optoptions);
        [xs(idx,:),~,~,output] = fmincon(@(x_) NegExpectedImprovement(x_,optimState.ftarget,gpstruct),x0,[],[],[],[],LB,UB,[],optoptions);
        funcCount = funcCount + output.funcCount;
    catch
        warning('ah');
        xs(idx,:) = x0;
    end
    idx = idx + 1;
end

end

%--------------------------------------------------------------------------
function [y,dy] = NegExpectedImprovement(xi,target,gpstruct,scale)
%NEGEXPECTEDIMPROVEMENT Return NFEI

if ~isempty(gpstruct.x0)
    xi = bsxfun(@minus,xi,gpstruct.x0);
    gpstruct.x = bsxfun(@minus,gpstruct.x,gpstruct.x0);
end

Nhyp = length(gpstruct.hyp);
hypw = zeros(Nhyp,1);

for i = 1:Nhyp; hypw(i) = gpstruct.hypweight(i); end
[ymu,ys2,fmu,fs2,dymu,dys2,dfmu,dfs2] = gpgrad(xi,gpstruct,'central');
fs = sqrt(fs2);
ys = sqrt(ys2);

gammaz = (target - fmu)./fs;
fpi = 0.5*erfc(-gammaz/sqrt(2));    % Probability of improvement
y = -fs.*(gammaz.*fpi + exp(-0.5*gammaz.^2)/sqrt(2*pi));
y = sum(bsxfun(@times,hypw,y),1);

if any(isnan(y) | isinf(y))
    y = 0;
    dy = zeros(1,size(xi,2));
    return;
end

dfs = 0.5*dfs2./fs;
dgammaz = -(dfmu.*fs + (target - fmu).*dfs)./fs2;
dfpi = -0.5*dgammaz/sqrt(2)*(-2*exp(-gammaz.^2/2)/sqrt(pi));

dy = -(dfs.*gammaz.*fpi + dgammaz.*fs.*fpi + dfpi.*gammaz.*fs) ...
    + (fs.*gammaz.*dgammaz - dfs).*exp(-0.5*gammaz.^2)/sqrt(2*pi);
dy = sum(bsxfun(@times,hypw,dy),1);

end

%--------------------------------------------------------------------------
function [y,dy] = LowestUpperBound(xi,gpstruct,scale,kappa)
%LOWESTUPPERBOUND Return LUB

if nargin < 4 || isempty(kappa); kappa = 1; end

if ~isempty(gpstruct.x0)
    xi = bsxfun(@minus,xi,gpstruct.x0);
    gpstruct.x = bsxfun(@minus,gpstruct.x,gpstruct.x0);
end

Nhyp = length(gpstruct.hyp);
hypw = zeros(Nhyp,1);

for i = 1:Nhyp; hypw(i) = gpstruct.hypweight(i); end
if nargout > 1
    [ymu,ys2,fmu,fs2,dymu,dys2,dfmu,dfs2] = gpgrad(xi,gpstruct,'central');
else
    [ymu,ys2,fmu,fs2] = dgp(xi,gpstruct);
end
fs = sqrt(fs2);
% ys = sqrt(ys2);

y = fmu + kappa.*fs;
y = sum(bsxfun(@times,hypw,y),1);

if any(isnan(y) | isinf(y))
    y = 0;
    dy = zeros(1,size(xi,2));
    return;
end

if nargout > 1
    dy = dfmu + 0.5*kappa.*dfs2./fs;
    dy = sum(bsxfun(@times,hypw,dy),1);
    if any(isnan(dy) | isinf(dy)); dy = zeros(1,size(xi,2)); end
end

end


function NodeValue = hlp_computeGraphMeasure(varargin)
% NodeValue = hlp_computeGraphMeasure(causality,ch1,selectedvars,graphMetric)
%
% Compute a univariate graph-theoretic measure for a given node of a graph
%
% Inputs:
%
%   causality:      [num_vars_to x num_vars_from x <num_times> x <num_freqs>] causal matrix
%                   obtained from est_mvarConnectivity().
%   srcNodes:       indices of source nodes
%   selectedvars:   indices of target nodes
%   graphMetric:   which graph measure to compute (see below)
%
% Outputs:
%
%   NodeValue:      [srcNodes x <num_times> x <num_freqs>] graph measures for the source nodes
%
%
% See Also: vis_causalBrainMovie3D(), est_mvarConnectivity(),
%           hlp_collapseFrequencies()
%
%
% References:
%
%   Mullen T (2010) The Source Information Flow Toolbox (SIFT):
%   Theoretical Handbook and User Manual. Chapter 6.
%   Available at: http://www.sccn.ucsd.edu/wiki/SIFT
%
% Author: Tim Mullen, 2010-2013, SCCN/INC/UCSD.
% Email:  tim@sccn.ucsd.edu

% This function is part of the Source Information Flow Toolbox (SIFT)
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

arg_define([0 Inf],varargin, ...
    arg_norep({'cmatrix','CausalMatrix'}, mandatory,[],'Causality array. Shape is [num_vars_to x num_vars_from x <num_times> x <num_freqs>]. Last two dims are optional','shape','matrix'), ...
    arg({'srcNodes','SourceNodes'},  [],[],'Indices of source nodes. Default is all nodes'), ...
    arg({'targNodes','TargetNodes'}, [],[],'Indices of target nodes. Default is all nodes'), ...
    arg({'graphMetric','GraphMetric'},'none',{'none','outflow','mag_outflow','inflow', ...
                                              'causalflow','outdegree','indegree',     ...
                                              'causaldegree','asymmetryratio'}, ...
                                              'Graph measure to compute'), ...
    arg({'ignoreSelfConn','IgnoreSelfConn'},true,[],'Ignore self connectivity in graph measure'));

% handle defaults
if isempty(srcNodes)
    srcNodes = 1:size(cmatrix,2);
end
if isempty(targNodes)
    targNodes = 1:size(cmatrix,1);
end

% remove source node from target nodes
if ignoreSelfConn
    cmatrix=hlp_setdiags(cmatrix,0);
%     targNodes = setdiff_bc(targNodes,srcNodes);
end

% compute graph metrics
switch lower(graphMetric)
    case 'none'
        NodeValue = zeros(1,size(cmatrix,3));
    case 'outflow'
        % Compute outflow from srcNodes in each freq
        NodeValue = squeeze(sum(cmatrix(targNodes,srcNodes,:,:),1));
    case 'mag_outflow'
        % Compute outflow from srcNodes in each freq, ignoring sign
        NodeValue = squeeze(sum(abs(cmatrix(targNodes,srcNodes,:,:)),1));
    case 'inflow'
        % Compute inflow to srcNodes in each freq
        NodeValue = squeeze(sum(cmatrix(srcNodes,targNodes,:,:),2));
    case 'causalflow'
        outflow =   squeeze(sum(cmatrix(targNodes,srcNodes,:,:),1));
        inflow  =   squeeze(sum(cmatrix(srcNodes,targNodes,:,:),2));
        NodeValue = outflow - inflow;
    case 'outdegree'
        % Compute number of outgoing edges from srcNodes in each freq
        NodeValue = squeeze(sum(logical(cmatrix(targNodes,srcNodes,:,:)),1));
    case 'indegree'
        % number of incoming edges to srcNodes in each freq
        NodeValue = squeeze(sum(logical(cmatrix(srcNodes,targNodes,:,:)),2));
    case 'causaldegree'
        % outdegree - indegree
        outflow =   squeeze(sum(logical(cmatrix(targNodes,srcNodes,:,:)),1));
        inflow  =   squeeze(sum(logical(cmatrix(srcNodes,targNodes,:,:)),2));
        NodeValue = outflow - inflow;
    case 'asymmetryratio'
        % 1 if all edges are outgoing, -1 if all edges are incoming.
        % 0 if balanced
        outflow =   squeeze(sum(cmatrix(targNodes,srcNodes,:,:),1));
        inflow  =   squeeze(sum(cmatrix(srcNodes,targNodes,:,:),2));
        NodeValue = (outflow - inflow)./(outflow+inflow);
    otherwise
        % user wants to map a different Conn measure to this
        % (e.g., ERSP)
end

% enforce row vector output
if iscolumn(NodeValue)
    NodeValue = NodeValue';
end


function [data, opt] = HogRawPixelNormExtractor(im, tmpl, opt)

[dataHog] = HogExtractor(im, tmpl, opt);
temp = opt;
temp.FeatureExtractor.tmplsize = temp.FeatureExtractor.tmplsize / 2;
[dataRaw] = RawPixelExtractor(im, tmpl, temp);

data.feat = [dataHog.feat; dataRaw.feat];
dataNorm = sqrt(sum(data.feat .* data.feat));
data.feat = bsxfun(@rdivide, data.feat, dataNorm);
data.tmpl = dataHog.tmpl;


function vTrunc= util_trunc(v, digits, policy)
%vTrunc= util_trunc(v, <digits=4, policy>)
%
% Truncates the number v, digits gives the amount of digits after the
% comma.
%
% policy is 'floor', 'ceil', or 'round'

if ~exist('digits', 'var'), digits=4; end
if ~exist('policy','var'), policy='round'; end

a= 10^digits;
vTrunc= feval(policy, a*v)/a;


function varargout = chunking(varargin)

% function f = chunking(v,num)
%
% <v> is a vector
% <num> is desired length of a chunk
%
% return a cell vector of chunks.  the last vector
% may have fewer than <num> elements.
%
% example:
% isequal(chunking(1:5,3),{1:3 4:5})
%
% OR
%
% function [f,xbegin,xend] = chunking(v,num,n)
%
% <v> is a vector
% <num> is length of a chunk
% <n> is chunk number desired
%
% return the desired chunk in <f>.
% also return the beginning and ending indices associated with
% this chunk in <xbegin> and <xend>.
%
% example:
% isequal(chunking([4 2 3],2,2),3)

switch length(varargin)

case 2
  v = varargin{1};
  num = varargin{2};

  f = {};
  for p=1:ceil(length(v)/num)
    f{p} = v((p-1)*num+1 : min(length(v),p*num));
  end

  varargout = {f};

case 3
  v = varargin{1};
  num = varargin{2};
  n = varargin{3};

  xbegin = (n-1)*num+1;
  xend = min(length(v),n*num);
  f = v(xbegin:xend);

  varargout = {f xbegin xend};

end


function distmap = test_depth_first_search(A,u)

distmap = -1*ones(size(A,1),1);

distmap = ipdouble(distmap);
distmap(u) = 0;

    function on_tree_edge(ei,u,v)
        distmap(v) = distmap(u)+1;
    end

depth_first_search(A,u,struct('tree_edge',@on_tree_edge));

distmap = double(distmap);
end


%example_mat_dual

clear all;
clc
clear opt;
% add path
addpath(genpath('../../SLEP/'));

% load data and set regularization parameter
load('../../data/scene.mat');
lambda = 10^-4;

% center data
D = CenterRowData(D);
L = CenterRowData(L);

% call the main function
[W,fmin] = mat_dual(D,L,lambda);


function linpack_s_test24 ( )

%*****************************************************************************80
%
%% TEST24 tests SSICO.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    24 June 2009
%
%  Author:
%
%    John Burkardt
%
  n = 100;
  lda = n;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST24\n' );
  fprintf ( 1, '  For a symmetric indefinite matrix,\n' );
  fprintf ( 1, '  SSICO estimates the reciprocal condition number.\n' );
  fprintf ( 1, '  The matrix size is N = %d\n', n );
%
%  Assign values to the matrix A.
%
  a(1:n,1:n) = 0.0;

  for i = 1 : n
    a(i,i) = 2.0;
    if ( i < n )
      a(i,i+1) = -1.0;
    end
  end
%
%  Estimate the condition.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Estimate the condition.\n' );

  [ a, ipvt, rcond, z ] = ssico ( a, lda, n );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Estimated reciprocal condition = %f\n', rcond );

  return
end


function [elem,idx,area,bdFlag] = fixorder(node,elem,bdFlag)
%% FIXORDER set all triangles counter-clockwise
%
%   elem = FIXORDER(node,elem) computes signed area (volume) of all
%   triangles (tetrahedron) in the triangulation and switch the vertices
%   such that all signed area (volume) are positive.
%
%   [elem,idx,area] = FIXORDER(node,elem) also outputs the index set of
%   elements whose area (volume) are negative and the absolute value of area.
%
%   [elem,idx,area,bdFlag] = FIXORDER(node,elem,bdFlag) changes the bdFlag
%   for boundary conditions.
%
%   fixorder is recommend to use if the mesh is obtained by delaunay
%   function in matlab.
%
% See also fixorientation, fixorder3
%
% Copyright (C) Long Chen. See COPYRIGHT.txt for details.

if (nargin==2)
    bdFlag = [];
end
% compute signed area of each triangle
[area,elemSign] = simplexvolume(node,elem);
% find triangles with negative area and switch the vertices
idx = find(elemSign==-1);
elem(idx,[2 3]) = elem(idx,[3 2]);
if exist('bdFlag','var') && ~isempty(bdFlag)
    bdFlag(idx,[2 3]) = bdFlag(idx,[3 2]);
end


function fstats = lme_F(stats,C)
% fstats = lme_F(stats,C)
%
% Inference for the fixed-effects in the linear mixed-effects model(Depends
% on the Statistics toolbox).
%
% Input
% stats: Structure obtained from any of the model fitting functions:
% lme_fit_EM, lme_fit_FS and lme_fit_NR.
% C: Contrast matrix.
%
% Output
% fstats.F: F-Statistic.
% fstats.pval: P-value of the F-Statistic.
% fstats.sgn: Sign of the contrast.
% fstats.df: Degrees of freedom of the F-Statistic.
%
% $Revision: 1.4 $  $Date: 2016/04/08 19:36:05 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
%    $Author: mreuter $
%    $Date: 2016/04/08 19:36:05 $
%    $Revision: 1.4 $
% References: Bernal-Rusiel J.L., Greve D.N., Reuter M., Fischl B., Sabuncu
% M.R., 2012. Statistical Analysis of Longitudinal Neuroimage Data with Linear
% Mixed Effects Models, NeuroImage, doi:10.1016/j.neuroimage.2012.10.065.
%
if nargin < 2
    error('Too few inputs');
end;
X = stats.X;
if size(C,2) ~= size(X,2)
     error(['The number of colums in C must be equal to the number of ' ...
                                         'colums in the design matrix X']);
end;
p = size(X,2);
ni = stats.ni;
Zcols = stats.Zcols;
Z = X(:,Zcols);
q = length(Zcols);
nth = q*(q+1)/2+1;
W = stats.W;
V = stats.SIGMA;
CBhat = stats.CovBhat;
L = chol(stats.Dhat);
phi = sqrt(stats.phisqhat);
% Computation of Rijs
Rthth = zeros(nth,nth,p,p);
jk = 0;
for k = 1:q
    for j = 1:k
        jk = jk+1;

        uv = 0;
        for v = 1:q
            for u = 1:v
                uv = uv+1;

                posi = 1; SumR = 0;
                for i = 1:length(ni)
                    posf = posi+ni(i)-1;

                    Xi = X(posi:posf,:); Zi = Z(posi:posf,:); Wi = W(posi:posf,1:ni(i));
                    Ekj = zeros(q,q); Ekj(k,j) = 1; Euv = zeros(q,q); Euv(u,v) = 1;
                    Ai = Zi*Ekj*Euv*Zi';
                    Ri = Xi'*Wi*(Ai+Ai')*Wi*Xi;
                    SumR = SumR+Ri;

                    posi = posf+1;
                end
                Rthth(jk,uv,:,:) = SumR;
            end
        end
    end
end
%Computation of Pis,Qijs and the expected information matrix EI.
[EI,Pth,Qthth] = lme_EI(X,Zcols,W,CBhat,V,L,phi,ni);
invEI = EI\eye(nth);
%Estimation of the bias in the covariance matrix and computation of the
%F-statistic and the degrees of freedom of the test.
Bias = 0;
OM = C'*(C*CBhat*C')^-1*C;
A1 = 0; A2 = 0;
Term1 = OM*CBhat;
Term2 = CBhat*OM*CBhat;
for k=1:nth
    Pk = squeeze(Pth(k,:,:));
    for j=1:nth
        Qkj = squeeze(Qthth(k,j,:,:)); Rkj = squeeze(Rthth(k,j,:,:));
        Pj = squeeze(Pth(j,:,:));
        Bias = Bias + invEI(k,j)*(Qkj-Pk*CBhat*Pj-0.25*Rkj);
        A1 = A1+invEI(k,j)*trace(Term1*Pk*CBhat)*trace(Term1*Pj*CBhat);
        A2 = A2+invEI(k,j)*trace(Term1*Pk*Term2*Pj*CBhat);
    end;
end;
szC = size(C,1);
Bdf = (A1+6*A2)/(2*szC);
g = ((szC+1)*A1-(szC+4)*A2)/((szC+2)*A2);
d = 3*szC+2*(1-g);
c1 = g/d;
c2 = (szC-g)/d;
c3 = (szC+2-g)/d;
EF = (1-A2/szC)^-1;
VF = (2/szC)*(1+c1*Bdf)/((1-c2*Bdf)^2*(1-c3*Bdf));
ro = VF/(2*EF^2);
m = 4+(szC+2)/(szC*ro-1);
l = m/(EF*(m-2));
Bias = CBhat*Bias*CBhat;
CovBhat = CBhat + 2*Bias;
Bhat = stats.Bhat;
F = l*Bhat'*C'*(C*CovBhat*C')^-1*C*Bhat/szC;
if F<0
    F = 0;
end;
fstats.F = F;
fstats.pval = max([1-fcdf(F,szC,m), 1e-30]);
fstats.sgn = sign(C*Bhat);
fstats.df = [szC m];


function plot_vector(X,f,plt,Xerr,c,w)
% Function to plot a frequency dependent vector X. If error bars are specified in Xerr,
% it also plots them. Xerr can either contain upper and lower confidence intervals
% on X, or simply a theoretical confidence level (for the coherence). Used
% to plot the spectrum and coherency.
% Usage: plot_vector(X,f,plt,Xerr,c)
% Inputs:
% X: input vector as a function of frequency (f), see third argument
% f: f axis grid for plot. Default. [1:length(X)]
% plt: 'l' for log, 'n' for no log.
% Xerr: lower and upper confidence intervals for X1: lower/upper x f. Or
%       simply a single number specifying an f-independent confidence
%       level.
% c: controls the color of the plot - input 'b','g','r' etc. Default 'b'
% w: controls the width of the lines - input 1, 1.5, 2 etc

if nargin < 1; error('Need data'); end;
N=length(X);
if nargin < 2 || isempty(f);
    f=1:N;
end;
if length(f)~=N; error('frequencies and data have incompatible lengths'); end;
if nargin < 3 || isempty(plt) ;
    plt='l';
end;
if nargin < 4 || isempty(Xerr);
    Xerr=[];
end;
if nargin < 5 || isempty(c)
    c='b';
end;
if nargin < 6 || isempty(w);
    w=1;
end;

if strcmp(plt,'l');
    X=10*log10(X);
    if nargin >=4 & ~isempty(Xerr); Xerr=10*log10(Xerr); end;
end;

if nargin < 4 || isempty(Xerr);
    plot(f,X,c,'Linewidth',w);
else
    if length(Xerr)==1;
       plot(f,X,c);
       line(get(gca,'xlim'),[Xerr,Xerr],'Color',c,'LineStyle','--','Linewidth',w);
    elseif ~isempty(Xerr);
       plot(f,X,c);
       hold on; plot(f,Xerr(1,:),[c '--'],'Linewidth',w); plot(f,Xerr(2,:),[c '--'],'Linewidth',w);
    end
end
xlabel('f');
if strcmp(plt,'l'); ylabel('10*log10(X)'); else ylabel('X'); end;


function value = r8_aint ( x )

%****************************************************************************80
%
%% R8_AINT truncates an R8 argument to an integer.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    18 October 2011
%
%  Author:
%
%    John Burkardt.
%
%  Parameters:
%
%    Input, real X, the argument.
%
%    Output, real VALUE, the truncated version of X.
%
  if ( x < 0.0 )
    value = - floor ( abs ( x ) );
  else
    value =   floor ( abs ( x ) );
  end

  return
end


% Test file for trigtechh/mtimes.m

function pass = test_mtimes(pref)

% Get preferences.
if ( nargin < 1 )
    pref = trigtech.techPref();
end

testclass = trigtech();

% Generate a few random points to use as test values.
seedRNG(6178);
x = 2 * rand(100, 1) - 1;

% A random number to use as an arbitrary scalar multiplier.
alpha = randn() + 1i*randn();

%%
% Check operation in the face of empty arguments.

f = testclass.make(@(x) sin(10*pi*x), [], pref);
g = testclass.make();
pass(1) = isempty(f*[]) && isempty([]*f) && isempty(2*g) && isempty(g*2);

%%
% Check operation for scalar TRIGTECH objects.

f = testclass.make(@(x) sin(10*pi*x), [], pref);
g1 = alpha*f;
g2 = f*alpha;
pass(2) = isequal(g1, g2);
g_exact = @(x) alpha*sin(10*pi*x);
pass(3) = norm(feval(g1, x) - g_exact(x), inf) < ...
    100*vscale(g1)*eps;

g = 0*f;
pass(4) = all(g.coeffs == 0);

%%
% Check operation for array-valued TRIGTECH objects.

f = testclass.make(@(x) [sin(10*pi*x) cos(20*pi*x) cos(sin(pi*x))], [], pref);
g1 = alpha*f;
g2 = f*alpha;
pass(5) = isequal(g1, g2);
g_exact = @(x) alpha*[sin(10*pi*x) cos(20*pi*x) cos(sin(pi*x))];
err = abs(feval(g1, x) - g_exact(x));
pass(6) = max(err(:)) < 100*max(vscale(g1)*eps);

g = 0*f;
pass(7) =  all(g.coeffs == 0);

A = randn(3, 3);
g = f*A;
g_exact = @(x) [sin(10*pi*x) cos(20*pi*x) cos(sin(pi*x))]*A;
err = abs(feval(g, x) - g_exact(x));
pass(8) = max(err(:)) < 100*max(vscale(g)*eps);

f = testclass.make(@(x) [exp(1i*11*pi*x) cos(20*pi*x) cos(sin(pi*x))], [], pref);
g = f*A;
g_exact = @(x) [exp(1i*11*pi*x) cos(20*pi*x) cos(sin(pi*x))]*A;
err = abs(feval(g, x) - g_exact(x));
pass(9) = max(err(:)) < 100*max(vscale(g)*eps);

f = testclass.make(@(x) [exp(1i*11*pi*x) cos(20*pi*x) cos(sin(pi*x))], [], pref);
A = randn(3, 3) + 1i*randn(3, 3);
g = f*A;
g_exact = @(x) [exp(1i*11*pi*x) cos(20*pi*x) cos(sin(pi*x))]*A;
err = abs(feval(g, x) - g_exact(x));
pass(10) = max(err(:)) < 100*max(vscale(g)*eps);

%%
% Verify error handling and corner cases.

% Multiply non-scalar double and TRIGTECH.
try
    f = testclass.make(@(x) exp(cos(pi*x)));
    disp([1 2 3]*f);
    pass(11) = false;
catch ME
    pass(11) = strcmp(ME.identifier, 'CHEBFUN:TRIGTECH:mtimes:size') ...
        && strcmp(ME.message, 'Inner matrix dimensions must agree.');
end

% Multiply TRIGTECH and non-scalar double with mismatching dimensions.
try
    f = testclass.make(@(x) [sin(10*pi*x) cos(20*pi*x)]);
    disp(f*[1 ; 2 ; 3]);
    pass(12) = false;
catch ME
    pass(12) = strcmp(ME.identifier, 'CHEBFUN:TRIGTECH:mtimes:size2') ...
        && strcmp(ME.message, 'Inner matrix dimensions must agree.');
end

% Using * for multiplication of two TRIGTECH objects.
try
    g = testclass.make(@(x) cos(20*pi*x));
    disp(f*g);
    pass(13) = false;
catch ME
    pass(13) = strcmp(ME.message, 'Use .* to multiply TRIGTECH objects.');
end

% Using * to multiply a TRIGTECH and something else.
try
    disp(f*uint8(128));
    pass(14) = false;
catch ME
    pass(14) = strcmp(ME.message, ...
        'mtimes does not know how to multiply a TRIGTECH and a uint8.');
end

end


function mu = meanmv(X, dim);
% handles missing values (NaN).

error(nargchk(1,2,nargin));

if nargin < 2
  dim = min(find(size(X)~=1));
  if isempty(dim), dim = 1; end
end

% NaNs to zero
Imv = isnan(X);
X(Imv) = 0;

% Sum and divide by the number of non-missing values
div = sum(~Imv, dim);
div(div==0) = 1;
mu = sum(X, dim) ./ div;
%mu(isinf(xmean)) = NaN;


function linpack_c_test23 ( )

%*****************************************************************************80
%
%% TEST23 tests CPPCO.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    25 June 2009
%
%  Author:
%
%    John Burkardt
%
  n = 3;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST23\n' );
  fprintf ( 1, '  For a single precision complex (C)\n' );
  fprintf ( 1, '  Hermitian positive definite packed matrix (PP),\n' );
  fprintf ( 1, '  CPPCO estimates the reciprocal condition number.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  The matrix order is N = %d\n', n );
%
%  Set the values of the matrix A.
%
  a(1) = complex ( 2.5281,  0.0000 );

  a(2) = complex ( 2.1341, -0.2147 );
  a(3) = complex ( 3.0371,  0.0000 );

  a(4) = complex ( 2.4187,  0.2932 );
  a(5) = complex ( 2.0905,  1.1505 );
  a(6) = complex ( 2.7638,  0.0000 );
%
%  Estimate the condition.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Estimate the condition number.\n' );

  [ a, rcond, info ] = cppco ( a, n );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Reciprocal condition number = %f\n', rcond );

  return
end


function [gx] = g_exp2d(x,phi,u,in)

gkaf = in.gkaf;
gkas = in.gkas;
%
% X = [gkaf,gkas,gkaf.^2,gkas.^2,gkaf.*gkas];
% X = [X,zeros(size(X,1),1)];
%
% gx = X*phi;
% dgdx = [];
% dgdphi = X';

gx = phi(1).*exp(0.5.*gkaf).*exp(-abs(gkas-0.5.*gkaf));
% gx = real(gx);